DoComm.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  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 defined DD360Audi
  559. if (getSelGunWaitToAuthor(plugNum) == FAIL) {
  560. log_error("Remote start gun already charging");
  561. break;
  562. }
  563. if (pSysInfo->CurGunSelected != (plugNum)) {
  564. if (plugNum == LEFT_GUN_NUM &&
  565. (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  566. ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  567. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  568. strcpy((char *)pSysConfig->UserId, "");
  569. pSysInfo->WaitForPlugit = NO;
  570. sleep(1); //Jerry add
  571. pSysInfo->SystemPage = _LCM_SELECT_GUN;
  572. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  573. pSysInfo->SystemTimeoutFlag = Timeout_None;
  574. }
  575. if (plugNum == RIGHT_GUN_NUM &&
  576. (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  577. ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  578. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  579. strcpy((char *)pSysConfig->UserId, "");
  580. pSysInfo->WaitForPlugit = NO;
  581. sleep(1); //Jerry add
  582. pSysInfo->SystemPage = _LCM_SELECT_GUN;
  583. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  584. pSysInfo->SystemTimeoutFlag = Timeout_None;
  585. }
  586. pSysInfo->CurGunSelected = (plugNum);
  587. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  588. }
  589. #else
  590. pSysInfo->CurGunSelected = (plugNum);
  591. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  592. #endif //DD360Audi
  593. setConfirmSelGun(pSysInfo->CurGunSelected);
  594. ShmOCPP16Data->CsMsg.bits[plugNum].RemoteStartTransactionReq = YES;
  595. ShmSelectGunInfo->PricesInfo[plugNum].Balance = 0.00;
  596. clearMiscCommand();
  597. break;
  598. case MISC_CMD_REMOTE_STOP_CHARGING:
  599. if (value != YES) {
  600. if (plugNum == 1) {
  601. clearMiscCommand();
  602. }
  603. break;
  604. }
  605. strcpy((char *)pSysConfig->UserId, "");
  606. pSysInfo->WaitForPlugit = NO;
  607. pSysInfo->SystemPage = _LCM_SELECT_GUN;
  608. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  609. pSysInfo->SystemTimeoutFlag = Timeout_None;
  610. destroySelectGun(plugNum);
  611. clearMiscCommand();
  612. break;
  613. case MISC_CMD_REMOTE_UNLOCK:
  614. if (value != YES) {
  615. if (plugNum == 1) {
  616. clearMiscCommand();
  617. }
  618. break;
  619. }
  620. if (isDetectPlugin() == YES) {
  621. ClearDetectPluginFlag();
  622. strcpy((char *)pSysConfig->UserId, "");
  623. pSysInfo->WaitForPlugit = NO;
  624. pSysInfo->SystemPage = _LCM_SELECT_GUN;
  625. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  626. pSysInfo->SystemTimeoutFlag = Timeout_None;
  627. destroySelectGun(plugNum);
  628. } else {
  629. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  630. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  631. }
  632. break;
  633. case MISC_CMD_AUTH_DISABLE:
  634. pSysConfig->AuthorisationMode = value;
  635. log_info("Auto Authorize Disable:%d", pSysConfig->AuthorisationMode);
  636. //clearMiscCommand();
  637. break;
  638. case MISC_CMD_EVCCID_ENABLE:
  639. pSysConfig->isAuthrizeByEVCCID = value;
  640. log_info("Authorize By EVCCID:%d",pSysConfig->isAuthrizeByEVCCID);
  641. //clearMiscCommand();
  642. break;
  643. default:
  644. clearMiscCommand();
  645. break;
  646. }
  647. usleep(128);
  648. }
  649. return ret;
  650. }
  651. static int chargingcapabilityHandle(uint8_t *data, uint8_t plugNum)
  652. {
  653. uint8_t addr = 0;
  654. float MaxVolt, MaxCurrent, MaxPower;
  655. static PricesInfo pricesInfo[2] = {0};
  656. CapabilityInfo *pCapabilityInfo = NULL;
  657. AccountInfo *pAccountInfo = NULL;
  658. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  659. //--- charging capability information --------------------------------------
  660. pCapabilityInfo = (CapabilityInfo *)&data[0];
  661. MaxVolt = (float)(ntohs(pCapabilityInfo->OutputVoltage));
  662. if (MaxVolt >= 0) {
  663. if (MaxVolt > MAX_VOLTAGE) {
  664. MaxVolt = MAX_VOLTAGE;
  665. }
  666. pDcChargingInfo->MaximumChargingVoltage = MaxVolt;
  667. }
  668. MaxCurrent = (float)(ntohs(pCapabilityInfo->OutputCurrent));
  669. if (MaxCurrent >= 0) {
  670. if (MaxCurrent > MAX_CURRENCY) {
  671. MaxCurrent = MAX_CURRENCY;
  672. }
  673. pDcChargingInfo->AvailableChargingCurrent = MaxCurrent;
  674. }
  675. MaxPower = (float)(ntohs(pCapabilityInfo->OutputPower));
  676. if (MaxPower >= 0) {
  677. if (MaxPower > MAX_POWER) {
  678. MaxPower = MAX_POWER;
  679. }
  680. pDcChargingInfo->AvailableChargingPower = MaxPower;
  681. }
  682. //MaxVolt = (float)(data[0] << 8 |data[1]);
  683. //MaxCurrent = (float)(data[2] << 8 | data[3]);
  684. //MaxPower = (float)(data[4] << 8 | data[5]);
  685. //printf("MaxVolt=%f, MaxCurrent=%f, MaxPower=%f,", MaxVolt, MaxCurrent, MaxPower);
  686. //--- user prices information ----------------------------------------------
  687. addr = (sizeof(CapabilityInfo) - 2); //2 byte reserved
  688. pAccountInfo = (AccountInfo *)&data[addr];
  689. pSysConfig->BillingData.Currency = pAccountInfo->Currency;
  690. ShmSelectGunInfo->PricesInfo[plugNum].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
  691. pDcChargingInfo->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
  692. ShmSelectGunInfo->PricesInfo[plugNum].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
  693. if ((pricesInfo[plugNum].UserPrices != ShmSelectGunInfo->PricesInfo[plugNum].UserPrices) ||
  694. (pricesInfo[plugNum].Balance != ShmSelectGunInfo->PricesInfo[plugNum].Balance)) {
  695. pricesInfo[plugNum].UserPrices = ShmSelectGunInfo->PricesInfo[plugNum].UserPrices;
  696. pricesInfo[plugNum].Balance = ShmSelectGunInfo->PricesInfo[plugNum].Balance;
  697. log_info("id = %d, user prices = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s",
  698. plugNum,
  699. ShmSelectGunInfo->PricesInfo[plugNum].UserPrices,
  700. pDcChargingInfo->ChargingFee,
  701. ShmSelectGunInfo->PricesInfo[plugNum].Balance,
  702. (uint8_t *)GetCurrency(pSysConfig->BillingData.Currency)
  703. );
  704. }
  705. return PASS;
  706. }
  707. static void addFaultCodeToBuf(uint8_t *Code)
  708. {
  709. uint8_t warningCount = pSysWarning->WarningCount;
  710. if (warningCount < 10) {
  711. memcpy(&pSysWarning->WarningCode[warningCount][0],
  712. Code,
  713. strlen((char *)Code));
  714. pSysWarning->WarningCount++;
  715. }
  716. }
  717. static void removeFaultCodeToBuf(uint8_t *Code)
  718. {
  719. uint8_t find = 0x01;
  720. uint8_t i = 0;
  721. char _code[7] = {0};
  722. sprintf(_code, "%s", Code);
  723. // 把相關的錯誤碼一次移除,避免重複顯示
  724. while (find) {
  725. usleep(128);
  726. find = 0x00;
  727. for (i = 0; i < pSysWarning->WarningCount; i++) {
  728. usleep(128);
  729. if (find == 0x00) {
  730. if (memcmp(&pSysWarning->WarningCode[i][0], _code, 7) == 0) {
  731. find = 0x01;
  732. }
  733. } else {
  734. memcpy(&pSysWarning->WarningCode[i - 1][0],
  735. &pSysWarning->WarningCode[i][0], 7);
  736. }
  737. }
  738. if (find) {
  739. pSysWarning->WarningCount--;
  740. }
  741. }
  742. }
  743. bool CompareArrayIsZero(uint8_t *array, unsigned int array_size)
  744. {
  745. uint8_t i;
  746. if (array != NULL) {
  747. for (i = 0; i < array_size; i++) {
  748. if (array[i] != 0) {
  749. return false;
  750. }
  751. }
  752. }
  753. return true;
  754. }
  755. static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
  756. {
  757. uint8_t ret = 0;
  758. uint8_t i = 0;
  759. uint8_t count = 0;
  760. uint8_t EventCodeTmp[7] = {0};
  761. uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
  762. uint8_t remaindLen = 0;
  763. //uint8_t statusCodeError = 0;
  764. if (dataLen > 0) {
  765. Hexdump((uint8_t *)data, dataLen);
  766. remaindLen = dataLen % WARNING_CODE_SIZE;
  767. if (remaindLen != 0) {
  768. log_info("fail status code length = %d", dataLen);
  769. dataLen -= remaindLen;
  770. }
  771. if (dataLen < WARNING_CODE_SIZE) {
  772. log_error("fail status code length = %d", dataLen);
  773. Hexdump(data, dataLen);
  774. if (pSysWarning->WarningCount > 0) {
  775. for (i = 0; i < pSysWarning->WarningCount; i++) {
  776. usleep(128);
  777. if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet
  778. (pSysWarning->WarningCode[i][0] >= 'B' &&
  779. pSysWarning->WarningCode[i][1] >= '4')) {
  780. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  781. memcpy(EventCodeTmp,
  782. pSysWarning->WarningCode[i],
  783. sizeof(EventCodeTmp));
  784. removeFaultCodeToBuf(EventCodeTmp);
  785. }
  786. }
  787. }
  788. return FAIL;
  789. }
  790. for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
  791. usleep(128);
  792. if (strncmp((char *)&data[count + 1], "1", 1) != 0 &&
  793. strncmp((char *)&data[count + 1], "2", 1) != 0 &&
  794. strncmp((char *)&data[count + 1], "3", 1) != 0 &&
  795. (strncmp((char *)&data[count], "B", 1) != 0 &&
  796. strncmp((char *)&data[count + 1], "4", 1) != 0)
  797. ) {
  798. log_error("error status code = %s", (char *)&data[count]);
  799. continue;
  800. }
  801. // misc command status code handle
  802. if (strncmp((char *)&data[count], MISC_ST_MISC_CMD, WARNING_CODE_SIZE) == 0) {
  803. gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
  804. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  805. continue;
  806. } else if (strncmp((char *)&data[count], MISC_ST_VERSION, WARNING_CODE_SIZE) == 0) {
  807. gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
  808. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  809. continue;
  810. }
  811. if (CompareArrayIsZero((uint8_t *)&data[count], WARNING_CODE_SIZE) == true) {
  812. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  813. memcpy(EventCodeTmp, (char *)&data[count], sizeof(EventCodeTmp));
  814. log_error("error status = %s", EventCodeTmp);
  815. continue;
  816. }
  817. strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
  818. ret = 0;
  819. for (i = 0; i < pSysWarning->WarningCount; i++) {
  820. usleep(128);
  821. if (memcmp(&pSysWarning->WarningCode[i][0],
  822. StatusArray[count / WARNING_CODE_SIZE],
  823. WARNING_CODE_SIZE) == 0) {
  824. ret = 1;
  825. break;
  826. }
  827. }
  828. if (ret == 0) {
  829. addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
  830. }
  831. //Rtn=StatusCodeProcessing(StatusArray[count/6]);
  832. }
  833. } else {
  834. if (CompareArrayIsZero(data, WARNING_CODE_SIZE) == false) {
  835. log_error("power cabinet status code data length is zero, but have data");
  836. Hexdump((uint8_t *)data, WARNING_CODE_SIZE);
  837. }
  838. }
  839. for (i = 0; i < pSysWarning->WarningCount; i++) {
  840. usleep(128);
  841. if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet 0x33
  842. (pSysWarning->WarningCode[i][0] >= 'B' &&
  843. pSysWarning->WarningCode[i][1] >= '4')) {
  844. ret = 0;
  845. for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
  846. usleep(128);
  847. if (memcmp(&pSysWarning->WarningCode[i][0],
  848. StatusArray[count],
  849. WARNING_CODE_SIZE) == 0) {
  850. ret = 1;
  851. break;
  852. }
  853. }
  854. if (ret == 0) {
  855. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  856. memcpy(EventCodeTmp,
  857. pSysWarning->WarningCode[i],
  858. sizeof(EventCodeTmp));
  859. removeFaultCodeToBuf(EventCodeTmp);
  860. }
  861. }
  862. }
  863. return PASS;
  864. }
  865. static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t plugNum, uint8_t reg)
  866. {
  867. int ret = PASS;
  868. uint8_t rawDataLen = 0;
  869. CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
  870. SoftwareUpdInfo *pSoftwareUpd = NULL;
  871. PcPsuOutput *pPcPsuOutput = NULL;
  872. //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
  873. if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
  874. compareResult(pCsuResult->Data.Result) == FAIL ||
  875. compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
  876. if (reg != REG_CHARGING_PERMISSION) {
  877. return FAIL;
  878. }
  879. }
  880. rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
  881. switch (pCsuResult->Data.Register) {
  882. case REG_MODEL_NAME:
  883. break;
  884. case REG_CONNECTOR_ID:
  885. gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
  886. gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
  887. log_info("OK (Left connector ID = %d, Right connector ID = %d)",
  888. pCsuResult->Data.Data[0],
  889. pCsuResult->Data.Data[1]);
  890. break;
  891. case REG_POWER_CABINET_STATUS:
  892. ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
  893. break;
  894. case REG_DISPENSER_STATUS:
  895. memset(&gPreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
  896. memcpy((uint8_t *)&gPreSysWarningInfo,
  897. pSysWarning,
  898. sizeof(struct WARNING_CODE_INFO));
  899. break;
  900. case REG_CHARGING_CAP:
  901. chargingcapabilityHandle(pCsuResult->Data.Data, plugNum);
  902. break;
  903. case REG_CHARGING_TARGET:
  904. break;
  905. case REG_SOFTWARE_UPDATE:
  906. pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
  907. if ((strcmp((char *)pSoftwareUpd->ImgName, "") == 0) ||
  908. (rawDataLen == 0) ||
  909. pSoftwareUpd->UpdateState != 1) {
  910. ret = FAIL;
  911. break;
  912. }
  913. ret = FAIL;
  914. ret = updateFirmwareHandle(pSoftwareUpd->ImgName);
  915. break;
  916. case REG_PLUG_IN_STATE:
  917. break;
  918. case REG_CONNECTOR_STATE:
  919. break;
  920. case REG_USER_ID:
  921. //log_info("USER ID:%s", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
  922. //if (pCsuResult->Data.Data[0] <= 0) {
  923. // return FAIL;
  924. //}
  925. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  926. return COMMAND_RESULT_NG;
  927. }
  928. ShmSelectGunInfo->AuthorStateFromCabinet[plugNum] = pCsuResult->Data.Data[0];
  929. return pCsuResult->Data.Data[0];
  930. break;
  931. case REG_CHARGING_PERMISSION:
  932. //log_info("id = %d, result = %s, action = %s",
  933. // pCsuResult->Head.ID,
  934. // pCsuResult->Data.Result == 1 ? "OK" : "NG",
  935. // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
  936. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  937. return COMMAND_RESULT_NG;
  938. }
  939. ShmSelectGunInfo->WaitDoCommPermission[plugNum] = pCsuResult->Data.Data[0];
  940. return pCsuResult->Data.Data[0];
  941. break;
  942. case REG_MISC_CONTROL:
  943. miscCommandHandle(rawDataLen, plugNum, pCsuResult->Data.Data);
  944. break;
  945. case REG_REPORT_CSU_VERSION:
  946. case REG_REPORT_OTHER_VERSION:
  947. break;
  948. case REG_PRESENT_CHARGING_INFO:
  949. pPcPsuOutput = (PcPsuOutput *)&pCsuResult->Data.Data[0];
  950. ShmDcCommonData->PcPsuOutput[plugNum].Voltage = ntohs((uint16_t)pPcPsuOutput->Voltage);
  951. ShmDcCommonData->PcPsuOutput[plugNum].Current = ntohs((uint16_t)pPcPsuOutput->Current);
  952. //log_info("%d power cabinet PSU output vol = %f, cur = %f",
  953. // plugNum,
  954. // (float)ShmDcCommonData->PcPsuOutput[plugNum].Voltage,
  955. // (float)ShmDcCommonData->PcPsuOutput[plugNum].Current);
  956. break;
  957. case REG_QRCODE_URL_INFO:
  958. ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
  959. break;
  960. case REG_WAIT_PLUG_IT_STATE:
  961. break;
  962. case REG_Ground_Fault_Detection:
  963. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  964. return COMMAND_RESULT_NG;
  965. }
  966. //集電弓relay 不打開才能進入動作
  967. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  968. if(pDcChargingInfo->PantographFlag == YES)
  969. {
  970. GroundFaultDetection *gfd = (GroundFaultDetection *)&pCsuResult->Data.Data[0];
  971. if(pDcChargingInfo->GroundFaultStatus != gfd->Status)
  972. {
  973. log_info("id = %d, GFD Result = %d",
  974. pCsuResult->Head.ID,
  975. gfd->Status);
  976. }
  977. pDcChargingInfo->GroundFaultStatus = gfd->Status;
  978. }
  979. break;
  980. default:
  981. break;
  982. }
  983. return ret;
  984. }
  985. static int composeSocketData(int fd,
  986. uint8_t id,
  987. uint8_t opCode,
  988. uint8_t reg,
  989. uint8_t dataLen,
  990. uint8_t *data)
  991. {
  992. int ret = PASS;
  993. int size = 0;
  994. int sendPktLen = 0;
  995. uint8_t i = 0;
  996. uint8_t plugNum = 0;
  997. CsuCmdPkt csuCmdPkt = {0};
  998. CsuResultPkt csuResult = {0};
  999. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  1000. csuCmdPkt.Head.ID = id;
  1001. csuCmdPkt.Head.OP = opCode;
  1002. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  1003. csuCmdPkt.Data.Register = reg;
  1004. if ((data != NULL) && (dataLen > 0)) {
  1005. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  1006. }
  1007. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  1008. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  1009. //send command packet
  1010. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  1011. log_error("TCP socket send packet fail = %d", size);
  1012. gDoCommGblData.DisConnCount++;
  1013. return FAIL;
  1014. }
  1015. //receive result head
  1016. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  1017. log_error("TCP socket RX head fail = %d", size);
  1018. gDoCommGblData.DisConnCount++;
  1019. return FAIL;
  1020. }
  1021. //receive result raw data
  1022. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  1023. log_error("TCP socket RX data fail = %d", size);
  1024. gDoCommGblData.DisConnCount++;
  1025. return FAIL;
  1026. }
  1027. for (i = 0; i < sizeof(gDoCommGblData.ConnectorID); i++) {
  1028. if (id == gDoCommGblData.ConnectorID[i]) {
  1029. plugNum = i;
  1030. break;
  1031. }
  1032. }
  1033. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, plugNum, csuCmdPkt.Data.Register);
  1034. gDoCommGblData.DisConnCount = 0; //送收資料沒有問題, 清除中斷網路計數
  1035. return ret;
  1036. }
  1037. static int writeWaitPlugItState(int fd, uint8_t id)
  1038. {
  1039. int ret = PASS;
  1040. uint8_t data[2] = {pSysInfo->WaitForPlugit, 0};
  1041. //printf("WaitForPlugit = %d", pSysInfo->WaitForPlugit);
  1042. if ((ret = composeSocketData(fd,
  1043. id,
  1044. OP_WRITE_DATA,
  1045. REG_WAIT_PLUG_IT_STATE,
  1046. 1,
  1047. &data[0])) == FAIL) {
  1048. return ret;
  1049. }
  1050. return ret;
  1051. }
  1052. static int readQRcodeURLAndSystemDate(int fd)
  1053. {
  1054. int ret = PASS;
  1055. ret = composeSocketData(fd,
  1056. ID_REGISTER,
  1057. OP_READ_DATA,
  1058. REG_QRCODE_URL_INFO,
  1059. 0,
  1060. NULL);
  1061. return ret;
  1062. }
  1063. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  1064. {
  1065. int ret = PASS;
  1066. uint8_t dataBuf[16] = {0};
  1067. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  1068. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1069. if(pDcChargingInfo->PantographFlag == NO)
  1070. {
  1071. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(pDcChargingInfo->PresentChargingVoltage * 10));
  1072. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(pDcChargingInfo->PresentChargingCurrent * 10));
  1073. }
  1074. else
  1075. {
  1076. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Voltage));
  1077. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Current));
  1078. }
  1079. pPreChargingInfo->RemainChargingDuration = htonl(pDcChargingInfo->RemainChargingDuration);
  1080. pPreChargingInfo->EvBatterySoc = pDcChargingInfo->EvBatterySoc;
  1081. ret = composeSocketData(fd,
  1082. id,
  1083. OP_WRITE_DATA,
  1084. REG_PRESENT_CHARGING_INFO,
  1085. sizeof(PreChargingInfo),
  1086. (uint8_t *)pPreChargingInfo);
  1087. return ret;
  1088. }
  1089. static int writeOtherModuleVersion(int fd)
  1090. {
  1091. int ret = PASS;
  1092. uint8_t data[255] = {0};
  1093. uint8_t dataLen = 0;
  1094. //report other module version message
  1095. memcpy(&data[dataLen], pSysInfo->FanModuleFwRev, VERSION_BUF_SIZE);
  1096. dataLen += VERSION_BUF_SIZE;
  1097. memcpy(&data[dataLen], pSysInfo->RelayModuleFwRev, VERSION_BUF_SIZE);
  1098. dataLen += VERSION_BUF_SIZE;
  1099. memcpy(&data[dataLen], pSysInfo->Connector1FwRev, VERSION_BUF_SIZE);
  1100. dataLen += VERSION_BUF_SIZE;
  1101. memcpy(&data[dataLen], pSysInfo->Connector2FwRev, VERSION_BUF_SIZE);
  1102. dataLen += VERSION_BUF_SIZE;
  1103. memcpy(&data[dataLen], pSysInfo->LedModuleFwRev, VERSION_BUF_SIZE);
  1104. dataLen += VERSION_BUF_SIZE;
  1105. ret = composeSocketData(fd,
  1106. ID_REGISTER,
  1107. OP_WRITE_DATA,
  1108. REG_REPORT_OTHER_VERSION,
  1109. dataLen,
  1110. &data[0]);
  1111. return ret;
  1112. }
  1113. static int writeCsuModuleVersion(int fd)
  1114. {
  1115. int ret = PASS;
  1116. uint8_t data[255] = {0};
  1117. uint8_t dataLen = 0;
  1118. //report CSU platform version message
  1119. memcpy(&data[dataLen], pSysInfo->CsuBootLoadFwRev, VERSION_BUF_SIZE);
  1120. dataLen += VERSION_BUF_SIZE;
  1121. memcpy(&data[dataLen], pSysInfo->CsuKernelFwRev, VERSION_BUF_SIZE);
  1122. dataLen += VERSION_BUF_SIZE;
  1123. memcpy(&data[dataLen], pSysInfo->CsuRootFsFwRev, VERSION_BUF_SIZE);
  1124. dataLen += VERSION_BUF_SIZE;
  1125. memcpy(&data[dataLen], pSysInfo->CsuPrimFwRev, VERSION_BUF_SIZE);
  1126. dataLen += VERSION_BUF_SIZE;
  1127. ret = composeSocketData(fd,
  1128. ID_REGISTER,
  1129. OP_WRITE_DATA,
  1130. REG_REPORT_CSU_VERSION,
  1131. dataLen,
  1132. &data[0]);
  1133. return ret;
  1134. }
  1135. static int readMiscCommand(int fd, uint8_t id)
  1136. {
  1137. int ret = PASS;
  1138. ret = composeSocketData(fd,
  1139. id,
  1140. OP_READ_DATA,
  1141. REG_MISC_CONTROL,
  1142. 0,
  1143. NULL);
  1144. return ret;
  1145. }
  1146. static int readChargePermission(int fd, uint8_t id)
  1147. {
  1148. return composeSocketData(fd,
  1149. id,
  1150. OP_READ_DATA,
  1151. REG_CHARGING_PERMISSION,
  1152. 0,
  1153. NULL);
  1154. }
  1155. static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id)
  1156. {
  1157. int ret = PASS;
  1158. uint8_t dataBuf[1] = {status};
  1159. ret = composeSocketData(fd,
  1160. id,
  1161. OP_WRITE_DATA,
  1162. REG_Ground_Fault_Detection,
  1163. 1,
  1164. &dataBuf[0]);
  1165. return ret;
  1166. }
  1167. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  1168. {
  1169. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  1170. return FAIL;
  1171. }
  1172. return composeSocketData(fd,
  1173. id,
  1174. OP_WRITE_DATA,
  1175. REG_USER_ID,
  1176. strlen((char *)pUserID),
  1177. pUserID);
  1178. }
  1179. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  1180. {
  1181. uint8_t dataBuf[10] = {'\0'};
  1182. ConnectorState *pConnState = (ConnectorState *)dataBuf;
  1183. static char vendorErrorCodeTmp[2][WARNING_CODE_SIZE] = {0};
  1184. int ret = PASS;
  1185. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1186. pConnState->ConnectorTemp = pDcChargingInfo->ConnectorTemp;
  1187. pConnState->ChillerTemp = pDcChargingInfo->ChillerTemp;
  1188. if (pDcChargingInfo->SystemStatus <= S_AUTHORIZING) {
  1189. pConnState->State = CONN_ST_IDLE; //idle
  1190. strncpy((char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1191. "",
  1192. sizeof(ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode));
  1193. strncpy(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE);
  1194. } else if ((pDcChargingInfo->SystemStatus <= S_PREPARING_FOR_EVSE) ||
  1195. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST0) ||
  1196. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST1)) {
  1197. pConnState->State = CONN_ST_PREPARING; //preparing
  1198. } else if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  1199. pConnState->State = CONN_ST_CHARGING; //charging
  1200. } else if (pDcChargingInfo->SystemStatus == S_TERMINATING ||
  1201. pDcChargingInfo->SystemStatus == S_COMPLETE) {
  1202. pConnState->State = CONN_ST_TERMINATING; //terminating
  1203. } else if ((pDcChargingInfo->SystemStatus == S_ALARM) ||
  1204. (pDcChargingInfo->SystemStatus == S_FAULT)) {
  1205. pConnState->State = CONN_ST_ALARM;
  1206. strncpy((char *)pConnState->WarningCode,
  1207. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1208. WARNING_CODE_SIZE);
  1209. if (strncmp(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE) == 0) {
  1210. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1211. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1212. WARNING_CODE_SIZE);
  1213. vendorErrorCodeTmp[plugNum][6] = '\0';
  1214. log_info("1 ID = %d, VendorErrorCode = %s",
  1215. plugNum,
  1216. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1217. } else {
  1218. if (strncmp(&vendorErrorCodeTmp[plugNum][0],
  1219. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1220. WARNING_CODE_SIZE) != 0
  1221. ) {
  1222. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1223. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1224. WARNING_CODE_SIZE);
  1225. vendorErrorCodeTmp[plugNum][6] = '\0';
  1226. log_info("2 ID = %d, VendorErrorCode = %s",
  1227. plugNum,
  1228. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1229. }
  1230. }
  1231. }
  1232. ret = composeSocketData(fd,
  1233. id,
  1234. OP_WRITE_DATA,
  1235. REG_CONNECTOR_STATE,
  1236. sizeof(dataBuf) - 1,
  1237. &dataBuf[0]);
  1238. return ret;
  1239. }
  1240. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  1241. {
  1242. int ret = PASS;
  1243. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1244. uint8_t dataBuf[2] = {pDcChargingInfo->ConnectorPlugIn, 0};
  1245. ret = composeSocketData(fd,
  1246. id,
  1247. OP_WRITE_DATA,
  1248. REG_PLUG_IN_STATE,
  1249. 1,
  1250. &dataBuf[0]);
  1251. return ret;
  1252. }
  1253. static int readSoftwareUpdate(int fd, uint8_t gunID)
  1254. {
  1255. int ret = PASS;
  1256. uint8_t dataBuf[2] = {pSysInfo->FirmwareUpdate, 0};
  1257. ret = composeSocketData(fd,
  1258. gunID,
  1259. OP_READ_DATA,
  1260. REG_SOFTWARE_UPDATE,
  1261. 1,
  1262. &dataBuf[0]);
  1263. return ret;
  1264. }
  1265. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  1266. {
  1267. int ret = PASS;
  1268. float ChargingVolt, ChargingAmp;
  1269. uint8_t dataBuf[4] = {0};
  1270. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1271. ChargingVolt = pDcChargingInfo->EvBatterytargetVoltage;
  1272. ChargingAmp = pDcChargingInfo->EvBatterytargetCurrent;
  1273. ChargingVolt *= 10;
  1274. ChargingAmp *= 10;
  1275. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1276. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1277. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1278. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1279. ret = composeSocketData(fd,
  1280. id,
  1281. OP_WRITE_DATA,
  1282. REG_CHARGING_TARGET,
  1283. sizeof(dataBuf),
  1284. &dataBuf[0]);
  1285. return ret;
  1286. }
  1287. static int readChargingCapability(int fd, uint8_t id)
  1288. {
  1289. int ret = PASS;
  1290. ret = composeSocketData(fd,
  1291. id,
  1292. OP_READ_DATA,
  1293. REG_CHARGING_CAP,
  1294. 0,
  1295. NULL);
  1296. return ret;
  1297. }
  1298. static int writeDispenserStatus(int fd, uint8_t gunID)
  1299. {
  1300. uint8_t warningCount = 0;
  1301. uint8_t count = 0;
  1302. uint8_t CurWarnCodeTmp[10][6] = {0};
  1303. uint8_t PreWarnCodeTmp[10][6] = {0};
  1304. int ret = PASS;
  1305. if ((pSysWarning->WarningCount <= 0) &&
  1306. (gPreSysWarningInfo.WarningCount <= 0)) {
  1307. return FAIL;
  1308. }
  1309. warningCount = pSysWarning->WarningCount;
  1310. for (count = 0; count < warningCount; count++) {
  1311. memcpy(CurWarnCodeTmp[count], pSysWarning->WarningCode[count], WARNING_CODE_SIZE);
  1312. }
  1313. warningCount = gPreSysWarningInfo.WarningCount;
  1314. for (count = 0; count < warningCount; count++) {
  1315. memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1316. }
  1317. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1318. return FAIL;
  1319. }
  1320. ret = composeSocketData(fd,
  1321. gunID,
  1322. OP_WRITE_DATA,
  1323. REG_DISPENSER_STATUS,
  1324. strlen((char *)CurWarnCodeTmp),
  1325. &CurWarnCodeTmp[0][0]);
  1326. return ret;
  1327. }
  1328. static int readPowerCabinetStatus(int fd, uint8_t gunID)
  1329. {
  1330. int ret = PASS;
  1331. ret = composeSocketData(fd,
  1332. gunID,
  1333. OP_READ_DATA,
  1334. REG_POWER_CABINET_STATUS,
  1335. 0,
  1336. NULL);
  1337. return ret;
  1338. }
  1339. static int readConnectorID(int fd)
  1340. {
  1341. int ret = PASS;
  1342. ret = composeSocketData(fd,
  1343. ID_REGISTER,
  1344. OP_READ_DATA,
  1345. REG_CONNECTOR_ID,
  1346. 0,
  1347. NULL);
  1348. return ret;
  1349. }
  1350. static int WriteModelName(int fd)
  1351. {
  1352. int ret = PASS;
  1353. uint8_t Tmp = 0;
  1354. uint8_t TmpBuf[255] = {0};
  1355. memcpy(TmpBuf,
  1356. pSysConfig->ModelName,
  1357. strlen((char *)pSysConfig->ModelName));
  1358. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1359. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1360. ShmPrimaryMcuData->InputDet.bits.Key0);
  1361. TmpBuf[strlen((char *)pSysConfig->ModelName)] = Tmp; //Dispenser switch value
  1362. ret = composeSocketData(fd,
  1363. ID_REGISTER,
  1364. OP_WRITE_DATA,
  1365. REG_MODEL_NAME,
  1366. strlen((char *)pSysConfig->ModelName) + 1,
  1367. &TmpBuf[0]);
  1368. return ret;
  1369. }
  1370. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1371. {
  1372. sleep(3); //wait 1 second
  1373. if ((*pValue) >= maxCount) {
  1374. setTcpStatus(ABNORMAL);
  1375. } else {
  1376. (*pValue)++;
  1377. }
  1378. }
  1379. static int CheckNetworkStatus(void)
  1380. {
  1381. char *ipAddr = (char *)&pSysConfig->Eth0Interface.EthIpAddress;
  1382. //printf("Check network IP Header = %s\n", ipAddr);
  1383. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1384. return 1;
  1385. }
  1386. return 0;
  1387. }
  1388. static void updateFirmwareProcess(int fd, uint8_t gunID, uint8_t totalConnCount)
  1389. {
  1390. bool canUpdateFirmware = true;
  1391. uint8_t plugNum = 0;
  1392. uint8_t ackCount = 5;
  1393. struct timeb updateTime;
  1394. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1395. if (pSysWarning->Level != 2) {
  1396. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1397. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  1398. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  1399. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  1400. canUpdateFirmware = false;
  1401. }
  1402. }
  1403. }
  1404. if (canUpdateFirmware) {
  1405. ftime(&updateTime);
  1406. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  1407. readSoftwareUpdate(fd, gunID);
  1408. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1409. }
  1410. if (pSysInfo->FirmwareUpdate == YES) {
  1411. while (ackCount != 0) {
  1412. ftime(&updateTime);
  1413. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  1414. readSoftwareUpdate(fd, gunID);
  1415. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1416. if (pSysInfo->FirmwareUpdate == NO) {
  1417. ackCount--;
  1418. }
  1419. }
  1420. usleep(128);
  1421. }
  1422. }
  1423. }
  1424. }
  1425. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1426. {
  1427. int ret = 0;
  1428. uint8_t gunID = 0;
  1429. struct timeb AuthNowTime;
  1430. #if defined DD360Audi
  1431. gunID = gDoCommGblData.ConnectorID[pSysInfo->CurGunSelected];
  1432. //gunID = gDoCommGblData.ConnectorID[plugNum];
  1433. if (pSysConfig->AuthorisationMode) {
  1434. gunID = ID_REGISTER;
  1435. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0;
  1436. }
  1437. #else
  1438. gunID = ID_REGISTER;
  1439. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0; //非Audi 不需要等待主櫃回報餘額
  1440. #endif // DD360Audi
  1441. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) ||
  1442. (pSysInfo->AuthorizeFlag == YES)) {
  1443. ftime(&AuthNowTime);
  1444. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1445. ret = writeUserID(fd,
  1446. gunID,
  1447. pSysConfig->UserId);
  1448. if (ret >= 0) {
  1449. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1450. 0,
  1451. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1452. if (ret == 0) {
  1453. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1454. } else {
  1455. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1456. }
  1457. //printf("%d Balance = %.2f, %.2f\n",
  1458. // plugNum,
  1459. // ShmSelectGunInfo->PricesInfo[plugNum].Balance,
  1460. // FAIL_BALANCE_PRICES);
  1461. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1462. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = YES; //isAuthorizedComplete
  1463. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
  1464. pSysInfo->AuthorizeFlag = NO;
  1465. ShmPsuData->SystemAvailablePower = NO;
  1466. ShmPsuData->SystemPresentPsuQuantity = NO;
  1467. }
  1468. }
  1469. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1470. }
  1471. }
  1472. }
  1473. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1474. {
  1475. int ret = DISPENSER_INIT_SUCC;
  1476. int isContinue = 1;
  1477. struct timeb NowTime;
  1478. while (isContinue) {
  1479. usleep(128);
  1480. ftime(&NowTime);
  1481. switch (curReg) {
  1482. case REG_MODEL_NAME:
  1483. if (WriteModelName(fd) == PASS) {
  1484. gDoCommGblData.DisConnCount = 0;
  1485. curReg = REG_CONNECTOR_ID;
  1486. } else {
  1487. sleep(1);
  1488. }
  1489. break;
  1490. case REG_CONNECTOR_ID:
  1491. if (readConnectorID(fd) == PASS) {
  1492. gDoCommGblData.DisConnCount = 0;
  1493. curReg = REG_REPORT_CSU_VERSION;
  1494. } else {
  1495. sleep(1);
  1496. }
  1497. break;
  1498. case REG_POWER_CABINET_STATUS:
  1499. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1500. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1501. readPowerCabinetStatus(fd, gunID);
  1502. ftime(&gRegTimeUp[plugNum][curReg]);
  1503. }
  1504. curReg = REG_DISPENSER_STATUS;
  1505. break;
  1506. case REG_DISPENSER_STATUS:
  1507. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1508. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1509. writeDispenserStatus(fd, gunID);
  1510. ftime(&gRegTimeUp[plugNum][curReg]);
  1511. }
  1512. curReg = REG_PLUG_IN_STATE;
  1513. break;
  1514. //case REG_CHARGING_CAP:
  1515. // readChargingCapability(fd, plugNum, gunID);
  1516. // break;
  1517. //case REG_CHARGING_TARGET:
  1518. // writeChargingTarget(fd, plugNum, gunID);
  1519. // break;
  1520. //case REG_SOFTWARE_UPDATE:
  1521. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1522. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > L OOP_RETRY_TIME||
  1523. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0 * 1) {
  1524. // readSoftwareUpdate(fd);
  1525. // ftime(&gRegTimeUp[plugNum][curReg]);
  1526. // }
  1527. // while (pSysInfo->FirmwareUpdate == YES) {
  1528. // ftime(&NowTime);
  1529. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowT ime) > LOOP_RETRY_TIME||
  1530. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1531. // readSoftwareUpdate(fd);
  1532. // }
  1533. // usleep(128);
  1534. // }
  1535. // }
  1536. // isContinue = 0;
  1537. // break;
  1538. case REG_PLUG_IN_STATE:
  1539. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1540. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1541. writePlugInStatus(fd, plugNum, gunID);
  1542. ftime(&gRegTimeUp[plugNum][curReg]);
  1543. }
  1544. curReg = REG_CONNECTOR_STATE;
  1545. break;
  1546. case REG_CONNECTOR_STATE:
  1547. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1548. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1549. writeConnectorState(fd, plugNum, gunID);
  1550. ftime(&gRegTimeUp[plugNum][curReg]);
  1551. }
  1552. //check misc command from power cabinet
  1553. if (gDoCommGblData.MiscCmd != 0) {
  1554. if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  1555. log_error("misc command failed = %x", gDoCommGblData.MiscCmd);
  1556. }
  1557. curReg = gDoCommGblData.MiscCmd;
  1558. } else {
  1559. curReg = REG_QRCODE_URL_INFO;
  1560. }
  1561. break;
  1562. //case REG_USER_ID:
  1563. // writeUserID(fd, gunID, uint8_t *pUserID);
  1564. // break;
  1565. //case REG_CHARGING_PERMISSION:
  1566. // readChargePermission(fd, gunID);
  1567. // break;
  1568. case REG_MISC_CONTROL:
  1569. readMiscCommand(fd, gunID);
  1570. isContinue = 0;
  1571. break;
  1572. case REG_REPORT_CSU_VERSION:
  1573. case REG_REPORT_OTHER_VERSION:
  1574. if (gDoCommGblData.MiscCmd != 0) {
  1575. writeCsuModuleVersion(fd);
  1576. writeOtherModuleVersion(fd);
  1577. clearMiscCommand();
  1578. isContinue = 0;
  1579. } else {
  1580. //power up write dispenser version and get power cabinet system ID
  1581. if ((writeCsuModuleVersion(fd) == PASS) &&
  1582. (writeOtherModuleVersion(fd) == PASS) &&
  1583. (readQRcodeURLAndSystemDate(fd) == PASS)
  1584. ) {
  1585. //gDoCommGblData.DisConnCount = 0;
  1586. isContinue = 0;
  1587. }
  1588. }
  1589. break;
  1590. //case REG_PRESENT_CHARGING_INFO:
  1591. // break;
  1592. case REG_QRCODE_URL_INFO:
  1593. if (gunID == 1) {
  1594. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10) ||
  1595. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0
  1596. ) {
  1597. readQRcodeURLAndSystemDate(fd);
  1598. ftime(&gRegTimeUp[plugNum][curReg]);
  1599. }
  1600. }
  1601. isContinue = 0;
  1602. //curReg = REG_SOFTWARE_UPDATE;
  1603. break;
  1604. //case REG_WAIT_PLUG_IT_STATE:
  1605. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1606. // writeWaitPlugItState(fd, gunID);
  1607. // ftime(&gRegTimeUp[plugNum][curReg]);
  1608. // }
  1609. // isContinue = 0;
  1610. // break;
  1611. default:
  1612. log_error("error curReg = %x", curReg);
  1613. gDoCommGblData.MiscCmd = 0;
  1614. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  1615. isContinue = 0;
  1616. break;
  1617. }
  1618. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  1619. return DISPENSER_SOCKET_RECONN;
  1620. }
  1621. }//for
  1622. return ret;
  1623. }
  1624. //static bool isDetectPlugin()
  1625. //{
  1626. // if (pSysInfo->WaitForPlugit == YES) {
  1627. // return YES;
  1628. // }
  1629. //
  1630. // return NO;
  1631. //}
  1632. static void systemStatusProcess(int fd, uint8_t totalGun, uint8_t plugNum, uint8_t gunID)
  1633. {
  1634. uint8_t i = 0;
  1635. struct timeb AuthNowTime = {0};
  1636. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1637. switch (pDcChargingInfo->SystemStatus) {
  1638. case S_IDLE:
  1639. case S_RESERVATION:
  1640. case S_MAINTAIN:
  1641. case S_ALARM:
  1642. case S_AUTHORIZING:
  1643. checkAuthorProcess(fd, plugNum);
  1644. //authorization complete, write wait plug it state
  1645. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance == FAIL_BALANCE_PRICES) {
  1646. break;
  1647. }
  1648. ftime(&AuthNowTime);
  1649. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  1650. DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) < 0
  1651. ) {
  1652. writeWaitPlugItState(fd, gunID);
  1653. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  1654. }
  1655. /*
  1656. ftime(&AuthNowTime);
  1657. if (DiffTimeb(gRegTimeUp[plugNum][REG_MISC_CONTROL], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  1658. DiffTimeb(gRegTimeUp[plugNum][REG_MISC_CONTROL], AuthNowTime) < 0
  1659. ) {
  1660. readMiscCommand(fd, gunID);
  1661. ftime(&gRegTimeUp[plugNum][REG_MISC_CONTROL]);
  1662. }
  1663. */
  1664. ftime(&AuthNowTime);
  1665. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1666. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1667. ) {
  1668. readChargingCapability(fd, gunID);
  1669. if (pDcChargingInfo->PantographFlag)
  1670. writeGroundFaultDetection(fd, 0, gunID);
  1671. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1672. }
  1673. break;
  1674. case S_PREPARNING: //get permission
  1675. ftime(&AuthNowTime);
  1676. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME ||
  1677. DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) < 0
  1678. ) {
  1679. writeConnectorState(fd, plugNum, gunID);
  1680. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  1681. }
  1682. ftime(&AuthNowTime);
  1683. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1684. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1685. ) {
  1686. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  1687. for (i = 0; i < totalGun; i++) {
  1688. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1689. ShmPsuData->SystemAvailablePower += pDcChargingInfo->AvailableChargingPower;
  1690. }
  1691. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  1692. }
  1693. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1694. }
  1695. ftime(&AuthNowTime);
  1696. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1697. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1698. ) {
  1699. writePresentChargingInfo(fd, plugNum, gunID);
  1700. if (pDcChargingInfo->PantographFlag)
  1701. writeGroundFaultDetection(fd, 0, gunID);
  1702. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1703. }
  1704. break;
  1705. case S_PREPARING_FOR_EV://wait connector lock
  1706. ftime(&AuthNowTime);
  1707. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1708. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1709. ) {
  1710. readChargingCapability(fd, gunID);
  1711. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1712. }
  1713. ftime(&AuthNowTime);
  1714. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1715. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1716. ) {
  1717. if (readChargePermission(fd, gunID) == 0) {
  1718. log_info("S_PREPARING_FOR_EV Stop charging by power cabinet's permission = %d, %d",
  1719. plugNum,
  1720. REG_CHARGING_PERMISSION);
  1721. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1722. }
  1723. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1724. }
  1725. ftime(&AuthNowTime);
  1726. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1727. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1728. ) {
  1729. writePresentChargingInfo(fd, plugNum, gunID);
  1730. if (pDcChargingInfo->PantographFlag)
  1731. writeGroundFaultDetection(fd, 0, gunID);
  1732. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1733. }
  1734. break;
  1735. case S_PREPARING_FOR_EVSE: //insaulation test
  1736. case S_CCS_PRECHARGE_ST0:
  1737. case S_CCS_PRECHARGE_ST1:
  1738. writeChargingTarget(fd, plugNum, gunID);
  1739. if (pDcChargingInfo->PantographFlag)
  1740. writeGroundFaultDetection(fd, 1, gunID);
  1741. ftime(&AuthNowTime);
  1742. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1743. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1744. ) {
  1745. readChargingCapability(fd, gunID);
  1746. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1747. }
  1748. ftime(&AuthNowTime);
  1749. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1750. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1751. ) {
  1752. if (readChargePermission(fd, gunID) == 0) {
  1753. log_info("S_PREPARING_FOR_EVSE Stop charging by power cabinet's permission = %d, %d",
  1754. plugNum,
  1755. REG_CHARGING_PERMISSION);
  1756. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1757. }
  1758. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1759. }
  1760. ftime(&AuthNowTime);
  1761. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1762. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1763. ) {
  1764. writePresentChargingInfo(fd, plugNum, gunID);
  1765. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1766. }
  1767. break;
  1768. case S_CHARGING: //charging
  1769. case S_TERMINATING:
  1770. if(pDcChargingInfo->Type == _Type_GB || pDcChargingInfo->Type == _Type_Chademo)
  1771. {
  1772. if (pDcChargingInfo->PantographFlag)
  1773. writeGroundFaultDetection(fd, 0, gunID);
  1774. }
  1775. else
  1776. {
  1777. if (pDcChargingInfo->PantographFlag)
  1778. writeGroundFaultDetection(fd, 1, gunID);
  1779. }
  1780. ftime(&AuthNowTime);
  1781. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1782. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1783. ) {
  1784. readChargingCapability(fd, gunID);
  1785. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1786. }
  1787. ftime(&AuthNowTime);
  1788. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1789. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1790. ) {
  1791. if (readChargePermission(fd, gunID) == 0) {
  1792. log_info("Stop charging by power cabinet's permission = %d, %d",
  1793. plugNum,
  1794. REG_CHARGING_PERMISSION);
  1795. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1796. //printf("%d StopChargeFlag = %d\n", plugNum, pDcChargingInfo->StopChargeFlag);
  1797. }
  1798. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1799. }
  1800. writeChargingTarget(fd, plugNum, gunID);
  1801. ftime(&AuthNowTime);
  1802. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1803. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1804. ) {
  1805. writePresentChargingInfo(fd, plugNum, gunID);
  1806. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1807. }
  1808. break;
  1809. case S_COMPLETE:
  1810. ftime(&AuthNowTime);
  1811. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1812. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1813. ) {
  1814. readChargingCapability(fd, gunID);
  1815. if (pDcChargingInfo->PantographFlag)
  1816. writeGroundFaultDetection(fd, 0, gunID);
  1817. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1818. }
  1819. break;
  1820. default:
  1821. break;
  1822. }
  1823. }
  1824. static int networkCreatePorcess(void)
  1825. {
  1826. int fd = 0;
  1827. uint8_t discount = 0;
  1828. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  1829. usleep(128);
  1830. }
  1831. setTcpStatus(ABNORMAL);
  1832. /**************** Check Network **********/
  1833. //log_info("Check Dispenser Network Information");
  1834. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  1835. usleep(125);
  1836. }
  1837. while (!CheckNetworkStatus()) {
  1838. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  1839. if( gDoCommGblData.DisConnCount != discount ) {
  1840. discount = gDoCommGblData.DisConnCount;
  1841. log_error("disconnect count = % d", gDoCommGblData.DisConnCount);
  1842. }
  1843. }
  1844. log_info("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  1845. pSysConfig->Eth0Interface.EthIpAddress,
  1846. pSysConfig->Eth0Interface.EthSubmaskAddress,
  1847. pSysConfig->Eth0Interface.EthGatewayAddress);
  1848. gDoCommGblData.DisConnCount = 0;
  1849. discount = 0;
  1850. /**************** Power cabinet connection **********/
  1851. log_info("Connect to Power Cabinet");
  1852. while ((fd = doCommConnToServer()) <= 0) {
  1853. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  1854. if( gDoCommGblData.DisConnCount != discount && fd == -1) {
  1855. discount = gDoCommGblData.DisConnCount;
  1856. log_error("disconnect count = %d, fd = %d", gDoCommGblData.DisConnCount, fd);
  1857. }
  1858. }
  1859. log_info("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  1860. fd,
  1861. pSysConfig->Eth0Interface.EthIpAddress,
  1862. pSysConfig->Eth0Interface.EthSubmaskAddress,
  1863. pSysConfig->Eth0Interface.EthGatewayAddress);
  1864. gDoCommGblData.DisConnCount = 0;
  1865. destroySelectGun(DESTROY_ALL_SEL);
  1866. sleep(3);
  1867. setTcpStatus(NORMAL);
  1868. return fd;
  1869. }
  1870. int main(int argc, char *argv[])
  1871. {
  1872. uint8_t plugNum = 0, gunID = 0;
  1873. uint8_t index = 0;
  1874. uint8_t totalConnCount = 0;
  1875. uint8_t initDone = DISPENER_INIT_FAIL;
  1876. char tmpbuf[256] = {0};
  1877. int fd = 0;
  1878. int isContinue = 1;
  1879. InitSocketSigPipe();
  1880. /**************** Initialization **********/
  1881. if (CreateAllCsuShareMemory() == FAIL) {
  1882. log_error("create share memory error");
  1883. return FAIL;
  1884. }
  1885. MappingGunChargingInfo("DoComm Task");
  1886. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  1887. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  1888. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  1889. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  1890. ShmPsuData = (struct PsuData *)GetShmPsuData();
  1891. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  1892. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  1893. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  1894. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  1895. totalConnCount = pSysConfig->TotalConnectorCount;
  1896. for (index = 0; index < totalConnCount; index++) {
  1897. clearPricesInfo(index);
  1898. }
  1899. //initial trigger message timer
  1900. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  1901. ftime(&gRegTimeUp[0][index]);
  1902. usleep(128);
  1903. ftime(&gRegTimeUp[1][index]);
  1904. usleep(50000);
  1905. }
  1906. setTcpStatus(NORMAL);
  1907. while (isContinue) {
  1908. if (initDone == DISPENER_INIT_FAIL) {
  1909. fd = networkCreatePorcess();
  1910. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  1911. } else {
  1912. plugNum = 0;
  1913. gunID = 0;
  1914. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1915. //checkAuthorProcess(fd, plugNum);
  1916. //plugNum : setup chargingData value for bottom layer
  1917. //gunID : connector Id from power cabinet, 1 = left gun, 2 = right gun,
  1918. gunID = gDoCommGblData.ConnectorID[plugNum];
  1919. systemStatusProcess(fd, totalConnCount, plugNum, gunID);
  1920. initDone = messageTrigger(fd,
  1921. plugNum,
  1922. gunID,
  1923. REG_POWER_CABINET_STATUS);
  1924. if (initDone == DISPENSER_SOCKET_RECONN) {
  1925. break;
  1926. }
  1927. initDone = messageTrigger(fd,
  1928. plugNum,
  1929. gunID,
  1930. REG_MISC_CONTROL);
  1931. if (initDone == DISPENSER_SOCKET_RECONN) {
  1932. break;
  1933. }
  1934. }
  1935. }
  1936. if (initDone != DISPENSER_SOCKET_RECONN) {
  1937. //update dispenser firmware
  1938. updateFirmwareProcess(fd, gDoCommGblData.ConnectorID[0], totalConnCount);
  1939. usleep(100000);
  1940. continue;
  1941. }
  1942. //Ethernet error recovery handle
  1943. log_info("Disconnected from power cabinet...re-connecting");
  1944. setTcpStatus(ABNORMAL);
  1945. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  1946. sprintf(tmpbuf,
  1947. "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  1948. pSysConfig->SystemId);
  1949. system(tmpbuf);
  1950. }
  1951. gDoCommGblData.DisConnCount = 0;
  1952. gDoCommGblData.SeqNum = 0;
  1953. closeSocket(fd);
  1954. fd = 0;
  1955. initDone = DISPENER_INIT_FAIL;
  1956. }
  1957. }