Module_PowerSharing.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * Module_PowerSharing.c
  3. *
  4. * Created on: 2020/12/07
  5. * Author: foluswen
  6. */
  7. #include "Module_PowerSharing.h"
  8. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  9. struct StatusCodeData *ShmStatusCodeData;
  10. struct OCPP16Data *ShmOCPP16Data;
  11. struct Charger *ShmCharger;
  12. struct POWER_SHARING *ShmPowerSharing;
  13. //==========================================
  14. // Common routine
  15. //==========================================
  16. int StoreLogMsg(const char *fmt, ...)
  17. {
  18. char Buf[4096+256];
  19. char buffer[4096];
  20. time_t CurrentTime;
  21. struct tm *tm;
  22. struct timeval tv;
  23. va_list args;
  24. va_start(args, fmt);
  25. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  26. va_end(args);
  27. memset(Buf,0,sizeof(Buf));
  28. CurrentTime = time((time_t*)NULL);
  29. tm=localtime(&CurrentTime);
  30. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  31. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]Module_PowerSharingLog",
  32. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  33. buffer,
  34. tm->tm_year+1900,tm->tm_mon+1);
  35. #ifdef SystemLogMessage
  36. system(Buf);
  37. #endif
  38. #ifdef ConsloePrintLog
  39. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  40. #endif
  41. return rc;
  42. }
  43. int DiffTimeb(struct timeb ST, struct timeb ET)
  44. {
  45. //return milli-second
  46. unsigned int StartTime,StopTime;
  47. StartTime=(unsigned int)ST.time;
  48. StopTime=(unsigned int)ET.time;
  49. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  50. }
  51. void dM(uint8_t *data, uint16_t len, uint8_t isRX)
  52. {
  53. #ifdef DEBUG
  54. uint8_t output[8192];
  55. if(isRX)
  56. {
  57. DEBUG_INFO("- RX --------------------------------------------\n");
  58. }
  59. else
  60. {
  61. DEBUG_INFO("- TX --------------------------------------------\n");
  62. }
  63. memset(output, 0x00, ARRAY_SIZE(output));
  64. for(uint16_t idx=0;idx<16;idx++)
  65. sprintf((char*)output, "%s %02X", output, idx);
  66. DEBUG_INFO("%s\n", output);
  67. DEBUG_INFO("-------------------------------------------------\n");
  68. for(uint16_t idx = 0;idx<len;idx++)
  69. {
  70. if((idx%16)>0)
  71. {
  72. sprintf((char*)output, "%s %02X", output, data[idx]);
  73. }
  74. else
  75. {
  76. if(idx != 0)
  77. DEBUG_INFO("%s\n", output);
  78. memset(output, 0x00, ARRAY_SIZE(output));
  79. sprintf((char*)output, "%s %02X", output, data[idx]);
  80. }
  81. }
  82. DEBUG_INFO("%s\n", output);
  83. DEBUG_INFO("-------------------------------------------------\n");
  84. #endif
  85. }
  86. int isValidCheckSum(struct Message *message)
  87. {
  88. uint8_t chksum = 0x00;
  89. for(int idx=0;idx<(((message->buffer[2]) | message->buffer[3]<<8)>ARRAY_SIZE(message->buffer)?ARRAY_SIZE(message->buffer):((message->buffer[2]) | message->buffer[3]<<8));idx++)
  90. {
  91. chksum ^= message->buffer[4+idx];
  92. }
  93. return ((chksum == message->buffer[4+((message->buffer[2] | message->buffer[3]<<8)>ARRAY_SIZE(message->buffer)?ARRAY_SIZE(message->buffer):(message->buffer[2] | message->buffer[3]<<8))]) ? PASS : FAIL);
  94. }
  95. uint8_t chksumCal(struct Message *message)
  96. {
  97. uint8_t chksum=0;
  98. for(int idx=0;idx<(((message->buffer[2]) | message->buffer[3]<<8)>ARRAY_SIZE(message->buffer)?ARRAY_SIZE(message->buffer):((message->buffer[2]) | message->buffer[3]<<8));idx++)
  99. {
  100. chksum ^= message->buffer[4+idx];
  101. }
  102. return chksum & 0xff;
  103. }
  104. //==========================================
  105. // Init all share memory
  106. //==========================================
  107. int InitShareMemory()
  108. {
  109. int result = PASS;
  110. int MeterSMId;
  111. //Initial ShmSysConfigAndInfo
  112. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  113. {
  114. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  115. result = FAIL;
  116. }
  117. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  118. {
  119. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  120. result = FAIL;
  121. }
  122. else
  123. {}
  124. //Initial ShmStatusCodeData
  125. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  126. {
  127. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  128. result = FAIL;
  129. }
  130. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  131. {
  132. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  133. result = FAIL;
  134. }
  135. else
  136. {}
  137. //Initial ShmOCPP16Data
  138. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  139. {
  140. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  141. result = FAIL;
  142. }
  143. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  144. {
  145. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  146. result = FAIL;
  147. }
  148. else
  149. {}
  150. //Initial ShmCharger
  151. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), 0777)) < 0)
  152. {
  153. DEBUG_ERROR("shmget ShmCharger NG\n");
  154. result = FAIL;
  155. }
  156. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  157. {
  158. DEBUG_ERROR("shmat ShmCharger NG\n");
  159. result = FAIL;
  160. }
  161. //Create ShmPowerSharing
  162. if ((MeterSMId = shmget(ShmPowerShargingKey, sizeof(struct POWER_SHARING), IPC_CREAT | 0777)) < 0)
  163. {
  164. DEBUG_ERROR("shmget ShmPowerShargingKey NG\n");
  165. result = FAIL;
  166. }
  167. else if ((ShmPowerSharing = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  168. {
  169. DEBUG_ERROR("shmat ShmPowerShargingKey NG\n");
  170. result = FAIL;
  171. }
  172. memset(ShmPowerSharing,0,sizeof(struct POWER_SHARING));
  173. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  174. ShmPowerSharing->Connection_Info[idx].socketFd = (idx+1);
  175. return result;
  176. }
  177. //==========================================
  178. // UDP socket server routine
  179. //==========================================
  180. int udpSocketServerStart(void)
  181. {
  182. int sockFd;
  183. struct sockaddr_in servaddr;
  184. struct sockaddr_in peeraddr;
  185. socklen_t peerlen = sizeof(peeraddr);
  186. uint8_t inputBuffer[2048] = {};
  187. uint8_t outBuffer[2048] = {};
  188. int16_t read_size;
  189. int16_t tx_size;
  190. memset(&servaddr, 0, sizeof(servaddr));
  191. servaddr.sin_family = AF_INET;
  192. servaddr.sin_port = htons(LISTEN_PORT_UDP);
  193. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  194. if ((sockFd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  195. {
  196. DEBUG_ERROR("UDP server socket create fail.\n");
  197. return FAIL;
  198. }
  199. if (bind(sockFd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
  200. {
  201. DEBUG_ERROR("UDP server socket bind fail.\n");
  202. return FAIL;
  203. }
  204. else
  205. DEBUG_INFO("UDP server initial.\n");
  206. for(;;)
  207. {
  208. if((read_size = recvfrom(sockFd, inputBuffer, sizeof(inputBuffer), 0, (struct sockaddr *)&peeraddr, &peerlen)) > 0)
  209. {
  210. DEBUG_INFO("Revieve from: %s:%d\n", inet_ntoa(peeraddr.sin_addr), htons(peeraddr.sin_port));
  211. DEBUG_INFO("read_size: %d\n",read_size);
  212. dM(inputBuffer, read_size, YES);
  213. if(read_size>=6)
  214. {
  215. /*
  216. * TODO:
  217. * 1. Protocol validation
  218. * 2. Protocol message parsing
  219. */
  220. if(TRUE)
  221. {
  222. DEBUG_INFO("Receive UDP broadcast command.\n");
  223. memset(outBuffer, 0x00, ARRAY_SIZE(outBuffer));
  224. tx_size = 41;
  225. outBuffer[0] = 0xff;
  226. outBuffer[1] = 0xff;
  227. outBuffer[2] = (0x25 << 0x08) & 0xff;
  228. outBuffer[3] = 0x25 & 0xff;
  229. outBuffer[4] = 0x00;
  230. dM(outBuffer, tx_size, NO);
  231. sendto(sockFd, outBuffer, tx_size, 0, (struct sockaddr *)&peeraddr, peerlen);
  232. }
  233. }
  234. }
  235. }
  236. return FAIL;
  237. }
  238. //==========================================
  239. // TCP socket server routine
  240. //==========================================
  241. int conn_getDupFd(void)
  242. {
  243. int result = 0;
  244. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  245. {
  246. if(!ShmPowerSharing->Connection_Info[idx].isConnected)
  247. {
  248. result = ShmPowerSharing->Connection_Info[idx].socketFd;
  249. break;
  250. }
  251. }
  252. return result;
  253. }
  254. int conn_register(int socketFd)
  255. {
  256. int result = FAIL;
  257. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  258. {
  259. if(!ShmPowerSharing->Connection_Info[idx].isConnected)
  260. {
  261. DEBUG_INFO("Dupfd-%d register to conn-%d.\n", socketFd, idx);
  262. ShmPowerSharing->Connection_Info[idx].isConnected = TRUE;
  263. ShmPowerSharing->Connection_Info[idx].socketFd = socketFd;
  264. ShmPowerSharing->Connection_Info[idx].lastHeartBeatTime = time((time_t*)NULL);
  265. result = PASS;
  266. break;
  267. }
  268. }
  269. return result;
  270. }
  271. int conn_reject(int socketFd)
  272. {
  273. int result = FAIL;
  274. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  275. {
  276. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  277. {
  278. DEBUG_INFO("Dupfd-%d register from conn_info-%d.\n", socketFd, idx);
  279. ShmPowerSharing->Connection_Info[idx].isConnected = FALSE;
  280. ShmPowerSharing->Connection_Info[idx].isCharging = FALSE;
  281. result = PASS;
  282. break;
  283. }
  284. }
  285. return result;
  286. }
  287. int conn_getConectedQuantity(void)
  288. {
  289. int result = 0;
  290. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  291. {
  292. if(ShmPowerSharing->Connection_Info[idx].isConnected)
  293. {
  294. result += 1;
  295. }
  296. }
  297. DEBUG_INFO("Connection quantity: %d\n", result);
  298. ShmPowerSharing->connectedQty = result;
  299. return result;
  300. }
  301. int conn_getChargingQuantity(void)
  302. {
  303. int result = 0;
  304. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  305. {
  306. if(ShmPowerSharing->Connection_Info[idx].isCharging)
  307. {
  308. result += 1;
  309. }
  310. }
  311. //DEBUG_INFO("Charging quantity: %d\n", result);
  312. return result;
  313. }
  314. int conn_getTotalAvailableSharingCurrent(void)
  315. {
  316. int result = 0;
  317. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  318. {
  319. if(ShmPowerSharing->Connection_Info[idx].isConnected)
  320. {
  321. result += ShmPowerSharing->Connection_Info[idx].availableSharingCurrent;
  322. }
  323. }
  324. DEBUG_INFO("Total sharing current: %d\n", result);
  325. return result;
  326. }
  327. int conn_getTotalPresentOutputCurrent(void)
  328. {
  329. int result = 0;
  330. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  331. {
  332. if(ShmPowerSharing->Connection_Info[idx].isConnected && ShmPowerSharing->Connection_Info[idx].isCharging)
  333. {
  334. result += ShmPowerSharing->Connection_Info[idx].presentOutputCurrent;
  335. }
  336. }
  337. DEBUG_INFO("Total actual current: %d\n", result);
  338. return result;
  339. }
  340. uint16_t conn_querySharingCurrent(int socketFd)
  341. {
  342. uint16_t result = 0x00;
  343. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  344. {
  345. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  346. {
  347. //DEBUG_INFO("Dupfd-%d on conn_info-%d query sharing current(0.1A): %d\n", socketFd, idx, ShmPowerSharing->Connection_Info[idx].sharingCurrent);
  348. result = ShmPowerSharing->Connection_Info[idx].availableSharingCurrent;
  349. break;
  350. }
  351. }
  352. return result;
  353. }
  354. int conn_updateHeartBeatTime(int socketFd)
  355. {
  356. int result = FAIL;
  357. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  358. {
  359. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  360. {
  361. //DEBUG_INFO("Dupfd-%d register from conn_info-%d update heart beat time.\n", socketFd, idx);
  362. ShmPowerSharing->Connection_Info[idx].lastHeartBeatTime = time((time_t*)NULL);
  363. result = PASS;
  364. break;
  365. }
  366. }
  367. return result;
  368. }
  369. int conn_updatePresentCurrentOutput(int socketFd, uint8_t isCharging, uint16_t outputCurrent)
  370. {
  371. int result = FAIL;
  372. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  373. {
  374. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  375. {
  376. //DEBUG_INFO("Dupfd-%d on conn_info-%d update actual output current(0.1A): %d\n", socketFd, idx, outputCurrent);
  377. ShmPowerSharing->Connection_Info[idx].isCharging = isCharging;
  378. ShmPowerSharing->Connection_Info[idx].presentOutputCurrent = outputCurrent;
  379. result = PASS;
  380. }
  381. }
  382. return result;
  383. }
  384. int conn_check_loop(void)
  385. {
  386. for(;;)
  387. {
  388. // Check conn heart beat
  389. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  390. {
  391. if(ShmPowerSharing->Connection_Info[idx].isConnected &&
  392. (difftime(time((time_t*)NULL), ShmPowerSharing->Connection_Info[idx].lastHeartBeatTime) > 300))
  393. {
  394. DEBUG_INFO("SocketFd-%d heart beat is over 300 seconds.\n", ShmPowerSharing->Connection_Info[idx].socketFd);
  395. ShmPowerSharing->Connection_Info[idx].isCharging = FALSE;
  396. ShmPowerSharing->Connection_Info[idx].isConnected = FALSE;
  397. }
  398. }
  399. // Check available power
  400. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  401. {
  402. if(ShmPowerSharing->Connection_Info[idx].isConnected &&
  403. ShmPowerSharing->Connection_Info[idx].isCharging)
  404. {
  405. if(ShmPowerSharing->Connection_Info[idx].availableSharingCurrent != ((ShmCharger->gun_info[0].primaryMcuState.rating_current*10) / conn_getChargingQuantity()))
  406. {
  407. DEBUG_INFO("Dupfd-%d on conn_info-%d update sharing current(0.1A): %d\n", ShmPowerSharing->Connection_Info[idx].socketFd, idx, ((ShmCharger->gun_info[0].primaryMcuState.rating_current*10) / conn_getChargingQuantity()));
  408. }
  409. ShmPowerSharing->Connection_Info[idx].availableSharingCurrent = (ShmCharger->gun_info[0].primaryMcuState.rating_current*10) / conn_getChargingQuantity();
  410. }
  411. else
  412. {
  413. if(ShmPowerSharing->Connection_Info[idx].availableSharingCurrent != 0)
  414. {
  415. DEBUG_INFO("Dupfd-%d on conn_info-%d update sharing current(0.1A): 0\n", ShmPowerSharing->Connection_Info[idx].socketFd, idx);
  416. }
  417. ShmPowerSharing->Connection_Info[idx].availableSharingCurrent = 0;
  418. }
  419. }
  420. sleep(1);
  421. }
  422. return FAIL;
  423. }
  424. int tcpSocketServerStart(void)
  425. {
  426. int sockFd = 0;
  427. int clientSockFd = 0;
  428. int dupFd = 0;
  429. struct Message input;
  430. struct Message output;
  431. struct sockaddr_in serverInfo, clientInfo;
  432. socklen_t addrlen = sizeof(clientInfo);
  433. uint16_t sharingCurrent=0;
  434. sockFd = socket(AF_INET , SOCK_STREAM , 0);
  435. if(sockFd == -1)
  436. {
  437. DEBUG_ERROR("TCP service socket create fail.\n");
  438. sleep(5);
  439. return FAIL;
  440. }
  441. bzero(&serverInfo,sizeof(serverInfo));
  442. serverInfo.sin_family = PF_INET;
  443. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  444. serverInfo.sin_port = htons(LISTEN_PORT_TCP);
  445. if(bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo)) < 0)
  446. DEBUG_ERROR("TCP server socket bind fail.\n");
  447. if(listen(sockFd, CONNECTION_LIMIT) < 0)
  448. DEBUG_ERROR("TCP server socket listen fail.\n");
  449. else
  450. DEBUG_INFO("Power sharing TCP server initial listen on port %d.\n", LISTEN_PORT_TCP);
  451. // Main loop
  452. for(;;)
  453. {
  454. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  455. fcntl(clientSockFd, F_SETFD, FD_CLOEXEC);
  456. DEBUG_INFO("Client connect in.\n");
  457. DEBUG_INFO("clientSockFd : %d\n", clientSockFd);
  458. if(clientSockFd > 0)
  459. {
  460. if(conn_getConectedQuantity() < CONNECTION_LIMIT)
  461. {
  462. // Fork a child process to handle the new conn
  463. if(fork()==0)
  464. {
  465. // Assign socket handle as available handle in conn info pool
  466. dupFd = dup2(clientSockFd, conn_getDupFd());
  467. conn_register(dupFd);
  468. while((input.size = recv(dupFd, input.buffer, sizeof(input.buffer), 0)) > 0)
  469. {
  470. dM(input.buffer, input.size, YES);
  471. if(isValidCheckSum(&input))
  472. {
  473. conn_updateHeartBeatTime(dupFd);
  474. memset(output.buffer, 0x00, ARRAY_SIZE(output.buffer));
  475. switch(input.buffer[1])
  476. {
  477. case SHARING_CMD_QUERY_SHARING:
  478. sharingCurrent = conn_querySharingCurrent(dupFd);
  479. output.size = 7;
  480. output.buffer[0] = 0xaa;
  481. output.buffer[1] = SHARING_CMD_QUERY_SHARING;
  482. output.buffer[2] = 0x02;
  483. output.buffer[3] = 0x00;
  484. output.buffer[4] = ((sharingCurrent>>0) & 0xff);
  485. output.buffer[5] = ((sharingCurrent>>8) & 0xff);
  486. output.buffer[6] = chksumCal(&output);
  487. break;
  488. case SHARING_CMD_SYNC_INFO:
  489. conn_updatePresentCurrentOutput(dupFd, input.buffer[4], (input.buffer[5] | (input.buffer[6]<<0x08)));
  490. output.size = 6;
  491. output.buffer[0] = 0xaa;
  492. output.buffer[1] = SHARING_CMD_SYNC_INFO;
  493. output.buffer[2] = 0x01;
  494. output.buffer[3] = 0x00;
  495. output.buffer[4] = 0x01;
  496. output.buffer[5] = chksumCal(&output);
  497. break;
  498. default:
  499. DEBUG_WARN("Receive unknown command.\n");
  500. output.size = 5;
  501. output.buffer[0] = 0xaa;
  502. output.buffer[1] = SHARING_CMD_UNKNOWN;
  503. output.buffer[2] = 0x00;
  504. output.buffer[3] = 0x00;
  505. output.buffer[4] = chksumCal(&output);
  506. break;
  507. }
  508. }
  509. else
  510. {
  511. DEBUG_WARN("Receive command check sum error.\n");
  512. output.size = 5;
  513. output.buffer[0] = 0xaa;
  514. output.buffer[1] = SHARING_CMD_CHKSUM_ERROR;
  515. output.buffer[2] = 0x00;
  516. output.buffer[3] = 0x00;
  517. output.buffer[4] = chksumCal(&output);
  518. }
  519. dM(output.buffer, output.size, NO);
  520. send(clientSockFd, output.buffer, output.size, 0);
  521. }
  522. if(input.size == 0)
  523. {
  524. DEBUG_INFO("Client disconnected.\n");
  525. conn_reject(dupFd);
  526. close(dupFd);
  527. close(clientSockFd);
  528. fflush(stdout);
  529. }
  530. else if(input.size == -1)
  531. {
  532. DEBUG_ERROR("Socket recv failed.\n");
  533. conn_reject(dupFd);
  534. close(dupFd);
  535. close(clientSockFd);
  536. fflush(stdout);
  537. }
  538. conn_getConectedQuantity();
  539. exit(0);
  540. }
  541. else
  542. {
  543. // if parent, close the socket and go back to listening new requests
  544. close(clientSockFd);
  545. }
  546. }
  547. else
  548. {
  549. DEBUG_WARN("Connection is over limit.\n");
  550. output.size = 5;
  551. output.buffer[0] = 0xaa;
  552. output.buffer[1] = SHARING_CMD_CONNECTION_FULL;
  553. output.buffer[2] = 0x00;
  554. output.buffer[3] = 0x00;
  555. output.buffer[4] = chksumCal(&output);
  556. send(clientSockFd, output.buffer, output.size, 0);
  557. close(clientSockFd);
  558. }
  559. }
  560. sleep(1);
  561. }
  562. return FAIL;
  563. }
  564. //==========================================
  565. // Client routine
  566. //==========================================
  567. void create_cmd_sync(struct Message *out)
  568. {
  569. memset(out->buffer, 0, ARRAY_SIZE(out->buffer));
  570. out->size = 8;
  571. out->buffer[0] = 0xaa;
  572. out->buffer[1] = SHARING_CMD_SYNC_INFO;
  573. out->buffer[2] = 0x03;
  574. out->buffer[3] = 0x00;
  575. out->buffer[4] = (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus == SYS_MODE_CHARGING?YES:NO);
  576. out->buffer[5] = (((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrent*10))>>0) & 0xff;
  577. out->buffer[6] = (((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrent*10))>>8) & 0xff;
  578. out->buffer[7] = chksumCal(out);
  579. dM(out->buffer, out->size, FALSE);
  580. }
  581. void create_cmd_query(struct Message *out)
  582. {
  583. memset(out->buffer, 0, ARRAY_SIZE(out->buffer));
  584. out->size = 5;
  585. out->buffer[0] = 0xaa;
  586. out->buffer[1] = SHARING_CMD_QUERY_SHARING;
  587. out->buffer[2] = 0x00;
  588. out->buffer[3] = 0x00;
  589. out->buffer[4] = chksumCal(out);
  590. dM(out->buffer, out->size, FALSE);
  591. }
  592. int tcpSocketClientStart(void)
  593. {
  594. int sockfd;
  595. struct sockaddr_in info;
  596. struct hostent *ghbn;
  597. struct timeval tv;
  598. uint8_t socketEnable;
  599. struct Message input;
  600. struct Message output;
  601. uint8_t cmdIdx;
  602. bzero(&info,sizeof(info));
  603. ghbn = gethostbyname((char*)"192.168.10.10");
  604. info.sin_family = PF_INET;
  605. info.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)ghbn->h_addr_list[0]));
  606. info.sin_port = htons(LISTEN_PORT_TCP);
  607. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = OFF;
  608. DEBUG_INFO("Connect to %s:%d\n", inet_ntoa(*(struct in_addr *)ghbn->h_addr_list[0]), LISTEN_PORT_TCP);
  609. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  610. if (sockfd == -1)
  611. {
  612. DEBUG_ERROR("Fail to create a socket.");
  613. return 0;
  614. }
  615. if(connect(sockfd, (struct sockaddr *)&info,sizeof(info)) ==-1)
  616. {
  617. DEBUG_ERROR("Connection error.\n");
  618. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = OFF;
  619. socketEnable = OFF;
  620. }
  621. else
  622. {
  623. DEBUG_INFO("Connect success.\n");
  624. tv.tv_sec = 0;
  625. tv.tv_usec = 500000;
  626. setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  627. socketEnable = ON;
  628. }
  629. while(socketEnable)
  630. {
  631. memset(input.buffer, 0, ARRAY_SIZE(input.buffer));
  632. if((input.size = recv(sockfd, input.buffer, ARRAY_SIZE(input.buffer), 0)) > 0)
  633. {
  634. //DEBUG_INFO("Receive size: %d.\n", input.size);
  635. dM(input.buffer, input.size, TRUE);
  636. if(isValidCheckSum(&input))
  637. {
  638. switch(input.buffer[1])
  639. {
  640. case SHARING_CMD_QUERY_SHARING:
  641. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent != ((input.buffer[4] | (input.buffer[5] << 8))/10))
  642. {
  643. ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent = ((input.buffer[4] | (input.buffer[5] << 8))/10);
  644. DEBUG_INFO("Update available sharing current(A): %d\n", ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent);
  645. }
  646. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = ON;
  647. break;
  648. case SHARING_CMD_SYNC_INFO:
  649. if(!input.buffer[4])
  650. DEBUG_INFO("Charger status sync reject.\n");
  651. break;
  652. default:
  653. DEBUG_WARN("Receive unknown command.\n");
  654. break;
  655. }
  656. }
  657. else
  658. {
  659. DEBUG_WARN("Receive command check sum error.\n");
  660. }
  661. }
  662. else if(input.size == 0)
  663. {
  664. DEBUG_INFO("Disconnected.\n");
  665. fflush(stdout);
  666. socketEnable = OFF;
  667. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = OFF;
  668. }
  669. else if(input.size == -1)
  670. {
  671. switch(cmdIdx)
  672. {
  673. case 0:
  674. create_cmd_sync(&output);
  675. cmdIdx += 1;
  676. break;
  677. default:
  678. create_cmd_query(&output);
  679. cmdIdx = 0;
  680. break;
  681. }
  682. send(sockfd, output.buffer, output.size, 0);
  683. }
  684. usleep(1000000);
  685. }
  686. close(sockfd);
  687. return FAIL;
  688. }
  689. //==========================================
  690. // Main process
  691. //==========================================
  692. int main(void)
  693. {
  694. signal(SIGCHLD,SIG_IGN);
  695. // Initial share memory
  696. if(InitShareMemory() == FAIL)
  697. {
  698. DEBUG_ERROR("InitShareMemory NG\n");
  699. if(ShmStatusCodeData!=NULL)
  700. {
  701. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
  702. }
  703. sleep(5);
  704. return 0;
  705. }
  706. // Enable server if rotary switch not slave mode
  707. if((ShmCharger->gun_info[0].primaryMcuState.rotatory_switch != SWITCH_F_SLAVE) &&
  708. (AC_QUANTITY==1?TRUE:(ShmCharger->gun_info[1].primaryMcuState.rotatory_switch != SWITCH_F_SLAVE)))
  709. {
  710. // UDP socket server start
  711. /*
  712. if(fork() == 0)
  713. {
  714. if(udpSocketServerStart() == FAIL)
  715. {
  716. DEBUG_ERROR("UDP socket server down.\n");
  717. return 0;
  718. }
  719. }*/
  720. // TCP socket server start
  721. if(fork() == 0)
  722. {
  723. if(tcpSocketServerStart() == FAIL)
  724. {
  725. DEBUG_ERROR("TCP socket server down.\n");
  726. return 0;
  727. }
  728. }
  729. // Connection check loop
  730. if(fork() == 0)
  731. {
  732. if(conn_check_loop() == FAIL)
  733. {
  734. DEBUG_ERROR("Connection check loop fail.\n");
  735. return 0;
  736. }
  737. }
  738. }
  739. sleep(10);
  740. for(;;)
  741. {
  742. // Slave logic
  743. tcpSocketClientStart();
  744. usleep(100000);
  745. }
  746. return FAIL;
  747. }