ReDoComm.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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 - 2) != 0) {
  330. memset(ShmSysConfigAndInfo->SysConfig.SystemId, '\0', sizeof(ShmSysConfigAndInfo->SysConfig.SystemId));
  331. //printf("data = %s\r\n", data);
  332. len = string2ByteArray((char *)data, (uint8_t *)ShmSysConfigAndInfo->SysConfig.SystemId);
  333. //printf("SystemId = %s\r\n", ShmSysConfigAndInfo->SysConfig.SystemId);
  334. }
  335. //if ((char *)&data[len] == '\0') {
  336. // DEBUG_ERROR("power cabinet system date error\r\n");
  337. // return FAIL;
  338. //}
  339. //set system date
  340. //if (strncmp((char *)localTime, (char *)&data[len], timeLen) != 0) {
  341. // sprintf(cmdBuf, "date -s \"%s\" >> /dev/null", (char *)&data[len]);
  342. // system(cmdBuf);
  343. // DEBUG_INFO("local time = %s, data time = %s\r\n", localTime, (char *)&data[len]);
  344. // ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  345. // strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  346. // sleep(3);
  347. // //gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  348. //}
  349. return PASS;
  350. }
  351. static int updateFirmwareHandle(uint8_t *imgName)
  352. {
  353. int ret = PASS;
  354. char cmdBuf[1024] = {0};
  355. sprintf(cmdBuf, "/mnt/%s", imgName);
  356. DEBUG_INFO("Program ready to check file %s\n", cmdBuf);
  357. if ( access(cmdBuf, F_OK) != -1) {
  358. DEBUG_INFO("File '%s' exist.\n", cmdBuf);
  359. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = YES;
  360. } else {
  361. DEBUG_INFO("File '%s' doesn't exist.\n", cmdBuf);
  362. ret = FAIL;
  363. }
  364. #if 0
  365. char cmdBuf[1024] = {0};
  366. int status = 0;
  367. system("touch ./tftpUpdate.sh"); //創建shell
  368. sprintf(cmdBuf, "echo tftp -gr %s -l %s/%s %s; > ./tftpUpdate.sh",
  369. imgName,
  370. IMAGE_FILE_PATH,
  371. imgName,
  372. DoIPAddress);
  373. system("chmod +x ./tftpUpdate.sh"); //修改權限
  374. status = system("sh tftpUpdate.sh"); //執行shell
  375. if (-1 == status) {
  376. printf("system error!");
  377. } else {
  378. printf("exit status value = [0x%x]\n", status);
  379. if (WIFEXITED(status)) {
  380. if (0 == WEXITSTATUS(status)) {
  381. printf("run shell script successfully.\n");
  382. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = YES;
  383. } else {
  384. printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));
  385. }
  386. } else {
  387. printf("exit status = [%d]\n", WEXITSTATUS(status));
  388. }
  389. }
  390. system("rm -rf ./tftpUpdate.sh"); //刪除shell
  391. #endif //0
  392. return ret;
  393. }
  394. static int miscCommandHandle(uint8_t dataLen, uint8_t id, uint8_t *data)
  395. {
  396. int ret = PASS;
  397. uint8_t i = 0, j = 0;
  398. uint8_t cmdCount = (dataLen / 6);
  399. uint16_t cmd = 0;
  400. uint32_t value = 0;
  401. float prices = 0;
  402. MiscCommand *pMiscCmd = NULL;
  403. if (cmdCount < 1) {
  404. gDoCommGblData.MiscCmd = 0;
  405. //printf("cmdCount fail = %d, data len = %d\r\n", cmdCount, dataLen);
  406. return FAIL;
  407. }
  408. for (i = 0; i < cmdCount; i++) {
  409. pMiscCmd = (MiscCommand *)data + i;
  410. cmd = ntohs(pMiscCmd->CMD);
  411. value = ntohl(*((uint32_t *)&pMiscCmd->Value[0]));
  412. //DEBUG_INFO("misc command = %x, value = %d\r\n", cmd, value);
  413. switch (cmd) {
  414. //--- Execute parameter ---
  415. case MISC_CMD_CONNECOTOR_TIMEOUT:
  416. gAudiCustInfo->RemoteSetup.ConnectionTimeout = value;
  417. DEBUG_INFO("connection timeout = %d\r\n", gAudiCustInfo->RemoteSetup.ConnectionTimeout);
  418. clearMiscCommand();
  419. break;
  420. case MISC_CMD_OPERATIVE_STATE:
  421. if ((value > YES) || (value < NO)) {
  422. if (id == 2) {
  423. clearMiscCommand();
  424. }
  425. break;
  426. }
  427. DEBUG_INFO("change availability id = %d, value = %d\r\n", id - 1, value);
  428. ShmOCPP16Data->CsMsg.bits[id - 1].ChangeAvailabilityReq = YES;
  429. if (value == YES) {
  430. strcpy((char *)ShmOCPP16Data->ChangeAvailability[id - 1].Type, "Operative");
  431. } else if (value == NO) {
  432. strcpy((char *)ShmOCPP16Data->ChangeAvailability[id - 1].Type, "Inoperative");
  433. }
  434. if (id == 2) {
  435. clearMiscCommand();
  436. }
  437. break;
  438. case MISC_CMD_DEFAULT_PRICES:
  439. prices = transPricesUnit((int)value);
  440. DEBUG_INFO("default prices = %.2f\r\n", prices);
  441. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = YES;
  442. for (j = 0; j < 24; j++) {
  443. ShmSysConfigAndInfo->SysConfig.BillingData.Fee[j] = prices;
  444. }
  445. clearMiscCommand();
  446. break;
  447. case MISC_CMD_DEFAULT_CURRENCY:
  448. if (value < 0) {
  449. clearMiscCommand();
  450. return FAIL;
  451. }
  452. DEBUG_INFO("default currency = %s%c\r\n", (uint8_t *)Currency[value]);
  453. ShmSysConfigAndInfo->SysConfig.BillingData.Currency = value;
  454. clearMiscCommand();
  455. break;
  456. case MISC_CMD_ACCOUNT_BALANCE:
  457. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != (id - 1)) {
  458. clearMiscCommand();
  459. break;
  460. }
  461. if (getConfirmSelectedGun(id - 1) == FAIL) {
  462. DEBUG_INFO("Remote start not select gun\r\n");
  463. break;
  464. }
  465. gAudiCustInfo->PricesInfo[id - 1].Balance = transPricesUnit((int)value);
  466. DEBUG_INFO("%d misc balance = %.2f\r\n", id, gAudiCustInfo->PricesInfo[id - 1].Balance);
  467. clearMiscCommand();
  468. break;
  469. //--- Control Dispenser ---
  470. case MISC_CMD_HARDWARE_REBOOT:
  471. if (value != YES) {
  472. clearMiscCommand();
  473. break;
  474. }
  475. DEBUG_INFO("Hardware reboot\r\n");
  476. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  477. strcpy((char *)ShmOCPP16Data->Reset.Type, "Hard");
  478. clearMiscCommand();
  479. break;
  480. case MISC_CMD_SOFTWARE_RESTART:
  481. if (value != YES) {
  482. clearMiscCommand();
  483. break;
  484. }
  485. clearMiscCommand();
  486. DEBUG_INFO("Software reboot\r\n");
  487. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  488. strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  489. break;
  490. case MISC_CMD_REMOTE_START_CHARGING:
  491. if (value != YES) {
  492. if (id == 2) {
  493. clearMiscCommand();
  494. }
  495. break;
  496. }
  497. DEBUG_INFO("Remote start charging id = %d\r\n", id);
  498. if (getConfirmSelectedGun(id - 1) == FAIL) {
  499. DEBUG_INFO("Remote start not select gun\r\n");
  500. break;
  501. }
  502. //if (getSelGunWaitToAuthor(id - 1) == FAIL) {
  503. // DEBUG_ERROR("Remote start gun already charging\r\n");
  504. // break;
  505. //}
  506. //if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != (id - 1)) {
  507. // ShmSysConfigAndInfo->SysInfo.CurGunSelected = (id - 1);
  508. // ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  509. //}
  510. //setConfirmSelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  511. ShmOCPP16Data->CsMsg.bits[id - 1].RemoteStartTransactionReq = YES;
  512. gAudiCustInfo->PricesInfo[id - 1].Balance = 0.00;
  513. if (id == 2) {
  514. clearMiscCommand();
  515. }
  516. break;
  517. default:
  518. clearMiscCommand();
  519. break;
  520. }
  521. usleep(128);
  522. }
  523. return ret;
  524. }
  525. static int chargingcapabilityHandle(uint8_t *data, uint8_t gunID)
  526. {
  527. uint8_t addr = 0;
  528. float MaxVolt, MaxCurrent, MaxPower;
  529. static PricesInfo pricesInfo[2] = {0};
  530. CapabilityInfo *pCapabilityInfo = NULL;
  531. AccountInfo *pAccountInfo = NULL;
  532. //--- charging capability information --------------------------------------
  533. pCapabilityInfo = (CapabilityInfo *)&data[0];
  534. MaxVolt = (float)(ntohs(pCapabilityInfo->OutputVoltage));
  535. if ((MaxVolt >= 0) && (MaxVolt <= MAX_VOLTAGE)) {
  536. ChargingData[gunID]->MaximumChargingVoltage = MaxVolt;
  537. }
  538. MaxCurrent = (float)(ntohs(pCapabilityInfo->OutputCurrent));
  539. if ((MaxCurrent >= 0) && (MaxCurrent <= MAX_CURRENCY)) {
  540. ChargingData[gunID]->AvailableChargingCurrent = MaxCurrent;
  541. }
  542. MaxPower = (float)(ntohs(pCapabilityInfo->OutputPower));
  543. if ((MaxPower >= 0) && (MaxPower <= MAX_POWER)) {
  544. ChargingData[gunID]->AvailableChargingPower = MaxPower;
  545. }
  546. //MaxVolt = (float)(data[0] << 8 |data[1]);
  547. //MaxCurrent = (float)(data[2] << 8 | data[3]);
  548. //MaxPower = (float)(data[4] << 8 | data[5]);
  549. //printf("MaxVolt=%f, MaxCurrent=%f, MaxPower=%f,\n", MaxVolt, MaxCurrent, MaxPower);
  550. //--- user prices information ----------------------------------------------
  551. addr = (sizeof(CapabilityInfo) - 2); //2 byte reserved
  552. pAccountInfo = (AccountInfo *)&data[addr];
  553. ShmSysConfigAndInfo->SysConfig.BillingData.Currency = pAccountInfo->Currency;
  554. gAudiCustInfo->PricesInfo[gunID].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
  555. ChargingData[gunID]->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
  556. gAudiCustInfo->PricesInfo[gunID].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
  557. if ((pricesInfo[gunID].UserPrices != gAudiCustInfo->PricesInfo[gunID].UserPrices) ||
  558. (pricesInfo[gunID].Balance != gAudiCustInfo->PricesInfo[gunID].Balance)) {
  559. pricesInfo[gunID].UserPrices = gAudiCustInfo->PricesInfo[gunID].UserPrices;
  560. pricesInfo[gunID].Balance = gAudiCustInfo->PricesInfo[gunID].Balance;
  561. DEBUG_INFO("id = %d, user prices = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s\r\n",
  562. gunID,
  563. gAudiCustInfo->PricesInfo[gunID].UserPrices,
  564. ChargingData[gunID]->ChargingFee,
  565. gAudiCustInfo->PricesInfo[gunID].Balance,
  566. (uint8_t *)Currency[ShmSysConfigAndInfo->SysConfig.BillingData.Currency]);
  567. }
  568. return PASS;
  569. }
  570. static void addFaultCodeToBuf(uint8_t *Code)
  571. {
  572. uint8_t warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  573. if (warningCount < 10) {
  574. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[warningCount][0],
  575. Code,
  576. strlen((char *)Code));
  577. ShmSysConfigAndInfo->SysWarningInfo.WarningCount++;
  578. }
  579. }
  580. static void removeFaultCodeToBuf(uint8_t *Code)
  581. {
  582. uint8_t find = 0x01;
  583. uint8_t i = 0;
  584. char _code[7] = {0};
  585. sprintf(_code, "%s", Code);
  586. // 把相關的錯誤碼一次移除,避免重複顯示
  587. while (find) {
  588. usleep(128);
  589. find = 0x00;
  590. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  591. if (find == 0x00) {
  592. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], _code, 7) == 0) {
  593. find = 0x01;
  594. }
  595. } else {
  596. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i - 1][0],
  597. &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], 7);
  598. }
  599. }
  600. if (find) {
  601. ShmSysConfigAndInfo->SysWarningInfo.WarningCount--;
  602. }
  603. }
  604. }
  605. static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
  606. {
  607. uint8_t ret = 0;
  608. uint8_t i = 0;
  609. uint8_t count = 0;
  610. uint8_t EventCodeTmp[7] = {0};
  611. uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
  612. if (dataLen < WARNING_CODE_SIZE) {
  613. DEBUG_ERROR("raw data len < 6 = %d\r\n", dataLen);
  614. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0) {
  615. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  616. usleep(128);
  617. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= 0x33) { //from backend or power cabinet
  618. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  619. memcpy(EventCodeTmp,
  620. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  621. sizeof(EventCodeTmp));
  622. removeFaultCodeToBuf(EventCodeTmp);
  623. }
  624. }
  625. }
  626. return FAIL;
  627. }
  628. for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
  629. usleep(128);
  630. // misc command status code handle
  631. if (strcmp((char *)&data[count], MISC_ST_MISC_CMD) == 0) {
  632. if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  633. gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
  634. }
  635. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  636. continue;
  637. } else if (strcmp((char *)&data[count], MISC_ST_VERSION) == 0) {
  638. if (gDoCommGblData.MiscCmd != REG_REPORT_CSU_VERSION) {
  639. gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
  640. }
  641. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  642. continue;
  643. }
  644. strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
  645. ret = 0;
  646. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  647. usleep(128);
  648. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  649. StatusArray[count / WARNING_CODE_SIZE],
  650. WARNING_CODE_SIZE) == 0) {
  651. ret = 1;
  652. break;
  653. }
  654. }
  655. if (ret == 0) {
  656. addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
  657. }
  658. //Rtn=StatusCodeProcessing(StatusArray[count/6]);
  659. }
  660. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  661. usleep(128);
  662. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '3') { //from backend or power cabinet 0x33
  663. ret = 0;
  664. for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
  665. usleep(128);
  666. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  667. StatusArray[count],
  668. WARNING_CODE_SIZE) == 0) {
  669. ret = 1;
  670. break;
  671. }
  672. }
  673. if (ret == 0) {
  674. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  675. memcpy(EventCodeTmp,
  676. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  677. sizeof(EventCodeTmp));
  678. removeFaultCodeToBuf(EventCodeTmp);
  679. }
  680. }
  681. }
  682. return PASS;
  683. }
  684. static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t id, uint8_t reg)
  685. {
  686. int ret = PASS;
  687. uint8_t rawDataLen = 0;
  688. CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
  689. SoftwareUpdInfo *pSoftwareUpd = NULL;
  690. //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
  691. if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
  692. compareResult(pCsuResult->Data.Result) == FAIL ||
  693. compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
  694. if (reg != REG_CHARGING_PERMISSION) {
  695. return FAIL;
  696. }
  697. }
  698. rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
  699. switch (pCsuResult->Data.Register) {
  700. case REG_MODEL_NAME:
  701. break;
  702. case REG_CONNECTOR_ID:
  703. gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
  704. gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
  705. DEBUG_INFO("OK (Left connector ID = %d, Right connector ID = %d)\n",
  706. pCsuResult->Data.Data[0],
  707. pCsuResult->Data.Data[1]);
  708. break;
  709. case REG_POWER_CABINET_STATUS:
  710. ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
  711. break;
  712. case REG_DISPENSER_STATUS:
  713. memset(&PreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
  714. memcpy(&PreSysWarningInfo,
  715. &(ShmSysConfigAndInfo->SysWarningInfo),
  716. sizeof(struct WARNING_CODE_INFO));
  717. break;
  718. case REG_CHARGING_CAP:
  719. chargingcapabilityHandle(pCsuResult->Data.Data, id - 1);
  720. break;
  721. case REG_CHARGING_TARGET:
  722. break;
  723. case REG_SOFTWARE_UPDATE:
  724. pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
  725. if ((strcmp((char *)pSoftwareUpd->ImgName, "") == 0) ||
  726. (rawDataLen == 0) ||
  727. pSoftwareUpd->UpdateState != 1) {
  728. ret = FAIL;
  729. break;
  730. }
  731. ret = FAIL;
  732. ret = updateFirmwareHandle(pSoftwareUpd->ImgName);
  733. break;
  734. case REG_PLUG_IN_STATE:
  735. break;
  736. case REG_CONNECTOR_STATE:
  737. break;
  738. case REG_USER_ID:
  739. //DEBUG_INFO("%s\n", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
  740. //if (pCsuResult->Data.Data[0] <= 0) {
  741. // return FAIL;
  742. //}
  743. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  744. return COMMAND_RESULT_NG;
  745. }
  746. return pCsuResult->Data.Data[0];
  747. break;
  748. case REG_CHARGING_PERMISSION:
  749. //DEBUG_INFO("id = %d, result = %s, action = %s\r\n",
  750. // pCsuResult->Head.ID,
  751. // pCsuResult->Data.Result == 1 ? "OK" : "NG",
  752. // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
  753. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  754. return COMMAND_RESULT_NG;
  755. }
  756. return pCsuResult->Data.Data[0];
  757. break;
  758. case REG_MISC_CONTROL:
  759. miscCommandHandle(rawDataLen, pCsuResult->Head.ID, pCsuResult->Data.Data);
  760. break;
  761. case REG_REPORT_CSU_VERSION:
  762. case REG_REPORT_OTHER_VERSION:
  763. break;
  764. case REG_PRESENT_CHARGING_INFO:
  765. break;
  766. case REG_QRCODE_URL_INFO:
  767. ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
  768. break;
  769. case REG_WAIT_PLUG_IT_STATE:
  770. break;
  771. default:
  772. break;
  773. }
  774. return ret;
  775. }
  776. static int composeSocketData(int fd,
  777. uint8_t id,
  778. uint8_t opCode,
  779. uint8_t reg,
  780. uint8_t dataLen,
  781. uint8_t *data)
  782. {
  783. int ret = PASS;
  784. int size = 0;
  785. int sendPktLen = 0;
  786. CsuCmdPkt csuCmdPkt = {0};
  787. CsuResultPkt csuResult = {0};
  788. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  789. csuCmdPkt.Head.ID = id;
  790. csuCmdPkt.Head.OP = opCode;
  791. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  792. csuCmdPkt.Data.Register = reg;
  793. if ((data != NULL) && (dataLen > 0)) {
  794. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  795. }
  796. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  797. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  798. //send command packet
  799. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  800. DEBUG_ERROR("TCP socket send packet fail = %d\r\n", size);
  801. gDoCommGblData.DisConnCount++;
  802. return FAIL;
  803. }
  804. //receive result head
  805. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  806. DEBUG_ERROR("TCP socket RX head fail = %d\r\n", size);
  807. gDoCommGblData.DisConnCount++;
  808. return FAIL;
  809. }
  810. //receive result raw data
  811. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  812. DEBUG_ERROR("TCP socket RX data fail = %d\r\n", size);
  813. gDoCommGblData.DisConnCount++;
  814. return FAIL;
  815. }
  816. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, id, csuCmdPkt.Data.Register);
  817. return ret;
  818. }
  819. static int writeWaitPlugItState(int fd, uint8_t id)
  820. {
  821. int ret = PASS;
  822. uint8_t data[2] = {ShmSysConfigAndInfo->SysInfo.WaitForPlugit, 0};
  823. //printf("WaitForPlugit = %d\r\n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  824. if ((ret = composeSocketData(fd,
  825. id,
  826. OP_WRITE_DATA,
  827. REG_WAIT_PLUG_IT_STATE,
  828. 1,
  829. &data[0])) == FAIL) {
  830. return ret;
  831. }
  832. return ret;
  833. }
  834. static int readQRcodeURLAndSystemDate(int fd)
  835. {
  836. int ret = PASS;
  837. ret = composeSocketData(fd,
  838. ID_REGISTER,
  839. OP_READ_DATA,
  840. REG_QRCODE_URL_INFO,
  841. 0,
  842. NULL);
  843. return ret;
  844. }
  845. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  846. {
  847. int ret = PASS;
  848. uint8_t dataBuf[16] = {0};
  849. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  850. pPreChargingInfo->EvBatterySoc = htonl(ChargingData[plugNum]->EvBatterySoc);
  851. pPreChargingInfo->RemainChargingDuration = htonl(ChargingData[plugNum]->RemainChargingDuration);
  852. pPreChargingInfo->PresentChargingVoltage = htonl(ChargingData[plugNum]->PresentChargingVoltage);
  853. pPreChargingInfo->PresentChargingCurrent = htonl(ChargingData[plugNum]->PresentChargingCurrent);
  854. ret = composeSocketData(fd,
  855. id,
  856. OP_WRITE_DATA,
  857. REG_PRESENT_CHARGING_INFO,
  858. sizeof(PreChargingInfo),
  859. (uint8_t *)pPreChargingInfo);
  860. return ret;
  861. }
  862. static int writeOtherModuleVersion(int fd)
  863. {
  864. int ret = PASS;
  865. uint8_t data[255] = {0};
  866. uint8_t dataLen = 0;
  867. //report other module version message
  868. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, VERSION_BUF_SIZE);
  869. dataLen += VERSION_BUF_SIZE;
  870. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, VERSION_BUF_SIZE);
  871. dataLen += VERSION_BUF_SIZE;
  872. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector1FwRev, VERSION_BUF_SIZE);
  873. dataLen += VERSION_BUF_SIZE;
  874. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector2FwRev, VERSION_BUF_SIZE);
  875. dataLen += VERSION_BUF_SIZE;
  876. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, VERSION_BUF_SIZE);
  877. dataLen += VERSION_BUF_SIZE;
  878. ret = composeSocketData(fd,
  879. ID_REGISTER,
  880. OP_WRITE_DATA,
  881. REG_REPORT_OTHER_VERSION,
  882. dataLen,
  883. &data[0]);
  884. return ret;
  885. }
  886. static int writeCsuModuleVersion(int fd)
  887. {
  888. int ret = PASS;
  889. uint8_t data[255] = {0};
  890. uint8_t dataLen = 0;
  891. //report CSU platform version message
  892. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, VERSION_BUF_SIZE);
  893. dataLen += VERSION_BUF_SIZE;
  894. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, VERSION_BUF_SIZE);
  895. dataLen += VERSION_BUF_SIZE;
  896. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, VERSION_BUF_SIZE);
  897. dataLen += VERSION_BUF_SIZE;
  898. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, VERSION_BUF_SIZE);
  899. dataLen += VERSION_BUF_SIZE;
  900. ret = composeSocketData(fd,
  901. ID_REGISTER,
  902. OP_WRITE_DATA,
  903. REG_REPORT_CSU_VERSION,
  904. dataLen,
  905. &data[0]);
  906. return ret;
  907. }
  908. static int readMiscCommand(int fd, uint8_t id)
  909. {
  910. int ret = PASS;
  911. ret = composeSocketData(fd,
  912. id,
  913. OP_READ_DATA,
  914. REG_MISC_CONTROL,
  915. 0,
  916. NULL);
  917. return ret;
  918. }
  919. static int readChargePermission(int fd, uint8_t id)
  920. {
  921. //int ret = PASS;
  922. //if ((ret = composeSocketData(fd,
  923. // id,
  924. // OP_READ_DATA,
  925. // REG_CHARGING_PERMISSION,
  926. // 0,
  927. // NULL)) == FAIL) {
  928. // return ret;
  929. //}
  930. return composeSocketData(fd,
  931. id,
  932. OP_READ_DATA,
  933. REG_CHARGING_PERMISSION,
  934. 0,
  935. NULL);
  936. }
  937. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  938. {
  939. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  940. return FAIL;
  941. }
  942. return composeSocketData(fd,
  943. id,
  944. OP_WRITE_DATA,
  945. REG_USER_ID,
  946. strlen((char *)pUserID),
  947. pUserID);
  948. }
  949. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  950. {
  951. uint8_t dataBuf[2] = {0};
  952. int ret = PASS;
  953. if (ChargingData[plugNum]->SystemStatus <= 2) {
  954. dataBuf[0] = 0; //idle
  955. } else if ((ChargingData[plugNum]->SystemStatus <= 7) ||
  956. (ChargingData[plugNum]->SystemStatus == 17) ||
  957. (ChargingData[plugNum]->SystemStatus == 18)) {
  958. dataBuf[0] = 1; //preparing
  959. } else if (ChargingData[plugNum]->SystemStatus == 8) {
  960. dataBuf[0] = 2; //charging
  961. } else if (ChargingData[plugNum]->SystemStatus == 9) {
  962. dataBuf[0] = 3; //terminating
  963. }
  964. ret = composeSocketData(fd,
  965. id,
  966. OP_WRITE_DATA,
  967. REG_CONNECTOR_STATE,
  968. 1,
  969. &dataBuf[0]);
  970. return ret;
  971. }
  972. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  973. {
  974. int ret = PASS;
  975. uint8_t dataBuf[2] = {ChargingData[plugNum]->ConnectorPlugIn, 0};
  976. ret = composeSocketData(fd,
  977. id,
  978. OP_WRITE_DATA,
  979. REG_PLUG_IN_STATE,
  980. 1,
  981. &dataBuf[0]);
  982. return ret;
  983. }
  984. static int readSoftwareUpdate(int fd)
  985. {
  986. int ret = PASS;
  987. uint8_t dataBuf[2] = {ShmSysConfigAndInfo->SysInfo.FirmwareUpdate, 0};
  988. ret = composeSocketData(fd,
  989. 0x01,
  990. OP_READ_DATA,
  991. REG_SOFTWARE_UPDATE,
  992. 1,
  993. &dataBuf[0]);
  994. return ret;
  995. }
  996. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  997. {
  998. int ret = PASS;
  999. float ChargingVolt, ChargingAmp;
  1000. uint8_t dataBuf[4] = {0};
  1001. ChargingVolt = ChargingData[plugNum]->EvBatterytargetVoltage;
  1002. ChargingAmp = ChargingData[plugNum]->EvBatterytargetCurrent;
  1003. ChargingVolt *= 10;
  1004. ChargingAmp *= 10;
  1005. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1006. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1007. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1008. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1009. ret = composeSocketData(fd,
  1010. id,
  1011. OP_WRITE_DATA,
  1012. REG_CHARGING_TARGET,
  1013. sizeof(dataBuf),
  1014. &dataBuf[0]);
  1015. return ret;
  1016. }
  1017. static int readChargingCapability(int fd, uint8_t id)
  1018. {
  1019. int ret = PASS;
  1020. ret = composeSocketData(fd,
  1021. id,
  1022. OP_READ_DATA,
  1023. REG_CHARGING_CAP,
  1024. 0,
  1025. NULL);
  1026. return ret;
  1027. }
  1028. static int writeDispenserStatus(int fd)
  1029. {
  1030. uint8_t warningCount = 0;
  1031. uint8_t count = 0;
  1032. uint8_t CurWarnCodeTmp[10][6] = {0};
  1033. uint8_t PreWarnCodeTmp[10][6] = {0};
  1034. int ret = PASS;
  1035. if ((ShmSysConfigAndInfo->SysWarningInfo.WarningCount <= 0) &&
  1036. (PreSysWarningInfo.WarningCount <= 0)) {
  1037. return FAIL;
  1038. }
  1039. warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  1040. for (count = 0; count < warningCount; count++) {
  1041. memcpy(CurWarnCodeTmp[count], ShmSysConfigAndInfo->SysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1042. }
  1043. warningCount = PreSysWarningInfo.WarningCount;
  1044. for (count = 0; count < warningCount; count++) {
  1045. memcpy(PreWarnCodeTmp[count], PreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1046. }
  1047. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1048. return FAIL;
  1049. }
  1050. ret = composeSocketData(fd,
  1051. ID_OF_DISPENSER,
  1052. OP_WRITE_DATA,
  1053. REG_DISPENSER_STATUS,
  1054. strlen((char *)CurWarnCodeTmp),
  1055. &CurWarnCodeTmp[0][0]);
  1056. return ret;
  1057. }
  1058. static int readPowerCabinetStatus(int fd)
  1059. {
  1060. int ret = PASS;
  1061. ret = composeSocketData(fd,
  1062. ID_OF_DISPENSER,
  1063. OP_READ_DATA,
  1064. REG_POWER_CABINET_STATUS,
  1065. 0,
  1066. NULL);
  1067. return ret;
  1068. }
  1069. static int readConnectorID(int fd)
  1070. {
  1071. int ret = PASS;
  1072. ret = composeSocketData(fd,
  1073. ID_REGISTER,
  1074. OP_READ_DATA,
  1075. REG_CONNECTOR_ID,
  1076. 0,
  1077. NULL);
  1078. return ret;
  1079. }
  1080. static int WriteModelName(int fd)
  1081. {
  1082. int ret = PASS;
  1083. uint8_t Tmp = 0;
  1084. uint8_t TmpBuf[255] = {0};
  1085. memcpy(TmpBuf,
  1086. ShmSysConfigAndInfo->SysConfig.ModelName,
  1087. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName));
  1088. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1089. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1090. ShmPrimaryMcuData->InputDet.bits.Key0);
  1091. TmpBuf[strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName)] = Tmp; //Dispenser switch value
  1092. ret = composeSocketData(fd,
  1093. ID_REGISTER,
  1094. OP_WRITE_DATA,
  1095. REG_MODEL_NAME,
  1096. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName) + 1,
  1097. &TmpBuf[0]);
  1098. return ret;
  1099. }
  1100. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1101. {
  1102. sleep(3); //wait 1 second
  1103. if ((*pValue) >= maxCount) {
  1104. setTcpStatus(ABNORMAL);
  1105. } else {
  1106. (*pValue)++;
  1107. }
  1108. }
  1109. static int CheckNetworkStatus(void)
  1110. {
  1111. char *ipAddr = (char *)&ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress;
  1112. //printf("Check network IP Header = %s\n", ipAddr);
  1113. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1114. return 1;
  1115. }
  1116. return 0;
  1117. }
  1118. static int FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  1119. {
  1120. uint8_t index = 0;
  1121. for (index = 0; index < CHAdeMO_QUANTITY; index++) {
  1122. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target) {
  1123. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  1124. return 1;
  1125. }
  1126. }
  1127. for (index = 0; index < CCS_QUANTITY; index++) {
  1128. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target) {
  1129. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  1130. return 1;
  1131. }
  1132. }
  1133. for (index = 0; index < GB_QUANTITY; index++) {
  1134. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target) {
  1135. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  1136. return 1;
  1137. }
  1138. }
  1139. return 0;
  1140. }
  1141. static int InitShareMemory(void)
  1142. {
  1143. int result = 1;
  1144. int MeterSMId = 0;
  1145. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  1146. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1147. result = 0;
  1148. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1149. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  1150. result = 0;
  1151. }
  1152. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  1153. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1154. result = 0;
  1155. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1156. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1157. result = 0;
  1158. }
  1159. //creat ShmPsuData
  1160. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0) {
  1161. DEBUG_ERROR("shmget ShmPsuData NG \n");
  1162. result = 0;
  1163. } else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1164. DEBUG_ERROR("shmat ShmPsuData NG \n");
  1165. result = 0;
  1166. }
  1167. //creat ShmOCPP16Data
  1168. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0) {
  1169. DEBUG_ERROR("shmget ShmOCPP16Data NG \n");
  1170. result = 0;
  1171. } else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1172. DEBUG_ERROR("shmat ShmOCPP16Data NG \n");
  1173. result = 0;
  1174. }
  1175. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0) {
  1176. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1177. result = 0;
  1178. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1179. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1180. result = 0;
  1181. }
  1182. //creat Audi customization info
  1183. if ((MeterSMId = shmget(ShmAudiCustInfoKey, sizeof(AudiCustInfo), IPC_CREAT | 0777)) < 0) {
  1184. DEBUG_ERROR("[main]CreatShareMemory:shmget select gun info NG \n");
  1185. return 0;
  1186. } else if ((gAudiCustInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1187. DEBUG_ERROR("[main]CreatShareMemory:shmat shmget select gun info \n");
  1188. return 0;
  1189. }
  1190. return result;
  1191. }
  1192. static void updateFirmwareProcess(int fd, uint8_t totalConnCount)
  1193. {
  1194. bool canUpdateFirmware = true;
  1195. uint8_t plugNum = 0;
  1196. uint8_t ackCount = 5;
  1197. struct timeb updateTime;
  1198. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2) {
  1199. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1200. if (ChargingData[plugNum]->SystemStatus != S_IDLE &&
  1201. ChargingData[plugNum]->SystemStatus != S_RESERVATION &&
  1202. ChargingData[plugNum]->SystemStatus != S_MAINTAIN) {
  1203. canUpdateFirmware = false;
  1204. }
  1205. }
  1206. }
  1207. if (canUpdateFirmware) {
  1208. ftime(&updateTime);
  1209. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  1210. readSoftwareUpdate(fd);
  1211. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1212. }
  1213. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  1214. while (ackCount != 0) {
  1215. ftime(&updateTime);
  1216. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  1217. readSoftwareUpdate(fd);
  1218. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1219. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == NO) {
  1220. ackCount--;
  1221. }
  1222. }
  1223. usleep(128);
  1224. }
  1225. }
  1226. }
  1227. }
  1228. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1229. {
  1230. int ret = 0;
  1231. struct timeb AuthNowTime;
  1232. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) || (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == YES)) {
  1233. ftime(&AuthNowTime);
  1234. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1235. ret = writeUserID(fd,
  1236. ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1,
  1237. ShmSysConfigAndInfo->SysConfig.UserId);
  1238. if (ret >= 0) {
  1239. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1240. 0,
  1241. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1242. if (ret == 0) {
  1243. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1244. } else {
  1245. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1246. }
  1247. //printf("gAudiCustInfo->PricesInfo[plugNum].Balance = %.2f, %.2f\r\n",
  1248. // gAudiCustInfo->PricesInfo[plugNum].Balance,
  1249. // FAIL_BALANCE_PRICES);
  1250. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1251. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = 1; //isAuthorizedComplete
  1252. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = 0;
  1253. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = 0;
  1254. ShmPsuData->SystemAvailablePower = 0;
  1255. ShmPsuData->SystemPresentPsuQuantity = 0;
  1256. }
  1257. }
  1258. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1259. }
  1260. }
  1261. }
  1262. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1263. {
  1264. int ret = DISPENSER_INIT_SUCC;
  1265. int isContinue = 1;
  1266. struct timeb NowTime;
  1267. while (isContinue) {
  1268. usleep(128);
  1269. ftime(&NowTime);
  1270. switch (curReg) {
  1271. case REG_MODEL_NAME:
  1272. if (WriteModelName(fd) == PASS) {
  1273. gDoCommGblData.DisConnCount = 0;
  1274. curReg = REG_CONNECTOR_ID;
  1275. }
  1276. break;
  1277. case REG_CONNECTOR_ID:
  1278. if (readConnectorID(fd) == PASS) {
  1279. gDoCommGblData.DisConnCount = 0;
  1280. curReg = REG_REPORT_CSU_VERSION;
  1281. }
  1282. break;
  1283. case REG_POWER_CABINET_STATUS:
  1284. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1285. readPowerCabinetStatus(fd);
  1286. ftime(&gRegTimeUp[plugNum][curReg]);
  1287. }
  1288. curReg = REG_DISPENSER_STATUS;
  1289. break;
  1290. case REG_DISPENSER_STATUS:
  1291. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1292. writeDispenserStatus(fd);
  1293. ftime(&gRegTimeUp[plugNum][curReg]);
  1294. }
  1295. curReg = REG_PLUG_IN_STATE;
  1296. break;
  1297. //case REG_CHARGING_CAP:
  1298. // readChargingCapability(fd, plugNum, gunID);
  1299. // break;
  1300. //case REG_CHARGING_TARGET:
  1301. // writeChargingTarget(fd, plugNum, gunID);
  1302. // break;
  1303. //case REG_SOFTWARE_UPDATE:
  1304. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1305. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME * 10) {
  1306. // readSoftwareUpdate(fd);
  1307. // ftime(&gRegTimeUp[plugNum][curReg]);
  1308. // }
  1309. // while (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  1310. // ftime(&NowTime);
  1311. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1312. // readSoftwareUpdate(fd);
  1313. // }
  1314. // usleep(128);
  1315. // }
  1316. // }
  1317. // isContinue = 0;
  1318. // break;
  1319. case REG_PLUG_IN_STATE:
  1320. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1321. writePlugInStatus(fd, plugNum, gunID);
  1322. ftime(&gRegTimeUp[plugNum][curReg]);
  1323. }
  1324. curReg = REG_CONNECTOR_STATE;
  1325. break;
  1326. case REG_CONNECTOR_STATE:
  1327. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1328. writeConnectorState(fd, plugNum, gunID);
  1329. ftime(&gRegTimeUp[plugNum][curReg]);
  1330. }
  1331. //check misc command from power cabinet
  1332. if (gDoCommGblData.MiscCmd != 0) {
  1333. curReg = gDoCommGblData.MiscCmd;
  1334. } else {
  1335. curReg = REG_QRCODE_URL_INFO;
  1336. }
  1337. break;
  1338. //case REG_USER_ID:
  1339. // writeUserID(fd, gunID, uint8_t *pUserID);
  1340. // break;
  1341. //case REG_CHARGING_PERMISSION:
  1342. // readChargePermission(fd, gunID);
  1343. // break;
  1344. case REG_MISC_CONTROL:
  1345. readMiscCommand(fd, gunID);
  1346. isContinue = 0;
  1347. break;
  1348. case REG_REPORT_CSU_VERSION:
  1349. case REG_REPORT_OTHER_VERSION:
  1350. if (gDoCommGblData.MiscCmd != 0) {
  1351. writeCsuModuleVersion(fd);
  1352. writeOtherModuleVersion(fd);
  1353. clearMiscCommand();
  1354. isContinue = 0;
  1355. } else {
  1356. //power up write dispenser version and get power cabinet system ID
  1357. if ((writeCsuModuleVersion(fd) == PASS) &&
  1358. (writeOtherModuleVersion(fd) == PASS) &&
  1359. (readQRcodeURLAndSystemDate(fd) == PASS)) {
  1360. //gDoCommGblData.DisConnCount = 0;
  1361. isContinue = 0;
  1362. }
  1363. }
  1364. break;
  1365. //case REG_PRESENT_CHARGING_INFO:
  1366. // break;
  1367. case REG_QRCODE_URL_INFO:
  1368. if (gunID == 1) {
  1369. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10)) {
  1370. readQRcodeURLAndSystemDate(fd);
  1371. ftime(&gRegTimeUp[plugNum][curReg]);
  1372. }
  1373. }
  1374. isContinue = 0;
  1375. //curReg = REG_SOFTWARE_UPDATE;
  1376. break;
  1377. //case REG_WAIT_PLUG_IT_STATE:
  1378. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1379. // writeWaitPlugItState(fd, gunID);
  1380. // ftime(&gRegTimeUp[plugNum][curReg]);
  1381. // }
  1382. // isContinue = 0;
  1383. // break;
  1384. default:
  1385. DEBUG_ERROR("error curReg = %x\r\n", curReg);
  1386. gDoCommGblData.MiscCmd = 0;
  1387. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  1388. isContinue = 0;
  1389. break;
  1390. }
  1391. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  1392. return DISPENSER_SOCKET_RECONN;
  1393. }
  1394. }//for
  1395. return ret;
  1396. }
  1397. static bool isDetectPlugin()
  1398. {
  1399. if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES) {
  1400. return YES;
  1401. }
  1402. return NO;
  1403. }
  1404. static void systemStatusProcess(int fd, uint8_t plugNum, uint8_t gunID)
  1405. {
  1406. struct timeb AuthNowTime = {0};
  1407. switch (ChargingData[plugNum]->SystemStatus) {
  1408. case S_IDLE:
  1409. case S_RESERVATION:
  1410. case S_MAINTAIN:
  1411. case S_ALARM:
  1412. case S_AUTHORIZING:
  1413. checkAuthorProcess(fd, plugNum);
  1414. //authorization complete, write wait plug it state
  1415. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1416. ftime(&AuthNowTime);
  1417. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10)) {
  1418. writeWaitPlugItState(fd, gunID);
  1419. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  1420. }
  1421. ftime(&AuthNowTime);
  1422. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1423. readChargingCapability(fd, gunID);
  1424. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1425. }
  1426. }
  1427. break;
  1428. case S_PREPARNING: //get permission
  1429. ftime(&AuthNowTime);
  1430. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME) {
  1431. writeConnectorState(fd, plugNum, gunID);
  1432. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  1433. }
  1434. ftime(&AuthNowTime);
  1435. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1436. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  1437. ShmPsuData->SystemAvailablePower = (ChargingData[0]->AvailableChargingPower +
  1438. ChargingData[1]->AvailableChargingPower);
  1439. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  1440. }
  1441. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1442. }
  1443. ftime(&AuthNowTime);
  1444. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1445. writePresentChargingInfo(fd, plugNum, gunID);
  1446. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1447. }
  1448. break;
  1449. case S_PREPARING_FOR_EV://wait connector lock
  1450. ftime(&AuthNowTime);
  1451. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1452. readChargingCapability(fd, gunID);
  1453. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1454. }
  1455. ftime(&AuthNowTime);
  1456. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1457. if (readChargePermission(fd, gunID) == 0) {
  1458. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1459. plugNum,
  1460. REG_CHARGING_PERMISSION);
  1461. ChargingData[plugNum]->StopChargeFlag = 1;
  1462. }
  1463. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1464. }
  1465. ftime(&AuthNowTime);
  1466. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1467. writePresentChargingInfo(fd, plugNum, gunID);
  1468. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1469. }
  1470. break;
  1471. case S_PREPARING_FOR_EVSE: //insaulation test
  1472. case S_CCS_PRECHARGE_ST0:
  1473. case S_CCS_PRECHARGE_ST1:
  1474. writeChargingTarget(fd, plugNum, gunID);
  1475. ftime(&AuthNowTime);
  1476. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1477. readChargingCapability(fd, gunID);
  1478. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1479. }
  1480. ftime(&AuthNowTime);
  1481. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1482. if (readChargePermission(fd, gunID) == 0) {
  1483. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1484. plugNum,
  1485. REG_CHARGING_PERMISSION);
  1486. ChargingData[plugNum]->StopChargeFlag = 1;
  1487. }
  1488. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1489. }
  1490. ftime(&AuthNowTime);
  1491. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1492. writePresentChargingInfo(fd, plugNum, gunID);
  1493. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1494. }
  1495. break;
  1496. case S_CHARGING: //charging
  1497. case S_TERMINATING:
  1498. ftime(&AuthNowTime);
  1499. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1500. readChargingCapability(fd, gunID);
  1501. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1502. }
  1503. ftime(&AuthNowTime);
  1504. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1505. if (readChargePermission(fd, gunID) == 0) {
  1506. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1507. plugNum,
  1508. REG_CHARGING_PERMISSION);
  1509. ChargingData[plugNum]->StopChargeFlag = 1;
  1510. }
  1511. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1512. }
  1513. writeChargingTarget(fd, plugNum, gunID);
  1514. ftime(&AuthNowTime);
  1515. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1516. writePresentChargingInfo(fd, plugNum, gunID);
  1517. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1518. }
  1519. break;
  1520. case S_COMPLETE:
  1521. ftime(&AuthNowTime);
  1522. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1523. readChargingCapability(fd, gunID);
  1524. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1525. }
  1526. break;
  1527. default:
  1528. break;
  1529. }
  1530. }
  1531. static int networkCreatePorcess(void)
  1532. {
  1533. int fd = 0;
  1534. /**************** Check Network **********/
  1535. DEBUG_INFO("Check Dispenser Network Information");
  1536. while (!CheckNetworkStatus()) {
  1537. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  1538. DEBUG_ERROR("disconnect count = % d\r\n", gDoCommGblData.DisConnCount);
  1539. }
  1540. DEBUG_INFO("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  1541. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1542. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1543. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1544. gDoCommGblData.DisConnCount = 0;
  1545. /**************** Power cabinet connection **********/
  1546. DEBUG_INFO("Connect to Power Cabinet");
  1547. while ((fd = doCommConnToServer()) <= 0) {
  1548. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  1549. DEBUG_ERROR("disconnect count = %d, fd = %d\r\n", gDoCommGblData.DisConnCount, fd);
  1550. }
  1551. DEBUG_INFO("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  1552. fd,
  1553. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1554. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1555. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1556. gDoCommGblData.DisConnCount = 0;
  1557. sleep(3);
  1558. setTcpStatus(NORMAL);
  1559. return fd;
  1560. }
  1561. int main(int argc, char *argv[])
  1562. {
  1563. uint8_t plugNum = 0, gunID = 0;
  1564. uint8_t index = 0;
  1565. uint8_t totalConnCount = 0;
  1566. uint8_t initDone = DISPENER_INIT_FAIL;
  1567. int fd = 0;
  1568. int isContinue = 1;
  1569. InitSocketSigPipe();
  1570. /**************** Initialization **********/
  1571. if (InitShareMemory() == 0) {
  1572. if (ShmStatusCodeData != NULL) {
  1573. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = YES;
  1574. }
  1575. DEBUG_ERROR("DoComm Initial share memory fail\r\n");
  1576. sleep(5);
  1577. return 0;
  1578. }
  1579. totalConnCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1580. for (index = 0; index < totalConnCount; index++) {
  1581. if (!FindChargingInfoData(index, &ChargingData[0])) {
  1582. DEBUG_ERROR("FindChargingInfoData false \n");
  1583. break;
  1584. }
  1585. gAudiCustInfo->PricesInfo[index].Balance = FAIL_BALANCE_PRICES;
  1586. }
  1587. //initial trigger message timer
  1588. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  1589. ftime(&gRegTimeUp[0][index]);
  1590. ftime(&gRegTimeUp[1][index]);
  1591. usleep(50000);
  1592. }
  1593. setTcpStatus(NORMAL);
  1594. while (isContinue) {
  1595. if (initDone == DISPENER_INIT_FAIL) {
  1596. fd = networkCreatePorcess();
  1597. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  1598. } else {
  1599. checkAuthorProcess(fd, 0);
  1600. plugNum = 0;
  1601. gunID = 0;
  1602. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1603. //plugNum = setup chargingData value for bottom layer
  1604. gunID = (plugNum + 1); //connector Id, 1 = left gun, 2 = right gun
  1605. systemStatusProcess(fd, plugNum, gunID);
  1606. initDone = messageTrigger(fd,
  1607. plugNum,
  1608. gunID,
  1609. REG_POWER_CABINET_STATUS);
  1610. if (initDone == DISPENSER_SOCKET_RECONN) {
  1611. break;
  1612. }
  1613. }
  1614. }
  1615. if (initDone == DISPENSER_SOCKET_RECONN) {
  1616. DEBUG_INFO("Disconnected from power cabinet...re-connecting");
  1617. setTcpStatus(ABNORMAL);
  1618. gDoCommGblData.DisConnCount = 0;
  1619. gDoCommGblData.SeqNum = 0;
  1620. closeSocket(fd);
  1621. fd = 0;
  1622. initDone = DISPENER_INIT_FAIL;
  1623. }
  1624. //update dispenser firmware
  1625. if (initDone != DISPENER_INIT_FAIL) {
  1626. updateFirmwareProcess(fd, totalConnCount);
  1627. }
  1628. usleep(1000);
  1629. }
  1630. }