Module_Cabinet.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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].mcuFlag.isMcuUpgradeReq = gunInfo.mcuFlag.isMcuUpgradeReq;
  412. ShmCharger->gun_info[gun_index].ccsHandshakeState = gunInfo.ccsHandshakeState;
  413. ShmCharger->gun_info[gun_index].isUpgradeEnd = gunInfo.isUpgradeEnd;
  414. ShmCharger->gun_info[gun_index].isUpgradePASS = gunInfo.isUpgradePASS;
  415. ShmCharger->gun_info[gun_index].isGunPlugged = gunInfo.isGunPlugged;
  416. memcpy(&ShmCharger->gun_info[gun_index].ver, &gunInfo.ver, sizeof(Ver));
  417. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState = gunInfo.primaryMcuState.cp_state;
  418. 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));
  419. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotVoltage = gunInfo.primaryMcuState.cp_voltage_positive;
  420. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].RelayK1K2Status = gunInfo.primaryMcuState.relay_state;
  421. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage = (float)gunInfo.inputVoltage.L1N_L12;
  422. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2 = (float)gunInfo.inputVoltage.L2N_L23;
  423. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3 = (float)gunInfo.inputVoltage.L3N_L31;
  424. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)gunInfo.outputCurrent.L1N_L12[0];
  425. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2 = (float)gunInfo.outputCurrent.L2N_L23[0];
  426. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3 = (float)gunInfo.outputCurrent.L3N_L31[0];
  427. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ConnectorTemp = gunInfo.temperature.point[0];
  428. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVaGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVaGain;
  429. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVbGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVbGain;
  430. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVcGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVcGain;
  431. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVaOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVaOffset;
  432. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVbOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVbOffset;
  433. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVcOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedVcOffset;
  434. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCaGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCaGain;
  435. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCbGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCbGain;
  436. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCcGain = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCcGain;
  437. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCaOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCaOffset;
  438. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCbOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCbOffset;
  439. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCcOffset = gunInfo.meterIcCorrectionStatus.bits.isCalibratedCcOffset;
  440. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedPa = gunInfo.meterIcCorrectionStatus.bits.isCalibratedPa;
  441. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedPb = gunInfo.meterIcCorrectionStatus.bits.isCalibratedPb;
  442. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedPc = gunInfo.meterIcCorrectionStatus.bits.isCalibratedPc;
  443. result = PASS;
  444. break;
  445. }
  446. }
  447. return result;
  448. }
  449. /**
  450. *
  451. * @param socketFd
  452. * @return
  453. */
  454. int conn_getGunIndex(int socketFd)
  455. {
  456. int result = -1;
  457. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  458. {
  459. if(ShmCabinet->Connection_Info[idx].socketFd == socketFd)
  460. {
  461. result = ShmCabinet->Connection_Info[idx].gun_index;
  462. }
  463. }
  464. return result;
  465. }
  466. /**
  467. *
  468. * @param out
  469. */
  470. void create_cmd_getChargingData(struct CABINET_Message *out)
  471. {
  472. memset(out->buffer, 0, ARRAY_SIZE(out->buffer));
  473. out->size = 5;
  474. out->buffer[0] = 0x55;
  475. out->buffer[1] = 0x00;
  476. out->buffer[2] = 0x00;
  477. out->buffer[3] = CABINET_CMD_GET_CHARGING_DATA;
  478. out->buffer[out->size-1] = chksumCal(out);
  479. dM_Server(out->buffer, out->size, FALSE);
  480. }
  481. /**
  482. *
  483. * @param out
  484. */
  485. void create_cmd_setConfigData(struct CABINET_Message *out, int socketFd)
  486. {
  487. memset(out->buffer, 0, ARRAY_SIZE(out->buffer));
  488. out->size = (5 + sizeof(struct SysConfigData));
  489. out->buffer[0] = 0x55;
  490. out->buffer[1] = (sizeof(struct SysConfigData) >> 8) & 0xff;
  491. out->buffer[2] = (sizeof(struct SysConfigData) >> 0) & 0xff;
  492. out->buffer[3] = CABINET_CMD_SET_CONFIG_DATA;
  493. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  494. {
  495. if(ShmCabinet->Connection_Info[idx].socketFd == socketFd)
  496. {
  497. memcpy(&out->buffer[4], &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  498. break;
  499. }
  500. }
  501. out->buffer[out->size-1] = chksumCal(out);
  502. dM_Server(out->buffer, out->size, FALSE);
  503. }
  504. /**
  505. *
  506. * @param out
  507. */
  508. void create_cmd_setGunInfo(struct CABINET_Message *out, int socketFd)
  509. {
  510. memset(out->buffer, 0, ARRAY_SIZE(out->buffer));
  511. out->size = (5 + sizeof(Gun_Info) + sizeof(Timeout_Spec));
  512. out->buffer[0] = 0x55;
  513. out->buffer[1] = ((sizeof(Gun_Info) + sizeof(Timeout_Spec)) >> 8) & 0xff;
  514. out->buffer[2] = ((sizeof(Gun_Info) + sizeof(Timeout_Spec)) >> 0) & 0xff;
  515. out->buffer[3] = CABINET_CMD_SET_GUNINFO;
  516. for(uint8_t idx=0;idx<CABINET_CONNECTION_LIMIT;idx++)
  517. {
  518. if(ShmCabinet->Connection_Info[idx].socketFd == socketFd)
  519. {
  520. memcpy(&out->buffer[4], &ShmCharger->gun_info[ShmCabinet->Connection_Info[idx].gun_index], sizeof(Gun_Info));
  521. memcpy(&out->buffer[4+sizeof(Gun_Info)], &ShmCharger->timeoutSpec, sizeof(Timeout_Spec));
  522. break;
  523. }
  524. }
  525. out->buffer[out->size-1] = chksumCal(out);
  526. dM_Server(out->buffer, out->size, FALSE);
  527. }
  528. /**
  529. *
  530. * @return
  531. */
  532. int tcpSocketServerStart(void)
  533. {
  534. int sockFd = 0;
  535. int clientSockFd = 0;
  536. int dupFd = 0;
  537. struct CABINET_Message input;
  538. struct CABINET_Message output;
  539. struct sockaddr_in serverInfo, clientInfo;
  540. socklen_t addrlen = sizeof(clientInfo);
  541. sockFd = socket(AF_INET , SOCK_STREAM , 0);
  542. if(sockFd == -1)
  543. {
  544. DEBUG_SERVER_ERROR("TCP cabinet socket create fail.\n");
  545. sleep(5);
  546. return FAIL;
  547. }
  548. bzero(&serverInfo, sizeof(struct sockaddr_in));
  549. serverInfo.sin_family = PF_INET;
  550. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  551. serverInfo.sin_port = htons(CABINET_LISTEN_PORT_TCP);
  552. if(bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo)) < 0)
  553. DEBUG_SERVER_ERROR("TCP cabinet server socket bind fail.\n");
  554. if(listen(sockFd, CABINET_CONNECTION_LIMIT) < 0)
  555. DEBUG_SERVER_ERROR("TCP cabinet server socket listen fail.\n");
  556. else
  557. DEBUG_SERVER_INFO("TCP cabinet server listen on port %d.\n", CABINET_LISTEN_PORT_TCP);
  558. // Main loop
  559. for(;;)
  560. {
  561. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  562. fcntl(clientSockFd, F_SETFD, FD_CLOEXEC);
  563. DEBUG_SERVER_INFO("Client connect in.\n");
  564. DEBUG_SERVER_INFO("clientSockFd : %d\n", clientSockFd);
  565. if(clientSockFd > 0)
  566. {
  567. if(conn_getConectedQuantity() < CABINET_CONNECTION_LIMIT)
  568. {
  569. // Fork a child process to handle the new conn
  570. if(fork()==0)
  571. {
  572. uint8_t idxStep = 0;
  573. uint8_t socketEnable = YES;
  574. struct FLAG
  575. {
  576. uint8_t isConfigured:1;
  577. }flag;
  578. struct timeval tv;
  579. tv.tv_sec = 0;
  580. tv.tv_usec = TIMEOUT_SOCKET_RX;
  581. setsockopt(clientSockFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv));
  582. // Assign socket handle as available handle in conn info pool
  583. dupFd = dup2(clientSockFd, conn_getDupFd());
  584. conn_register(dupFd);
  585. while(socketEnable)
  586. {
  587. if((input.size = recv(dupFd, input.buffer, sizeof(input.buffer), 0)) > 0)
  588. {
  589. dM_Server(input.buffer, input.size, YES);
  590. if(isValidCheckSum(&input))
  591. {
  592. conn_updateHeartBeatTime(dupFd);
  593. memset(output.buffer, 0x00, ARRAY_SIZE(output.buffer));
  594. switch(input.buffer[3])
  595. {
  596. case CABINET_CMD_GET_CHARGING_DATA:
  597. conn_updateGunInfo(dupFd, input.buffer[4], (Gun_Info*)&input.buffer[5]);
  598. //DEBUG_SERVER_INFO("Get charging data response.\n");
  599. break;
  600. case CABINET_CMD_SET_CONFIG_DATA:
  601. if(input.buffer[4])
  602. {
  603. flag.isConfigured = ON;
  604. }
  605. else
  606. DEBUG_SERVER_INFO("Get set configuration response: %d.\n", input.buffer[4]);
  607. break;
  608. case CABINET_CMD_SET_GUNINFO:
  609. if(input.buffer[4])
  610. {
  611. }
  612. else
  613. DEBUG_SERVER_INFO("Get set gun info response: %d.\n", input.buffer[4]);
  614. break;
  615. default:
  616. DEBUG_SERVER_WARN("Receive unknown response.\n");
  617. break;
  618. }
  619. }
  620. else
  621. {
  622. DEBUG_SERVER_WARN("Receive response check sum error.\n");
  623. }
  624. }
  625. else if(input.size == 0)
  626. {
  627. DEBUG_SERVER_INFO("Client disSocketConnected.\n");
  628. conn_reject(dupFd);
  629. socketEnable = NO;
  630. close(dupFd);
  631. close(clientSockFd);
  632. fflush(stdout);
  633. }
  634. else if(input.size == -1)
  635. {
  636. // Server slave handler
  637. switch(idxStep)
  638. {
  639. case 0:
  640. create_cmd_getChargingData(&output);
  641. send(clientSockFd, output.buffer, output.size, 0);
  642. idxStep++;
  643. break;
  644. default:
  645. if(!flag.isConfigured)
  646. {
  647. create_cmd_setConfigData(&output, dupFd);
  648. send(clientSockFd, output.buffer, output.size, 0);
  649. }
  650. else
  651. {
  652. create_cmd_setGunInfo(&output, dupFd);
  653. send(clientSockFd, output.buffer, output.size, 0);
  654. ShmCharger->gun_info[conn_getGunIndex(dupFd)].mcuResetRequest.isMcuResetRequest = OFF;
  655. }
  656. idxStep = 0;
  657. break;
  658. }
  659. }
  660. }
  661. conn_getConectedQuantity();
  662. exit(0);
  663. }
  664. else
  665. {
  666. // if parent, close the socket and go back to listening new requests
  667. close(clientSockFd);
  668. }
  669. }
  670. else
  671. {
  672. DEBUG_SERVER_WARN("Connection is over limit.\n");
  673. output.size = 5;
  674. output.buffer[0] = 0x55;
  675. output.buffer[1] = 0x00;
  676. output.buffer[2] = 0x00;
  677. output.buffer[3] = CABINET_CMD_CONNECTION_FULL;
  678. output.buffer[output.size-1] = chksumCal(&output);
  679. send(clientSockFd, output.buffer, output.size, 0);
  680. close(clientSockFd);
  681. }
  682. }
  683. sleep(1);
  684. }
  685. return FAIL;
  686. }
  687. /**
  688. * Cabinet socket client
  689. * @return
  690. */
  691. int tcpSocketClientStart(void)
  692. {
  693. int sockfd;
  694. struct sockaddr_in info;
  695. struct hostent *ghbn;
  696. struct timeval tv;
  697. uint8_t socketEnable;
  698. uint8_t cntSocketErr;
  699. struct CABINET_Message input;
  700. struct CABINET_Message output;
  701. bzero(&info,sizeof(info));
  702. ghbn = gethostbyname((char*)"192.168.201.201");
  703. info.sin_family = PF_INET;
  704. info.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)ghbn->h_addr_list[0]));
  705. info.sin_port = htons(CABINET_LISTEN_PORT_TCP);
  706. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = OFF;
  707. DEBUG_CLIENT_INFO("Connect to %s:%d\n", inet_ntoa(*(struct in_addr *)ghbn->h_addr_list[0]), CABINET_LISTEN_PORT_TCP);
  708. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  709. if (sockfd == -1)
  710. {
  711. DEBUG_CLIENT_ERROR("Fail to create a socket.");
  712. return 0;
  713. }
  714. if(connect(sockfd, (struct sockaddr *)&info,sizeof(info)) ==-1)
  715. {
  716. DEBUG_CLIENT_ERROR("Connection error.\n");
  717. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = OFF;
  718. socketEnable = OFF;
  719. }
  720. else
  721. {
  722. DEBUG_CLIENT_INFO("Connect success.\n");
  723. tv.tv_sec = 0;
  724. tv.tv_usec = TIMEOUT_SOCKET_RX;
  725. setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  726. socketEnable = ON;
  727. cntSocketErr = 0;
  728. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = ON;
  729. }
  730. while(socketEnable)
  731. {
  732. memset(input.buffer, 0, ARRAY_SIZE(input.buffer));
  733. if((input.size = recv(sockfd, input.buffer, ARRAY_SIZE(input.buffer), 0)) > 0)
  734. {
  735. dM_Client(input.buffer, input.size, YES);
  736. if(isValidCheckSum(&input))
  737. {
  738. switch(input.buffer[3])
  739. {
  740. case CABINET_CMD_GET_CHARGING_DATA:
  741. output.size = 6 + sizeof(Gun_Info);
  742. output.buffer[0] = 0x55;
  743. output.buffer[1] = ((sizeof(Gun_Info)+1) >> 8) & 0xff;
  744. output.buffer[2] = ((sizeof(Gun_Info)+1) >> 0) & 0xff;
  745. output.buffer[3] = input.buffer[3];
  746. output.buffer[4] = 0; // TODO: gun_index assign
  747. memcpy(&output.buffer[5], &ShmDispenser->gun_info, sizeof(Gun_Info));
  748. output.buffer[output.size-1] = chksumCal(&output);
  749. break;
  750. case CABINET_CMD_SET_CONFIG_DATA:
  751. output.size = 6;
  752. output.buffer[0] = 0x55;
  753. output.buffer[1] = 0x00;
  754. output.buffer[2] = 0x01;
  755. output.buffer[3] = input.buffer[3];
  756. output.buffer[4] = 0x01;
  757. output.buffer[output.size-1] = chksumCal(&output);
  758. memcpy(&ShmDispenser->ConfigData, &input.buffer[4], sizeof(struct SysConfigData));
  759. ShmDispenser->isCcsEnable = OFF;
  760. for(uint8_t idx=0;idx<3;idx++)
  761. {
  762. if(ShmDispenser->ConfigData.ModelName[7+idx] == '7')
  763. ShmDispenser->isCcsEnable = ON;
  764. }
  765. break;
  766. case CABINET_CMD_SET_GUNINFO:
  767. output.size = 6;
  768. output.buffer[0] = 0x55;
  769. output.buffer[1] = 0x00;
  770. output.buffer[2] = 0x01;
  771. output.buffer[3] = input.buffer[3];
  772. output.buffer[4] = 0x01;
  773. output.buffer[output.size-1] = chksumCal(&output);
  774. Gun_Info rxGunInfo;
  775. memcpy(&rxGunInfo, &input.buffer[4], sizeof(Gun_Info));
  776. /*
  777. * TODO:
  778. * 1. Variable set to share memory
  779. */
  780. memcpy(&ShmDispenser->timeoutSpec, &input.buffer[4+sizeof(Gun_Info)], sizeof(Timeout_Spec));
  781. memcpy(&ShmDispenser->gun_info.primaryMcuState.relayState, &rxGunInfo.primaryMcuState.relayState, sizeof(Relay));
  782. ShmDispenser->gun_info.mcuResetRequest.isMcuResetRequest = rxGunInfo.mcuResetRequest.isMcuResetRequest;
  783. ShmDispenser->gun_info.primaryMcuLed.mode = rxGunInfo.primaryMcuLed.mode;
  784. ShmDispenser->gun_info.isOperactive = rxGunInfo.isOperactive;
  785. ShmDispenser->gun_info.legacyRequest.isLegacyRequest = rxGunInfo.legacyRequest.isLegacyRequest;
  786. ShmDispenser->gun_info.legacyRequest.isRelayOn = rxGunInfo.legacyRequest.isRelayOn;
  787. ShmDispenser->gun_info.targetCurrent = rxGunInfo.targetCurrent;
  788. ShmDispenser->gun_info.isResetSoftReq = rxGunInfo.isResetSoftReq;
  789. ShmDispenser->gun_info.isResetHardReq = rxGunInfo.isResetHardReq;
  790. ShmDispenser->gun_info.isUpgradeReq = rxGunInfo.isUpgradeReq;
  791. break;
  792. default:
  793. DEBUG_CLIENT_WARN("Receive unknown command.\n");
  794. output.size = 5;
  795. output.buffer[0] = 0x55;
  796. output.buffer[1] = 0x00;
  797. output.buffer[2] = 0x00;
  798. output.buffer[3] = CABINET_CMD_UNKNOWN;
  799. output.buffer[output.size-1] = chksumCal(&output);
  800. break;
  801. }
  802. }
  803. else
  804. {
  805. DEBUG_CLIENT_WARN("Receive command check sum error.\n");
  806. output.size = 5;
  807. output.buffer[0] = 0x55;
  808. output.buffer[1] = 0x00;
  809. output.buffer[2] = 0x00;
  810. output.buffer[3] = CABINET_CMD_CHKSUM_ERROR;
  811. output.buffer[output.size-1] = chksumCal(&output);
  812. }
  813. dM_Client(output.buffer, output.size, NO);
  814. send(sockfd, output.buffer, output.size, 0);
  815. }
  816. else if(input.size == 0)
  817. {
  818. DEBUG_CLIENT_INFO("DisSocketConnected.\n");
  819. fflush(stdout);
  820. socketEnable = OFF;
  821. }
  822. else if(input.size == -1)
  823. {
  824. if(cntSocketErr > 5)
  825. {
  826. socketEnable = OFF;
  827. DEBUG_CLIENT_ERROR("Socket error occur.\n");
  828. }
  829. else
  830. {
  831. cntSocketErr++;
  832. }
  833. }
  834. usleep(100000);
  835. }
  836. close(sockfd);
  837. return FAIL;
  838. }
  839. /**
  840. * Main process routine
  841. * @return
  842. */
  843. int main(void)
  844. {
  845. // Initial share memory
  846. if(InitShareMemory() == FAIL)
  847. {
  848. DEBUG_SERVER_ERROR("InitShareMemory NG\n");
  849. if(ShmStatusCodeData!=NULL)
  850. {
  851. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
  852. }
  853. sleep(5);
  854. return 0;
  855. }
  856. /*
  857. * TODO:
  858. * 1. Rotary switch define change with system specification
  859. */
  860. for(uint8_t idx=0;idx<10;idx++)
  861. {
  862. if(ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_UNKNOWN)
  863. break;
  864. DEBUG_SERVER_INFO("Wait CP state query...%d\n", idx);
  865. sleep(5);
  866. };
  867. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch == ROTARY_SWITCH_E_DISPENSER)
  868. {
  869. system("/sbin/ifconfig eth0:1 192.168.201.202 netmask 255.255.255.248 up &");
  870. }
  871. else
  872. {
  873. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  874. }
  875. DEBUG_SERVER_INFO("Module_Cabinet initialized...\n");
  876. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch != ROTARY_SWITCH_E_DISPENSER)
  877. {
  878. // TCP socket server start
  879. if(fork() == 0)
  880. {
  881. if(tcpSocketServerStart() == FAIL)
  882. {
  883. DEBUG_SERVER_ERROR("TCP socket server down.\n");
  884. return 0;
  885. }
  886. }
  887. sleep(3);
  888. }
  889. for(;;)
  890. {
  891. // Slave logic
  892. tcpSocketClientStart();
  893. usleep(100000);
  894. }
  895. return -1;
  896. }