Module_PowerSharing.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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 OCPP20Data *ShmOCPP20Data;
  12. struct POWER_SHARING *ShmPowerSharing;
  13. ParsingRatedCur modelnameInfo = {0};
  14. uint8_t gunType[4] = {0};
  15. uint8_t gunTotalNumber = 0;
  16. //==========================================
  17. // Common routine
  18. //==========================================
  19. int StoreLogMsg(const char *fmt, ...)
  20. {
  21. char Buf[4096+256];
  22. char buffer[4096];
  23. time_t CurrentTime;
  24. struct tm *tm;
  25. struct timeval tv;
  26. va_list args;
  27. va_start(args, fmt);
  28. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  29. va_end(args);
  30. memset(Buf,0,sizeof(Buf));
  31. CurrentTime = time((time_t*)NULL);
  32. tm=localtime(&CurrentTime);
  33. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  34. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%03ld]%s\" >> /Storage/SystemLog/[%04d.%02d]Module_PowerSharingServerLog",
  35. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec/1000,
  36. buffer,
  37. tm->tm_year+1900,tm->tm_mon+1);
  38. #ifdef SystemLogMessage
  39. system(Buf);
  40. #endif
  41. #ifdef ConsloePrintLog
  42. printf("[%04d.%02d.%02d %02d:%02d:%02d.%03ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec/1000, buffer);
  43. #endif
  44. return rc;
  45. }
  46. int StoreLogMsgClient(const char *fmt, ...)
  47. {
  48. char Buf[4096+256];
  49. char buffer[4096];
  50. time_t CurrentTime;
  51. struct tm *tm;
  52. struct timeval tv;
  53. va_list args;
  54. va_start(args, fmt);
  55. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  56. va_end(args);
  57. memset(Buf,0,sizeof(Buf));
  58. CurrentTime = time((time_t*)NULL);
  59. tm=localtime(&CurrentTime);
  60. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  61. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%03ld]%s\" >> /Storage/SystemLog/[%04d.%02d]Module_PowerSharingClientLog",
  62. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec/1000,
  63. buffer,
  64. tm->tm_year+1900,tm->tm_mon+1);
  65. #ifdef SystemLogMessage
  66. system(Buf);
  67. #endif
  68. #ifdef ConsloePrintLog
  69. printf("[%04d.%02d.%02d %02d:%02d:%02d.%03ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec/1000, buffer);
  70. #endif
  71. return rc;
  72. }
  73. int mystrcmp(char *p1,char *p2)
  74. {
  75. while(*p1==*p2)
  76. {
  77. if(*p1=='\0' || *p2=='\0')
  78. break;
  79. p1++;
  80. p2++;
  81. }
  82. if(*p1=='\0' && *p2=='\0')
  83. return(PASS);
  84. else
  85. return(FAIL);
  86. }
  87. long long DiffTimebWithNow(struct timeb ST)
  88. {
  89. //return milli-second
  90. struct timeb ET;
  91. long long StartTime,StopTime;
  92. ftime(&ET);
  93. StartTime=(long long)ST.time;
  94. StopTime=(long long)ET.time;
  95. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  96. }
  97. int DiffTimeb(struct timeb ST, struct timeb ET)
  98. {
  99. //return milli-second
  100. unsigned int StartTime,StopTime;
  101. StartTime=(unsigned int)ST.time;
  102. StopTime=(unsigned int)ET.time;
  103. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  104. }
  105. void refreshStartTimer(struct timespec *timer)
  106. {
  107. clock_gettime(CLOCK_MONOTONIC, timer);
  108. }
  109. int getDiffSecNow(struct timespec timer)
  110. {
  111. struct timespec timerNow;
  112. clock_gettime(CLOCK_MONOTONIC, &timerNow);
  113. return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
  114. }
  115. int getDiffSecBetween(struct timespec start, struct timespec end)
  116. {
  117. return (int)((((unsigned long)(end.tv_sec - start.tv_sec) * 1000) + ((unsigned long)((end.tv_nsec / 1000000) - (start.tv_nsec / 1000000))))/1000);
  118. }
  119. void dM(uint8_t *data, uint16_t len, uint8_t isRX)
  120. {
  121. #ifdef DEBUG
  122. uint8_t output[8192];
  123. if(isRX)
  124. {
  125. DEBUG_INFO_CLIENT("- RX --------------------------------------------\n");
  126. }
  127. else
  128. {
  129. DEBUG_INFO_CLIENT("- TX --------------------------------------------\n");
  130. }
  131. memset(output, 0x00, ARRAY_SIZE(output));
  132. for(uint16_t idx=0;idx<16;idx++)
  133. sprintf((char*)output, "%s %02X", output, idx);
  134. DEBUG_INFO_CLIENT("%s\n", output);
  135. DEBUG_INFO_CLIENT("-------------------------------------------------\n");
  136. for(uint16_t idx = 0;idx<len;idx++)
  137. {
  138. if((idx%16)>0)
  139. {
  140. sprintf((char*)output, "%s %02X", output, data[idx]);
  141. }
  142. else
  143. {
  144. if(idx != 0)
  145. DEBUG_INFO("%s\n", output);
  146. memset(output, 0x00, ARRAY_SIZE(output));
  147. sprintf((char*)output, "%s %02X", output, data[idx]);
  148. }
  149. }
  150. DEBUG_INFO_CLIENT("%s\n", output);
  151. DEBUG_INFO_CLIENT("-------------------------------------------------\n");
  152. #endif
  153. }
  154. int isValidCheckSum(struct Message *message)
  155. {
  156. uint8_t chksum = 0x00;
  157. for(int idx=0;idx<((message->buffer[1]+3)>ARRAY_SIZE(message->buffer)?ARRAY_SIZE(message->buffer):(message->buffer[1]+3));idx++)
  158. {
  159. chksum ^= message->buffer[idx];
  160. }
  161. return ((chksum == message->buffer[((message->buffer[1]+3)>ARRAY_SIZE(message->buffer)?ARRAY_SIZE(message->buffer):(message->buffer[1]+3))]) ? PASS : FAIL);
  162. }
  163. uint8_t chksumCal(struct Message *message)
  164. {
  165. uint8_t chksum=0;
  166. for(int idx=0;idx<((message->buffer[1]+3)>ARRAY_SIZE(message->buffer)?ARRAY_SIZE(message->buffer):(message->buffer[1]+3));idx++)
  167. {
  168. chksum ^= message->buffer[idx];
  169. }
  170. return chksum & 0xff;
  171. }
  172. uint8_t ocpp_get_maxcharging_profileId()
  173. {
  174. uint8_t result = 0;
  175. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  176. {
  177. result = ShmOCPP16Data->MaxChargingProfile.ChargingProfileId;
  178. }
  179. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  180. {
  181. result = ShmOCPP20Data->MaxChargingProfile.id;
  182. }
  183. return result;
  184. }
  185. int getMaxScheduleStart()
  186. {
  187. int result = -1;
  188. struct tm tmScheduleStart;;
  189. struct timeb tbScheduleStart;
  190. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  191. {
  192. if((sscanf((char*)ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule, "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday, &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6))
  193. {
  194. tmScheduleStart.tm_year -= 1900;
  195. tmScheduleStart.tm_mon -= 1;
  196. tbScheduleStart.time = mktime(&tmScheduleStart);
  197. tbScheduleStart.millitm = 0;
  198. result = DiffTimebWithNow(tbScheduleStart)/1000;
  199. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  200. }
  201. else
  202. {
  203. DEBUG_WARN("Max schedule start date parsing error.\n");
  204. }
  205. }
  206. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  207. {
  208. if((sscanf((char*)ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule, "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday, &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6))
  209. {
  210. tmScheduleStart.tm_year -= 1900;
  211. tmScheduleStart.tm_mon -= 1;
  212. tbScheduleStart.time = mktime(&tmScheduleStart);
  213. tbScheduleStart.millitm = 0;
  214. result = DiffTimebWithNow(tbScheduleStart)/1000;
  215. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  216. }
  217. else
  218. {
  219. DEBUG_WARN("Max schedule start date parsing error.\n");
  220. }
  221. }
  222. return result;
  223. }
  224. uint16_t checkChargingProfileLimit()
  225. {
  226. /*
  227. * TODO:
  228. * 1. Default capacity check
  229. */
  230. uint16_t targetMaxCurrent = ((ShmSysConfigAndInfo->SysConfig.PowerSharingCapacityPower==0)?(modelnameInfo.ratedPower/220):(ShmSysConfigAndInfo->SysConfig.PowerSharingCapacityPower/220));
  231. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  232. {
  233. // Get max charging profile limit
  234. if((ocpp_get_maxcharging_profileId() > 0))
  235. {
  236. // Checking limitation
  237. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  238. {
  239. if((getMaxScheduleStart() >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  240. ((idx_period == 0) || (ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  241. )
  242. {
  243. targetMaxCurrent = (mystrcmp((char*)ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/(220*ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases):ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  244. //DEBUG_INFO("targetMaxCurrent on period[%d]: %d\n", idx_period, targetMaxCurrent);
  245. }
  246. else
  247. break;
  248. }
  249. }
  250. }
  251. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  252. {
  253. // Get max charging profile limit
  254. if((ocpp_get_maxcharging_profileId() > 0))
  255. {
  256. // Checking limitation
  257. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  258. {
  259. if((getMaxScheduleStart() >= ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  260. ((idx_period == 0) || (ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  261. )
  262. {
  263. targetMaxCurrent = (mystrcmp((char*)ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingRateUnit,"W")==PASS?ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit/(220*ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases):ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit);
  264. //DEBUG_INFO("targetMaxCurrent on period[%d]: %d\n", idx_period, targetMaxCurrent);
  265. }
  266. else
  267. break;
  268. }
  269. }
  270. }
  271. return targetMaxCurrent;
  272. }
  273. //==========================================
  274. // Init all share memory
  275. //==========================================
  276. int InitShareMemory()
  277. {
  278. int result = PASS;
  279. int MeterSMId;
  280. //Initial ShmSysConfigAndInfo
  281. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  282. {
  283. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  284. result = FAIL;
  285. }
  286. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  287. {
  288. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  289. result = FAIL;
  290. }
  291. else
  292. {}
  293. //Initial ShmStatusCodeData
  294. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  295. {
  296. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  297. result = FAIL;
  298. }
  299. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  300. {
  301. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  302. result = FAIL;
  303. }
  304. else
  305. {}
  306. //Initial ShmOCPP16Data
  307. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  308. {
  309. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  310. result = FAIL;
  311. }
  312. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  313. {
  314. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  315. result = FAIL;
  316. }
  317. else
  318. {}
  319. //Initial ShmOCPP20Data
  320. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), 0777)) < 0)
  321. {
  322. DEBUG_ERROR("shmget ShmOCPP20Data NG");
  323. result = FAIL;
  324. }
  325. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  326. {
  327. DEBUG_ERROR("shmat ShmOCPP20Data NG");
  328. result = FAIL;
  329. }
  330. else
  331. {}
  332. //Create ShmPowerSharing
  333. if ((MeterSMId = shmget(ShmPowerShargingKey, sizeof(struct POWER_SHARING), IPC_CREAT | 0777)) < 0)
  334. {
  335. DEBUG_ERROR("shmget ShmPowerShargingKey NG\n");
  336. result = FAIL;
  337. }
  338. else if ((ShmPowerSharing = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  339. {
  340. DEBUG_ERROR("shmat ShmPowerShargingKey NG\n");
  341. result = FAIL;
  342. }
  343. memset(ShmPowerSharing,0,sizeof(struct POWER_SHARING));
  344. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  345. ShmPowerSharing->Connection_Info[idx].socketFd = (idx+1);
  346. // Model name parsing
  347. if(RatedCurrentParsing((char*)ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo) != -1)
  348. {
  349. DEBUG_INFO("Model name rated power: %d\n", modelnameInfo.ratedPower);
  350. if((ShmSysConfigAndInfo->SysConfig.ModelName[0]=='D') &&
  351. ((ShmSysConfigAndInfo->SysConfig.ModelName[1]=='B') ||
  352. (ShmSysConfigAndInfo->SysConfig.ModelName[1]=='K') ||
  353. (ShmSysConfigAndInfo->SysConfig.ModelName[1]=='O'))
  354. ) // 'D' means DC
  355. {
  356. // DO series
  357. for(int gun_index=0; gun_index<GENERAL_GUN_QUANTITY ; gun_index++)
  358. {
  359. gunTotalNumber += 1;
  360. gunType[gun_index] = GUN_TYPE_DO;
  361. switch(modelnameInfo.ParsingInfo[gun_index].GunType)
  362. {
  363. case Gun_Type_Chademo:
  364. DEBUG_INFO("Gun-%02d type: Cabinet CHAdeMO\n", gun_index);
  365. break;
  366. case Gun_Type_CCS_2:
  367. DEBUG_INFO("Gun-%02d type: Cabinet CCS\n", gun_index);
  368. break;
  369. case Gun_Type_GB:
  370. DEBUG_INFO("Gun-%02d type: Cabinet GBT\n", gun_index);
  371. break;
  372. case Gun_Type_AC:
  373. DEBUG_INFO("Gun-%02d type: Cabinet AC\n", gun_index);
  374. break;
  375. default:
  376. DEBUG_WARN("Gun-%02d type: Cabinet unknown\n", gun_index);
  377. break;
  378. }
  379. }
  380. }
  381. else
  382. {
  383. for(int gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  384. {
  385. gunTotalNumber += 1;
  386. switch(modelnameInfo.ParsingInfo[gun_index].GunType)
  387. {
  388. case Gun_Type_Chademo:
  389. gunType[gun_index] = GUN_TYPE_CHAdeMO;
  390. DEBUG_INFO("Gun-%02d type: CHAdeMO\n", gun_index);
  391. break;
  392. case Gun_Type_CCS_2:
  393. gunType[gun_index] = GUN_TYPE_CCS;
  394. DEBUG_INFO("Gun-%02d type: CCS\n", gun_index);
  395. break;
  396. case Gun_Type_GB:
  397. gunType[gun_index] = GUN_TYPE_GBT;
  398. DEBUG_INFO("Gun-%02d type: GBT\n", gun_index);
  399. break;
  400. case Gun_Type_AC:
  401. gunType[gun_index] = GUN_TYPE_AC;
  402. DEBUG_INFO("Gun-%02d type: AC\n", gun_index);
  403. break;
  404. default:
  405. DEBUG_WARN("Gun-%02d type: Unknown\n", gun_index);
  406. break;
  407. }
  408. }
  409. }
  410. }
  411. else
  412. {
  413. DEBUG_ERROR("Model name parsing fail.\n");
  414. result = FAIL;
  415. }
  416. return result;
  417. }
  418. //==========================================
  419. // TCP socket server routine
  420. //==========================================
  421. int conn_getDupFd(void)
  422. {
  423. int result = 0;
  424. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  425. {
  426. if(!ShmPowerSharing->Connection_Info[idx].isSocketConnected)
  427. {
  428. result = ShmPowerSharing->Connection_Info[idx].socketFd;
  429. break;
  430. }
  431. }
  432. return result;
  433. }
  434. int conn_register(int socketFd)
  435. {
  436. int result = FAIL;
  437. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  438. {
  439. if(!ShmPowerSharing->Connection_Info[idx].isSocketConnected)
  440. {
  441. DEBUG_INFO("Dupfd-%d register to conn-%d.\n", socketFd, idx);
  442. ShmPowerSharing->Connection_Info[idx].isSocketConnected = TRUE;
  443. ShmPowerSharing->Connection_Info[idx].socketFd = socketFd;
  444. refreshStartTimer(&ShmPowerSharing->Connection_Info[idx].timer[POWERSHARING_TMR_IDX_HEARTBEAT]);
  445. result = PASS;
  446. break;
  447. }
  448. }
  449. return result;
  450. }
  451. int conn_reject(int socketFd)
  452. {
  453. int result = FAIL;
  454. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  455. {
  456. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  457. {
  458. DEBUG_INFO("Dupfd-%d register from conn_info-%d.\n", socketFd, idx);
  459. ShmPowerSharing->Connection_Info[idx].isSocketConnected = FALSE;
  460. for(uint8_t gun_index=0;gun_index<ARRAY_SIZE(ShmPowerSharing->Connection_Info[idx].connectorInfo);gun_index++)
  461. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].isGunConnected = FALSE;
  462. result = PASS;
  463. break;
  464. }
  465. }
  466. return result;
  467. }
  468. int conn_getConectedQuantity(void)
  469. {
  470. int result = 0;
  471. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  472. {
  473. if(ShmPowerSharing->Connection_Info[idx].isSocketConnected)
  474. {
  475. result += 1;
  476. }
  477. }
  478. if(ShmPowerSharing->connectedConnectionQty != result)
  479. {
  480. DEBUG_INFO("Connection quantity: %d\n", result);
  481. ShmPowerSharing->connectedConnectionQty = result;
  482. }
  483. return result;
  484. }
  485. int conn_getConectedConnector(void)
  486. {
  487. int result = 0;
  488. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  489. {
  490. for(uint8_t gun_index=0;gun_index<ShmPowerSharing->Connection_Info[idx].connectorCount;gun_index++)
  491. {
  492. if(ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].isGunConnected)
  493. {
  494. result += 1;
  495. }
  496. }
  497. }
  498. if(ShmPowerSharing->connectedConnectorQty != result)
  499. {
  500. DEBUG_INFO("Connected connector quantity: %d\n", result);
  501. ShmPowerSharing->connectedConnectorQty = result;
  502. }
  503. return result;
  504. }
  505. int conn_updateHeartBeatTime(int socketFd)
  506. {
  507. int result = FAIL;
  508. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  509. {
  510. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  511. {
  512. //DEBUG_INFO("Dupfd-%d register from conn_info-%d update heart beat time.\n", socketFd, idx);
  513. refreshStartTimer(&ShmPowerSharing->Connection_Info[idx].timer[POWERSHARING_TMR_IDX_HEARTBEAT]);
  514. result = PASS;
  515. break;
  516. }
  517. }
  518. return result;
  519. }
  520. struct timespec conn_getStatusStarttime(int socketFd)
  521. {
  522. struct timespec result;
  523. refreshStartTimer(&result);
  524. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  525. {
  526. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  527. {
  528. result = ShmPowerSharing->Connection_Info[idx].timer[POWERSHARING_TMR_IDX_GET_STATUS];
  529. break;
  530. }
  531. }
  532. return result;
  533. }
  534. int conn_getStatusStarttimeUpdate(int socketFd)
  535. {
  536. int result = FAIL;
  537. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  538. {
  539. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  540. {
  541. refreshStartTimer(&ShmPowerSharing->Connection_Info[idx].timer[POWERSHARING_TMR_IDX_GET_STATUS]);
  542. result = PASS;
  543. break;
  544. }
  545. }
  546. return result;
  547. }
  548. struct timespec conn_setCapacityStarttime(int socketFd)
  549. {
  550. struct timespec result;
  551. refreshStartTimer(&result);
  552. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  553. {
  554. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  555. {
  556. result = ShmPowerSharing->Connection_Info[idx].timer[POWERSHARING_TMR_IDX_SET_CAPACITY];
  557. break;
  558. }
  559. }
  560. return result;
  561. }
  562. int conn_setCapacityStarttimeUpdate(int socketFd)
  563. {
  564. int result = FAIL;
  565. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  566. {
  567. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  568. {
  569. refreshStartTimer(&ShmPowerSharing->Connection_Info[idx].timer[POWERSHARING_TMR_IDX_SET_CAPACITY]);
  570. result = PASS;
  571. break;
  572. }
  573. }
  574. return result;
  575. }
  576. int conn_update_status(int socketFd, Connector_Info *connectorInfo, uint8_t connectorCount)
  577. {
  578. int result = FAIL;
  579. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  580. {
  581. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  582. {
  583. ShmPowerSharing->Connection_Info[idx].connectorCount = connectorCount;
  584. for(uint8_t gun_index;gun_index<connectorCount;gun_index++)
  585. {
  586. if(!ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].isGunConnected && connectorInfo[gun_index].isGunConnected)
  587. {
  588. ShmPowerSharing->isDetectNewConnected = YES;
  589. }
  590. if((ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].isGunConnected != connectorInfo[gun_index].isGunConnected?YES:NO) ||
  591. (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent != connectorInfo[gun_index].availableSharingCurrent) ||
  592. (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].presentOutputCurrent != connectorInfo[gun_index].presentOutputCurrent))
  593. {
  594. switch(connectorInfo[gun_index].connectorType)
  595. {
  596. case CONNECTOR_TYPE_AC:
  597. DEBUG_INFO("Conn-%d gun_%d type: AC signle phase.\n", idx, gun_index);
  598. break;
  599. case CONNECTOR_TYPE_AC_THREE:
  600. DEBUG_INFO("Conn-%d gun_%d type: AC three phase.\n", idx, gun_index);
  601. break;
  602. case CONNECTOR_TYPE_DC:
  603. DEBUG_INFO("Conn-%d gun_%d type: DC.\n", idx, gun_index);
  604. break;
  605. }
  606. DEBUG_INFO("Conn-%d gun_%d connected: %s\n", idx, gun_index, (connectorInfo[gun_index].isGunConnected?"Yes":"No"));
  607. DEBUG_INFO("Conn-%d gun_%d available current: %d\n", idx, gun_index, connectorInfo[gun_index].availableSharingCurrent);
  608. DEBUG_INFO("Conn-%d gun_%d preset output current: %d\n", idx, gun_index, connectorInfo[gun_index].presentOutputCurrent);
  609. DEBUG_INFO("==================================\n");
  610. }
  611. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].connectorType = connectorInfo[gun_index].connectorType;
  612. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].isGunConnected = connectorInfo[gun_index].isGunConnected;
  613. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].presentOutputCurrent = connectorInfo[gun_index].presentOutputCurrent;
  614. }
  615. result = PASS;
  616. }
  617. }
  618. return result;
  619. }
  620. int conn_getOnHandCurrent(void)
  621. {
  622. int result = 0;
  623. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  624. {
  625. if(ShmPowerSharing->Connection_Info[idx].isSocketConnected)
  626. {
  627. for(uint8_t gun_index=0;gun_index<ShmPowerSharing->Connection_Info[idx].connectorCount;gun_index++)
  628. {
  629. result += (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].connectorType == CONNECTOR_TYPE_AC_THREE?ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent*3:ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent);
  630. }
  631. }
  632. }
  633. result = checkChargingProfileLimit() - result;
  634. if(ShmPowerSharing->onHandCurrent != result)
  635. {
  636. DEBUG_INFO("Total available current: %d\n", result);
  637. ShmPowerSharing->onHandCurrent = result;
  638. }
  639. return result;
  640. }
  641. void create_cmd_getStatus(struct Message *out)
  642. {
  643. memset(out->buffer, 0, ARRAY_SIZE(out->buffer));
  644. out->size = 4;
  645. out->buffer[0] = 0x55;
  646. out->buffer[1] = 0x00;
  647. out->buffer[2] = SHARING_CMD_GET_STATUS;
  648. out->buffer[out->size-1] = chksumCal(out);
  649. //dM(out->buffer, out->size, FALSE);
  650. }
  651. void create_cmd_SetAvailableCurrent(struct Message *out, int socketFd)
  652. {
  653. memset(out->buffer, 0, ARRAY_SIZE(out->buffer));
  654. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  655. {
  656. if(ShmPowerSharing->Connection_Info[idx].socketFd == socketFd)
  657. {
  658. out->size = ShmPowerSharing->Connection_Info[idx].connectorCount*2 + 5;
  659. out->buffer[0] = 0x55;
  660. out->buffer[1] = ShmPowerSharing->Connection_Info[idx].connectorCount*2 + 1;
  661. out->buffer[2] = SHARING_CMD_SET_CAPACITY;
  662. out->buffer[3] = ShmPowerSharing->Connection_Info[idx].connectorCount;
  663. for(uint8_t gun_index=0;gun_index<ShmPowerSharing->Connection_Info[idx].connectorCount;gun_index++)
  664. {
  665. out->buffer[4+(gun_index*2)] = (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent>>8)&0xff;
  666. out->buffer[4+(gun_index*2)+1] = (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent>>0)&0xff;
  667. }
  668. }
  669. }
  670. out->buffer[out->size-1] = chksumCal(out);
  671. //dM(out->buffer, out->size, FALSE);
  672. }
  673. int tcpSocketServer(void)
  674. {
  675. int sockFd = 0;
  676. int clientSockFd = 0;
  677. int dupFd = 0;
  678. struct Message input;
  679. struct Message output;
  680. struct sockaddr_in serverInfo, clientInfo;
  681. socklen_t addrlen = sizeof(clientInfo);
  682. sockFd = socket(AF_INET , SOCK_STREAM , 0);
  683. if(sockFd == -1)
  684. {
  685. DEBUG_ERROR("TCP service socket create fail.\n");
  686. sleep(5);
  687. return FAIL;
  688. }
  689. bzero(&serverInfo,sizeof(serverInfo));
  690. serverInfo.sin_family = PF_INET;
  691. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  692. serverInfo.sin_port = htons(LISTEN_PORT_TCP);
  693. if(bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo)) < 0)
  694. DEBUG_ERROR("TCP server socket bind fail.\n");
  695. if(listen(sockFd, CONNECTION_LIMIT) < 0)
  696. DEBUG_ERROR("TCP server socket listen fail.\n");
  697. else
  698. DEBUG_INFO("Power sharing TCP server initial listen on port %d.\n", LISTEN_PORT_TCP);
  699. // Main loop
  700. for(;;)
  701. {
  702. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  703. fcntl(clientSockFd, F_SETFD, FD_CLOEXEC);
  704. DEBUG_INFO("Client connect in.\n");
  705. DEBUG_INFO("clientSockFd : %d\n", clientSockFd);
  706. if(clientSockFd > 0)
  707. {
  708. if(ShmPowerSharing->connectedConnectionQty < CONNECTION_LIMIT)
  709. {
  710. // Fork a child process to handle the new conn
  711. if(fork()==0)
  712. {
  713. uint8_t idxStep = 0;
  714. uint8_t socketEnable = YES;
  715. struct timeval tv;
  716. tv.tv_sec = 0;
  717. tv.tv_usec = 500000;
  718. setsockopt(clientSockFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  719. // Assign socket handle as available handle in conn info pool
  720. dupFd = dup2(clientSockFd, conn_getDupFd());
  721. conn_register(dupFd);
  722. while(socketEnable)
  723. {
  724. if((input.size = recv(dupFd, input.buffer, sizeof(input.buffer), 0)) > 0)
  725. {
  726. //dM(input.buffer, input.size, YES);
  727. if(isValidCheckSum(&input))
  728. {
  729. Connector_Info connectorInfo[4];
  730. conn_updateHeartBeatTime(dupFd);
  731. memset(output.buffer, 0x00, ARRAY_SIZE(output.buffer));
  732. switch(input.buffer[2])
  733. {
  734. case SHARING_CMD_GET_STATUS:
  735. for(uint8_t gun_index=0;gun_index<input.buffer[3];gun_index++)
  736. {
  737. connectorInfo[gun_index].connectorType = input.buffer[4+(gun_index*6)];
  738. if((connectorInfo[gun_index].connectorType == CONNECTOR_TYPE_AC) || (connectorInfo[gun_index].connectorType == CONNECTOR_TYPE_AC_THREE))
  739. {
  740. connectorInfo[gun_index].isGunConnected = ((2<=input.buffer[4+(gun_index*6)+1]) && (input.buffer[4+(gun_index*6)+1]<=7)?TRUE:FALSE);
  741. }
  742. else
  743. {
  744. connectorInfo[gun_index].isGunConnected = input.buffer[4+(gun_index*6)+1];
  745. }
  746. connectorInfo[gun_index].availableSharingCurrent = (input.buffer[4+(gun_index*6)+2]<<8) | input.buffer[4+(gun_index*6)+3];
  747. connectorInfo[gun_index].presentOutputCurrent = (input.buffer[4+(gun_index*6)+4]<<8) | input.buffer[4+(gun_index*6)+5];
  748. }
  749. conn_update_status(dupFd, &connectorInfo[0], input.buffer[3]);
  750. break;
  751. case SHARING_CMD_SET_CAPACITY:
  752. if(!input.buffer[3])
  753. DEBUG_INFO("Set connection-%d available current fail \n");
  754. break;
  755. default:
  756. DEBUG_WARN("Receive unknown command.\n");
  757. break;
  758. }
  759. }
  760. else
  761. {
  762. DEBUG_WARN("Receive command check sum error.\n");
  763. }
  764. }
  765. else if(input.size == 0)
  766. {
  767. DEBUG_INFO("Client disSocketConnected.\n");
  768. conn_reject(dupFd);
  769. socketEnable = NO;
  770. close(dupFd);
  771. close(clientSockFd);
  772. fflush(stdout);
  773. }
  774. else if(input.size == -1)
  775. {
  776. // Server slave handler
  777. switch(idxStep)
  778. {
  779. case 0:
  780. if((getDiffSecNow(conn_getStatusStarttime(dupFd)) >= INTERVAL_SPEC_POLLING_CMD))
  781. {
  782. create_cmd_getStatus(&output);
  783. conn_getStatusStarttimeUpdate(dupFd);
  784. send(clientSockFd, output.buffer, output.size, 0);
  785. }
  786. idxStep++;
  787. break;
  788. default:
  789. if((getDiffSecNow(conn_setCapacityStarttime(dupFd)) >= INTERVAL_SPEC_POLLING_CMD))
  790. {
  791. create_cmd_SetAvailableCurrent(&output, dupFd);
  792. conn_setCapacityStarttimeUpdate(dupFd);
  793. send(clientSockFd, output.buffer, output.size, 0);
  794. }
  795. idxStep = 0;
  796. break;
  797. }
  798. }
  799. }
  800. exit(0);
  801. }
  802. else
  803. {
  804. // if parent, close the socket and go back to listening new requests
  805. close(clientSockFd);
  806. }
  807. }
  808. else
  809. {
  810. DEBUG_WARN("Connection is over limit.\n");
  811. output.size = 4;
  812. output.buffer[0] = 0x55;
  813. output.buffer[1] = 0x00;
  814. output.buffer[2] = SHARING_CMD_CONNECTION_FULL;
  815. output.buffer[3] = chksumCal(&output);
  816. send(clientSockFd, output.buffer, output.size, 0);
  817. close(clientSockFd);
  818. }
  819. }
  820. usleep(500000);
  821. }
  822. return FAIL;
  823. }
  824. //==========================================
  825. // Client routine
  826. //==========================================
  827. int tcpSocketClient(void)
  828. {
  829. int sockfd;
  830. struct sockaddr_in info;
  831. struct hostent *ghbn;
  832. struct timeval tv;
  833. uint8_t socketEnable;
  834. uint8_t cntSocketErr;
  835. struct Message input;
  836. struct Message output;
  837. bzero(&info,sizeof(info));
  838. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing == 1)
  839. {
  840. ghbn = gethostbyname((char*)"127.0.0.1");
  841. }
  842. else
  843. {
  844. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.PowerSharingServerIP) > 0)
  845. ghbn = gethostbyname((char*)ShmSysConfigAndInfo->SysConfig.PowerSharingServerIP);
  846. else
  847. ghbn = gethostbyname((char*)"192.168.10.10");
  848. }
  849. info.sin_family = PF_INET;
  850. info.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)ghbn->h_addr_list[0]));
  851. info.sin_port = htons(LISTEN_PORT_TCP);
  852. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = OFF;
  853. DEBUG_INFO_CLIENT("Connect to %s:%d\n", inet_ntoa(*(struct in_addr *)ghbn->h_addr_list[0]), LISTEN_PORT_TCP);
  854. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  855. if (sockfd == -1)
  856. {
  857. DEBUG_ERROR_CLIENT("Fail to create a socket.");
  858. return 0;
  859. }
  860. if(connect(sockfd, (struct sockaddr *)&info,sizeof(info)) ==-1)
  861. {
  862. DEBUG_ERROR_CLIENT("Connection error.\n");
  863. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = OFF;
  864. socketEnable = OFF;
  865. }
  866. else
  867. {
  868. DEBUG_INFO_CLIENT("Connect success.\n");
  869. tv.tv_sec = 0;
  870. tv.tv_usec = 500000;
  871. setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  872. socketEnable = ON;
  873. cntSocketErr = 0;
  874. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = ON;
  875. }
  876. while(socketEnable)
  877. {
  878. memset(input.buffer, 0, ARRAY_SIZE(input.buffer));
  879. if((input.size = recv(sockfd, input.buffer, ARRAY_SIZE(input.buffer), 0)) > 0)
  880. {
  881. //DEBUG_INFO("Receive size: %d.\n", input.size);
  882. dM(input.buffer, input.size, YES);
  883. if(isValidCheckSum(&input))
  884. {
  885. switch(input.buffer[2])
  886. {
  887. case SHARING_CMD_GET_STATUS:
  888. output.size = 5 + (gunTotalNumber*6);
  889. output.buffer[0] = 0x55;
  890. output.buffer[1] = 1 + (gunTotalNumber*6);
  891. output.buffer[2] = input.buffer[2];
  892. output.buffer[3] = gunTotalNumber;
  893. for(uint8_t gun_index=0;gun_index<gunTotalNumber;gun_index++)
  894. {
  895. uint8_t tempIndex;
  896. output.buffer[4+(gun_index*6)] = (gunType[gun_index]==GUN_TYPE_AC?(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==3?CONNECTOR_TYPE_AC_THREE:CONNECTOR_TYPE_AC):CONNECTOR_TYPE_DC);
  897. switch(gunType[gun_index])
  898. {
  899. case GUN_TYPE_CHAdeMO:
  900. if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0')
  901. {
  902. tempIndex = ((gun_index==2) ? 1: 0);
  903. }
  904. else
  905. {
  906. tempIndex = gun_index;
  907. }
  908. for (int index = 0; index < CHAdeMO_QUANTITY; index++)
  909. {
  910. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == tempIndex)
  911. {
  912. output.buffer[4+(gun_index*6)+1] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[tempIndex].ConnectorPlugIn;
  913. output.buffer[4+(gun_index*6)+2] = (ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>>8)&0xff;
  914. output.buffer[4+(gun_index*6)+3] = (ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>>0)&0xff;
  915. output.buffer[4+(gun_index*6)+4] = ((int)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[gun_index].PresentChargingPower/220)>>8)&0xff;
  916. output.buffer[4+(gun_index*6)+5] = ((int)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[gun_index].PresentChargingPower/220)>>0)&0xff;
  917. }
  918. }
  919. break;
  920. case GUN_TYPE_CCS:
  921. if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0')
  922. {
  923. tempIndex = ((gun_index==2) ? 1: 0);
  924. }
  925. else
  926. {
  927. tempIndex = gun_index;
  928. }
  929. for (int index = 0; index < CCS_QUANTITY; index++)
  930. {
  931. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == tempIndex)
  932. {
  933. output.buffer[4+(gun_index*6)+1] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[tempIndex].ConnectorPlugIn;
  934. output.buffer[4+(gun_index*6)+2] = (ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>>8)&0xff;
  935. output.buffer[4+(gun_index*6)+3] = (ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>>0)&0xff;
  936. output.buffer[4+(gun_index*6)+4] = ((int)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[gun_index].PresentChargingPower/220)>>8)&0xff;
  937. output.buffer[4+(gun_index*6)+5] = ((int)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[gun_index].PresentChargingPower/220)>>0)&0xff;
  938. }
  939. }
  940. break;
  941. case GUN_TYPE_GBT:
  942. if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0')
  943. {
  944. tempIndex = ((gun_index==2) ? 1: 0);
  945. }
  946. else
  947. {
  948. tempIndex = gun_index;
  949. }
  950. for (int index = 0; index < GB_QUANTITY; index++)
  951. {
  952. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == tempIndex)
  953. {
  954. output.buffer[4+(gun_index*6)+1] = ShmSysConfigAndInfo->SysInfo.GbChargingData[tempIndex].ConnectorPlugIn;
  955. output.buffer[4+(gun_index*6)+2] = (ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>>8)&0xff;
  956. output.buffer[4+(gun_index*6)+3] = (ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>>0)&0xff;
  957. output.buffer[4+(gun_index*6)+4] = ((int)(ShmSysConfigAndInfo->SysInfo.GbChargingData[gun_index].PresentChargingPower/220)>>8)&0xff;
  958. output.buffer[4+(gun_index*6)+5] = ((int)(ShmSysConfigAndInfo->SysInfo.GbChargingData[gun_index].PresentChargingPower/220)>>0)&0xff;
  959. }
  960. }
  961. break;
  962. case GUN_TYPE_DO:
  963. tempIndex = gun_index;
  964. for (int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  965. {
  966. if (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.Index == tempIndex)
  967. {
  968. output.buffer[4+(gun_index*6)+1] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[tempIndex].GeneralChargingData.ConnectorPlugIn;
  969. output.buffer[4+(gun_index*6)+2] = (ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>>8)&0xff;
  970. output.buffer[4+(gun_index*6)+3] = (ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>>0)&0xff;
  971. output.buffer[4+(gun_index*6)+4] = ((int)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].GeneralChargingData.PresentChargingPower/220)>>8)&0xff;
  972. output.buffer[4+(gun_index*6)+5] = ((int)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].GeneralChargingData.PresentChargingPower/220)>>0)&0xff;
  973. }
  974. }
  975. break;
  976. case GUN_TYPE_AC:
  977. if(ShmSysConfigAndInfo->SysConfig.ModelName[0]=='D')
  978. {
  979. tempIndex = 2;
  980. }
  981. else
  982. {
  983. tempIndex = gun_index;
  984. }
  985. for (int index = 0; index < AC_QUANTITY; index++)
  986. {
  987. if (ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index == tempIndex)
  988. {
  989. output.buffer[4+(gun_index*6)+1] = ShmSysConfigAndInfo->SysInfo.AcChargingData[tempIndex].PilotState;
  990. output.buffer[4+(gun_index*6)+2] = (ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>>8)&0xff;
  991. output.buffer[4+(gun_index*6)+3] = (ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>>0)&0xff;
  992. output.buffer[4+(gun_index*6)+4] = ((int)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent>>8)&0xff;
  993. output.buffer[4+(gun_index*6)+5] = ((int)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent>>0)&0xff;
  994. }
  995. }
  996. break;
  997. default:
  998. break;
  999. }
  1000. }
  1001. output.buffer[output.size-1] = chksumCal(&output);
  1002. break;
  1003. case SHARING_CMD_SET_CAPACITY:
  1004. output.size = 5;
  1005. output.buffer[0] = 0x55;
  1006. output.buffer[1] = 0x01;
  1007. output.buffer[2] = input.buffer[2];
  1008. output.buffer[3] = 0x01;
  1009. output.buffer[output.size-1] = chksumCal(&output);
  1010. for(uint8_t gun_index=0;gun_index<input.buffer[3];gun_index++)
  1011. {
  1012. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index] != ((input.buffer[4+(gun_index*2)]<<8) | (input.buffer[4+(gun_index*2)+1])))
  1013. {
  1014. ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index] = ((input.buffer[4+(gun_index*2)]<<8) | (input.buffer[4+(gun_index*2)+1]));
  1015. DEBUG_INFO_CLIENT("Get available current from server: %d\n", ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]);
  1016. }
  1017. }
  1018. break;
  1019. default:
  1020. DEBUG_WARN_CLIENT("Receive unknown command.\n");
  1021. output.size = 4;
  1022. output.buffer[0] = 0x55;
  1023. output.buffer[1] = 0x00;
  1024. output.buffer[2] = SHARING_CMD_UNKNOWN;
  1025. output.buffer[3] = chksumCal(&output);
  1026. break;
  1027. }
  1028. }
  1029. else
  1030. {
  1031. DEBUG_WARN_CLIENT("Receive command check sum error.\n");
  1032. output.size = 4;
  1033. output.buffer[0] = 0x55;
  1034. output.buffer[1] = 0x00;
  1035. output.buffer[2] = SHARING_CMD_CHKSUM_ERROR;
  1036. output.buffer[3] = chksumCal(&output);
  1037. }
  1038. dM(output.buffer, output.size, NO);
  1039. send(sockfd, output.buffer, output.size, 0);
  1040. }
  1041. else if(input.size == 0)
  1042. {
  1043. DEBUG_INFO_CLIENT("DisSocketConnected.\n");
  1044. fflush(stdout);
  1045. socketEnable = OFF;
  1046. ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer = OFF;
  1047. }
  1048. else if(input.size == -1)
  1049. {
  1050. if(cntSocketErr > 5)
  1051. {
  1052. socketEnable = OFF;
  1053. DEBUG_ERROR_CLIENT("Socket error occur\n");
  1054. }
  1055. else
  1056. {
  1057. cntSocketErr++;
  1058. }
  1059. }
  1060. usleep(500000);
  1061. }
  1062. close(sockfd);
  1063. return FAIL;
  1064. }
  1065. //==========================================
  1066. // Local loading balance check
  1067. //==========================================
  1068. int balance_check_loop(void)
  1069. {
  1070. for(;;)
  1071. {
  1072. // Get connection info
  1073. conn_getConectedQuantity();
  1074. conn_getOnHandCurrent();
  1075. conn_getConectedConnector();
  1076. // Check conn heart beat
  1077. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  1078. {
  1079. if(ShmPowerSharing->Connection_Info[idx].isSocketConnected &&
  1080. (getDiffSecNow(ShmPowerSharing->Connection_Info[idx].timer[POWERSHARING_TMR_IDX_HEARTBEAT]) > TIMEOUT_SPEC_HEARTBEAT))
  1081. {
  1082. DEBUG_INFO("SocketFd-%d heart beat is over %d seconds.\n", ShmPowerSharing->Connection_Info[idx].socketFd, TIMEOUT_SPEC_HEARTBEAT);
  1083. for(uint8_t gun_index=0;gun_index<ShmPowerSharing->Connection_Info[idx].connectorCount;gun_index++)
  1084. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].isGunConnected = FALSE;
  1085. ShmPowerSharing->Connection_Info[idx].isSocketConnected = FALSE;
  1086. }
  1087. }
  1088. // Check available power
  1089. if(ShmPowerSharing->isDetectNewConnected || (ShmPowerSharing->onHandCurrent < 0))
  1090. {
  1091. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  1092. {
  1093. for(uint8_t gun_index=0;gun_index<ShmPowerSharing->Connection_Info[idx].connectorCount;gun_index++)
  1094. {
  1095. if(ShmPowerSharing->Connection_Info[idx].isSocketConnected &&
  1096. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].isGunConnected)
  1097. {
  1098. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent = (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].connectorType==CONNECTOR_TYPE_DC?SHARE_MIN_DC:SHARE_MIN_AC);
  1099. }
  1100. else
  1101. {
  1102. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent = 0;
  1103. }
  1104. }
  1105. }
  1106. if(ShmPowerSharing->onHandCurrent < 0)
  1107. DEBUG_INFO("On hand current < 0 re-allocate available current to each connection.\n");
  1108. if(ShmPowerSharing->isDetectNewConnected)
  1109. {
  1110. DEBUG_INFO("Detect gun connected re-allocate available current to each connection.\n");
  1111. ShmPowerSharing->isDetectNewConnected = NO;
  1112. }
  1113. }
  1114. for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
  1115. {
  1116. for(uint8_t gun_index=0;gun_index<ShmPowerSharing->Connection_Info[idx].connectorCount;gun_index++)
  1117. {
  1118. if(ShmPowerSharing->Connection_Info[idx].isSocketConnected &&
  1119. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].isGunConnected)
  1120. {
  1121. if((getDiffSecNow(ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].tmrCheckCapacity) >= INTERVAL_SPEC_CHECK_CAPACITY))
  1122. {
  1123. if((ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent-ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].presentOutputCurrent) > 3)
  1124. {
  1125. if((ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent - ((ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent-ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].presentOutputCurrent)>>1)) >= (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].connectorType==CONNECTOR_TYPE_DC?SHARE_MIN_DC:SHARE_MIN_AC))
  1126. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent -= (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent-ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].presentOutputCurrent)>>1;
  1127. else
  1128. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent = (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].connectorType==CONNECTOR_TYPE_DC?SHARE_MIN_DC:SHARE_MIN_AC);
  1129. }
  1130. else if((abs(ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].presentOutputCurrent-ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent) <= 1) &&
  1131. (ShmPowerSharing->onHandCurrent > 0))
  1132. {
  1133. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent += (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].connectorType==CONNECTOR_TYPE_AC_THREE?(ShmPowerSharing->onHandCurrent/(ShmPowerSharing->connectedConnectorQty==0?1:ShmPowerSharing->connectedConnectorQty))/3:ShmPowerSharing->onHandCurrent/(ShmPowerSharing->connectedConnectorQty==0?1:ShmPowerSharing->connectedConnectorQty));
  1134. }
  1135. else
  1136. {}
  1137. refreshStartTimer(&ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].tmrCheckCapacity);
  1138. }
  1139. }
  1140. else
  1141. {
  1142. if(ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent != 0)
  1143. {
  1144. DEBUG_INFO("Dupfd-%d on conn-%d available current reset to 0A\n", ShmPowerSharing->Connection_Info[idx].socketFd, idx);
  1145. }
  1146. ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent = 0;
  1147. }
  1148. }
  1149. }
  1150. usleep(100000);
  1151. }
  1152. return FAIL;
  1153. }
  1154. //==========================================
  1155. // Main process
  1156. //==========================================
  1157. int main(void)
  1158. {
  1159. signal(SIGCHLD,SIG_IGN);
  1160. // Initial share memory
  1161. if(InitShareMemory() == FAIL)
  1162. {
  1163. DEBUG_ERROR("InitShareMemory NG\n");
  1164. if(ShmStatusCodeData!=NULL)
  1165. {
  1166. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
  1167. }
  1168. sleep(5);
  1169. return 0;
  1170. }
  1171. // Enable server if rotary switch not slave mode
  1172. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing == 1)
  1173. {
  1174. // TCP socket server start
  1175. if(fork() == 0)
  1176. {
  1177. if(tcpSocketServer() == FAIL)
  1178. {
  1179. DEBUG_ERROR("TCP socket server down.\n");
  1180. return 0;
  1181. }
  1182. }
  1183. // Connection check loop
  1184. if(fork() == 0)
  1185. {
  1186. if(balance_check_loop() == FAIL)
  1187. {
  1188. DEBUG_ERROR("Local loading balance check loop fail.\n");
  1189. return 0;
  1190. }
  1191. }
  1192. }
  1193. sleep(10);
  1194. for(;;)
  1195. {
  1196. // Slave logic
  1197. tcpSocketClient();
  1198. usleep(100000);
  1199. }
  1200. return FAIL;
  1201. }