Module_PowerSharing.c 53 KB

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