ReDoComm.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  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 "../../define.h"
  21. #include "Config.h"
  22. #include "ReDoComm.h"
  23. #include "AudiCust.h"
  24. //------------------------------------------------------------------------------
  25. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  26. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  27. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  28. //------------------------------------------------------------------------------
  29. //static int TcpSock = 0;
  30. //static uint8_t PacketSe;
  31. static DoCommGblData gDoCommGblData = {0};
  32. static struct SysConfigAndInfo *ShmSysConfigAndInfo;
  33. static struct StatusCodeData *ShmStatusCodeData;
  34. static struct PsuData *ShmPsuData;
  35. static struct OCPP16Data *ShmOCPP16Data;
  36. static struct PrimaryMcuData *ShmPrimaryMcuData;
  37. static struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  38. static struct timeb gRegTimeUp[2][MAX_REGISTER_NUM] = {0};
  39. static struct WARNING_CODE_INFO PreSysWarningInfo = {0};
  40. static AudiCustInfo *gAudiCustInfo = NULL;
  41. //------------------------------------------------------------------------------
  42. static void removeFaultCodeToBuf(uint8_t *Code);
  43. static void addFaultCodeToBuf(uint8_t *Code);
  44. static int readMiscCommand(int fd, uint8_t id);
  45. static int writeCsuModuleVersion(int fd);
  46. //------------------------------------------------------------------------------
  47. //--- Common function ---
  48. //------------------------------------------------------------------------------
  49. static int StoreLogMsg(const char *fmt, ...)
  50. {
  51. char Buf[4096 + 256];
  52. char buffer[4096];
  53. va_list args;
  54. struct timeb SeqEndTime;
  55. struct tm *tm;
  56. va_start(args, fmt);
  57. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  58. va_end(args);
  59. memset(Buf, 0, sizeof(Buf));
  60. ftime(&SeqEndTime);
  61. SeqEndTime.time = time(NULL);
  62. tm = localtime(&SeqEndTime.time);
  63. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == 1) {
  64. sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
  65. tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer);
  66. printf("%s \n", Buf);
  67. } else {
  68. sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  69. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm,
  70. buffer,
  71. tm->tm_year + 1900, tm->tm_mon + 1);
  72. system(Buf);
  73. }
  74. return rc;
  75. }
  76. static int DiffTimeb(struct timeb ST, struct timeb ET)
  77. {
  78. //return milli-second
  79. unsigned int StartTime, StopTime;
  80. StartTime = (unsigned int)ST.time;
  81. StopTime = (unsigned int)ET.time;
  82. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  83. }
  84. /**
  85. * [hexdump : check data]
  86. * @Author Jerry
  87. * @DateTime 2018-12-21
  88. * @param p [description]
  89. * @param size [description]
  90. */
  91. static void Hexdump(const void *p, size_t size)
  92. {
  93. const uint8_t *c = p;
  94. char message[10240] = {0};
  95. uint32_t i = 0;
  96. uint32_t message_len = 0;
  97. assert(p);
  98. //printf("Dumping %u bytes from %p:\r\n", size, p);
  99. message_len += sprintf(&message[message_len], "\r\nDumping %u bytes from %p:\r\n", (unsigned int)size, p);
  100. while (size > 0) {
  101. for (i = 0; i < 16; i++) {
  102. if (i < size) {
  103. //printf("%02x ", c[i]);
  104. message_len += sprintf(&message[message_len], "%02x ", c[i]);
  105. } else {
  106. //printf(" ");
  107. message_len += sprintf(&message[message_len], " ");
  108. }
  109. }
  110. for (i = 0; i < 16; i++) {
  111. if (i < size) {
  112. //printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
  113. message_len += sprintf(&message[message_len], "%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
  114. } else {
  115. //printf(" ");
  116. message_len += sprintf(&message[message_len], " ");
  117. }
  118. }
  119. //printf("\n");
  120. message_len += sprintf(&message[message_len], "\r\n");
  121. c += 16;
  122. if (size <= 16) {
  123. break;
  124. }
  125. size -= 16;
  126. }
  127. message_len += sprintf(&message[message_len], "\r\n");
  128. printf("%s", message);
  129. }
  130. static int string2ByteArray(char *input, byte *output)
  131. {
  132. int loop = 0;
  133. int i = 0;
  134. while (input[loop] != '\0') {
  135. output[i++] = input[loop++];
  136. }
  137. output[loop] = '\0';
  138. return loop + 1;
  139. }
  140. static void unixSocketSigPipeHandle(int sig)
  141. {
  142. DEBUG_ERROR("socket packet error %x\r\n", sig);
  143. }
  144. static void InitSocketSigPipe(void)
  145. {
  146. struct sigaction action;
  147. action.sa_handler = unixSocketSigPipeHandle;
  148. sigemptyset(&action.sa_mask);
  149. action.sa_flags = 0;
  150. sigaction(SIGPIPE, &action, NULL);
  151. }
  152. //------------------------------------------------------------------------------
  153. static void setTcpStatus(uint8_t setValue)
  154. {
  155. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = setValue;
  156. }
  157. //------------------------------------------------------------------------------
  158. //--- TCP socket function ---
  159. //------------------------------------------------------------------------------
  160. static int sendTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
  161. {
  162. int size = -1;
  163. size = send(fd, data, dataLen , 0);
  164. if ((size < 0) || (errno == EAGAIN)) {
  165. if (size < 0) {
  166. DEBUG_ERROR("Size = %d, EAGAIN error %d:%s\r\n", size, errno, strerror(errno));
  167. }
  168. }
  169. return size;
  170. }
  171. static int recvTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
  172. {
  173. int size = -1;
  174. uint8_t *pdata = (uint8_t *)data;
  175. size = recv(fd, pdata, dataLen, MSG_WAITALL);
  176. if ((errno == EAGAIN) || (size < 0)) {
  177. DEBUG_ERROR("Size = %d, EAGAIN error %d:%s\r\n", size, errno, strerror(errno));
  178. }
  179. return size;
  180. }
  181. static int getSO_ERROR(int fd)
  182. {
  183. int err = 1;
  184. socklen_t len = sizeof err;
  185. if (-1 == getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &len)) {
  186. DEBUG_ERROR("getSO_ERROR\r\n");
  187. }
  188. if (err) {
  189. errno = err; // set errno to the socket SO_ERROR
  190. }
  191. return err;
  192. }
  193. static void closeSocket(int fd) // *not* the Windows closesocket()
  194. {
  195. if (fd < 0) {
  196. return;
  197. }
  198. getSO_ERROR(fd); // first clear any errors, which can cause close to fail
  199. if (shutdown(fd, SHUT_RDWR) < 0) { // secondly, terminate the 'reliable' delivery
  200. if (errno != ENOTCONN && errno != EINVAL) { // SGI causes EINVAL
  201. DEBUG_INFO("shutdown\r\n");
  202. }
  203. }
  204. if (close(fd) < 0) { // finally call close()
  205. DEBUG_INFO("client socket close\r\n");
  206. }
  207. }
  208. static int doCommConnToServer(void)
  209. {
  210. struct sockaddr_in dest;
  211. struct timeval tv;
  212. int flag;
  213. int TcpSock = 0;
  214. //if (TcpSock > 0) {
  215. // close(TcpSock);
  216. //}
  217. if ((TcpSock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  218. DEBUG_ERROR("Open TCP socket NG");
  219. return -1;
  220. }
  221. //flag = fcntl (TcpSock, F_GETFL, 0);
  222. //if (flag >= 0) {
  223. // flag |= O_NONBLOCK;
  224. // fcntl(TcpSock, F_SETFL, flag );
  225. //}
  226. tv.tv_sec = 0;
  227. tv.tv_usec = 100000;
  228. setsockopt(TcpSock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
  229. setsockopt(TcpSock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval));
  230. flag = 1;
  231. setsockopt(TcpSock, SOL_SOCKET, MSG_NOSIGNAL, &flag, sizeof(flag));
  232. memset(&dest, 0, sizeof(dest));
  233. dest.sin_family = AF_INET;
  234. dest.sin_port = htons(DoTcpPort);
  235. inet_aton(DoIPAddress, (struct in_addr *) &dest.sin_addr.s_addr);
  236. if (connect(TcpSock, (struct sockaddr *) &dest, sizeof(dest)) != 0) {
  237. close(TcpSock);
  238. return -1;
  239. }
  240. return TcpSock;
  241. }
  242. //------------------------------------------------------------------------------
  243. //--- DoComm function ---
  244. //------------------------------------------------------------------------------
  245. static int compareOpcode(uint8_t opCode)
  246. {
  247. if (opCode != OP_WAIT_RESPONSE) {
  248. //DEBUG_ERROR("response operative code fail\r\n");
  249. return FAIL;
  250. }
  251. return PASS;
  252. }
  253. static int compareResult(uint8_t result)
  254. {
  255. if (result != COMMAND_RESULT_OK) {
  256. //DEBUG_ERROR("response result fail\r\n");
  257. return FAIL;
  258. }
  259. return PASS;
  260. }
  261. static int compareRegister(uint8_t srcReg, uint8_t destReg)
  262. {
  263. if (srcReg != destReg) {
  264. //DEBUG_ERROR("response register fail\r\n");
  265. return FAIL;
  266. }
  267. return PASS;
  268. }
  269. static int getConfirmSelectedGun(uint8_t curSel)
  270. {
  271. if (((curSel == LEFT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
  272. ((curSel == RIGHT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
  273. return PASS;
  274. }
  275. return FAIL;
  276. }
  277. static int getSelGunWaitToAuthor(uint8_t curGun)
  278. {
  279. if (curGun == LEFT_GUN_NUM && gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
  280. return FAIL;
  281. }
  282. if (curGun == RIGHT_GUN_NUM && gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
  283. return FAIL;
  284. }
  285. return PASS;
  286. }
  287. static void setConfirmSelGun(uint8_t selGun)
  288. {
  289. if (selGun == LEFT_GUN_NUM && gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_RELEASE) {
  290. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
  291. printf("confirmSelGun left\r\n");
  292. } else if (selGun == RIGHT_GUN_NUM && gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_RELEASE) {
  293. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
  294. printf("confirmSelGun right\r\n");
  295. }
  296. }
  297. static float transPricesUnit(int prices)
  298. {
  299. //printf("prices = %.2f\r\n", prices * PRICES_UNIT);
  300. return (prices * PRICES_UNIT);
  301. }
  302. static void clearMiscCommand(void)
  303. {
  304. gDoCommGblData.MiscCmd = 0;
  305. }
  306. static int qrCodeUrlInfoHandle(uint8_t *data)
  307. {
  308. int len = 0;
  309. char cmdBuf[128] = {0};
  310. char localTime[128] = {0};
  311. uint16_t timeLen = 0;
  312. struct timeb SeqEndTime;
  313. struct tm *tm;
  314. if ((char *)&data[0] == '\0') {
  315. DEBUG_ERROR("QR code date error\r\n");
  316. return FAIL;
  317. }
  318. //get local system time
  319. ftime(&SeqEndTime);
  320. SeqEndTime.time = time(NULL);
  321. tm = localtime(&SeqEndTime.time);
  322. timeLen = sprintf(localTime, "%04d-%02d-%02d %02d:%02d",
  323. tm->tm_year + 1900,
  324. tm->tm_mon + 1,
  325. tm->tm_mday,
  326. tm->tm_hour,
  327. tm->tm_min);
  328. //copy QR code string
  329. if (strncmp((char *)localTime, (char *)&data[0], timeLen) != 0) {
  330. memset(ShmSysConfigAndInfo->SysConfig.SystemId, '\0', sizeof(ShmSysConfigAndInfo->SysConfig.SystemId));
  331. len = string2ByteArray((char *)data, (uint8_t *)ShmSysConfigAndInfo->SysConfig.SystemId);
  332. }
  333. if ((char *)&data[len] == '\0') {
  334. DEBUG_ERROR("power cabinet system date error\r\n");
  335. return FAIL;
  336. }
  337. //set system date
  338. if (strncmp((char *)localTime, (char *)&data[len], timeLen) != 0) {
  339. sprintf(cmdBuf, "date -s \"%s\" >> /dev/null", (char *)&data[len]);
  340. system(cmdBuf);
  341. DEBUG_INFO("local time = %s, data time = %s\r\n", localTime, (char *)&data[len]);
  342. }
  343. return PASS;
  344. }
  345. static int updateFirmwareHandle(uint8_t *imgName)
  346. {
  347. char cmdBuf[1024] = {0};
  348. int status = 0;
  349. system("touch ./tftpUpdate.sh"); //創建shell
  350. sprintf(cmdBuf, "echo tftp -gr %s -l %s/%s %s; > ./tftpUpdate.sh",
  351. imgName,
  352. IMAGE_FILE_PATH,
  353. imgName,
  354. DoIPAddress);
  355. system("chmod +x ./tftpUpdate.sh"); //修改權限
  356. status = system("sh tftpUpdate.sh"); //執行shell
  357. if (-1 == status) {
  358. printf("system error!");
  359. } else {
  360. printf("exit status value = [0x%x]\n", status);
  361. if (WIFEXITED(status)) {
  362. if (0 == WEXITSTATUS(status)) {
  363. printf("run shell script successfully.\n");
  364. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = YES;
  365. } else {
  366. printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));
  367. }
  368. } else {
  369. printf("exit status = [%d]\n", WEXITSTATUS(status));
  370. }
  371. }
  372. system("rm -rf ./tftpUpdate.sh"); //刪除shell
  373. return PASS;
  374. }
  375. static int miscCommandHandle(uint8_t dataLen, uint8_t id, uint8_t *data)
  376. {
  377. int ret = PASS;
  378. uint8_t i = 0, j = 0;
  379. uint8_t cmdCount = (dataLen / 6);
  380. uint16_t cmd = 0;
  381. uint32_t value = 0;
  382. float prices = 0;
  383. MiscCommand *pMiscCmd = NULL;
  384. if (cmdCount < 1) {
  385. gDoCommGblData.MiscCmd = 0;
  386. //printf("cmdCount fail = %d, data len = %d\r\n", cmdCount, dataLen);
  387. return FAIL;
  388. }
  389. for (i = 0; i < cmdCount; i++) {
  390. pMiscCmd = (MiscCommand *)data + i;
  391. cmd = ntohs(pMiscCmd->CMD);
  392. value = ntohl(*((uint32_t *)&pMiscCmd->Value[0]));
  393. //DEBUG_INFO("misc command = %x, value = %d\r\n", cmd, value);
  394. switch (cmd) {
  395. //--- Execute parameter ---
  396. case MISC_CMD_CONNECOTOR_TIMEOUT:
  397. gAudiCustInfo->RemoteSetup.ConnectionTimeout = value;
  398. DEBUG_INFO("connection timeout = %d\r\n", gAudiCustInfo->RemoteSetup.ConnectionTimeout);
  399. clearMiscCommand();
  400. break;
  401. case MISC_CMD_OPERATIVE_STATE:
  402. if ((value > YES) || (value < NO)) {
  403. break;
  404. }
  405. DEBUG_INFO("change availability id = %d, value = %d\r\n", id - 1, value);
  406. ShmOCPP16Data->CsMsg.bits[id - 1].ChangeAvailabilityReq = YES;
  407. if (value == YES) {
  408. strcpy((char *)ShmOCPP16Data->ChangeAvailability[id - 1].Type, "Operative");
  409. } else if (value == NO) {
  410. strcpy((char *)ShmOCPP16Data->ChangeAvailability[id - 1].Type, "Inoperative");
  411. }
  412. if (id == 2) {
  413. clearMiscCommand();
  414. }
  415. break;
  416. case MISC_CMD_DEFAULT_PRICES:
  417. prices = transPricesUnit((int)value);
  418. DEBUG_INFO("default prices = %.2f\r\n", prices);
  419. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = YES;
  420. for (j = 0; j < 24; j++) {
  421. ShmSysConfigAndInfo->SysConfig.BillingData.Fee[j] = prices;
  422. }
  423. clearMiscCommand();
  424. break;
  425. case MISC_CMD_DEFAULT_CURRENCY:
  426. if (value < 0) {
  427. return FAIL;
  428. }
  429. DEBUG_INFO("default currency = %s%c\r\n", (uint8_t *)Currency[value]);
  430. ShmSysConfigAndInfo->SysConfig.BillingData.Currency = value;
  431. clearMiscCommand();
  432. break;
  433. case MISC_CMD_ACCOUNT_BALANCE:
  434. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != (id - 1)) {
  435. break;
  436. }
  437. gAudiCustInfo->PricesInfo[id - 1].Balance = transPricesUnit((int)value);
  438. DEBUG_INFO("%d misc balance = %.2f\r\n", id, gAudiCustInfo->PricesInfo[id - 1].Balance);
  439. clearMiscCommand();
  440. break;
  441. //--- Control Dispenser ---
  442. case MISC_CMD_HARDWARE_REBOOT:
  443. if (value != YES) {
  444. break;
  445. }
  446. DEBUG_INFO("Hardware reboot\r\n");
  447. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  448. strcpy((char *)ShmOCPP16Data->Reset.Type, "Hard");
  449. clearMiscCommand();
  450. break;
  451. case MISC_CMD_SOFTWARE_RESTART:
  452. if (value != YES) {
  453. break;
  454. }
  455. clearMiscCommand();
  456. DEBUG_INFO("Software reboot\r\n");
  457. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  458. strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  459. break;
  460. case MISC_CMD_REMOTE_START_CHARGING:
  461. if (value != YES) {
  462. break;
  463. }
  464. DEBUG_INFO("Remote start charging id = %d\r\n", id);
  465. //if (getConfirmSelectedGun(id - 1) == FAIL) {
  466. // DEBUG_INFO("Remote start not select gun\r\n");
  467. // break;
  468. //}
  469. if (getSelGunWaitToAuthor(id - 1) == FAIL) {
  470. DEBUG_ERROR("Remote start gun already charging\r\n");
  471. break;
  472. }
  473. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != (id - 1)) {
  474. ShmSysConfigAndInfo->SysInfo.CurGunSelected = (id - 1);
  475. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  476. }
  477. setConfirmSelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  478. ShmOCPP16Data->CsMsg.bits[id - 1].RemoteStartTransactionReq = YES;
  479. if (id == 2) {
  480. clearMiscCommand();
  481. }
  482. break;
  483. default:
  484. clearMiscCommand();
  485. break;
  486. }
  487. }
  488. return ret;
  489. }
  490. static int chargingcapabilityHandle(uint8_t *data, uint8_t gunID)
  491. {
  492. float MaxVolt, MaxCurrent, MaxPower;
  493. CapabilityInfo *pCapabilityInfo = NULL;
  494. AccountInfo *pAccountInfo = NULL;
  495. pCapabilityInfo = (CapabilityInfo *)&data[0];
  496. MaxVolt = (float)(ntohs(pCapabilityInfo->OutputVoltage));
  497. if ((MaxVolt >= 0) && (MaxVolt <= MAX_VOLTAGE)) {
  498. ChargingData[gunID]->MaximumChargingVoltage = MaxVolt;
  499. }
  500. MaxCurrent = (float)(ntohs(pCapabilityInfo->OutputCurrent));
  501. if ((MaxCurrent >= 0) && (MaxCurrent <= MAX_CURRENCY)) {
  502. ChargingData[gunID]->AvailableChargingCurrent = MaxCurrent;
  503. }
  504. MaxPower = (float)(ntohs(pCapabilityInfo->OutputPower));
  505. if ((MaxPower >= 0) && (MaxPower <= MAX_POWER)) {
  506. ChargingData[gunID]->AvailableChargingPower = MaxPower;
  507. }
  508. //MaxVolt = (float)(data[0] << 8 |data[1]);
  509. //MaxCurrent = (float)(data[2] << 8 | data[3]);
  510. //MaxPower = (float)(data[4] << 8 | data[5]);
  511. //printf("MaxVolt=%f, MaxCurrent=%f, MaxPower=%f,\n", MaxVolt, MaxCurrent, MaxPower);
  512. pAccountInfo = (AccountInfo *)&data[6];
  513. ShmSysConfigAndInfo->SysConfig.BillingData.Currency = pAccountInfo->Currency;
  514. gAudiCustInfo->PricesInfo[gunID].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
  515. ChargingData[gunID]->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
  516. gAudiCustInfo->PricesInfo[gunID].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
  517. DEBUG_INFO("id = %d, user prices = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s\r\n",
  518. gunID,
  519. gAudiCustInfo->PricesInfo[gunID].UserPrices,
  520. ChargingData[gunID]->ChargingFee,
  521. gAudiCustInfo->PricesInfo[gunID].Balance,
  522. (uint8_t *)Currency[ShmSysConfigAndInfo->SysConfig.BillingData.Currency]);
  523. return PASS;
  524. }
  525. static void addFaultCodeToBuf(uint8_t *Code)
  526. {
  527. uint8_t warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  528. if (warningCount < 10) {
  529. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[warningCount][0],
  530. Code,
  531. strlen((char *)Code));
  532. ShmSysConfigAndInfo->SysWarningInfo.WarningCount++;
  533. }
  534. }
  535. static void removeFaultCodeToBuf(uint8_t *Code)
  536. {
  537. uint8_t find = 0x01;
  538. uint8_t i = 0;
  539. char _code[7] = {0};
  540. sprintf(_code, "%s", Code);
  541. // 把相關的錯誤碼一次移除,避免重複顯示
  542. while (find) {
  543. find = 0x00;
  544. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  545. if (find == 0x00) {
  546. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], _code, 7) == 0) {
  547. find = 0x01;
  548. }
  549. } else {
  550. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i - 1][0],
  551. &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], 7);
  552. }
  553. }
  554. if (find) {
  555. ShmSysConfigAndInfo->SysWarningInfo.WarningCount--;
  556. }
  557. }
  558. }
  559. static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
  560. {
  561. uint8_t ret = 0;
  562. uint8_t i = 0;
  563. uint8_t count = 0;
  564. uint8_t EventCodeTmp[7] = {0};
  565. uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
  566. if (dataLen < WARNING_CODE_SIZE) {
  567. DEBUG_ERROR("raw data len < 6 = %d\r\n", dataLen);
  568. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0) {
  569. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  570. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= 0x33) { //from backend or power cabinet
  571. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  572. memcpy(EventCodeTmp,
  573. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  574. sizeof(EventCodeTmp));
  575. removeFaultCodeToBuf(EventCodeTmp);
  576. }
  577. }
  578. }
  579. return FAIL;
  580. }
  581. for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
  582. //printf("status code = %s\r\n", &data[count]);
  583. // misc command status code handle
  584. if (strcmp((char *)&data[count], MISC_ST_MISC_CMD) == 0) {
  585. gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
  586. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  587. continue;
  588. } else if (strcmp((char *)&data[count], MISC_ST_VERSION) == 0) {
  589. gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
  590. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  591. continue;
  592. }
  593. strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
  594. ret = 0;
  595. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  596. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  597. StatusArray[count / WARNING_CODE_SIZE],
  598. WARNING_CODE_SIZE) == 0) {
  599. ret = 1;
  600. break;
  601. }
  602. }
  603. if (ret == 0) {
  604. addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
  605. }
  606. //Rtn=StatusCodeProcessing(StatusArray[count/6]);
  607. }
  608. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  609. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '3') { //from backend or power cabinet 0x33
  610. ret = 0;
  611. for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
  612. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  613. StatusArray[count],
  614. WARNING_CODE_SIZE) == 0) {
  615. ret = 1;
  616. break;
  617. }
  618. }
  619. if (ret == 0) {
  620. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  621. memcpy(EventCodeTmp,
  622. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  623. sizeof(EventCodeTmp));
  624. removeFaultCodeToBuf(EventCodeTmp);
  625. }
  626. }
  627. }
  628. return PASS;
  629. }
  630. static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t id, uint8_t reg)
  631. {
  632. int ret = PASS;
  633. uint8_t rawDataLen = 0;
  634. CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
  635. SoftwareUpdInfo *pSoftwareUpd = NULL;
  636. //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
  637. if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
  638. compareResult(pCsuResult->Data.Result) == FAIL ||
  639. compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
  640. if (reg != REG_CHARGING_PERMISSION) {
  641. return FAIL;
  642. }
  643. }
  644. rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
  645. switch (pCsuResult->Data.Register) {
  646. case REG_MODEL_NAME:
  647. break;
  648. case REG_CONNECTOR_ID:
  649. gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
  650. gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
  651. DEBUG_INFO("OK (Left connector ID = %d, Right connector ID = %d)\n",
  652. pCsuResult->Data.Data[0],
  653. pCsuResult->Data.Data[1]);
  654. break;
  655. case REG_POWER_CABINET_STATUS:
  656. ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
  657. break;
  658. case REG_DISPENSER_STATUS:
  659. memset(&PreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
  660. memcpy(&PreSysWarningInfo,
  661. &(ShmSysConfigAndInfo->SysWarningInfo),
  662. sizeof(struct WARNING_CODE_INFO));
  663. break;
  664. case REG_CHARGING_CAP:
  665. chargingcapabilityHandle(pCsuResult->Data.Data, id - 1);
  666. break;
  667. case REG_CHARGING_TARGET:
  668. break;
  669. case REG_SOFTWARE_UPDATE:
  670. pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
  671. if ((pSoftwareUpd->ImgName == NULL) || (rawDataLen == 0)) {
  672. ret = FAIL;
  673. break;
  674. }
  675. updateFirmwareHandle(pSoftwareUpd->ImgName);
  676. break;
  677. case REG_PLUG_IN_STATE:
  678. break;
  679. case REG_CONNECTOR_STATE:
  680. break;
  681. case REG_USER_ID:
  682. //DEBUG_INFO("%s\n", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
  683. //if (pCsuResult->Data.Data[0] <= 0) {
  684. // return FAIL;
  685. //}
  686. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  687. return COMMAND_RESULT_NG;
  688. }
  689. return pCsuResult->Data.Data[0];
  690. break;
  691. case REG_CHARGING_PERMISSION:
  692. //DEBUG_INFO("id = %d, result = %s, action = %s\r\n",
  693. // pCsuResult->Head.ID,
  694. // pCsuResult->Data.Result == 1 ? "OK" : "NG",
  695. // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
  696. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  697. return COMMAND_RESULT_NG;
  698. }
  699. return pCsuResult->Data.Data[0];
  700. break;
  701. case REG_MISC_CONTROL:
  702. miscCommandHandle(rawDataLen, pCsuResult->Head.ID, pCsuResult->Data.Data);
  703. break;
  704. case REG_REPORT_CSU_VERSION:
  705. case REG_REPORT_OTHER_VERSION:
  706. break;
  707. case REG_PRESENT_CHARGING_INFO:
  708. break;
  709. case REG_QRCODE_URL_INFO:
  710. ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
  711. break;
  712. case REG_WAIT_PLUG_IT_STATE:
  713. break;
  714. default:
  715. break;
  716. }
  717. return ret;
  718. }
  719. static int composeSocketData(int fd,
  720. uint8_t id,
  721. uint8_t opCode,
  722. uint8_t reg,
  723. uint8_t dataLen,
  724. uint8_t *data)
  725. {
  726. int ret = PASS;
  727. int size = 0;
  728. int sendPktLen = 0;
  729. CsuCmdPkt csuCmdPkt = {0};
  730. CsuResultPkt csuResult = {0};
  731. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  732. csuCmdPkt.Head.ID = id;
  733. csuCmdPkt.Head.OP = opCode;
  734. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  735. csuCmdPkt.Data.Register = reg;
  736. if ((data != NULL) && (dataLen > 0)) {
  737. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  738. }
  739. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  740. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  741. //send command packet
  742. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  743. DEBUG_ERROR("TCP socket send packet fail = %d\r\n", size);
  744. gDoCommGblData.DisConnCount++;
  745. return FAIL;
  746. }
  747. //receive result head
  748. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  749. DEBUG_ERROR("TCP socket RX head fail = %d\r\n", size);
  750. gDoCommGblData.DisConnCount++;
  751. return FAIL;
  752. }
  753. //receive result raw data
  754. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  755. DEBUG_ERROR("TCP socket RX data fail = %d\r\n", size);
  756. gDoCommGblData.DisConnCount++;
  757. return FAIL;
  758. }
  759. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, id, csuCmdPkt.Data.Register);
  760. return ret;
  761. }
  762. static int writeWaitPlugItState(int fd, uint8_t id)
  763. {
  764. int ret = PASS;
  765. uint8_t data[2] = {ShmSysConfigAndInfo->SysInfo.WaitForPlugit, 0};
  766. //printf("WaitForPlugit = %d\r\n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  767. if ((ret = composeSocketData(fd,
  768. id,
  769. OP_WRITE_DATA,
  770. REG_WAIT_PLUG_IT_STATE,
  771. 1,
  772. &data[0])) == FAIL) {
  773. return ret;
  774. }
  775. return ret;
  776. }
  777. static int readPowerCabinetSystemID(int fd)
  778. {
  779. int ret = PASS;
  780. ret = composeSocketData(fd,
  781. ID_REGISTER,
  782. OP_READ_DATA,
  783. REG_QRCODE_URL_INFO,
  784. 0,
  785. NULL);
  786. return ret;
  787. }
  788. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  789. {
  790. int ret = PASS;
  791. uint8_t dataBuf[16] = {0};
  792. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  793. pPreChargingInfo->EvBatterySoc = htonl(ChargingData[plugNum]->EvBatterySoc);
  794. pPreChargingInfo->RemainChargingDuration = htonl(ChargingData[plugNum]->RemainChargingDuration);
  795. pPreChargingInfo->PresentChargingVoltage = htonl(ChargingData[plugNum]->PresentChargingVoltage);
  796. pPreChargingInfo->PresentChargingCurrent = htonl(ChargingData[plugNum]->PresentChargingCurrent);
  797. ret = composeSocketData(fd,
  798. id,
  799. OP_WRITE_DATA,
  800. REG_PRESENT_CHARGING_INFO,
  801. sizeof(PreChargingInfo),
  802. (uint8_t *)pPreChargingInfo);
  803. return ret;
  804. }
  805. static int writeOtherModuleVersion(int fd)
  806. {
  807. int ret = PASS;
  808. uint8_t data[255] = {0};
  809. uint8_t dataLen = 0;
  810. //report other module version message
  811. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, VERSION_BUF_SIZE);
  812. dataLen += VERSION_BUF_SIZE;
  813. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, VERSION_BUF_SIZE);
  814. dataLen += VERSION_BUF_SIZE;
  815. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector1FwRev, VERSION_BUF_SIZE);
  816. dataLen += VERSION_BUF_SIZE;
  817. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector2FwRev, VERSION_BUF_SIZE);
  818. dataLen += VERSION_BUF_SIZE;
  819. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, VERSION_BUF_SIZE);
  820. dataLen += VERSION_BUF_SIZE;
  821. ret = composeSocketData(fd,
  822. ID_REGISTER,
  823. OP_WRITE_DATA,
  824. REG_REPORT_OTHER_VERSION,
  825. dataLen,
  826. &data[0]);
  827. return ret;
  828. }
  829. static int writeCsuModuleVersion(int fd)
  830. {
  831. int ret = PASS;
  832. uint8_t data[255] = {0};
  833. uint8_t dataLen = 0;
  834. //report CSU platform version message
  835. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, VERSION_BUF_SIZE);
  836. dataLen += VERSION_BUF_SIZE;
  837. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, VERSION_BUF_SIZE);
  838. dataLen += VERSION_BUF_SIZE;
  839. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, VERSION_BUF_SIZE);
  840. dataLen += VERSION_BUF_SIZE;
  841. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, VERSION_BUF_SIZE);
  842. dataLen += VERSION_BUF_SIZE;
  843. ret = composeSocketData(fd,
  844. ID_REGISTER,
  845. OP_WRITE_DATA,
  846. REG_REPORT_CSU_VERSION,
  847. dataLen,
  848. &data[0]);
  849. return ret;
  850. }
  851. static int readMiscCommand(int fd, uint8_t id)
  852. {
  853. int ret = PASS;
  854. ret = composeSocketData(fd,
  855. id,
  856. OP_READ_DATA,
  857. REG_MISC_CONTROL,
  858. 0,
  859. NULL);
  860. return ret;
  861. }
  862. static int readChargePermission(int fd, uint8_t id)
  863. {
  864. //int ret = PASS;
  865. //if ((ret = composeSocketData(fd,
  866. // id,
  867. // OP_READ_DATA,
  868. // REG_CHARGING_PERMISSION,
  869. // 0,
  870. // NULL)) == FAIL) {
  871. // return ret;
  872. //}
  873. return composeSocketData(fd,
  874. id,
  875. OP_READ_DATA,
  876. REG_CHARGING_PERMISSION,
  877. 0,
  878. NULL);
  879. }
  880. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  881. {
  882. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  883. return FAIL;
  884. }
  885. return composeSocketData(fd,
  886. id,
  887. OP_WRITE_DATA,
  888. REG_USER_ID,
  889. strlen((char *)pUserID),
  890. pUserID);
  891. }
  892. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  893. {
  894. uint8_t dataBuf[2] = {0};
  895. int ret = PASS;
  896. if (ChargingData[plugNum]->SystemStatus <= 2) {
  897. dataBuf[0] = 0; //idle
  898. } else if ((ChargingData[plugNum]->SystemStatus <= 7) ||
  899. (ChargingData[plugNum]->SystemStatus == 17) ||
  900. (ChargingData[plugNum]->SystemStatus == 18)) {
  901. dataBuf[0] = 1; //preparing
  902. } else if (ChargingData[plugNum]->SystemStatus == 8) {
  903. dataBuf[0] = 2; //charging
  904. } else if (ChargingData[plugNum]->SystemStatus == 9) {
  905. dataBuf[0] = 3; //terminating
  906. }
  907. ret = composeSocketData(fd,
  908. id,
  909. OP_WRITE_DATA,
  910. REG_CONNECTOR_STATE,
  911. 1,
  912. &dataBuf[0]);
  913. return ret;
  914. }
  915. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  916. {
  917. int ret = PASS;
  918. uint8_t dataBuf[2] = {ChargingData[plugNum]->ConnectorPlugIn, 0};
  919. ret = composeSocketData(fd,
  920. id,
  921. OP_WRITE_DATA,
  922. REG_PLUG_IN_STATE,
  923. 1,
  924. &dataBuf[0]);
  925. return ret;
  926. }
  927. static int readSoftwareUpdate(int fd)
  928. {
  929. int ret = PASS;
  930. ret = composeSocketData(fd,
  931. ID_REGISTER,
  932. OP_READ_DATA,
  933. REG_SOFTWARE_UPDATE,
  934. 0,
  935. NULL);
  936. return ret;
  937. }
  938. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  939. {
  940. int ret = PASS;
  941. float ChargingVolt, ChargingAmp;
  942. uint8_t dataBuf[4] = {0};
  943. ChargingVolt = ChargingData[plugNum]->EvBatterytargetVoltage;
  944. ChargingAmp = ChargingData[plugNum]->EvBatterytargetCurrent;
  945. ChargingVolt *= 10;
  946. ChargingAmp *= 10;
  947. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  948. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  949. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  950. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  951. ret = composeSocketData(fd,
  952. id,
  953. OP_WRITE_DATA,
  954. REG_CHARGING_TARGET,
  955. sizeof(dataBuf),
  956. &dataBuf[0]);
  957. return ret;
  958. }
  959. static int readChargingCapability(int fd, uint8_t id)
  960. {
  961. int ret = PASS;
  962. ret = composeSocketData(fd,
  963. id,
  964. OP_READ_DATA,
  965. REG_CHARGING_CAP,
  966. 0,
  967. NULL);
  968. return ret;
  969. }
  970. static int writeDispenserStatus(int fd)
  971. {
  972. uint8_t warningCount = 0;
  973. uint8_t count = 0;
  974. uint8_t CurWarnCodeTmp[10][6] = {0};
  975. uint8_t PreWarnCodeTmp[10][6] = {0};
  976. int ret = PASS;
  977. if ((ShmSysConfigAndInfo->SysWarningInfo.WarningCount <= 0) &&
  978. (PreSysWarningInfo.WarningCount <= 0)) {
  979. return FAIL;
  980. }
  981. warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  982. for (count = 0; count < warningCount; count++) {
  983. memcpy(CurWarnCodeTmp[count], ShmSysConfigAndInfo->SysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  984. }
  985. warningCount = PreSysWarningInfo.WarningCount;
  986. for (count = 0; count < warningCount; count++) {
  987. memcpy(PreWarnCodeTmp[count], PreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  988. }
  989. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  990. return FAIL;
  991. }
  992. ret = composeSocketData(fd,
  993. ID_OF_DISPENSER,
  994. OP_WRITE_DATA,
  995. REG_DISPENSER_STATUS,
  996. strlen((char *)CurWarnCodeTmp),
  997. &CurWarnCodeTmp[0][0]);
  998. return ret;
  999. }
  1000. static int readPowerCabinetStatus(int fd)
  1001. {
  1002. int ret = PASS;
  1003. ret = composeSocketData(fd,
  1004. ID_OF_DISPENSER,
  1005. OP_READ_DATA,
  1006. REG_POWER_CABINET_STATUS,
  1007. 0,
  1008. NULL);
  1009. return ret;
  1010. }
  1011. static int readConnectorID(int fd)
  1012. {
  1013. int ret = PASS;
  1014. ret = composeSocketData(fd,
  1015. ID_REGISTER,
  1016. OP_READ_DATA,
  1017. REG_CONNECTOR_ID,
  1018. 0,
  1019. NULL);
  1020. return ret;
  1021. }
  1022. static int WriteModelName(int fd)
  1023. {
  1024. int ret = PASS;
  1025. uint8_t Tmp = 0;
  1026. uint8_t TmpBuf[255] = {0};
  1027. memcpy(TmpBuf,
  1028. ShmSysConfigAndInfo->SysConfig.ModelName,
  1029. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName));
  1030. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1031. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1032. ShmPrimaryMcuData->InputDet.bits.Key0);
  1033. TmpBuf[strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName)] = Tmp; //Dispenser switch value
  1034. ret = composeSocketData(fd,
  1035. ID_REGISTER,
  1036. OP_WRITE_DATA,
  1037. REG_MODEL_NAME,
  1038. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName) + 1,
  1039. &TmpBuf[0]);
  1040. return ret;
  1041. }
  1042. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1043. {
  1044. sleep(3); //wait 1 second
  1045. if ((*pValue) >= maxCount) {
  1046. setTcpStatus(ABNORMAL);
  1047. } else {
  1048. (*pValue)++;
  1049. }
  1050. }
  1051. static int CheckNetworkStatus(void)
  1052. {
  1053. char *ipAddr = (char *)&ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress;
  1054. //printf("Check network IP Header = %s\n", ipAddr);
  1055. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1056. return 1;
  1057. }
  1058. return 0;
  1059. }
  1060. static int FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  1061. {
  1062. uint8_t index = 0;
  1063. for (index = 0; index < CHAdeMO_QUANTITY; index++) {
  1064. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target) {
  1065. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  1066. return 1;
  1067. }
  1068. }
  1069. for (index = 0; index < CCS_QUANTITY; index++) {
  1070. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target) {
  1071. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  1072. return 1;
  1073. }
  1074. }
  1075. for (index = 0; index < GB_QUANTITY; index++) {
  1076. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target) {
  1077. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  1078. return 1;
  1079. }
  1080. }
  1081. return 0;
  1082. }
  1083. static int InitShareMemory(void)
  1084. {
  1085. int result = 1;
  1086. int MeterSMId = 0;
  1087. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  1088. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1089. result = 0;
  1090. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1091. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  1092. result = 0;
  1093. }
  1094. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  1095. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1096. result = 0;
  1097. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1098. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1099. result = 0;
  1100. }
  1101. //creat ShmPsuData
  1102. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0) {
  1103. DEBUG_ERROR("shmget ShmPsuData NG \n");
  1104. result = 0;
  1105. } else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1106. DEBUG_ERROR("shmat ShmPsuData NG \n");
  1107. result = 0;
  1108. }
  1109. //creat ShmOCPP16Data
  1110. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0) {
  1111. DEBUG_ERROR("shmget ShmOCPP16Data NG \n");
  1112. result = 0;
  1113. } else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1114. DEBUG_ERROR("shmat ShmOCPP16Data NG \n");
  1115. result = 0;
  1116. }
  1117. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0) {
  1118. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1119. result = 0;
  1120. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1121. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1122. result = 0;
  1123. }
  1124. //creat Audi customization info
  1125. if ((MeterSMId = shmget(ShmAudiCustInfoKey, sizeof(AudiCustInfo), IPC_CREAT | 0777)) < 0) {
  1126. DEBUG_ERROR("[main]CreatShareMemory:shmget select gun info NG \n");
  1127. return 0;
  1128. } else if ((gAudiCustInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1129. DEBUG_ERROR("[main]CreatShareMemory:shmat shmget select gun info \n");
  1130. return 0;
  1131. }
  1132. return result;
  1133. }
  1134. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1135. {
  1136. int ret = 0;
  1137. struct timeb AuthNowTime;
  1138. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) || (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == YES)) {
  1139. ftime(&AuthNowTime);
  1140. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1141. ret = writeUserID(fd,
  1142. ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1,
  1143. ShmSysConfigAndInfo->SysConfig.UserId);
  1144. if (ret >= 0) {
  1145. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1146. 0,
  1147. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1148. if (ret == 0) {
  1149. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1150. } else {
  1151. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1152. }
  1153. //printf("gAudiCustInfo->PricesInfo[plugNum].Balance = %.2f, %.2f\r\n",
  1154. // gAudiCustInfo->PricesInfo[plugNum].Balance,
  1155. // FAIL_BALANCE_PRICES);
  1156. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1157. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = 1; //isAuthorizedComplete
  1158. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = 0;
  1159. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = 0;
  1160. ShmPsuData->SystemAvailablePower = 0;
  1161. ShmPsuData->SystemPresentPsuQuantity = 0;
  1162. }
  1163. }
  1164. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1165. }
  1166. }
  1167. }
  1168. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1169. {
  1170. int ret = DISPENSER_INIT_SUCC;
  1171. int isContinue = 1;
  1172. struct timeb NowTime;
  1173. while (isContinue) {
  1174. ftime(&NowTime);
  1175. switch (curReg) {
  1176. case REG_MODEL_NAME:
  1177. if (WriteModelName(fd) == PASS) {
  1178. gDoCommGblData.DisConnCount = 0;
  1179. curReg = REG_CONNECTOR_ID;
  1180. }
  1181. break;
  1182. case REG_CONNECTOR_ID:
  1183. if (readConnectorID(fd) == PASS) {
  1184. gDoCommGblData.DisConnCount = 0;
  1185. curReg = REG_REPORT_CSU_VERSION;
  1186. }
  1187. break;
  1188. case REG_POWER_CABINET_STATUS:
  1189. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1190. readPowerCabinetStatus(fd);
  1191. ftime(&gRegTimeUp[plugNum][curReg]);
  1192. }
  1193. curReg = REG_DISPENSER_STATUS;
  1194. break;
  1195. case REG_DISPENSER_STATUS:
  1196. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1197. writeDispenserStatus(fd);
  1198. ftime(&gRegTimeUp[plugNum][curReg]);
  1199. }
  1200. curReg = REG_PLUG_IN_STATE;
  1201. break;
  1202. //case REG_CHARGING_CAP:
  1203. // readChargingCapability(fd, plugNum, gunID);
  1204. // break;
  1205. //case REG_CHARGING_TARGET:
  1206. // writeChargingTarget(fd, plugNum, gunID);
  1207. // break;
  1208. //case REG_SOFTWARE_UPDATE:
  1209. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1210. // readSoftwareUpdate(fd);
  1211. // ftime(&gRegTimeUp[plugNum][curReg]);
  1212. // }
  1213. // curReg = REG_PLUG_IN_STATE;
  1214. // break;
  1215. case REG_PLUG_IN_STATE:
  1216. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1217. writePlugInStatus(fd, plugNum, gunID);
  1218. ftime(&gRegTimeUp[plugNum][curReg]);
  1219. }
  1220. curReg = REG_CONNECTOR_STATE;
  1221. break;
  1222. case REG_CONNECTOR_STATE:
  1223. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1224. writeConnectorState(fd, plugNum, gunID);
  1225. ftime(&gRegTimeUp[plugNum][curReg]);
  1226. }
  1227. //check misc command from power cabinet
  1228. if (gDoCommGblData.MiscCmd != 0) {
  1229. curReg = gDoCommGblData.MiscCmd;
  1230. } else {
  1231. curReg = REG_QRCODE_URL_INFO;
  1232. }
  1233. break;
  1234. //case REG_USER_ID:
  1235. // writeUserID(fd, gunID, uint8_t *pUserID);
  1236. // break;
  1237. //case REG_CHARGING_PERMISSION:
  1238. // readChargePermission(fd, gunID);
  1239. // break;
  1240. case REG_MISC_CONTROL:
  1241. readMiscCommand(fd, gunID);
  1242. isContinue = 0;
  1243. break;
  1244. case REG_REPORT_CSU_VERSION:
  1245. case REG_REPORT_OTHER_VERSION:
  1246. if (gDoCommGblData.MiscCmd != 0) {
  1247. writeCsuModuleVersion(fd);
  1248. writeOtherModuleVersion(fd);
  1249. clearMiscCommand();
  1250. isContinue = 0;
  1251. } else {
  1252. //power up write dispenser version and get power cabinet system ID
  1253. if ((writeCsuModuleVersion(fd) == PASS) &&
  1254. (writeOtherModuleVersion(fd) == PASS) &&
  1255. (readPowerCabinetSystemID(fd) == PASS)) {
  1256. gDoCommGblData.DisConnCount = 0;
  1257. isContinue = 0;
  1258. }
  1259. }
  1260. break;
  1261. //case REG_PRESENT_CHARGING_INFO:
  1262. // break;
  1263. case REG_QRCODE_URL_INFO:
  1264. if (gunID == 1) {
  1265. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10)) {
  1266. readPowerCabinetSystemID(fd);
  1267. ftime(&gRegTimeUp[plugNum][curReg]);
  1268. }
  1269. }
  1270. isContinue = 0;
  1271. //curReg = REG_WAIT_PLUG_IT_STATE;
  1272. break;
  1273. //case REG_WAIT_PLUG_IT_STATE:
  1274. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1275. // writeWaitPlugItState(fd, gunID);
  1276. // ftime(&gRegTimeUp[plugNum][curReg]);
  1277. // }
  1278. // isContinue = 0;
  1279. // break;
  1280. default:
  1281. break;
  1282. }
  1283. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  1284. return DISPENSER_SOCKET_RECONN;
  1285. }
  1286. }//for
  1287. return ret;
  1288. }
  1289. static void systemStatusProcess(int fd, uint8_t plugNum, uint8_t gunID)
  1290. {
  1291. struct timeb AuthNowTime = {0};
  1292. switch (ChargingData[plugNum]->SystemStatus) {
  1293. case S_IDLE:
  1294. case S_RESERVATION:
  1295. case S_MAINTAIN:
  1296. case S_ALARM:
  1297. case S_AUTHORIZING:
  1298. checkAuthorProcess(fd, plugNum);
  1299. //authorization complete, write wait plug it state
  1300. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1301. ftime(&AuthNowTime);
  1302. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10)) {
  1303. writeWaitPlugItState(fd, gunID);
  1304. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  1305. }
  1306. }
  1307. //check update firmware
  1308. //if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1309. // ftime(&AuthNowTime);
  1310. // if (DiffTimeb(gRegTimeUp[plugNum][REG_SOFTWARE_UPDATE], AuthNowTime) > LOOP_RETRY_TIME) {
  1311. // readSoftwareUpdate(fd);
  1312. // ftime(&gRegTimeUp[plugNum][REG_SOFTWARE_UPDATE]);
  1313. // }
  1314. //}
  1315. break;
  1316. case S_PREPARNING: //get permission
  1317. ftime(&AuthNowTime);
  1318. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME) {
  1319. writeConnectorState(fd, plugNum, gunID);
  1320. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  1321. }
  1322. ftime(&AuthNowTime);
  1323. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1324. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  1325. ShmPsuData->SystemAvailablePower = (ChargingData[0]->AvailableChargingPower +
  1326. ChargingData[1]->AvailableChargingPower);
  1327. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  1328. }
  1329. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1330. }
  1331. ftime(&AuthNowTime);
  1332. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1333. writePresentChargingInfo(fd, plugNum, gunID);
  1334. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1335. }
  1336. break;
  1337. case S_PREPARING_FOR_EV://wait connector lock
  1338. ftime(&AuthNowTime);
  1339. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1340. readChargingCapability(fd, gunID);
  1341. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1342. }
  1343. ftime(&AuthNowTime);
  1344. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1345. if (readChargePermission(fd, gunID) == 0) {
  1346. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1347. plugNum,
  1348. REG_CHARGING_PERMISSION);
  1349. ChargingData[plugNum]->StopChargeFlag = 1;
  1350. }
  1351. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1352. }
  1353. ftime(&AuthNowTime);
  1354. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1355. writePresentChargingInfo(fd, plugNum, gunID);
  1356. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1357. }
  1358. break;
  1359. case S_PREPARING_FOR_EVSE: //insaulation test
  1360. case S_CCS_PRECHARGE_ST0:
  1361. case S_CCS_PRECHARGE_ST1:
  1362. writeChargingTarget(fd, plugNum, gunID);
  1363. ftime(&AuthNowTime);
  1364. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1365. readChargingCapability(fd, gunID);
  1366. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1367. }
  1368. ftime(&AuthNowTime);
  1369. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1370. if (readChargePermission(fd, gunID) == 0) {
  1371. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1372. plugNum,
  1373. REG_CHARGING_PERMISSION);
  1374. ChargingData[plugNum]->StopChargeFlag = 1;
  1375. }
  1376. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1377. }
  1378. ftime(&AuthNowTime);
  1379. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1380. writePresentChargingInfo(fd, plugNum, gunID);
  1381. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1382. }
  1383. break;
  1384. case S_CHARGING: //charging
  1385. case S_TERMINATING:
  1386. ftime(&AuthNowTime);
  1387. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1388. readChargingCapability(fd, gunID);
  1389. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1390. }
  1391. ftime(&AuthNowTime);
  1392. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1393. if (readChargePermission(fd, gunID) == 0) {
  1394. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1395. plugNum,
  1396. REG_CHARGING_PERMISSION);
  1397. ChargingData[plugNum]->StopChargeFlag = 1;
  1398. }
  1399. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1400. }
  1401. writeChargingTarget(fd, plugNum, gunID);
  1402. ftime(&AuthNowTime);
  1403. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1404. writePresentChargingInfo(fd, plugNum, gunID);
  1405. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1406. }
  1407. break;
  1408. case S_COMPLETE:
  1409. ftime(&AuthNowTime);
  1410. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1411. readChargingCapability(fd, gunID);
  1412. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1413. }
  1414. break;
  1415. default:
  1416. break;
  1417. }
  1418. }
  1419. static int networkCreatePorcess(void)
  1420. {
  1421. int fd = 0;
  1422. /**************** Check Network **********/
  1423. DEBUG_INFO("Check Dispenser Network Information");
  1424. while (!CheckNetworkStatus()) {
  1425. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  1426. DEBUG_ERROR("disconnect count = % d\r\n", gDoCommGblData.DisConnCount);
  1427. }
  1428. DEBUG_INFO("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  1429. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1430. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1431. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1432. gDoCommGblData.DisConnCount = 0;
  1433. /**************** Power cabinet connection **********/
  1434. DEBUG_INFO("Connect to Power Cabinet");
  1435. while ((fd = doCommConnToServer()) <= 0) {
  1436. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  1437. DEBUG_ERROR("disconnect count = %d, fd = %d\r\n", gDoCommGblData.DisConnCount, fd);
  1438. }
  1439. DEBUG_INFO("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  1440. fd,
  1441. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1442. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1443. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1444. gDoCommGblData.DisConnCount = 0;
  1445. sleep(3);
  1446. setTcpStatus(NORMAL);
  1447. return fd;
  1448. }
  1449. int main(int argc, char *argv[])
  1450. {
  1451. uint8_t plugNum = 0, gunID = 0;
  1452. uint8_t index = 0;
  1453. uint8_t totalConnCount = 0;
  1454. uint8_t initDone = DISPENER_INIT_FAIL;
  1455. int fd = 0;
  1456. int isContinue = 1;
  1457. InitSocketSigPipe();
  1458. /**************** Initialization **********/
  1459. if (InitShareMemory() == 0) {
  1460. if (ShmStatusCodeData != NULL) {
  1461. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = YES;
  1462. }
  1463. DEBUG_ERROR("DoComm Initial share memory fail\r\n");
  1464. sleep(5);
  1465. return 0;
  1466. }
  1467. totalConnCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1468. for (index = 0; index < totalConnCount; index++) {
  1469. if (!FindChargingInfoData(index, &ChargingData[0])) {
  1470. DEBUG_ERROR("FindChargingInfoData false \n");
  1471. break;
  1472. }
  1473. gAudiCustInfo->PricesInfo[index].Balance = FAIL_BALANCE_PRICES;
  1474. }
  1475. //initial trigger message timer
  1476. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  1477. ftime(&gRegTimeUp[0][index]);
  1478. ftime(&gRegTimeUp[1][index]);
  1479. usleep(50000);
  1480. }
  1481. setTcpStatus(NORMAL);
  1482. while (isContinue) {
  1483. if (initDone == DISPENER_INIT_FAIL) {
  1484. fd = networkCreatePorcess();
  1485. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  1486. } else {
  1487. checkAuthorProcess(fd, 0);
  1488. plugNum = 0;
  1489. gunID = 0;
  1490. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1491. //plugNum = setup chargingData value for bottom layer
  1492. gunID = (plugNum + 1); //connector Id, 1 = left gun, 2 = right gun
  1493. systemStatusProcess(fd, plugNum, gunID);
  1494. if ((initDone = messageTrigger(fd,
  1495. plugNum,
  1496. gunID,
  1497. REG_POWER_CABINET_STATUS)) == DISPENSER_SOCKET_RECONN) {
  1498. break;
  1499. }
  1500. }
  1501. }
  1502. if (initDone == DISPENSER_SOCKET_RECONN) {
  1503. DEBUG_INFO("Disconnected from power cabinet...re-connecting");
  1504. setTcpStatus(ABNORMAL);
  1505. gDoCommGblData.DisConnCount = 0;
  1506. gDoCommGblData.SeqNum = 0;
  1507. closeSocket(fd);
  1508. fd = 0;
  1509. initDone = DISPENER_INIT_FAIL;
  1510. }
  1511. }
  1512. }