DoComm.c 75 KB

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