ReDoComm.c 62 KB

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