Module_Cabinet.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * Module_Cabinet.c
  3. *
  4. * Created on: 2021/10/25
  5. * Author: folus
  6. */
  7. #include "Module_Cabinet.h"
  8. //#define DEBUG
  9. #define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
  10. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  11. #define PASS 1
  12. #define FAIL -1
  13. #define YES 1
  14. #define NO 0
  15. #define ON 1
  16. #define OFF 0
  17. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  18. struct StatusCodeData *ShmStatusCodeData;
  19. struct PrimaryMcuData *ShmPrimaryMcuData;
  20. struct Charger *ShmCharger;
  21. struct CABINET *ShmCabinet;
  22. struct DISPENSER *ShmDispenser;
  23. /**
  24. *
  25. * @param fmt
  26. * @return
  27. */
  28. int StoreLogMsgServer(const char *fmt, ...)
  29. {
  30. char Buf[4096+256];
  31. char buffer[4096];
  32. time_t CurrentTime;
  33. struct tm *tm;
  34. struct timeval tv;
  35. va_list args;
  36. va_start(args, fmt);
  37. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  38. va_end(args);
  39. memset(Buf,0,sizeof(Buf));
  40. CurrentTime = time(NULL);
  41. tm=localtime(&CurrentTime);
  42. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  43. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]CabinetServerLog",
  44. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  45. buffer,
  46. tm->tm_year+1900,tm->tm_mon+1);
  47. #ifdef SystemLogMessage
  48. system(Buf);
  49. #endif
  50. #ifdef ConsloePrintLog
  51. 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);
  52. #endif
  53. return rc;
  54. }
  55. /**
  56. *
  57. * @param fmt
  58. * @return
  59. */
  60. int StoreLogMsgCient(const char *fmt, ...)
  61. {
  62. char Buf[4096+256];
  63. char buffer[4096];
  64. time_t CurrentTime;
  65. struct tm *tm;
  66. struct timeval tv;
  67. va_list args;
  68. va_start(args, fmt);
  69. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  70. va_end(args);
  71. memset(Buf,0,sizeof(Buf));
  72. CurrentTime = time(NULL);
  73. tm=localtime(&CurrentTime);
  74. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  75. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]CabinetClinetLog",
  76. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  77. buffer,
  78. tm->tm_year+1900,tm->tm_mon+1);
  79. #ifdef SystemLogMessage
  80. system(Buf);
  81. #endif
  82. #ifdef ConsloePrintLog
  83. 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);
  84. #endif
  85. return rc;
  86. }
  87. /**
  88. * Init all share memory
  89. * @return
  90. */
  91. int InitShareMemory()
  92. {
  93. int result = PASS;
  94. int MeterSMId;
  95. //Initial ShmSysConfigAndInfo
  96. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  97. {
  98. DEBUG_SERVER_ERROR("shmget ShmSysConfigAndInfo NG\n");
  99. result = FAIL;
  100. }
  101. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  102. {
  103. DEBUG_SERVER_ERROR("shmat ShmSysConfigAndInfo NG\n");
  104. result = FAIL;
  105. }
  106. else
  107. {}
  108. //Initial ShmStatusCodeData
  109. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  110. {
  111. DEBUG_SERVER_ERROR("shmget ShmStatusCodeData NG\n");
  112. result = FAIL;
  113. }
  114. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  115. {
  116. DEBUG_SERVER_ERROR("shmat ShmStatusCodeData NG\n");
  117. result = FAIL;
  118. }
  119. else
  120. {}
  121. //Initial ShmCharger
  122. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), 0777)) < 0)
  123. {
  124. DEBUG_SERVER_ERROR("shmget ShmCharger NG\n");
  125. result = FAIL;
  126. }
  127. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  128. {
  129. DEBUG_SERVER_ERROR("shmat ShmCharger NG\n");
  130. result = FAIL;
  131. }
  132. //Initial ShmSDispenser
  133. if ((MeterSMId = shmget(ShmDispenserKey, sizeof(struct DISPENSER), 0777)) < 0)
  134. {
  135. DEBUG_SERVER_ERROR("shmget ShmSDispenser NG\n");
  136. result = FAIL;
  137. }
  138. else if ((ShmDispenser = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  139. {
  140. DEBUG_SERVER_ERROR("shmat ShmSDispenser NG\n");
  141. result = FAIL;
  142. }
  143. //Create ShmCabinet
  144. if ((MeterSMId = shmget(ShmCabinetKey, sizeof(struct CABINET), IPC_CREAT | 0777)) < 0)
  145. {
  146. DEBUG_SERVER_ERROR("shmget ShmCabinet NG\n");
  147. result = FAIL;
  148. }
  149. else if ((ShmCabinet = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  150. {
  151. DEBUG_SERVER_ERROR("shmat ShmCabinet NG\n");
  152. result = FAIL;
  153. }
  154. memset(ShmCabinet,0,sizeof(struct CABINET));
  155. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  156. ShmCabinet->Connection_Info[idx].socketFd = (idx+1);
  157. return result;
  158. }
  159. /**
  160. * Cabinet socket server
  161. * @param data
  162. * @param len
  163. * @param isRX
  164. */
  165. void dM_Server(uint8_t *data, uint16_t len, uint8_t isRX)
  166. {
  167. #ifdef DEBUG
  168. uint8_t output[16384];
  169. if(isRX)
  170. {
  171. DEBUG_SERVER_INFO("= RX ===========================================\n");
  172. }
  173. else
  174. {
  175. DEBUG_SERVER_INFO("= TX ===========================================\n");
  176. }
  177. memset(output, 0x00, ARRAY_SIZE(output));
  178. for(uint16_t idx=0;idx<16;idx++)
  179. sprintf((char*)output, "%s %02X", output, idx);
  180. DEBUG_SERVER_INFO("%s\n", output);
  181. DEBUG_SERVER_INFO("================================================\n");
  182. for(uint16_t idx = 0;idx<len;idx++)
  183. {
  184. if((idx%16)>0)
  185. {
  186. sprintf((char*)output, "%s %02X", output, data[idx]);
  187. }
  188. else
  189. {
  190. if(idx != 0)
  191. DEBUG_SERVER_INFO("%s\n", output);
  192. memset(output, 0x00, ARRAY_SIZE(output));
  193. sprintf((char*)output, "%s %02X", output, data[idx]);
  194. }
  195. }
  196. DEBUG_SERVER_INFO("%s\n", output);
  197. DEBUG_SERVER_INFO("------------------------------------------------\n");
  198. #endif
  199. }
  200. /**
  201. * Cabinet socket server
  202. * @param data
  203. * @param len
  204. * @param isRX
  205. */
  206. void dM_Client(uint8_t *data, uint16_t len, uint8_t isRX)
  207. {
  208. #ifdef DEBUG
  209. uint8_t output[16384];
  210. if(isRX)
  211. {
  212. DEBUG_CLIENT_INFO("= RX ===========================================\n");
  213. }
  214. else
  215. {
  216. DEBUG_CLIENT_INFO("= TX ===========================================\n");
  217. }
  218. memset(output, 0x00, ARRAY_SIZE(output));
  219. for(uint16_t idx=0;idx<16;idx++)
  220. sprintf((char*)output, "%s %02X", output, idx);
  221. DEBUG_CLIENT_INFO("%s\n", output);
  222. DEBUG_CLIENT_INFO("================================================\n");
  223. for(uint16_t idx = 0;idx<len;idx++)
  224. {
  225. if((idx%16)>0)
  226. {
  227. sprintf((char*)output, "%s %02X", output, data[idx]);
  228. }
  229. else
  230. {
  231. if(idx != 0)
  232. DEBUG_CLIENT_INFO("%s\n", output);
  233. memset(output, 0x00, ARRAY_SIZE(output));
  234. sprintf((char*)output, "%s %02X", output, data[idx]);
  235. }
  236. }
  237. DEBUG_CLIENT_INFO("%s\n", output);
  238. DEBUG_CLIENT_INFO("------------------------------------------------\n");
  239. #endif
  240. }
  241. /**
  242. *
  243. * @param message
  244. * @return
  245. */
  246. int isValidCheckSum(struct CABINET_Message *message)
  247. {
  248. uint8_t chksum = 0x00;
  249. for(uint16_t idx=0;idx<((((message->buffer[1]<<8) | message->buffer[2])+4)>ARRAY_SIZE(message->buffer)?ARRAY_SIZE(message->buffer):(((message->buffer[1]<<8) | message->buffer[2])+4));idx++)
  250. {
  251. chksum ^= message->buffer[idx];
  252. }
  253. return ((chksum == message->buffer[((((message->buffer[1]<<8) | message->buffer[2])+4)>ARRAY_SIZE(message->buffer)?ARRAY_SIZE(message->buffer):(((message->buffer[1]<<8) | message->buffer[2])+4))]) ? PASS : FAIL);
  254. }
  255. /**
  256. *
  257. * @param message
  258. * @return
  259. */
  260. uint8_t chksumCal(struct CABINET_Message *message)
  261. {
  262. uint8_t chksum=0;
  263. for(uint16_t idx=0;idx<((((message->buffer[1]<<8) | message->buffer[2])+4)>ARRAY_SIZE(message->buffer)?ARRAY_SIZE(message->buffer):(((message->buffer[1]<<8) | message->buffer[2])+4));idx++)
  264. {
  265. chksum ^= message->buffer[idx];
  266. }
  267. return chksum & 0xff;
  268. }
  269. /**
  270. *
  271. * @return
  272. */
  273. int conn_getConectedQuantity(void)
  274. {
  275. int result = 0;
  276. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  277. {
  278. if(ShmCabinet->Connection_Info[idx].isSocketConnected)
  279. {
  280. result += 1;
  281. }
  282. }
  283. DEBUG_SERVER_INFO("Connection quantity: %d\n", result);
  284. ShmCabinet->connectedQty = result;
  285. return result;
  286. }
  287. /**
  288. *
  289. * @return
  290. */
  291. int conn_getDupFd(void)
  292. {
  293. int result = 0;
  294. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  295. {
  296. if(!ShmCabinet->Connection_Info[idx].isSocketConnected)
  297. {
  298. result = ShmCabinet->Connection_Info[idx].socketFd;
  299. break;
  300. }
  301. }
  302. return result;
  303. }
  304. /**
  305. *
  306. * @param socketFd
  307. * @return
  308. */
  309. int conn_register(int socketFd)
  310. {
  311. int result = FAIL;
  312. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  313. {
  314. if(!ShmCabinet->Connection_Info[idx].isSocketConnected)
  315. {
  316. DEBUG_SERVER_INFO("Dupfd-%d register to conn-%d.\n", socketFd, idx);
  317. ShmCabinet->Connection_Info[idx].isSocketConnected = TRUE;
  318. ShmCabinet->Connection_Info[idx].socketFd = socketFd;
  319. ShmCabinet->Connection_Info[idx].lastHeartBeatTime = time((time_t*)NULL);
  320. result = PASS;
  321. break;
  322. }
  323. }
  324. return result;
  325. }
  326. /**
  327. *
  328. * @param socketFd
  329. * @return
  330. */
  331. int conn_reject(int socketFd)
  332. {
  333. int result = FAIL;
  334. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  335. {
  336. if(ShmCabinet->Connection_Info[idx].socketFd == socketFd)
  337. {
  338. DEBUG_SERVER_INFO("Dupfd-%d register from conn_info-%d.\n", socketFd, idx);
  339. ShmCabinet->Connection_Info[idx].isSocketConnected = FALSE;
  340. result = PASS;
  341. break;
  342. }
  343. }
  344. return result;
  345. }
  346. /**
  347. *
  348. * @param socketFd
  349. * @return
  350. */
  351. int conn_updateHeartBeatTime(int socketFd)
  352. {
  353. int result = FAIL;
  354. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  355. {
  356. if(ShmCabinet->Connection_Info[idx].socketFd == socketFd)
  357. {
  358. //DEBUG_INFO("Dupfd-%d register from conn_info-%d update heart beat time.\n", socketFd, idx);
  359. ShmCabinet->Connection_Info[idx].lastHeartBeatTime = time((time_t*)NULL);
  360. result = PASS;
  361. break;
  362. }
  363. }
  364. return result;
  365. }
  366. /**
  367. *
  368. * @param socketFd
  369. * @param gun_index
  370. * @param infoData
  371. * @return
  372. */
  373. int conn_updateGunInfo(int socketFd, int gun_index, Gun_Info *infoData)
  374. {
  375. int result = FAIL;
  376. Gun_Info gunInfo;
  377. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  378. {
  379. if(ShmCabinet->Connection_Info[idx].socketFd == socketFd)
  380. {
  381. memcpy(&gunInfo, infoData, sizeof(Gun_Info));
  382. ShmCabinet->Connection_Info[idx].gun_index = gun_index;
  383. memcpy(&ShmCharger->gun_info[gun_index].primaryMcuLed, &gunInfo.primaryMcuLed, sizeof(Ac_Primary_Mcu_Led));
  384. memcpy(&ShmCharger->gun_info[gun_index].systemAlarmCode, &gunInfo.systemAlarmCode, sizeof(System_Alarm_Code));
  385. memcpy(&ShmCharger->gun_info[gun_index].primaryMcuAlarm, &gunInfo.primaryMcuAlarm, sizeof(Ac_Primary_Mcu_Alarm));
  386. memcpy(&ShmCharger->gun_info[gun_index].GPIO_Input, &gunInfo.GPIO_Input, sizeof(Gpio_in));
  387. memcpy(&ShmCharger->gun_info[gun_index].powerConsumptionTotal, &gunInfo.powerConsumptionTotal, sizeof(Power_Consumption));
  388. memcpy(&ShmCharger->gun_info[gun_index].powerConsumption[0], &gunInfo.powerConsumption[0], ARRAY_SIZE(gunInfo.powerConsumption)*sizeof(Power_Consumption));
  389. memcpy(&ShmCharger->gun_info[gun_index].inputVoltage, &gunInfo.inputVoltage, sizeof(PresentInputVoltage));
  390. memcpy(&ShmCharger->gun_info[gun_index].outputCurrent, &gunInfo.outputCurrent, sizeof(Presentoutputcurrent));
  391. memcpy(&ShmCharger->gun_info[gun_index].gunPluginTimes, &gunInfo.gunPluginTimes, sizeof(Gun_Plugin_Times));
  392. memcpy(&ShmCharger->gun_info[gun_index].temperature, &gunInfo.temperature, sizeof(Temperature));
  393. memcpy(&ShmCharger->gun_info[gun_index].rtc, &gunInfo.rtc, sizeof(Rtc));
  394. memcpy(&ShmCharger->gun_info[gun_index].acCcsInfo, &gunInfo.acCcsInfo, sizeof(Ac_Ccs_Info));
  395. memcpy(&ShmCharger->gun_info[gun_index].primaryMcuState.socket_e, &gunInfo.primaryMcuState.socket_e, sizeof(Socket_E));
  396. ShmCharger->gun_info[gun_index].primaryMcuState.cp_state = gunInfo.primaryMcuState.cp_state;
  397. ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive = gunInfo.primaryMcuState.cp_voltage_negtive;
  398. ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive = gunInfo.primaryMcuState.cp_voltage_positive;
  399. ShmCharger->gun_info[gun_index].primaryMcuState.current_limit = gunInfo.primaryMcuState.current_limit;
  400. ShmCharger->gun_info[gun_index].primaryMcuState.locker_state = gunInfo.primaryMcuState.locker_state;
  401. ShmCharger->gun_info[gun_index].primaryMcuState.meter_state = gunInfo.primaryMcuState.meter_state;
  402. ShmCharger->gun_info[gun_index].primaryMcuState.pp_state = gunInfo.primaryMcuState.pp_state;
  403. ShmCharger->gun_info[gun_index].primaryMcuState.rating_current = gunInfo.primaryMcuState.rating_current;
  404. ShmCharger->gun_info[gun_index].primaryMcuState.relay_state = gunInfo.primaryMcuState.relay_state;
  405. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = gunInfo.primaryMcuState.rotatory_switch;
  406. ShmCharger->gun_info[gun_index].primaryMcuState.shutter_state = gunInfo.primaryMcuState.shutter_state;
  407. ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass = gunInfo.mcuFlag.isReadFwVerPass;
  408. ShmCharger->gun_info[gun_index].mcuFlag.isReadMeterIcCorrectionStatus = gunInfo.mcuFlag.isReadMeterIcCorrectionStatus;
  409. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass = gunInfo.mcuFlag.isSetModelNamePass;
  410. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass = gunInfo.mcuFlag.isSetSerialNumberPass;
  411. ShmCharger->gun_info[gun_index].ccsHandshakeState = gunInfo.ccsHandshakeState;
  412. ShmCharger->gun_info[gun_index].isUpgradeEnd = gunInfo.isUpgradeEnd;
  413. ShmCharger->gun_info[gun_index].isUpgradePASS = gunInfo.isUpgradePASS;
  414. ShmCharger->gun_info[gun_index].isGunPlugged = gunInfo.isGunPlugged;
  415. memcpy(&ShmCharger->gun_info[gun_index].ver, &gunInfo.ver, sizeof(Ver));
  416. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState = gunInfo.primaryMcuState.cp_state;
  417. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotDuty = (gunInfo.primaryMcuState.current_limit>51?(unsigned char)((gunInfo.primaryMcuState.current_limit/2.5)+64):(unsigned char)(gunInfo.primaryMcuState.current_limit/0.6));
  418. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotVoltage = gunInfo.primaryMcuState.cp_voltage_positive;
  419. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].RelayK1K2Status = gunInfo.primaryMcuState.relay_state;
  420. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage = (float)gunInfo.inputVoltage.L1N_L12;
  421. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2 = (float)gunInfo.inputVoltage.L2N_L23;
  422. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3 = (float)gunInfo.inputVoltage.L3N_L31;
  423. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)gunInfo.outputCurrent.L1N_L12[0];
  424. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2 = (float)gunInfo.outputCurrent.L2N_L23[0];
  425. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3 = (float)gunInfo.outputCurrent.L3N_L31[0];
  426. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ConnectorTemp = gunInfo.temperature.point[0];
  427. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVaGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVaGain;
  428. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVbGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVbGain;
  429. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVcGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVcGain;
  430. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVaOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVaOffset;
  431. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVbOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVbOffset;
  432. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVcOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVcOffset;
  433. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCaGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCaGain;
  434. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCbGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCbGain;
  435. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCcGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCcGain;
  436. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCaOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCaOffset;
  437. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCbOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCbOffset;
  438. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCcOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCcOffset;
  439. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedPa = gunInfo.meterIcCorrectionStatus.bits.isCalibratedPa;
  440. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedPb = gunInfo.meterIcCorrectionStatus.bits.isCalibratedPb;
  441. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedPc = gunInfo.meterIcCorrectionStatus.bits.isCalibratedPc;
  442. result = PASS;
  443. break;
  444. }
  445. }
  446. return result;
  447. }
  448. /**
  449. *
  450. * @param socketFd
  451. * @return
  452. */
  453. int conn_getGunIndex(int socketFd)
  454. {
  455. int result = -1;
  456. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  457. {
  458. if(ShmCabinet->Connection_Info[idx].socketFd == socketFd)
  459. {
  460. result = ShmCabinet->Connection_Info[idx].gun_index;
  461. }
  462. }
  463. return result;
  464. }
  465. /**
  466. *
  467. * @param out
  468. */
  469. void create_cmd_getChargingData(struct CABINET_Message *out)
  470. {
  471. memset(out->buffer, 0, ARRAY_SIZE(out->buffer));
  472. out->size = 5;
  473. out->buffer[0] = 0x55;
  474. out->buffer[1] = 0x00;
  475. out->buffer[2] = 0x00;
  476. out->buffer[3] = CABINET_CMD_GET_CHARGING_DATA;
  477. out->buffer[out->size-1] = chksumCal(out);
  478. dM_Server(out->buffer, out->size, FALSE);
  479. }
  480. /**
  481. *
  482. * @param out
  483. */
  484. void create_cmd_setConfigData(struct CABINET_Message *out, int socketFd)
  485. {
  486. memset(out->buffer, 0, ARRAY_SIZE(out->buffer));
  487. out->size = (5 + sizeof(struct SysConfigData));
  488. out->buffer[0] = 0x55;
  489. out->buffer[1] = (sizeof(struct SysConfigData) >> 8) & 0xff;
  490. out->buffer[2] = (sizeof(struct SysConfigData) >> 0) & 0xff;
  491. out->buffer[3] = CABINET_CMD_SET_CONFIG_DATA;
  492. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  493. {
  494. if(ShmCabinet->Connection_Info[idx].socketFd == socketFd)
  495. {
  496. memcpy(&out->buffer[4], &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  497. break;
  498. }
  499. }
  500. out->buffer[out->size-1] = chksumCal(out);
  501. dM_Server(out->buffer, out->size, FALSE);
  502. }
  503. /**
  504. *
  505. * @param out
  506. */
  507. void create_cmd_setGunInfo(struct CABINET_Message *out, int socketFd)
  508. {
  509. memset(out->buffer, 0, ARRAY_SIZE(out->buffer));
  510. out->size = (5 + sizeof(Gun_Info) + sizeof(Timeout_Spec));
  511. out->buffer[0] = 0x55;
  512. out->buffer[1] = ((sizeof(Gun_Info) + sizeof(Timeout_Spec)) >> 8) & 0xff;
  513. out->buffer[2] = ((sizeof(Gun_Info) + sizeof(Timeout_Spec)) >> 0) & 0xff;
  514. out->buffer[3] = CABINET_CMD_SET_GUNINFO;
  515. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  516. {
  517. if(ShmCabinet->Connection_Info[idx].socketFd == socketFd)
  518. {
  519. memcpy(&out->buffer[4], &ShmCharger->gun_info[ShmCabinet->Connection_Info[idx].gun_index], sizeof(Gun_Info));
  520. memcpy(&out->buffer[4+sizeof(Gun_Info)], &ShmCharger->timeoutSpec, sizeof(Timeout_Spec));
  521. break;
  522. }
  523. }
  524. out->buffer[out->size-1] = chksumCal(out);
  525. dM_Server(out->buffer, out->size, FALSE);
  526. }
  527. /**
  528. *
  529. * @return
  530. */
  531. int tcpSocketServerStart(void)
  532. {
  533. int sockFd = 0;
  534. int clientSockFd = 0;
  535. int dupFd = 0;
  536. struct CABINET_Message input;
  537. struct CABINET_Message output;
  538. struct sockaddr_in serverInfo, clientInfo;
  539. socklen_t addrlen = sizeof(clientInfo);
  540. sockFd = socket(AF_INET , SOCK_STREAM , 0);
  541. if(sockFd == -1)
  542. {
  543. DEBUG_SERVER_ERROR("TCP cabinet socket create fail.\n");
  544. sleep(5);
  545. return FAIL;
  546. }
  547. bzero(&serverInfo, sizeof(struct sockaddr_in));
  548. serverInfo.sin_family = PF_INET;
  549. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  550. serverInfo.sin_port = htons(CABINET_LISTEN_PORT_TCP);
  551. if(bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo)) < 0)
  552. DEBUG_SERVER_ERROR("TCP cabinet server socket bind fail.\n");
  553. if(listen(sockFd, CABINET_CONNECTION_LIMIT) < 0)
  554. DEBUG_SERVER_ERROR("TCP cabinet server socket listen fail.\n");
  555. else
  556. DEBUG_SERVER_INFO("TCP cabinet server listen on port %d.\n", CABINET_LISTEN_PORT_TCP);
  557. // Main loop
  558. for(;;)
  559. {
  560. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  561. fcntl(clientSockFd, F_SETFD, FD_CLOEXEC);
  562. DEBUG_SERVER_INFO("Client connect in.\n");
  563. DEBUG_SERVER_INFO("clientSockFd : %d\n", clientSockFd);
  564. if(clientSockFd > 0)
  565. {
  566. if(conn_getConectedQuantity() < CABINET_CONNECTION_LIMIT)
  567. {
  568. // Fork a child process to handle the new conn
  569. if(fork()==0)
  570. {
  571. uint8_t idxStep = 0;
  572. uint8_t socketEnable = YES;
  573. struct FLAG
  574. {
  575. uint8_t isConfigured:1;
  576. }flag;
  577. struct timeval tv;
  578. tv.tv_sec = 0;
  579. tv.tv_usec = TIMEOUT_SOCKET_RX;
  580. setsockopt(clientSockFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv));
  581. // Assign socket handle as available handle in conn info pool
  582. dupFd = dup2(clientSockFd, conn_getDupFd());
  583. conn_register(dupFd);
  584. while(socketEnable)
  585. {
  586. if((input.size = recv(dupFd, input.buffer, sizeof(input.buffer), 0)) > 0)
  587. {
  588. dM_Server(input.buffer, input.size, YES);
  589. if(isValidCheckSum(&input))
  590. {
  591. conn_updateHeartBeatTime(dupFd);
  592. memset(output.buffer, 0x00, ARRAY_SIZE(output.buffer));
  593. switch(input.buffer[3])
  594. {
  595. case CABINET_CMD_GET_CHARGING_DATA:
  596. conn_updateGunInfo(dupFd, input.buffer[4], (Gun_Info*)&input.buffer[5]);
  597. //DEBUG_SERVER_INFO("Get charging data response.\n");
  598. break;
  599. case CABINET_CMD_SET_CONFIG_DATA:
  600. if(input.buffer[4])
  601. {
  602. flag.isConfigured = ON;
  603. }
  604. else
  605. DEBUG_SERVER_INFO("Get set configuration response: %d.\n", input.buffer[4]);
  606. break;
  607. case CABINET_CMD_SET_GUNINFO:
  608. if(input.buffer[4])
  609. {
  610. }
  611. else
  612. DEBUG_SERVER_INFO("Get set gun info response: %d.\n", input.buffer[4]);
  613. break;
  614. default:
  615. DEBUG_SERVER_WARN("Receive unknown response.\n");
  616. break;
  617. }
  618. }
  619. else
  620. {
  621. DEBUG_SERVER_WARN("Receive response check sum error.\n");
  622. }
  623. }
  624. else if(input.size == 0)
  625. {
  626. DEBUG_SERVER_INFO("Client disSocketConnected.\n");
  627. conn_reject(dupFd);
  628. socketEnable = NO;
  629. close(dupFd);
  630. close(clientSockFd);
  631. fflush(stdout);
  632. }
  633. else if(input.size == -1)
  634. {
  635. // Server slave handler
  636. switch(idxStep)
  637. {
  638. case 0:
  639. create_cmd_getChargingData(&output);
  640. send(clientSockFd, output.buffer, output.size, 0);
  641. idxStep++;
  642. break;
  643. default:
  644. if(!flag.isConfigured)
  645. {
  646. create_cmd_setConfigData(&output, dupFd);
  647. send(clientSockFd, output.buffer, output.size, 0);
  648. }
  649. else
  650. {
  651. create_cmd_setGunInfo(&output, dupFd);
  652. send(clientSockFd, output.buffer, output.size, 0);
  653. ShmCharger->gun_info[conn_getGunIndex(dupFd)].mcuResetRequest.isMcuResetRequest = OFF;
  654. }
  655. idxStep = 0;
  656. break;
  657. }
  658. }
  659. }
  660. conn_getConectedQuantity();
  661. exit(0);
  662. }
  663. else
  664. {
  665. // if parent, close the socket and go back to listening new requests
  666. close(clientSockFd);
  667. }
  668. }
  669. else
  670. {
  671. DEBUG_SERVER_WARN("Connection is over limit.\n");
  672. output.size = 5;
  673. output.buffer[0] = 0x55;
  674. output.buffer[1] = 0x00;
  675. output.buffer[2] = 0x00;
  676. output.buffer[3] = CABINET_CMD_CONNECTION_FULL;
  677. output.buffer[output.size-1] = chksumCal(&output);
  678. send(clientSockFd, output.buffer, output.size, 0);
  679. close(clientSockFd);
  680. }
  681. }
  682. sleep(1);
  683. }
  684. return FAIL;
  685. }
  686. /**
  687. * Cabinet socket client
  688. * @return
  689. */
  690. int tcpSocketClientStart(void)
  691. {
  692. int sockfd;
  693. struct sockaddr_in info;
  694. struct hostent *ghbn;
  695. struct timeval tv;
  696. uint8_t socketEnable;
  697. uint8_t cntSocketErr;
  698. struct CABINET_Message input;
  699. struct CABINET_Message output;
  700. bzero(&info,sizeof(info));
  701. ghbn = gethostbyname((char*)"192.168.201.201");
  702. info.sin_family = PF_INET;
  703. info.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)ghbn->h_addr_list[0]));
  704. info.sin_port = htons(CABINET_LISTEN_PORT_TCP);
  705. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = OFF;
  706. DEBUG_CLIENT_INFO("Connect to %s:%d\n", inet_ntoa(*(struct in_addr *)ghbn->h_addr_list[0]), CABINET_LISTEN_PORT_TCP);
  707. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  708. if (sockfd == -1)
  709. {
  710. DEBUG_CLIENT_ERROR("Fail to create a socket.");
  711. return 0;
  712. }
  713. if(connect(sockfd, (struct sockaddr *)&info,sizeof(info)) ==-1)
  714. {
  715. DEBUG_CLIENT_ERROR("Connection error.\n");
  716. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = OFF;
  717. socketEnable = OFF;
  718. }
  719. else
  720. {
  721. DEBUG_CLIENT_INFO("Connect success.\n");
  722. tv.tv_sec = 0;
  723. tv.tv_usec = TIMEOUT_SOCKET_RX;
  724. setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  725. socketEnable = ON;
  726. cntSocketErr = 0;
  727. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = ON;
  728. }
  729. while(socketEnable)
  730. {
  731. memset(input.buffer, 0, ARRAY_SIZE(input.buffer));
  732. if((input.size = recv(sockfd, input.buffer, ARRAY_SIZE(input.buffer), 0)) > 0)
  733. {
  734. dM_Client(input.buffer, input.size, YES);
  735. if(isValidCheckSum(&input))
  736. {
  737. switch(input.buffer[3])
  738. {
  739. case CABINET_CMD_GET_CHARGING_DATA:
  740. output.size = 6 + sizeof(Gun_Info);
  741. output.buffer[0] = 0x55;
  742. output.buffer[1] = ((sizeof(Gun_Info)+1) >> 8) & 0xff;
  743. output.buffer[2] = ((sizeof(Gun_Info)+1) >> 0) & 0xff;
  744. output.buffer[3] = input.buffer[3];
  745. output.buffer[4] = 0; // TODO: gun_index assign
  746. memcpy(&output.buffer[5], &ShmDispenser->gun_info, sizeof(Gun_Info));
  747. output.buffer[output.size-1] = chksumCal(&output);
  748. break;
  749. case CABINET_CMD_SET_CONFIG_DATA:
  750. output.size = 6;
  751. output.buffer[0] = 0x55;
  752. output.buffer[1] = 0x00;
  753. output.buffer[2] = 0x01;
  754. output.buffer[3] = input.buffer[3];
  755. output.buffer[4] = 0x01;
  756. output.buffer[output.size-1] = chksumCal(&output);
  757. memcpy(&ShmDispenser->ConfigData, &input.buffer[4], sizeof(struct SysConfigData));
  758. ShmDispenser->isCcsEnable = OFF;
  759. for(uint8_t idx=0;idx<3;idx++)
  760. {
  761. if(ShmDispenser->ConfigData.ModelName[7+idx] == '7')
  762. ShmDispenser->isCcsEnable = ON;
  763. }
  764. break;
  765. case CABINET_CMD_SET_GUNINFO:
  766. output.size = 6;
  767. output.buffer[0] = 0x55;
  768. output.buffer[1] = 0x00;
  769. output.buffer[2] = 0x01;
  770. output.buffer[3] = input.buffer[3];
  771. output.buffer[4] = 0x01;
  772. output.buffer[output.size-1] = chksumCal(&output);
  773. Gun_Info rxGunInfo;
  774. memcpy(&rxGunInfo, &input.buffer[4], sizeof(Gun_Info));
  775. /*
  776. * TODO:
  777. * 1. Variable set to share memory
  778. */
  779. memcpy(&ShmDispenser->timeoutSpec, &input.buffer[4+sizeof(Gun_Info)], sizeof(Timeout_Spec));
  780. memcpy(&ShmDispenser->gun_info.primaryMcuState.relayState, &rxGunInfo.primaryMcuState.relayState, sizeof(Relay));
  781. ShmDispenser->gun_info.mcuResetRequest.isMcuResetRequest = rxGunInfo.mcuResetRequest.isMcuResetRequest;
  782. ShmDispenser->gun_info.primaryMcuLed.mode = rxGunInfo.primaryMcuLed.mode;
  783. ShmDispenser->gun_info.isOperactive = rxGunInfo.isOperactive;
  784. ShmDispenser->gun_info.legacyRequest.isLegacyRequest = rxGunInfo.legacyRequest.isLegacyRequest;
  785. ShmDispenser->gun_info.legacyRequest.isRelayOn = rxGunInfo.legacyRequest.isRelayOn;
  786. ShmDispenser->gun_info.targetCurrent = rxGunInfo.targetCurrent;
  787. ShmDispenser->gun_info.isResetSoftReq = rxGunInfo.isResetSoftReq;
  788. ShmDispenser->gun_info.isResetHardReq = rxGunInfo.isResetHardReq;
  789. ShmDispenser->gun_info.isUpgradeReq = rxGunInfo.isUpgradeReq;
  790. break;
  791. default:
  792. DEBUG_CLIENT_WARN("Receive unknown command.\n");
  793. output.size = 5;
  794. output.buffer[0] = 0x55;
  795. output.buffer[1] = 0x00;
  796. output.buffer[2] = 0x00;
  797. output.buffer[3] = CABINET_CMD_UNKNOWN;
  798. output.buffer[output.size-1] = chksumCal(&output);
  799. break;
  800. }
  801. }
  802. else
  803. {
  804. DEBUG_CLIENT_WARN("Receive command check sum error.\n");
  805. output.size = 5;
  806. output.buffer[0] = 0x55;
  807. output.buffer[1] = 0x00;
  808. output.buffer[2] = 0x00;
  809. output.buffer[3] = CABINET_CMD_CHKSUM_ERROR;
  810. output.buffer[output.size-1] = chksumCal(&output);
  811. }
  812. dM_Client(output.buffer, output.size, NO);
  813. send(sockfd, output.buffer, output.size, 0);
  814. }
  815. else if(input.size == 0)
  816. {
  817. DEBUG_CLIENT_INFO("DisSocketConnected.\n");
  818. fflush(stdout);
  819. socketEnable = OFF;
  820. }
  821. else if(input.size == -1)
  822. {
  823. if(cntSocketErr > 5)
  824. {
  825. socketEnable = OFF;
  826. DEBUG_CLIENT_ERROR("Socket error occur.\n");
  827. }
  828. else
  829. {
  830. cntSocketErr++;
  831. }
  832. }
  833. usleep(100000);
  834. }
  835. close(sockfd);
  836. return FAIL;
  837. }
  838. /**
  839. * Main process routine
  840. * @return
  841. */
  842. int main(void)
  843. {
  844. // Initial share memory
  845. if(InitShareMemory() == FAIL)
  846. {
  847. DEBUG_SERVER_ERROR("InitShareMemory NG\n");
  848. if(ShmStatusCodeData!=NULL)
  849. {
  850. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
  851. }
  852. sleep(5);
  853. return 0;
  854. }
  855. /*
  856. * TODO:
  857. * 1. Rotary switch define change with system specification
  858. */
  859. while(ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_UNKNOWN)sleep(5);
  860. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch == ROTARY_SWITCH_E_DISPENSER)
  861. {
  862. system("/sbin/ifconfig eth0:1 192.168.201.202 netmask 255.255.255.248 up &");
  863. }
  864. else
  865. {
  866. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  867. }
  868. DEBUG_SERVER_INFO("Module_Cabinet initialized...\n");
  869. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch != ROTARY_SWITCH_E_DISPENSER)
  870. {
  871. // TCP socket server start
  872. if(fork() == 0)
  873. {
  874. if(tcpSocketServerStart() == FAIL)
  875. {
  876. DEBUG_SERVER_ERROR("TCP socket server down.\n");
  877. return 0;
  878. }
  879. }
  880. sleep(3);
  881. }
  882. for(;;)
  883. {
  884. // Slave logic
  885. tcpSocketClientStart();
  886. usleep(100000);
  887. }
  888. return -1;
  889. }