DoComm.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/socket.h>
  10. #include <sys/types.h>
  11. #include <sys/ipc.h>
  12. #include <sys/shm.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <assert.h>
  16. #include <time.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <signal.h>
  20. #include "../Log/log.h"
  21. #include "../ShareMemory/shmMem.h"
  22. #include "../Define/define.h"
  23. #include "../Config.h"
  24. #include "../SelectGun/SelectGun.h"
  25. #include "../timeout.h"
  26. #include "DoComm.h"
  27. //------------------------------------------------------------------------------
  28. //#define log_info(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  29. //#define log_warn(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  30. //#define log_error(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  31. //------------------------------------------------------------------------------
  32. static DoCommGblData gDoCommGblData = {0};
  33. static struct SysConfigData *pSysConfig = NULL;
  34. static struct SysInfoData *pSysInfo = NULL;
  35. static struct WARNING_CODE_INFO *pSysWarning = NULL;
  36. static struct AlarmCodeData *pAlarmCode = NULL;
  37. static struct PsuData *ShmPsuData = NULL;
  38. static struct OCPP16Data *ShmOCPP16Data = NULL;
  39. static struct PrimaryMcuData *ShmPrimaryMcuData = NULL;
  40. static SelectGunInfo *ShmSelectGunInfo = NULL;
  41. static DcCommonInfo *ShmDcCommonData = NULL;
  42. //static struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY]
  43. static struct ChargingInfoData *pDcChargingInfo = NULL;
  44. static struct timeb gRegTimeUp[2][MAX_REGISTER_NUM] = {0};
  45. static struct WARNING_CODE_INFO gPreSysWarningInfo = {0};
  46. //------------------------------------------------------------------------------
  47. static void removeFaultCodeToBuf(uint8_t *Code);
  48. static void addFaultCodeToBuf(uint8_t *Code);
  49. static int readMiscCommand(int fd, uint8_t id);
  50. static int writeCsuModuleVersion(int fd);
  51. static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id);
  52. //------------------------------------------------------------------------------
  53. //--- Common function ---
  54. //------------------------------------------------------------------------------
  55. void GetClockTime(struct timespec *_now_time, void *null)
  56. {
  57. clock_gettime(CLOCK_MONOTONIC, _now_time);
  58. }
  59. /*static int StoreLogMsg(const char *fmt, ...)
  60. {
  61. char Buf[4096 + 256];
  62. char buffer[4096];
  63. va_list args;
  64. struct timeb SeqEndTime;
  65. struct tm *tm;
  66. va_start(args, fmt);
  67. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  68. va_end(args);
  69. memset(Buf, 0, sizeof(Buf));
  70. ftime(&SeqEndTime);
  71. SeqEndTime.time = time(NULL);
  72. tm = localtime(&SeqEndTime.time);
  73. if (pSysConfig->SwitchDebugFlag == 1) {
  74. sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
  75. tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer);
  76. printf("%s \n", Buf);
  77. } else {
  78. sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  79. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm,
  80. buffer,
  81. tm->tm_year + 1900, tm->tm_mon + 1);
  82. system(Buf);
  83. }
  84. return rc;
  85. }
  86. */
  87. static int DiffTimeb(struct timeb ST, struct timeb ET)
  88. {
  89. //return milli-second
  90. unsigned int StartTime, StopTime;
  91. StartTime = (unsigned int)ST.time;
  92. StopTime = (unsigned int)ET.time;
  93. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  94. }
  95. /**
  96. * [hexdump : check data]
  97. * @Author Jerry
  98. * @DateTime 2018-12-21
  99. * @param p [description]
  100. * @param size [description]
  101. */
  102. static void Hexdump(const void *p, size_t size)
  103. {
  104. const uint8_t *c = p;
  105. char message[10240] = {0};
  106. uint32_t i = 0;
  107. uint32_t message_len = 0;
  108. assert(p);
  109. //printf("Dumping %u bytes from %p:\r\n", size, p);
  110. message_len += sprintf(&message[message_len], "\r\nDumping %u bytes from %p:\r\n", (unsigned int)size, p);
  111. while (size > 0) {
  112. for (i = 0; i < 16; i++) {
  113. if (i < size) {
  114. //printf("%02x ", c[i]);
  115. message_len += sprintf(&message[message_len], "%02x ", c[i]);
  116. } else {
  117. //printf(" ");
  118. message_len += sprintf(&message[message_len], " ");
  119. }
  120. }
  121. for (i = 0; i < 16; i++) {
  122. if (i < size) {
  123. //printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
  124. message_len += sprintf(&message[message_len], "%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
  125. } else {
  126. //printf(" ");
  127. message_len += sprintf(&message[message_len], " ");
  128. }
  129. }
  130. //printf("\n");
  131. message_len += sprintf(&message[message_len], "\r\n");
  132. c += 16;
  133. if (size <= 16) {
  134. break;
  135. }
  136. size -= 16;
  137. }
  138. message_len += sprintf(&message[message_len], "\r\n");
  139. log_info("%s", message);
  140. }
  141. static int string2ByteArray(char *input, uint8_t *output)
  142. {
  143. int loop = 0;
  144. int i = 0;
  145. while (input[loop] != '\0') {
  146. output[i++] = input[loop++];
  147. }
  148. output[loop] = '\0';
  149. return loop + 1;
  150. }
  151. static void unixSocketSigPipeHandle(int sig)
  152. {
  153. log_error("socket packet error %x\r\n", sig);
  154. }
  155. static void InitSocketSigPipe(void)
  156. {
  157. struct sigaction action;
  158. action.sa_handler = unixSocketSigPipeHandle;
  159. sigemptyset(&action.sa_mask);
  160. action.sa_flags = 0;
  161. sigaction(SIGPIPE, &action, NULL);
  162. }
  163. //------------------------------------------------------------------------------
  164. static void setTcpStatus(uint8_t setValue)
  165. {
  166. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = setValue;
  167. }
  168. //------------------------------------------------------------------------------
  169. //--- TCP socket function ---
  170. //------------------------------------------------------------------------------
  171. static int sendTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
  172. {
  173. int size = -1;
  174. size = send(fd, data, dataLen , 0);
  175. if ((size < 0) || (errno == EAGAIN)) {
  176. if (size < 0) {
  177. log_error("Size = %d, EAGAIN error %d:%s\r\n", size, errno, strerror(errno));
  178. }
  179. }
  180. return size;
  181. }
  182. static int recvTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
  183. {
  184. int size = -1;
  185. uint8_t *pdata = (uint8_t *)data;
  186. size = recv(fd, pdata, dataLen, MSG_WAITALL);
  187. if ((errno == EAGAIN) || (size < 0)) {
  188. log_error("Size = %d, EAGAIN error %d:%s\r\n", size, errno, strerror(errno));
  189. }
  190. return size;
  191. }
  192. static int getSO_ERROR(int fd)
  193. {
  194. int err = 1;
  195. socklen_t len = sizeof err;
  196. if (-1 == getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &len)) {
  197. log_error("getSO_ERROR\r\n");
  198. }
  199. if (err) {
  200. errno = err; // set errno to the socket SO_ERROR
  201. }
  202. return err;
  203. }
  204. static void closeSocket(int fd) // *not* the Windows closesocket()
  205. {
  206. if (fd < 0) {
  207. return;
  208. }
  209. getSO_ERROR(fd); // first clear any errors, which can cause close to fail
  210. if (shutdown(fd, SHUT_RDWR) < 0) { // secondly, terminate the 'reliable' delivery
  211. if (errno != ENOTCONN && errno != EINVAL) { // SGI causes EINVAL
  212. log_info("shutdown\r\n");
  213. }
  214. }
  215. if (close(fd) < 0) { // finally call close()
  216. log_info("client socket close\r\n");
  217. }
  218. }
  219. static int doCommConnToServer(void)
  220. {
  221. struct sockaddr_in dest;
  222. struct timeval tv;
  223. int flag;
  224. int TcpSock = 0;
  225. //if (TcpSock > 0) {
  226. // close(TcpSock);
  227. //}
  228. if ((TcpSock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  229. log_error("Open TCP socket NG");
  230. return -1;
  231. }
  232. //flag = fcntl (TcpSock, F_GETFL, 0);
  233. //if (flag >= 0) {
  234. // flag |= O_NONBLOCK;
  235. // fcntl(TcpSock, F_SETFL, flag );
  236. //}
  237. tv.tv_sec = 3;
  238. tv.tv_usec = 0;
  239. setsockopt(TcpSock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)); //設定等待3s
  240. setsockopt(TcpSock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)); //設定傳送3s
  241. flag = 1;
  242. setsockopt(TcpSock, SOL_SOCKET, MSG_NOSIGNAL, &flag, sizeof(flag));
  243. memset(&dest, 0, sizeof(dest));
  244. dest.sin_family = AF_INET;
  245. dest.sin_port = htons(DoTcpPort);
  246. inet_aton(DoIPAddress, (struct in_addr *) &dest.sin_addr.s_addr);
  247. if (connect(TcpSock, (struct sockaddr *) &dest, sizeof(dest)) != 0) {
  248. close(TcpSock);
  249. return -1;
  250. }
  251. return TcpSock;
  252. }
  253. //------------------------------------------------------------------------------
  254. //--- Audi select gun ---
  255. //------------------------------------------------------------------------------
  256. static void clearPricesInfo(uint8_t id)
  257. {
  258. memset(&ShmSelectGunInfo->PricesInfo[id], 0, sizeof(PricesInfo));
  259. ShmSelectGunInfo->PricesInfo[id].Balance = FAIL_BALANCE_PRICES;
  260. }
  261. static void ClearAuthorizedFlag(void)
  262. {
  263. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  264. pSysInfo->AuthorizeFlag = NO;
  265. }
  266. static void ClearDetectPluginFlag(void)
  267. {
  268. pSysInfo->WaitForPlugit = NO;
  269. }
  270. static bool isDetectPlugin(void)
  271. {
  272. if (pSysInfo->WaitForPlugit == YES) {
  273. return YES;
  274. }
  275. return NO;
  276. }
  277. static void destroySelectGun(uint8_t curGun)
  278. {
  279. uint8_t i = 0;
  280. uint8_t totalGun = pSysConfig->TotalConnectorCount;
  281. if (curGun == DESTROY_ALL_SEL) {
  282. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  283. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  284. for (i = 0; i < totalGun; i++) {
  285. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  286. pDcChargingInfo->TimeoutFlag = Timeout_None;
  287. clearPricesInfo(i);
  288. }
  289. pSysInfo->CurGunSelected = 0;
  290. strcpy((char *)pSysConfig->UserId, "");
  291. }
  292. //for charging timeout or complete
  293. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun != SEL_GUN_RELEASE)) {
  294. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  295. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  296. ClearAuthorizedFlag();
  297. }
  298. clearPricesInfo(curGun);
  299. }
  300. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun != SEL_GUN_RELEASE)) {
  301. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  302. ClearAuthorizedFlag();
  303. }
  304. clearPricesInfo(curGun);
  305. }
  306. }
  307. static int getConfirmSelectedGun(uint8_t curSel)
  308. {
  309. if (((curSel == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
  310. ((curSel == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
  311. return PASS;
  312. }
  313. return FAIL;
  314. }
  315. static int getSelGunWaitToAuthor(uint8_t curGun)
  316. {
  317. if (curGun == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
  318. return FAIL;
  319. }
  320. if (curGun == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
  321. return FAIL;
  322. }
  323. return PASS;
  324. }
  325. static void setConfirmSelGun(uint8_t selGun)
  326. {
  327. if (selGun == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_RELEASE) {
  328. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
  329. //printf("confirmSelGun left\r\n");
  330. } else if (selGun == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_RELEASE) {
  331. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
  332. //printf("confirmSelGun right\r\n");
  333. }
  334. }
  335. //------------------------------------------------------------------------------
  336. //--- DoComm function ---
  337. //------------------------------------------------------------------------------
  338. static int compareOpcode(uint8_t opCode)
  339. {
  340. if (opCode != OP_WAIT_RESPONSE) {
  341. //log_error("response operative code fail\r\n");
  342. return FAIL;
  343. }
  344. return PASS;
  345. }
  346. static int compareResult(uint8_t result)
  347. {
  348. if (result != COMMAND_RESULT_OK) {
  349. //log_error("response result fail\r\n");
  350. return FAIL;
  351. }
  352. return PASS;
  353. }
  354. static int compareRegister(uint8_t srcReg, uint8_t destReg)
  355. {
  356. if (srcReg != destReg) {
  357. //log_error("response register fail\r\n");
  358. return FAIL;
  359. }
  360. return PASS;
  361. }
  362. static float transPricesUnit(int prices)
  363. {
  364. //printf("prices = %.2f\r\n", prices * PRICES_UNIT);
  365. return (prices * PRICES_UNIT);
  366. }
  367. static void clearMiscCommand(void)
  368. {
  369. gDoCommGblData.MiscCmd = 0;
  370. }
  371. static int qrCodeUrlInfoHandle(uint8_t *data)
  372. {
  373. //int len = 0;
  374. //char cmdBuf[128] = {0};
  375. char localTime[128] = {0};
  376. uint16_t timeLen = 0;
  377. struct timeb SeqEndTime;
  378. struct tm *tm;
  379. if ((char *)&data[0] == '\0') {
  380. log_error("QR code date error\r\n");
  381. return FAIL;
  382. }
  383. //get local system time
  384. ftime(&SeqEndTime);
  385. SeqEndTime.time = time(NULL);
  386. tm = localtime(&SeqEndTime.time);
  387. timeLen = sprintf(localTime, "%04d-%02d-%02d %02d:%02d",
  388. tm->tm_year + 1900,
  389. tm->tm_mon + 1,
  390. tm->tm_mday,
  391. tm->tm_hour,
  392. tm->tm_min);
  393. //copy QR code string
  394. if (strncmp((char *)localTime, (char *)&data[0], timeLen - 2) != 0) {
  395. memset(pSysConfig->SystemId, '\0', sizeof(pSysConfig->SystemId));
  396. //printf("data = %s\r\n", data);
  397. //len =
  398. string2ByteArray((char *)data, (uint8_t *)pSysConfig->SystemId);
  399. //printf("SystemId = %s\r\n", pSysConfig->SystemId);
  400. }
  401. //if ((char *)&data[len] == '\0') {
  402. // log_error("power cabinet system date error\r\n");
  403. // return FAIL;
  404. //}
  405. //set system date
  406. //if (strncmp((char *)localTime, (char *)&data[len], timeLen) != 0) {
  407. // sprintf(cmdBuf, "date -s \"%s\" >> /dev/null", (char *)&data[len]);
  408. // system(cmdBuf);
  409. // log_info("local time = %s, data time = %s\r\n", localTime, (char *)&data[len]);
  410. // ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  411. // strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  412. // sleep(3);
  413. // //gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  414. //}
  415. return PASS;
  416. }
  417. static int updateFirmwareHandle(uint8_t *imgName)
  418. {
  419. int ret = PASS;
  420. char cmdBuf[1024] = {0};
  421. sprintf(cmdBuf, "/mnt/%s", imgName);
  422. log_info("Program ready to check file %s\n", cmdBuf);
  423. if ( access(cmdBuf, F_OK) != -1) {
  424. log_info("File '%s' exist.\n", cmdBuf);
  425. pSysInfo->FirmwareUpdate = YES;
  426. } else {
  427. log_info("File '%s' doesn't exist.\n", cmdBuf);
  428. ret = FAIL;
  429. }
  430. #if 0
  431. char cmdBuf[1024] = {0};
  432. int status = 0;
  433. system("touch ./tftpUpdate.sh"); //創建shell
  434. sprintf(cmdBuf, "echo tftp -gr %s -l %s/%s %s; > ./tftpUpdate.sh",
  435. imgName,
  436. IMAGE_FILE_PATH,
  437. imgName,
  438. DoIPAddress);
  439. system("chmod +x ./tftpUpdate.sh"); //修改權限
  440. status = system("sh tftpUpdate.sh"); //執行shell
  441. if (-1 == status) {
  442. printf("system error!");
  443. } else {
  444. printf("exit status value = [0x%x]\n", status);
  445. if (WIFEXITED(status)) {
  446. if (0 == WEXITSTATUS(status)) {
  447. printf("run shell script successfully.\n");
  448. pSysInfo->FirmwareUpdate = YES;
  449. } else {
  450. printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));
  451. }
  452. } else {
  453. printf("exit status = [%d]\n", WEXITSTATUS(status));
  454. }
  455. }
  456. system("rm -rf ./tftpUpdate.sh"); //刪除shell
  457. #endif //0
  458. return ret;
  459. }
  460. static int miscCommandHandle(uint8_t dataLen, uint8_t plugNum, uint8_t *data)
  461. {
  462. int ret = PASS;
  463. uint8_t i = 0, j = 0;
  464. uint8_t cmdCount = (dataLen / 6);
  465. uint16_t cmd = 0;
  466. uint32_t value = 0;
  467. float prices = 0;
  468. MiscCommand *pMiscCmd = NULL;
  469. if (cmdCount < 1) {
  470. gDoCommGblData.MiscCmd = 0;
  471. //printf("cmdCount fail = %d, data len = %d\r\n", cmdCount, dataLen);
  472. return FAIL;
  473. }
  474. for (i = 0; i < cmdCount; i++) {
  475. pMiscCmd = (MiscCommand *)data + i;
  476. cmd = ntohs(pMiscCmd->CMD);
  477. value = ntohl(*((uint32_t *)&pMiscCmd->Value[0]));
  478. //log_info("misc command = %x, value = %d\r\n", cmd, value);
  479. switch (cmd) {
  480. //--- Execute parameter ---
  481. case MISC_CMD_CONNECOTOR_TIMEOUT:
  482. ShmSelectGunInfo->RemoteSetup.ConnectionTimeout = value;
  483. log_info("connection timeout = %d\r\n", ShmSelectGunInfo->RemoteSetup.ConnectionTimeout);
  484. clearMiscCommand();
  485. break;
  486. case MISC_CMD_OPERATIVE_STATE:
  487. if ((value > YES) || (value < NO)) {
  488. if (plugNum == 1) {
  489. clearMiscCommand();
  490. }
  491. break;
  492. }
  493. log_info("change availability plugNum = %d, value = %d\r\n", plugNum, value);
  494. ShmOCPP16Data->CsMsg.bits[plugNum].ChangeAvailabilityReq = YES;
  495. if (value == YES) {
  496. strcpy((char *)ShmOCPP16Data->ChangeAvailability[plugNum].Type, "Operative");
  497. } else if (value == NO) {
  498. strcpy((char *)ShmOCPP16Data->ChangeAvailability[plugNum].Type, "Inoperative");
  499. }
  500. if (plugNum == 1) {
  501. clearMiscCommand();
  502. }
  503. break;
  504. case MISC_CMD_DEFAULT_PRICES:
  505. prices = transPricesUnit((int)value);
  506. log_info("default prices = %.2f\r\n", prices);
  507. //pSysConfig->BillingData.isBilling = YES;
  508. for (j = 0; j < 24; j++) {
  509. pSysConfig->BillingData.Fee[j] = prices;
  510. }
  511. clearMiscCommand();
  512. break;
  513. case MISC_CMD_DEFAULT_CURRENCY:
  514. if (value < 0) {
  515. clearMiscCommand();
  516. return FAIL;
  517. }
  518. //log_info("default currency = %s%c\r\n", (uint8_t *)Currency[value]);
  519. log_info("default currency = %s\r\n", (uint8_t *)GetCurrency(value));
  520. pSysConfig->BillingData.Currency = value;
  521. clearMiscCommand();
  522. break;
  523. case MISC_CMD_ACCOUNT_BALANCE:
  524. if (pSysInfo->CurGunSelected != (plugNum)) {
  525. clearMiscCommand();
  526. break;
  527. }
  528. #if defined DD360Audi
  529. if (getConfirmSelectedGun(plugNum) == FAIL) {
  530. log_info("Remote start not select gun\r\n");
  531. break;
  532. }
  533. #endif //DD360Audi
  534. ShmSelectGunInfo->PricesInfo[plugNum].Balance = transPricesUnit((int)value);
  535. log_info("%d misc balance = %.2f\r\n", plugNum, ShmSelectGunInfo->PricesInfo[plugNum].Balance);
  536. clearMiscCommand();
  537. break;
  538. case MISC_CMD_BACKEND_STATUS :
  539. ShmSelectGunInfo->EthDevStatus.Backend = value;
  540. break;
  541. case MISC_CMD_ETHERNET_STATUS :
  542. ShmSelectGunInfo->EthDevStatus.Ethernet = value;
  543. break;
  544. case MISC_CMD_WIFI_STATUS :
  545. ShmSelectGunInfo->EthDevStatus.Wifi = value;
  546. break;
  547. case MISC_CMD_4G_STATUS :
  548. ShmSelectGunInfo->EthDevStatus.FourG = value;
  549. break;
  550. case MISC_CMD_BILLING_INFO:
  551. pSysConfig->BillingData.isBilling = value;
  552. break;
  553. case MISC_CMD_WEB_STOP_CHARGING:
  554. pSysConfig->StopChargingByButton = value;
  555. break;
  556. //--- Control Dispenser ---
  557. case MISC_CMD_HARDWARE_REBOOT:
  558. if (value != YES) {
  559. clearMiscCommand();
  560. break;
  561. }
  562. log_info("Hardware reboot\r\n");
  563. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  564. strcpy((char *)ShmOCPP16Data->Reset.Type, "Hard");
  565. clearMiscCommand();
  566. break;
  567. case MISC_CMD_SOFTWARE_RESTART:
  568. if (value != YES) {
  569. clearMiscCommand();
  570. break;
  571. }
  572. clearMiscCommand();
  573. log_info("Software reboot\r\n");
  574. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  575. strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  576. break;
  577. case MISC_CMD_REMOTE_START_CHARGING:
  578. if (value != YES) {
  579. if (plugNum == 1) {
  580. clearMiscCommand();
  581. }
  582. break;
  583. }
  584. log_info("Remote start charging plugNum = %d\r\n", plugNum);
  585. #if defined DD360Audi
  586. if (getSelGunWaitToAuthor(plugNum) == FAIL) {
  587. log_error("Remote start gun already charging\r\n");
  588. break;
  589. }
  590. if (pSysInfo->CurGunSelected != (plugNum)) {
  591. if (plugNum == LEFT_GUN_NUM &&
  592. (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  593. ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  594. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  595. strcpy((char *)pSysConfig->UserId, "");
  596. pSysInfo->WaitForPlugit = NO;
  597. sleep(1); //Jerry add
  598. pSysInfo->SystemPage = _LCM_SELECT_GUN;
  599. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  600. pSysInfo->SystemTimeoutFlag = Timeout_None;
  601. }
  602. if (plugNum == RIGHT_GUN_NUM &&
  603. (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  604. ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  605. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  606. strcpy((char *)pSysConfig->UserId, "");
  607. pSysInfo->WaitForPlugit = NO;
  608. sleep(1); //Jerry add
  609. pSysInfo->SystemPage = _LCM_SELECT_GUN;
  610. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  611. pSysInfo->SystemTimeoutFlag = Timeout_None;
  612. }
  613. pSysInfo->CurGunSelected = (plugNum);
  614. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  615. }
  616. #else
  617. pSysInfo->CurGunSelected = (plugNum);
  618. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  619. #endif //DD360Audi
  620. setConfirmSelGun(pSysInfo->CurGunSelected);
  621. ShmOCPP16Data->CsMsg.bits[plugNum].RemoteStartTransactionReq = YES;
  622. ShmSelectGunInfo->PricesInfo[plugNum].Balance = 0.00;
  623. clearMiscCommand();
  624. break;
  625. case MISC_CMD_REMOTE_STOP_CHARGING:
  626. if (value != YES) {
  627. if (plugNum == 1) {
  628. clearMiscCommand();
  629. }
  630. break;
  631. }
  632. strcpy((char *)pSysConfig->UserId, "");
  633. pSysInfo->WaitForPlugit = NO;
  634. pSysInfo->SystemPage = _LCM_SELECT_GUN;
  635. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  636. pSysInfo->SystemTimeoutFlag = Timeout_None;
  637. destroySelectGun(plugNum);
  638. clearMiscCommand();
  639. break;
  640. case MISC_CMD_REMOTE_UNLOCK:
  641. if (value != YES) {
  642. if (plugNum == 1) {
  643. clearMiscCommand();
  644. }
  645. break;
  646. }
  647. if (isDetectPlugin() == YES) {
  648. ClearDetectPluginFlag();
  649. strcpy((char *)pSysConfig->UserId, "");
  650. pSysInfo->WaitForPlugit = NO;
  651. pSysInfo->SystemPage = _LCM_SELECT_GUN;
  652. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  653. pSysInfo->SystemTimeoutFlag = Timeout_None;
  654. destroySelectGun(plugNum);
  655. } else {
  656. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  657. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  658. }
  659. break;
  660. default:
  661. clearMiscCommand();
  662. break;
  663. }
  664. usleep(128);
  665. }
  666. return ret;
  667. }
  668. static int chargingcapabilityHandle(uint8_t *data, uint8_t plugNum)
  669. {
  670. uint8_t addr = 0;
  671. float MaxVolt, MaxCurrent, MaxPower;
  672. static PricesInfo pricesInfo[2] = {0};
  673. CapabilityInfo *pCapabilityInfo = NULL;
  674. AccountInfo *pAccountInfo = NULL;
  675. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  676. //--- charging capability information --------------------------------------
  677. pCapabilityInfo = (CapabilityInfo *)&data[0];
  678. MaxVolt = (float)(ntohs(pCapabilityInfo->OutputVoltage));
  679. if (MaxVolt >= 0) {
  680. if (MaxVolt > MAX_VOLTAGE) {
  681. MaxVolt = MAX_VOLTAGE;
  682. }
  683. pDcChargingInfo->MaximumChargingVoltage = MaxVolt;
  684. }
  685. MaxCurrent = (float)(ntohs(pCapabilityInfo->OutputCurrent));
  686. if (MaxCurrent >= 0) {
  687. if (MaxCurrent > MAX_CURRENCY) {
  688. MaxCurrent = MAX_CURRENCY;
  689. }
  690. pDcChargingInfo->AvailableChargingCurrent = MaxCurrent;
  691. }
  692. MaxPower = (float)(ntohs(pCapabilityInfo->OutputPower));
  693. if (MaxPower >= 0) {
  694. if (MaxPower > MAX_POWER) {
  695. MaxPower = MAX_POWER;
  696. }
  697. pDcChargingInfo->AvailableChargingPower = MaxPower;
  698. }
  699. //MaxVolt = (float)(data[0] << 8 |data[1]);
  700. //MaxCurrent = (float)(data[2] << 8 | data[3]);
  701. //MaxPower = (float)(data[4] << 8 | data[5]);
  702. //printf("MaxVolt=%f, MaxCurrent=%f, MaxPower=%f,\n", MaxVolt, MaxCurrent, MaxPower);
  703. //--- user prices information ----------------------------------------------
  704. addr = (sizeof(CapabilityInfo) - 2); //2 byte reserved
  705. pAccountInfo = (AccountInfo *)&data[addr];
  706. pSysConfig->BillingData.Currency = pAccountInfo->Currency;
  707. ShmSelectGunInfo->PricesInfo[plugNum].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
  708. pDcChargingInfo->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
  709. ShmSelectGunInfo->PricesInfo[plugNum].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
  710. if ((pricesInfo[plugNum].UserPrices != ShmSelectGunInfo->PricesInfo[plugNum].UserPrices) ||
  711. (pricesInfo[plugNum].Balance != ShmSelectGunInfo->PricesInfo[plugNum].Balance)) {
  712. pricesInfo[plugNum].UserPrices = ShmSelectGunInfo->PricesInfo[plugNum].UserPrices;
  713. pricesInfo[plugNum].Balance = ShmSelectGunInfo->PricesInfo[plugNum].Balance;
  714. log_info("id = %d, user prices = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s\r\n",
  715. plugNum,
  716. ShmSelectGunInfo->PricesInfo[plugNum].UserPrices,
  717. pDcChargingInfo->ChargingFee,
  718. ShmSelectGunInfo->PricesInfo[plugNum].Balance,
  719. (uint8_t *)GetCurrency(pSysConfig->BillingData.Currency)
  720. );
  721. }
  722. return PASS;
  723. }
  724. static void addFaultCodeToBuf(uint8_t *Code)
  725. {
  726. uint8_t warningCount = pSysWarning->WarningCount;
  727. if (warningCount < 10) {
  728. memcpy(&pSysWarning->WarningCode[warningCount][0],
  729. Code,
  730. strlen((char *)Code));
  731. pSysWarning->WarningCount++;
  732. }
  733. }
  734. static void removeFaultCodeToBuf(uint8_t *Code)
  735. {
  736. uint8_t find = 0x01;
  737. uint8_t i = 0;
  738. char _code[7] = {0};
  739. sprintf(_code, "%s", Code);
  740. // 把相關的錯誤碼一次移除,避免重複顯示
  741. while (find) {
  742. usleep(128);
  743. find = 0x00;
  744. for (i = 0; i < pSysWarning->WarningCount; i++) {
  745. usleep(128);
  746. if (find == 0x00) {
  747. if (memcmp(&pSysWarning->WarningCode[i][0], _code, 7) == 0) {
  748. find = 0x01;
  749. }
  750. } else {
  751. memcpy(&pSysWarning->WarningCode[i - 1][0],
  752. &pSysWarning->WarningCode[i][0], 7);
  753. }
  754. }
  755. if (find) {
  756. pSysWarning->WarningCount--;
  757. }
  758. }
  759. }
  760. bool CompareArrayIsZero(uint8_t *array, unsigned int array_size)
  761. {
  762. uint8_t i;
  763. if (array != NULL) {
  764. for (i = 0; i < array_size; i++) {
  765. if (array[i] != 0) {
  766. return false;
  767. }
  768. }
  769. }
  770. return true;
  771. }
  772. static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
  773. {
  774. uint8_t ret = 0;
  775. uint8_t i = 0;
  776. uint8_t count = 0;
  777. uint8_t EventCodeTmp[7] = {0};
  778. uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
  779. uint8_t remaindLen = 0;
  780. //uint8_t statusCodeError = 0;
  781. if (dataLen > 0) {
  782. Hexdump((uint8_t *)data, dataLen);
  783. remaindLen = dataLen % WARNING_CODE_SIZE;
  784. if (remaindLen != 0) {
  785. log_info("fail status code length = %d\r\n", dataLen);
  786. dataLen -= remaindLen;
  787. }
  788. if (dataLen < WARNING_CODE_SIZE) {
  789. log_error("fail status code length = %d\r\n", dataLen);
  790. Hexdump(data, dataLen);
  791. if (pSysWarning->WarningCount > 0) {
  792. for (i = 0; i < pSysWarning->WarningCount; i++) {
  793. usleep(128);
  794. if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet
  795. (pSysWarning->WarningCode[i][0] >= 'B' &&
  796. pSysWarning->WarningCode[i][1] >= '4')) {
  797. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  798. memcpy(EventCodeTmp,
  799. pSysWarning->WarningCode[i],
  800. sizeof(EventCodeTmp));
  801. removeFaultCodeToBuf(EventCodeTmp);
  802. }
  803. }
  804. }
  805. return FAIL;
  806. }
  807. for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
  808. usleep(128);
  809. if (strncmp((char *)&data[count + 1], "1", 1) != 0 &&
  810. strncmp((char *)&data[count + 1], "2", 1) != 0 &&
  811. strncmp((char *)&data[count + 1], "3", 1) != 0 &&
  812. (strncmp((char *)&data[count], "B", 1) != 0 &&
  813. strncmp((char *)&data[count + 1], "4", 1) != 0)
  814. ) {
  815. log_error("error status code = %s", (char *)&data[count]);
  816. continue;
  817. }
  818. // misc command status code handle
  819. if (strncmp((char *)&data[count], MISC_ST_MISC_CMD, WARNING_CODE_SIZE) == 0) {
  820. gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
  821. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  822. continue;
  823. } else if (strncmp((char *)&data[count], MISC_ST_VERSION, WARNING_CODE_SIZE) == 0) {
  824. gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
  825. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  826. continue;
  827. }
  828. if (CompareArrayIsZero((uint8_t *)&data[count], WARNING_CODE_SIZE) == true) {
  829. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  830. memcpy(EventCodeTmp, (char *)&data[count], sizeof(EventCodeTmp));
  831. log_error("error status = %s\r\n", EventCodeTmp);
  832. continue;
  833. }
  834. strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
  835. ret = 0;
  836. for (i = 0; i < pSysWarning->WarningCount; i++) {
  837. usleep(128);
  838. if (memcmp(&pSysWarning->WarningCode[i][0],
  839. StatusArray[count / WARNING_CODE_SIZE],
  840. WARNING_CODE_SIZE) == 0) {
  841. ret = 1;
  842. break;
  843. }
  844. }
  845. if (ret == 0) {
  846. addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
  847. }
  848. //Rtn=StatusCodeProcessing(StatusArray[count/6]);
  849. }
  850. } else {
  851. if (CompareArrayIsZero(data, WARNING_CODE_SIZE) == false) {
  852. log_error("power cabinet status code data length is zero, but have data\r\n");
  853. Hexdump((uint8_t *)data, WARNING_CODE_SIZE);
  854. }
  855. }
  856. for (i = 0; i < pSysWarning->WarningCount; i++) {
  857. usleep(128);
  858. if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet 0x33
  859. (pSysWarning->WarningCode[i][0] >= 'B' &&
  860. pSysWarning->WarningCode[i][1] >= '4')) {
  861. ret = 0;
  862. for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
  863. usleep(128);
  864. if (memcmp(&pSysWarning->WarningCode[i][0],
  865. StatusArray[count],
  866. WARNING_CODE_SIZE) == 0) {
  867. ret = 1;
  868. break;
  869. }
  870. }
  871. if (ret == 0) {
  872. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  873. memcpy(EventCodeTmp,
  874. pSysWarning->WarningCode[i],
  875. sizeof(EventCodeTmp));
  876. removeFaultCodeToBuf(EventCodeTmp);
  877. }
  878. }
  879. }
  880. return PASS;
  881. }
  882. static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t plugNum, uint8_t reg)
  883. {
  884. int ret = PASS;
  885. uint8_t rawDataLen = 0;
  886. CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
  887. SoftwareUpdInfo *pSoftwareUpd = NULL;
  888. PcPsuOutput *pPcPsuOutput = NULL;
  889. //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
  890. if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
  891. compareResult(pCsuResult->Data.Result) == FAIL ||
  892. compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
  893. if (reg != REG_CHARGING_PERMISSION) {
  894. return FAIL;
  895. }
  896. }
  897. rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
  898. switch (pCsuResult->Data.Register) {
  899. case REG_MODEL_NAME:
  900. break;
  901. case REG_CONNECTOR_ID:
  902. gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
  903. gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
  904. log_info("OK (Left connector ID = %d, Right connector ID = %d)\n",
  905. pCsuResult->Data.Data[0],
  906. pCsuResult->Data.Data[1]);
  907. break;
  908. case REG_POWER_CABINET_STATUS:
  909. ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
  910. break;
  911. case REG_DISPENSER_STATUS:
  912. memset(&gPreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
  913. memcpy((uint8_t *)&gPreSysWarningInfo,
  914. pSysWarning,
  915. sizeof(struct WARNING_CODE_INFO));
  916. break;
  917. case REG_CHARGING_CAP:
  918. chargingcapabilityHandle(pCsuResult->Data.Data, plugNum);
  919. break;
  920. case REG_CHARGING_TARGET:
  921. break;
  922. case REG_SOFTWARE_UPDATE:
  923. pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
  924. if ((strcmp((char *)pSoftwareUpd->ImgName, "") == 0) ||
  925. (rawDataLen == 0) ||
  926. pSoftwareUpd->UpdateState != 1) {
  927. ret = FAIL;
  928. break;
  929. }
  930. ret = FAIL;
  931. ret = updateFirmwareHandle(pSoftwareUpd->ImgName);
  932. break;
  933. case REG_PLUG_IN_STATE:
  934. break;
  935. case REG_CONNECTOR_STATE:
  936. break;
  937. case REG_USER_ID:
  938. //log_info("%s\n", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
  939. //if (pCsuResult->Data.Data[0] <= 0) {
  940. // return FAIL;
  941. //}
  942. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  943. return COMMAND_RESULT_NG;
  944. }
  945. ShmSelectGunInfo->AuthorStateFromCabinet[plugNum] = pCsuResult->Data.Data[0];
  946. return pCsuResult->Data.Data[0];
  947. break;
  948. case REG_CHARGING_PERMISSION:
  949. //log_info("id = %d, result = %s, action = %s\r\n",
  950. // pCsuResult->Head.ID,
  951. // pCsuResult->Data.Result == 1 ? "OK" : "NG",
  952. // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
  953. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  954. return COMMAND_RESULT_NG;
  955. }
  956. ShmSelectGunInfo->WaitDoCommPermission[plugNum] = pCsuResult->Data.Data[0];
  957. return pCsuResult->Data.Data[0];
  958. break;
  959. case REG_MISC_CONTROL:
  960. miscCommandHandle(rawDataLen, plugNum, pCsuResult->Data.Data);
  961. break;
  962. case REG_REPORT_CSU_VERSION:
  963. case REG_REPORT_OTHER_VERSION:
  964. break;
  965. case REG_PRESENT_CHARGING_INFO:
  966. pPcPsuOutput = (PcPsuOutput *)&pCsuResult->Data.Data[0];
  967. ShmDcCommonData->PcPsuOutput[plugNum].Voltage = ntohs((uint16_t)pPcPsuOutput->Voltage);
  968. ShmDcCommonData->PcPsuOutput[plugNum].Current = ntohs((uint16_t)pPcPsuOutput->Current);
  969. //log_info("%d power cabinet PSU output vol = %f, cur = %f\r\n",
  970. // plugNum,
  971. // (float)ShmDcCommonData->PcPsuOutput[plugNum].Voltage,
  972. // (float)ShmDcCommonData->PcPsuOutput[plugNum].Current);
  973. break;
  974. case REG_QRCODE_URL_INFO:
  975. ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
  976. break;
  977. case REG_WAIT_PLUG_IT_STATE:
  978. break;
  979. case REG_Ground_Fault_Detection:
  980. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  981. return COMMAND_RESULT_NG;
  982. }
  983. //集電弓relay 不打開才能進入動作
  984. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  985. if(pDcChargingInfo->PantographFlag == YES)
  986. {
  987. GroundFaultDetection *gfd = (GroundFaultDetection *)&pCsuResult->Data.Data[0];
  988. if(pDcChargingInfo->GroundFaultStatus != gfd->Status)
  989. {
  990. log_info("id = %d, GFD Result = %d\r\n",
  991. pCsuResult->Head.ID,
  992. gfd->Status);
  993. }
  994. pDcChargingInfo->GroundFaultStatus = gfd->Status;
  995. }
  996. break;
  997. default:
  998. break;
  999. }
  1000. return ret;
  1001. }
  1002. static int composeSocketData(int fd,
  1003. uint8_t id,
  1004. uint8_t opCode,
  1005. uint8_t reg,
  1006. uint8_t dataLen,
  1007. uint8_t *data)
  1008. {
  1009. int ret = PASS;
  1010. int size = 0;
  1011. int sendPktLen = 0;
  1012. uint8_t i = 0;
  1013. uint8_t plugNum = 0;
  1014. CsuCmdPkt csuCmdPkt = {0};
  1015. CsuResultPkt csuResult = {0};
  1016. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  1017. csuCmdPkt.Head.ID = id;
  1018. csuCmdPkt.Head.OP = opCode;
  1019. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  1020. csuCmdPkt.Data.Register = reg;
  1021. if ((data != NULL) && (dataLen > 0)) {
  1022. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  1023. }
  1024. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  1025. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  1026. //send command packet
  1027. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  1028. log_error("TCP socket send packet fail = %d\r\n", size);
  1029. gDoCommGblData.DisConnCount++;
  1030. return FAIL;
  1031. }
  1032. //receive result head
  1033. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  1034. log_error("TCP socket RX head fail = %d\r\n", size);
  1035. gDoCommGblData.DisConnCount++;
  1036. return FAIL;
  1037. }
  1038. //receive result raw data
  1039. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  1040. log_error("TCP socket RX data fail = %d\r\n", size);
  1041. gDoCommGblData.DisConnCount++;
  1042. return FAIL;
  1043. }
  1044. for (i = 0; i < sizeof(gDoCommGblData.ConnectorID); i++) {
  1045. if (id == gDoCommGblData.ConnectorID[i]) {
  1046. plugNum = i;
  1047. break;
  1048. }
  1049. }
  1050. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, plugNum, csuCmdPkt.Data.Register);
  1051. gDoCommGblData.DisConnCount = 0; //送收資料沒有問題, 清除中斷網路計數
  1052. return ret;
  1053. }
  1054. static int writeWaitPlugItState(int fd, uint8_t id)
  1055. {
  1056. int ret = PASS;
  1057. uint8_t data[2] = {pSysInfo->WaitForPlugit, 0};
  1058. //printf("WaitForPlugit = %d\r\n", pSysInfo->WaitForPlugit);
  1059. if ((ret = composeSocketData(fd,
  1060. id,
  1061. OP_WRITE_DATA,
  1062. REG_WAIT_PLUG_IT_STATE,
  1063. 1,
  1064. &data[0])) == FAIL) {
  1065. return ret;
  1066. }
  1067. return ret;
  1068. }
  1069. static int readQRcodeURLAndSystemDate(int fd)
  1070. {
  1071. int ret = PASS;
  1072. ret = composeSocketData(fd,
  1073. ID_REGISTER,
  1074. OP_READ_DATA,
  1075. REG_QRCODE_URL_INFO,
  1076. 0,
  1077. NULL);
  1078. return ret;
  1079. }
  1080. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  1081. {
  1082. int ret = PASS;
  1083. uint8_t dataBuf[16] = {0};
  1084. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  1085. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1086. if(pDcChargingInfo->PantographFlag == NO)
  1087. {
  1088. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(pDcChargingInfo->PresentChargingVoltage * 10));
  1089. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(pDcChargingInfo->PresentChargingCurrent * 10));
  1090. }
  1091. else
  1092. {
  1093. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Voltage));
  1094. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Current));
  1095. }
  1096. pPreChargingInfo->RemainChargingDuration = htonl(pDcChargingInfo->RemainChargingDuration);
  1097. pPreChargingInfo->EvBatterySoc = pDcChargingInfo->EvBatterySoc;
  1098. ret = composeSocketData(fd,
  1099. id,
  1100. OP_WRITE_DATA,
  1101. REG_PRESENT_CHARGING_INFO,
  1102. sizeof(PreChargingInfo),
  1103. (uint8_t *)pPreChargingInfo);
  1104. return ret;
  1105. }
  1106. static int writeOtherModuleVersion(int fd)
  1107. {
  1108. int ret = PASS;
  1109. uint8_t data[255] = {0};
  1110. uint8_t dataLen = 0;
  1111. //report other module version message
  1112. memcpy(&data[dataLen], pSysInfo->FanModuleFwRev, VERSION_BUF_SIZE);
  1113. dataLen += VERSION_BUF_SIZE;
  1114. memcpy(&data[dataLen], pSysInfo->RelayModuleFwRev, VERSION_BUF_SIZE);
  1115. dataLen += VERSION_BUF_SIZE;
  1116. memcpy(&data[dataLen], pSysInfo->Connector1FwRev, VERSION_BUF_SIZE);
  1117. dataLen += VERSION_BUF_SIZE;
  1118. memcpy(&data[dataLen], pSysInfo->Connector2FwRev, VERSION_BUF_SIZE);
  1119. dataLen += VERSION_BUF_SIZE;
  1120. memcpy(&data[dataLen], pSysInfo->LedModuleFwRev, VERSION_BUF_SIZE);
  1121. dataLen += VERSION_BUF_SIZE;
  1122. ret = composeSocketData(fd,
  1123. ID_REGISTER,
  1124. OP_WRITE_DATA,
  1125. REG_REPORT_OTHER_VERSION,
  1126. dataLen,
  1127. &data[0]);
  1128. return ret;
  1129. }
  1130. static int writeCsuModuleVersion(int fd)
  1131. {
  1132. int ret = PASS;
  1133. uint8_t data[255] = {0};
  1134. uint8_t dataLen = 0;
  1135. //report CSU platform version message
  1136. memcpy(&data[dataLen], pSysInfo->CsuBootLoadFwRev, VERSION_BUF_SIZE);
  1137. dataLen += VERSION_BUF_SIZE;
  1138. memcpy(&data[dataLen], pSysInfo->CsuKernelFwRev, VERSION_BUF_SIZE);
  1139. dataLen += VERSION_BUF_SIZE;
  1140. memcpy(&data[dataLen], pSysInfo->CsuRootFsFwRev, VERSION_BUF_SIZE);
  1141. dataLen += VERSION_BUF_SIZE;
  1142. memcpy(&data[dataLen], pSysInfo->CsuPrimFwRev, VERSION_BUF_SIZE);
  1143. dataLen += VERSION_BUF_SIZE;
  1144. ret = composeSocketData(fd,
  1145. ID_REGISTER,
  1146. OP_WRITE_DATA,
  1147. REG_REPORT_CSU_VERSION,
  1148. dataLen,
  1149. &data[0]);
  1150. return ret;
  1151. }
  1152. static int readMiscCommand(int fd, uint8_t id)
  1153. {
  1154. int ret = PASS;
  1155. ret = composeSocketData(fd,
  1156. id,
  1157. OP_READ_DATA,
  1158. REG_MISC_CONTROL,
  1159. 0,
  1160. NULL);
  1161. return ret;
  1162. }
  1163. static int readChargePermission(int fd, uint8_t id)
  1164. {
  1165. return composeSocketData(fd,
  1166. id,
  1167. OP_READ_DATA,
  1168. REG_CHARGING_PERMISSION,
  1169. 0,
  1170. NULL);
  1171. }
  1172. static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id)
  1173. {
  1174. int ret = PASS;
  1175. uint8_t dataBuf[1] = {status};
  1176. ret = composeSocketData(fd,
  1177. id,
  1178. OP_WRITE_DATA,
  1179. REG_Ground_Fault_Detection,
  1180. 1,
  1181. &dataBuf[0]);
  1182. return ret;
  1183. }
  1184. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  1185. {
  1186. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  1187. return FAIL;
  1188. }
  1189. return composeSocketData(fd,
  1190. id,
  1191. OP_WRITE_DATA,
  1192. REG_USER_ID,
  1193. strlen((char *)pUserID),
  1194. pUserID);
  1195. }
  1196. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  1197. {
  1198. uint8_t dataBuf[10] = {'\0'};
  1199. ConnectorState *pConnState = (ConnectorState *)dataBuf;
  1200. static char vendorErrorCodeTmp[2][WARNING_CODE_SIZE] = {0};
  1201. int ret = PASS;
  1202. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1203. pConnState->ConnectorTemp = pDcChargingInfo->ConnectorTemp;
  1204. pConnState->ChillerTemp = pDcChargingInfo->ChillerTemp;
  1205. if (pDcChargingInfo->SystemStatus <= S_AUTHORIZING) {
  1206. pConnState->State = CONN_ST_IDLE; //idle
  1207. strncpy((char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1208. "",
  1209. sizeof(ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode));
  1210. strncpy(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE);
  1211. } else if ((pDcChargingInfo->SystemStatus <= S_PREPARING_FOR_EVSE) ||
  1212. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST0) ||
  1213. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST1)) {
  1214. pConnState->State = CONN_ST_PREPARING; //preparing
  1215. } else if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  1216. pConnState->State = CONN_ST_CHARGING; //charging
  1217. } else if (pDcChargingInfo->SystemStatus == S_TERMINATING) {
  1218. pConnState->State = CONN_ST_TERMINATING; //terminating
  1219. } else if ((pDcChargingInfo->SystemStatus == S_ALARM) ||
  1220. (pDcChargingInfo->SystemStatus == S_FAULT)) {
  1221. pConnState->State = CONN_ST_ALARM;
  1222. strncpy((char *)pConnState->WarningCode,
  1223. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1224. WARNING_CODE_SIZE);
  1225. if (strncmp(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE) == 0) {
  1226. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1227. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1228. WARNING_CODE_SIZE);
  1229. vendorErrorCodeTmp[plugNum][6] = '\0';
  1230. log_info("1 ID = %d, VendorErrorCode = %s\r\n",
  1231. plugNum,
  1232. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1233. } else {
  1234. if (strncmp(&vendorErrorCodeTmp[plugNum][0],
  1235. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1236. WARNING_CODE_SIZE) != 0
  1237. ) {
  1238. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1239. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1240. WARNING_CODE_SIZE);
  1241. vendorErrorCodeTmp[plugNum][6] = '\0';
  1242. log_info("2 ID = %d, VendorErrorCode = %s\r\n",
  1243. plugNum,
  1244. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1245. }
  1246. }
  1247. }
  1248. ret = composeSocketData(fd,
  1249. id,
  1250. OP_WRITE_DATA,
  1251. REG_CONNECTOR_STATE,
  1252. sizeof(dataBuf) - 1,
  1253. &dataBuf[0]);
  1254. return ret;
  1255. }
  1256. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  1257. {
  1258. int ret = PASS;
  1259. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1260. uint8_t dataBuf[2] = {pDcChargingInfo->ConnectorPlugIn, 0};
  1261. ret = composeSocketData(fd,
  1262. id,
  1263. OP_WRITE_DATA,
  1264. REG_PLUG_IN_STATE,
  1265. 1,
  1266. &dataBuf[0]);
  1267. return ret;
  1268. }
  1269. static int readSoftwareUpdate(int fd, uint8_t gunID)
  1270. {
  1271. int ret = PASS;
  1272. uint8_t dataBuf[2] = {pSysInfo->FirmwareUpdate, 0};
  1273. ret = composeSocketData(fd,
  1274. gunID,
  1275. OP_READ_DATA,
  1276. REG_SOFTWARE_UPDATE,
  1277. 1,
  1278. &dataBuf[0]);
  1279. return ret;
  1280. }
  1281. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  1282. {
  1283. int ret = PASS;
  1284. float ChargingVolt, ChargingAmp;
  1285. uint8_t dataBuf[4] = {0};
  1286. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1287. ChargingVolt = pDcChargingInfo->EvBatterytargetVoltage;
  1288. ChargingAmp = pDcChargingInfo->EvBatterytargetCurrent;
  1289. ChargingVolt *= 10;
  1290. ChargingAmp *= 10;
  1291. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1292. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1293. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1294. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1295. ret = composeSocketData(fd,
  1296. id,
  1297. OP_WRITE_DATA,
  1298. REG_CHARGING_TARGET,
  1299. sizeof(dataBuf),
  1300. &dataBuf[0]);
  1301. return ret;
  1302. }
  1303. static int readChargingCapability(int fd, uint8_t id)
  1304. {
  1305. int ret = PASS;
  1306. ret = composeSocketData(fd,
  1307. id,
  1308. OP_READ_DATA,
  1309. REG_CHARGING_CAP,
  1310. 0,
  1311. NULL);
  1312. return ret;
  1313. }
  1314. static int writeDispenserStatus(int fd, uint8_t gunID)
  1315. {
  1316. uint8_t warningCount = 0;
  1317. uint8_t count = 0;
  1318. uint8_t CurWarnCodeTmp[10][6] = {0};
  1319. uint8_t PreWarnCodeTmp[10][6] = {0};
  1320. int ret = PASS;
  1321. if ((pSysWarning->WarningCount <= 0) &&
  1322. (gPreSysWarningInfo.WarningCount <= 0)) {
  1323. return FAIL;
  1324. }
  1325. warningCount = pSysWarning->WarningCount;
  1326. for (count = 0; count < warningCount; count++) {
  1327. memcpy(CurWarnCodeTmp[count], pSysWarning->WarningCode[count], WARNING_CODE_SIZE);
  1328. }
  1329. warningCount = gPreSysWarningInfo.WarningCount;
  1330. for (count = 0; count < warningCount; count++) {
  1331. memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1332. }
  1333. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1334. return FAIL;
  1335. }
  1336. ret = composeSocketData(fd,
  1337. gunID,
  1338. OP_WRITE_DATA,
  1339. REG_DISPENSER_STATUS,
  1340. strlen((char *)CurWarnCodeTmp),
  1341. &CurWarnCodeTmp[0][0]);
  1342. return ret;
  1343. }
  1344. static int readPowerCabinetStatus(int fd, uint8_t gunID)
  1345. {
  1346. int ret = PASS;
  1347. ret = composeSocketData(fd,
  1348. gunID,
  1349. OP_READ_DATA,
  1350. REG_POWER_CABINET_STATUS,
  1351. 0,
  1352. NULL);
  1353. return ret;
  1354. }
  1355. static int readConnectorID(int fd)
  1356. {
  1357. int ret = PASS;
  1358. ret = composeSocketData(fd,
  1359. ID_REGISTER,
  1360. OP_READ_DATA,
  1361. REG_CONNECTOR_ID,
  1362. 0,
  1363. NULL);
  1364. return ret;
  1365. }
  1366. static int WriteModelName(int fd)
  1367. {
  1368. int ret = PASS;
  1369. uint8_t Tmp = 0;
  1370. uint8_t TmpBuf[255] = {0};
  1371. memcpy(TmpBuf,
  1372. pSysConfig->ModelName,
  1373. strlen((char *)pSysConfig->ModelName));
  1374. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1375. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1376. ShmPrimaryMcuData->InputDet.bits.Key0);
  1377. TmpBuf[strlen((char *)pSysConfig->ModelName)] = Tmp; //Dispenser switch value
  1378. ret = composeSocketData(fd,
  1379. ID_REGISTER,
  1380. OP_WRITE_DATA,
  1381. REG_MODEL_NAME,
  1382. strlen((char *)pSysConfig->ModelName) + 1,
  1383. &TmpBuf[0]);
  1384. return ret;
  1385. }
  1386. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1387. {
  1388. sleep(3); //wait 1 second
  1389. if ((*pValue) >= maxCount) {
  1390. setTcpStatus(ABNORMAL);
  1391. } else {
  1392. (*pValue)++;
  1393. }
  1394. }
  1395. static int CheckNetworkStatus(void)
  1396. {
  1397. char *ipAddr = (char *)&pSysConfig->Eth0Interface.EthIpAddress;
  1398. //printf("Check network IP Header = %s\n", ipAddr);
  1399. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1400. return 1;
  1401. }
  1402. return 0;
  1403. }
  1404. static void updateFirmwareProcess(int fd, uint8_t gunID, uint8_t totalConnCount)
  1405. {
  1406. bool canUpdateFirmware = true;
  1407. uint8_t plugNum = 0;
  1408. uint8_t ackCount = 5;
  1409. struct timeb updateTime;
  1410. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1411. if (pSysWarning->Level != 2) {
  1412. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1413. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  1414. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  1415. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  1416. canUpdateFirmware = false;
  1417. }
  1418. }
  1419. }
  1420. if (canUpdateFirmware) {
  1421. ftime(&updateTime);
  1422. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  1423. readSoftwareUpdate(fd, gunID);
  1424. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1425. }
  1426. if (pSysInfo->FirmwareUpdate == YES) {
  1427. while (ackCount != 0) {
  1428. ftime(&updateTime);
  1429. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  1430. readSoftwareUpdate(fd, gunID);
  1431. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1432. if (pSysInfo->FirmwareUpdate == NO) {
  1433. ackCount--;
  1434. }
  1435. }
  1436. usleep(128);
  1437. }
  1438. }
  1439. }
  1440. }
  1441. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1442. {
  1443. int ret = 0;
  1444. uint8_t gunID = 0;
  1445. struct timeb AuthNowTime;
  1446. #if defined DD360Audi
  1447. gunID = gDoCommGblData.ConnectorID[pSysInfo->CurGunSelected];
  1448. //gunID = gDoCommGblData.ConnectorID[plugNum];
  1449. #else
  1450. gunID = ID_REGISTER;
  1451. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0; //非Audi 不需要等待主櫃回報餘額
  1452. #endif // DD360Audi
  1453. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) ||
  1454. (pSysInfo->AuthorizeFlag == YES)) {
  1455. ftime(&AuthNowTime);
  1456. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1457. ret = writeUserID(fd,
  1458. gunID,
  1459. pSysConfig->UserId);
  1460. if (ret >= 0) {
  1461. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1462. 0,
  1463. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1464. if (ret == 0) {
  1465. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1466. } else {
  1467. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1468. }
  1469. //printf("%d Balance = %.2f, %.2f\r\n",
  1470. // plugNum,
  1471. // ShmSelectGunInfo->PricesInfo[plugNum].Balance,
  1472. // FAIL_BALANCE_PRICES);
  1473. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1474. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = YES; //isAuthorizedComplete
  1475. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
  1476. pSysInfo->AuthorizeFlag = NO;
  1477. ShmPsuData->SystemAvailablePower = NO;
  1478. ShmPsuData->SystemPresentPsuQuantity = NO;
  1479. }
  1480. }
  1481. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1482. }
  1483. }
  1484. }
  1485. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1486. {
  1487. int ret = DISPENSER_INIT_SUCC;
  1488. int isContinue = 1;
  1489. struct timeb NowTime;
  1490. while (isContinue) {
  1491. usleep(128);
  1492. ftime(&NowTime);
  1493. switch (curReg) {
  1494. case REG_MODEL_NAME:
  1495. if (WriteModelName(fd) == PASS) {
  1496. gDoCommGblData.DisConnCount = 0;
  1497. curReg = REG_CONNECTOR_ID;
  1498. } else {
  1499. sleep(1);
  1500. }
  1501. break;
  1502. case REG_CONNECTOR_ID:
  1503. if (readConnectorID(fd) == PASS) {
  1504. gDoCommGblData.DisConnCount = 0;
  1505. curReg = REG_REPORT_CSU_VERSION;
  1506. } else {
  1507. sleep(1);
  1508. }
  1509. break;
  1510. case REG_POWER_CABINET_STATUS:
  1511. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1512. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1513. readPowerCabinetStatus(fd, gunID);
  1514. ftime(&gRegTimeUp[plugNum][curReg]);
  1515. }
  1516. curReg = REG_DISPENSER_STATUS;
  1517. break;
  1518. case REG_DISPENSER_STATUS:
  1519. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1520. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1521. writeDispenserStatus(fd, gunID);
  1522. ftime(&gRegTimeUp[plugNum][curReg]);
  1523. }
  1524. curReg = REG_PLUG_IN_STATE;
  1525. break;
  1526. //case REG_CHARGING_CAP:
  1527. // readChargingCapability(fd, plugNum, gunID);
  1528. // break;
  1529. //case REG_CHARGING_TARGET:
  1530. // writeChargingTarget(fd, plugNum, gunID);
  1531. // break;
  1532. //case REG_SOFTWARE_UPDATE:
  1533. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1534. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > L OOP_RETRY_TIME||
  1535. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0 * 1) {
  1536. // readSoftwareUpdate(fd);
  1537. // ftime(&gRegTimeUp[plugNum][curReg]);
  1538. // }
  1539. // while (pSysInfo->FirmwareUpdate == YES) {
  1540. // ftime(&NowTime);
  1541. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowT ime) > LOOP_RETRY_TIME||
  1542. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1543. // readSoftwareUpdate(fd);
  1544. // }
  1545. // usleep(128);
  1546. // }
  1547. // }
  1548. // isContinue = 0;
  1549. // break;
  1550. case REG_PLUG_IN_STATE:
  1551. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1552. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1553. writePlugInStatus(fd, plugNum, gunID);
  1554. ftime(&gRegTimeUp[plugNum][curReg]);
  1555. }
  1556. curReg = REG_CONNECTOR_STATE;
  1557. break;
  1558. case REG_CONNECTOR_STATE:
  1559. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1560. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1561. writeConnectorState(fd, plugNum, gunID);
  1562. ftime(&gRegTimeUp[plugNum][curReg]);
  1563. }
  1564. //check misc command from power cabinet
  1565. if (gDoCommGblData.MiscCmd != 0) {
  1566. if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  1567. log_error("misc command failed = %x\r\n", gDoCommGblData.MiscCmd);
  1568. }
  1569. curReg = gDoCommGblData.MiscCmd;
  1570. } else {
  1571. curReg = REG_QRCODE_URL_INFO;
  1572. }
  1573. break;
  1574. //case REG_USER_ID:
  1575. // writeUserID(fd, gunID, uint8_t *pUserID);
  1576. // break;
  1577. //case REG_CHARGING_PERMISSION:
  1578. // readChargePermission(fd, gunID);
  1579. // break;
  1580. case REG_MISC_CONTROL:
  1581. readMiscCommand(fd, gunID);
  1582. isContinue = 0;
  1583. break;
  1584. case REG_REPORT_CSU_VERSION:
  1585. case REG_REPORT_OTHER_VERSION:
  1586. if (gDoCommGblData.MiscCmd != 0) {
  1587. writeCsuModuleVersion(fd);
  1588. writeOtherModuleVersion(fd);
  1589. clearMiscCommand();
  1590. isContinue = 0;
  1591. } else {
  1592. //power up write dispenser version and get power cabinet system ID
  1593. if ((writeCsuModuleVersion(fd) == PASS) &&
  1594. (writeOtherModuleVersion(fd) == PASS) &&
  1595. (readQRcodeURLAndSystemDate(fd) == PASS)
  1596. ) {
  1597. //gDoCommGblData.DisConnCount = 0;
  1598. isContinue = 0;
  1599. }
  1600. }
  1601. break;
  1602. //case REG_PRESENT_CHARGING_INFO:
  1603. // break;
  1604. case REG_QRCODE_URL_INFO:
  1605. if (gunID == 1) {
  1606. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10) ||
  1607. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0
  1608. ) {
  1609. readQRcodeURLAndSystemDate(fd);
  1610. ftime(&gRegTimeUp[plugNum][curReg]);
  1611. }
  1612. }
  1613. isContinue = 0;
  1614. //curReg = REG_SOFTWARE_UPDATE;
  1615. break;
  1616. //case REG_WAIT_PLUG_IT_STATE:
  1617. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1618. // writeWaitPlugItState(fd, gunID);
  1619. // ftime(&gRegTimeUp[plugNum][curReg]);
  1620. // }
  1621. // isContinue = 0;
  1622. // break;
  1623. default:
  1624. log_error("error curReg = %x\r\n", curReg);
  1625. gDoCommGblData.MiscCmd = 0;
  1626. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  1627. isContinue = 0;
  1628. break;
  1629. }
  1630. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  1631. return DISPENSER_SOCKET_RECONN;
  1632. }
  1633. }//for
  1634. return ret;
  1635. }
  1636. //static bool isDetectPlugin()
  1637. //{
  1638. // if (pSysInfo->WaitForPlugit == YES) {
  1639. // return YES;
  1640. // }
  1641. //
  1642. // return NO;
  1643. //}
  1644. static void systemStatusProcess(int fd, uint8_t totalGun, uint8_t plugNum, uint8_t gunID)
  1645. {
  1646. uint8_t i = 0;
  1647. struct timeb AuthNowTime = {0};
  1648. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1649. switch (pDcChargingInfo->SystemStatus) {
  1650. case S_IDLE:
  1651. case S_RESERVATION:
  1652. case S_MAINTAIN:
  1653. case S_ALARM:
  1654. case S_AUTHORIZING:
  1655. checkAuthorProcess(fd, plugNum);
  1656. //authorization complete, write wait plug it state
  1657. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance == FAIL_BALANCE_PRICES) {
  1658. break;
  1659. }
  1660. ftime(&AuthNowTime);
  1661. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  1662. DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) < 0
  1663. ) {
  1664. writeWaitPlugItState(fd, gunID);
  1665. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  1666. }
  1667. ftime(&AuthNowTime);
  1668. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1669. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1670. ) {
  1671. readChargingCapability(fd, gunID);
  1672. writeGroundFaultDetection(fd, 0, gunID);
  1673. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1674. }
  1675. break;
  1676. case S_PREPARNING: //get permission
  1677. ftime(&AuthNowTime);
  1678. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME ||
  1679. DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) < 0
  1680. ) {
  1681. writeConnectorState(fd, plugNum, gunID);
  1682. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  1683. }
  1684. ftime(&AuthNowTime);
  1685. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1686. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1687. ) {
  1688. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  1689. for (i = 0; i < totalGun; i++) {
  1690. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1691. ShmPsuData->SystemAvailablePower += pDcChargingInfo->AvailableChargingPower;
  1692. }
  1693. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  1694. }
  1695. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1696. }
  1697. ftime(&AuthNowTime);
  1698. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1699. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1700. ) {
  1701. writePresentChargingInfo(fd, plugNum, gunID);
  1702. writeGroundFaultDetection(fd, 0, gunID);
  1703. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1704. }
  1705. break;
  1706. case S_PREPARING_FOR_EV://wait connector lock
  1707. ftime(&AuthNowTime);
  1708. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1709. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1710. ) {
  1711. readChargingCapability(fd, gunID);
  1712. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1713. }
  1714. ftime(&AuthNowTime);
  1715. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1716. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1717. ) {
  1718. if (readChargePermission(fd, gunID) == 0) {
  1719. log_info("S_PREPARING_FOR_EV Stop charging by power cabinet's permission = %d, %d\r\n",
  1720. plugNum,
  1721. REG_CHARGING_PERMISSION);
  1722. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1723. }
  1724. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1725. }
  1726. ftime(&AuthNowTime);
  1727. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1728. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1729. ) {
  1730. writePresentChargingInfo(fd, plugNum, gunID);
  1731. writeGroundFaultDetection(fd, 0, gunID);
  1732. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1733. }
  1734. break;
  1735. case S_PREPARING_FOR_EVSE: //insaulation test
  1736. case S_CCS_PRECHARGE_ST0:
  1737. case S_CCS_PRECHARGE_ST1:
  1738. writeChargingTarget(fd, plugNum, gunID);
  1739. writeGroundFaultDetection(fd, 1, gunID);
  1740. ftime(&AuthNowTime);
  1741. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1742. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1743. ) {
  1744. readChargingCapability(fd, gunID);
  1745. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1746. }
  1747. ftime(&AuthNowTime);
  1748. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1749. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1750. ) {
  1751. if (readChargePermission(fd, gunID) == 0) {
  1752. log_info("S_PREPARING_FOR_EVSE Stop charging by power cabinet's permission = %d, %d\r\n",
  1753. plugNum,
  1754. REG_CHARGING_PERMISSION);
  1755. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1756. }
  1757. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1758. }
  1759. ftime(&AuthNowTime);
  1760. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1761. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1762. ) {
  1763. writePresentChargingInfo(fd, plugNum, gunID);
  1764. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1765. }
  1766. break;
  1767. case S_CHARGING: //charging
  1768. case S_TERMINATING:
  1769. if(pDcChargingInfo->Type == _Type_GB || pDcChargingInfo->Type == _Type_Chademo)
  1770. {
  1771. writeGroundFaultDetection(fd, 0, gunID);
  1772. }
  1773. else
  1774. {
  1775. writeGroundFaultDetection(fd, 1, gunID);
  1776. }
  1777. ftime(&AuthNowTime);
  1778. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1779. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1780. ) {
  1781. readChargingCapability(fd, gunID);
  1782. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1783. }
  1784. ftime(&AuthNowTime);
  1785. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1786. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1787. ) {
  1788. if (readChargePermission(fd, gunID) == 0) {
  1789. log_info("Stop charging by power cabinet's permission = %d, %d\r\n",
  1790. plugNum,
  1791. REG_CHARGING_PERMISSION);
  1792. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1793. //printf("%d StopChargeFlag = %d\r\n", plugNum, pDcChargingInfo->StopChargeFlag);
  1794. }
  1795. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1796. }
  1797. writeChargingTarget(fd, plugNum, gunID);
  1798. ftime(&AuthNowTime);
  1799. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1800. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1801. ) {
  1802. writePresentChargingInfo(fd, plugNum, gunID);
  1803. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1804. }
  1805. break;
  1806. case S_COMPLETE:
  1807. ftime(&AuthNowTime);
  1808. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1809. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1810. ) {
  1811. readChargingCapability(fd, gunID);
  1812. writeGroundFaultDetection(fd, 0, gunID);
  1813. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1814. }
  1815. break;
  1816. default:
  1817. break;
  1818. }
  1819. }
  1820. static int networkCreatePorcess(void)
  1821. {
  1822. int fd = 0;
  1823. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  1824. usleep(128);
  1825. }
  1826. setTcpStatus(ABNORMAL);
  1827. /**************** Check Network **********/
  1828. log_info("Check Dispenser Network Information");
  1829. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  1830. usleep(125);
  1831. }
  1832. while (!CheckNetworkStatus()) {
  1833. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  1834. log_error("disconnect count = % d\r\n", gDoCommGblData.DisConnCount);
  1835. }
  1836. log_info("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  1837. pSysConfig->Eth0Interface.EthIpAddress,
  1838. pSysConfig->Eth0Interface.EthSubmaskAddress,
  1839. pSysConfig->Eth0Interface.EthGatewayAddress);
  1840. gDoCommGblData.DisConnCount = 0;
  1841. /**************** Power cabinet connection **********/
  1842. log_info("Connect to Power Cabinet");
  1843. while ((fd = doCommConnToServer()) <= 0) {
  1844. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  1845. log_error("disconnect count = %d, fd = %d\r\n", gDoCommGblData.DisConnCount, fd);
  1846. }
  1847. log_info("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  1848. fd,
  1849. pSysConfig->Eth0Interface.EthIpAddress,
  1850. pSysConfig->Eth0Interface.EthSubmaskAddress,
  1851. pSysConfig->Eth0Interface.EthGatewayAddress);
  1852. gDoCommGblData.DisConnCount = 0;
  1853. destroySelectGun(DESTROY_ALL_SEL);
  1854. sleep(3);
  1855. setTcpStatus(NORMAL);
  1856. return fd;
  1857. }
  1858. int main(int argc, char *argv[])
  1859. {
  1860. uint8_t plugNum = 0, gunID = 0;
  1861. uint8_t index = 0;
  1862. uint8_t totalConnCount = 0;
  1863. uint8_t initDone = DISPENER_INIT_FAIL;
  1864. char tmpbuf[256] = {0};
  1865. int fd = 0;
  1866. int isContinue = 1;
  1867. InitSocketSigPipe();
  1868. /**************** Initialization **********/
  1869. if (CreateAllCsuShareMemory() == FAIL) {
  1870. log_error("create share memory error\r\n");
  1871. return FAIL;
  1872. }
  1873. MappingGunChargingInfo("DoComm Task");
  1874. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  1875. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  1876. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  1877. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  1878. ShmPsuData = (struct PsuData *)GetShmPsuData();
  1879. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  1880. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  1881. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  1882. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  1883. totalConnCount = pSysConfig->TotalConnectorCount;
  1884. for (index = 0; index < totalConnCount; index++) {
  1885. clearPricesInfo(index);
  1886. }
  1887. //initial trigger message timer
  1888. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  1889. ftime(&gRegTimeUp[0][index]);
  1890. usleep(128);
  1891. ftime(&gRegTimeUp[1][index]);
  1892. usleep(50000);
  1893. }
  1894. setTcpStatus(NORMAL);
  1895. while (isContinue) {
  1896. if (initDone == DISPENER_INIT_FAIL) {
  1897. fd = networkCreatePorcess();
  1898. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  1899. } else {
  1900. plugNum = 0;
  1901. gunID = 0;
  1902. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1903. //checkAuthorProcess(fd, plugNum);
  1904. //plugNum : setup chargingData value for bottom layer
  1905. //gunID : connector Id from power cabinet, 1 = left gun, 2 = right gun,
  1906. gunID = gDoCommGblData.ConnectorID[plugNum];
  1907. systemStatusProcess(fd, totalConnCount, plugNum, gunID);
  1908. initDone = messageTrigger(fd,
  1909. plugNum,
  1910. gunID,
  1911. REG_POWER_CABINET_STATUS);
  1912. if (initDone == DISPENSER_SOCKET_RECONN) {
  1913. break;
  1914. }
  1915. }
  1916. }
  1917. if (initDone != DISPENSER_SOCKET_RECONN) {
  1918. //update dispenser firmware
  1919. updateFirmwareProcess(fd, gDoCommGblData.ConnectorID[0], totalConnCount);
  1920. usleep(100000);
  1921. continue;
  1922. }
  1923. //Ethernet error recovery handle
  1924. log_info("Disconnected from power cabinet...re-connecting");
  1925. setTcpStatus(ABNORMAL);
  1926. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  1927. sprintf(tmpbuf,
  1928. "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  1929. pSysConfig->SystemId);
  1930. system(tmpbuf);
  1931. }
  1932. gDoCommGblData.DisConnCount = 0;
  1933. gDoCommGblData.SeqNum = 0;
  1934. closeSocket(fd);
  1935. fd = 0;
  1936. initDone = DISPENER_INIT_FAIL;
  1937. }
  1938. }