DoComm.c 73 KB

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