ReDoComm.c 59 KB

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