Module_EvComm.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659
  1. /*
  2. * Module_EvComm.c
  3. *
  4. * Created on: 2020年9月14日
  5. * Author: Wendell
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/can.h>
  19. #include <linux/can/raw.h>
  20. #include <linux/wireless.h>
  21. #include <arpa/inet.h>
  22. #include <netinet/in.h>
  23. #include <unistd.h>
  24. #include <stdarg.h>
  25. #include <stdio.h> /*標準輸入輸出定義*/
  26. #include <stdlib.h> /*標準函數庫定義*/
  27. #include <unistd.h> /*Unix 標準函數定義*/
  28. #include <fcntl.h> /*檔控制定義*/
  29. #include <termios.h> /*PPSIX 終端控制定義*/
  30. #include <errno.h> /*錯誤號定義*/
  31. #include <errno.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <ctype.h>
  35. #include <ifaddrs.h>
  36. #include <signal.h>
  37. #include <net/if_arp.h>
  38. #include "../../define.h"
  39. #include "Module_EvComm.h"
  40. #include "Config.h"
  41. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  42. #define PASS 1
  43. #define FAIL -1
  44. #define YES 1
  45. #define NO 0
  46. #define ON 1
  47. #define OFF 0
  48. #define true 1
  49. #define false 0
  50. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  51. //struct WARNING_CODE_INFO LastWarningInfo[GENERAL_GUN_QUANTITY];
  52. void PRINTF_FUNC(char *string, ...);
  53. int StoreLogMsg(const char *fmt, ...);
  54. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  55. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  56. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  57. int StoreLogMsg(const char *fmt, ...)
  58. {
  59. char Buf[4096+256];
  60. char buffer[4096];
  61. va_list args;
  62. struct timeb SeqEndTime;
  63. struct tm *tm;
  64. va_start(args, fmt);
  65. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  66. va_end(args);
  67. memset(Buf,0,sizeof(Buf));
  68. ftime(&SeqEndTime);
  69. SeqEndTime.time = time(NULL);
  70. tm=localtime(&SeqEndTime.time);
  71. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  72. {
  73. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  74. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  75. printf("%s \n", Buf);
  76. }
  77. else
  78. {
  79. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  80. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  81. buffer,
  82. tm->tm_year+1900,tm->tm_mon+1);
  83. system(Buf);
  84. }
  85. return rc;
  86. }
  87. void PRINTF_FUNC(char *string, ...)
  88. {
  89. va_list args;
  90. char buffer[4096];
  91. va_start(args, string);
  92. vsnprintf(buffer, sizeof(buffer), string, args);
  93. va_end(args);
  94. DEBUG_INFO("%s \n", buffer);
  95. }
  96. void ShowSocketData(struct PACKET_STRUCTURE *packet)
  97. {
  98. printf("se: %02X, id: %02X, op: %d, len: %3d, reg: %02X",
  99. packet->Header.se, packet->Header.id, packet->Header.op, packet->Header.len, packet->Payload.reg);
  100. if(packet->Header.len > 1)
  101. {
  102. printf(", Data:");
  103. for(int i = 0; i < packet->Header.len - 1; i++)
  104. {
  105. printf(" %02X", packet->Payload.data[i]);
  106. }
  107. }
  108. printf("\n");
  109. }
  110. //==========================================
  111. // Init all share memory
  112. //==========================================
  113. int InitShareMemory()
  114. {
  115. int result = PASS;
  116. int MeterSMId;
  117. //creat ShmSysConfigAndInfo
  118. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  119. {
  120. #ifdef SystemLogMessage
  121. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  122. #endif
  123. result = FAIL;
  124. }
  125. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  126. {
  127. #ifdef SystemLogMessage
  128. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  129. #endif
  130. result = FAIL;
  131. }
  132. else
  133. {}
  134. /*
  135. //creat ShmStatusCodeData
  136. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  137. {
  138. #ifdef SystemLogMessage
  139. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  140. #endif
  141. result = FAIL;
  142. }
  143. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  144. {
  145. #ifdef SystemLogMessage
  146. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  147. #endif
  148. result = FAIL;
  149. }
  150. else
  151. {}
  152. //creat ShmOCPP16Data
  153. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  154. {
  155. #ifdef SystemLogMessage
  156. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  157. #endif
  158. result = FAIL;
  159. }
  160. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  161. {
  162. #ifdef SystemLogMessage
  163. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  164. #endif
  165. result = FAIL;
  166. }
  167. else
  168. {}
  169. //creat ShmPsuData
  170. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  171. {
  172. #ifdef SystemLogMessage
  173. DEBUG_ERROR("shmget ShmPsuData NG \n");
  174. #endif
  175. result = FAIL;
  176. }
  177. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  178. {
  179. #ifdef SystemLogMessage
  180. DEBUG_ERROR("shmat ShmPsuData NG \n");
  181. #endif
  182. result = FAIL;
  183. }
  184. memset(ShmPsuData,0,sizeof(struct PsuData));
  185. */
  186. return result;
  187. }
  188. //-------------------------------------------Socket Client Start-------------------------------------------
  189. unsigned char seqNum = 0;
  190. void sendCmdDispenserModelName(int socket, unsigned char se)
  191. {
  192. struct PACKET_STRUCTURE sendBuffer;
  193. char *modelName = "DSYE601J0EW2PH";
  194. //char *modelName = "DSYE601J00W2PH";
  195. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  196. sendBuffer.Header.se = se;
  197. sendBuffer.Header.id = 0xFF;
  198. sendBuffer.Header.op = _Header_Write;
  199. sendBuffer.Header.len = strlen(modelName) + 2;
  200. sendBuffer.Payload.reg = _Reg_Dispenser_Model_Name;
  201. memcpy(&sendBuffer.Payload.data[0], modelName, strlen(modelName));
  202. sendBuffer.Payload.data[strlen(modelName)] = 1;
  203. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  204. }
  205. void sendCmdDispenserConnectorIDRequest(int socket, unsigned char se)
  206. {
  207. struct PACKET_STRUCTURE sendBuffer;
  208. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  209. sendBuffer.Header.se = se;
  210. sendBuffer.Header.id = 0xFF;
  211. sendBuffer.Header.op = _Header_Read;
  212. sendBuffer.Header.len = 1;
  213. sendBuffer.Payload.reg = _Reg_Connector_ID;
  214. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  215. }
  216. void sendCmdPowerCabinetStatusRequest(int socket, unsigned char connector, unsigned char se)
  217. {
  218. struct PACKET_STRUCTURE sendBuffer;
  219. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  220. sendBuffer.Header.se = se;
  221. sendBuffer.Header.id = connector;
  222. sendBuffer.Header.op = _Header_Read;
  223. sendBuffer.Header.len = 1;
  224. sendBuffer.Payload.reg = _Reg_Power_Cabinet_Status;
  225. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  226. }
  227. void sendCmdWriteDispenserStatus(int socket, unsigned char connector, unsigned char se)
  228. {
  229. struct PACKET_STRUCTURE sendBuffer;
  230. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  231. sendBuffer.Header.se = se;
  232. sendBuffer.Header.id = connector;
  233. sendBuffer.Header.op = _Header_Write;
  234. sendBuffer.Header.len = 1;
  235. sendBuffer.Payload.reg = _Reg_Dispenser_Status;
  236. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  237. }
  238. void sendCmdReadChargingCapability(int socket, unsigned char connector, unsigned char se)
  239. {
  240. struct PACKET_STRUCTURE sendBuffer;
  241. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  242. sendBuffer.Header.se = se;
  243. sendBuffer.Header.id = connector;
  244. sendBuffer.Header.op = _Header_Read;
  245. sendBuffer.Header.len = 1;
  246. sendBuffer.Payload.reg = _Reg_Charging_Capability;
  247. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  248. }
  249. void sendCmdWriteChargingTarget(int socket, unsigned char connector, unsigned char se, unsigned short voltage, unsigned short current)
  250. {
  251. struct PACKET_STRUCTURE sendBuffer;
  252. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  253. sendBuffer.Header.se = se;
  254. sendBuffer.Header.id = connector;
  255. sendBuffer.Header.op = _Header_Write;
  256. sendBuffer.Header.len = 5;
  257. sendBuffer.Payload.reg = _Reg_Charging_Target;
  258. sendBuffer.Payload.data[0] = (voltage >> 8) & 0xFF;
  259. sendBuffer.Payload.data[1] = voltage & 0xFF;
  260. sendBuffer.Payload.data[2] = (current >> 8) & 0xFF;
  261. sendBuffer.Payload.data[3] = current & 0xFF;
  262. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  263. }
  264. void sendCmdWritePlugInStatus(int socket, unsigned char connector, unsigned char se)
  265. {
  266. struct PACKET_STRUCTURE sendBuffer;
  267. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  268. sendBuffer.Header.se = se;
  269. sendBuffer.Header.id = connector;
  270. sendBuffer.Header.op = _Header_Write;
  271. sendBuffer.Header.len = 2;
  272. sendBuffer.Payload.reg = _Reg_Plug_In_Status;
  273. sendBuffer.Payload.data[0] = se % 2 == 0 ?_PIS_UnPlugged : _PIS_PluggedIn;
  274. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  275. }
  276. void sendCmdWriteConnectorState(int socket, unsigned char connector, unsigned char se)
  277. {
  278. struct PACKET_STRUCTURE sendBuffer;
  279. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  280. sendBuffer.Header.se = se;
  281. sendBuffer.Header.id = connector;
  282. sendBuffer.Header.op = _Header_Write;
  283. sendBuffer.Header.len = 2;
  284. sendBuffer.Payload.reg = _Reg_Connector_State;
  285. sendBuffer.Payload.data[0] = _CRS_Idle;
  286. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  287. }
  288. void sendCmdWriteUserID(int socket, unsigned char connector, unsigned char se)
  289. {
  290. struct PACKET_STRUCTURE sendBuffer;
  291. char *userID = "010203040506";
  292. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  293. sendBuffer.Header.se = se;
  294. sendBuffer.Header.id = connector;
  295. sendBuffer.Header.op = _Header_Write;
  296. sendBuffer.Header.len = strlen(userID) + 1;
  297. sendBuffer.Payload.reg = _Reg_User_ID;
  298. memcpy(sendBuffer.Payload.data, userID, strlen(userID));
  299. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  300. }
  301. void sendCmdReadChargingPermission(int socket, unsigned char connector, unsigned char se)
  302. {
  303. struct PACKET_STRUCTURE sendBuffer;
  304. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  305. sendBuffer.Header.se = se;
  306. sendBuffer.Header.id = connector;
  307. sendBuffer.Header.op = _Header_Read;
  308. sendBuffer.Header.len = 1;
  309. sendBuffer.Payload.reg = _Reg_Charging_Permission;
  310. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  311. }
  312. int DispenserModelNameCheckIn(int socket, unsigned char se)
  313. {
  314. int rxLen = 0, timeout = 0;
  315. struct PACKET_STRUCTURE receiveBuffer;
  316. int result = FAIL;
  317. sendCmdDispenserModelName(socket, se);
  318. while(1)
  319. {
  320. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  321. {
  322. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  323. {
  324. ShowSocketData(&receiveBuffer);
  325. if(receiveBuffer.Header.len == 2)
  326. {
  327. if(receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name && receiveBuffer.Payload.data[0] == _R_OK)
  328. {
  329. PRINTF_FUNC("Write Model Name OK");
  330. result = PASS;
  331. break;
  332. }
  333. }
  334. }
  335. }
  336. timeout += SOCKET_RECEIVE_INTERVAL;
  337. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  338. if(timeout >= DISPENSER_MODEL_NAME_RESEND)
  339. {
  340. //timeout
  341. break;
  342. }
  343. }
  344. return result;
  345. }
  346. int DispenserConnectorRequest(int socket, unsigned char se)
  347. {
  348. int rxLen = 0, timeout = 0;
  349. struct PACKET_STRUCTURE receiveBuffer;
  350. int result = FAIL;
  351. sendCmdDispenserConnectorIDRequest(socket, se);
  352. while(1)
  353. {
  354. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  355. {
  356. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  357. {
  358. ShowSocketData(&receiveBuffer);
  359. if(receiveBuffer.Header.len == 4)
  360. {
  361. if(receiveBuffer.Payload.reg == _Reg_Connector_ID && receiveBuffer.Payload.data[0] == _R_OK)
  362. {
  363. PRINTF_FUNC("Get Connector ID: %02X, %02X", receiveBuffer.Payload.data[1], receiveBuffer.Payload.data[2]);
  364. result = PASS;
  365. break;
  366. }
  367. }
  368. }
  369. }
  370. timeout += SOCKET_RECEIVE_INTERVAL;
  371. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  372. if(timeout >= DISPENSER_CONNECTOR_RESEND)
  373. {
  374. //timeout
  375. break;
  376. }
  377. }
  378. return result;
  379. }
  380. int DispenserReadPowerCabinetStatus(int socket, unsigned char connector, unsigned char se)
  381. {
  382. int rxLen = 0, timeout = 0;
  383. struct PACKET_STRUCTURE receiveBuffer;
  384. int result = FAIL;
  385. sendCmdPowerCabinetStatusRequest(socket, connector, se);
  386. while(1)
  387. {
  388. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  389. {
  390. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  391. {
  392. ShowSocketData(&receiveBuffer);
  393. if(receiveBuffer.Payload.reg == _Reg_Power_Cabinet_Status && receiveBuffer.Payload.data[0] == _R_OK)
  394. {
  395. PRINTF_FUNC("Get Cabinet Status Code, Len: %d", (receiveBuffer.Header.len - 2) / 6);
  396. result = PASS;
  397. }
  398. }
  399. }
  400. timeout += SOCKET_RECEIVE_INTERVAL;
  401. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  402. if(timeout >= CABINET_STATUS_REQUEST_RESEND)
  403. {
  404. //timeout
  405. break;
  406. }
  407. }
  408. return result;
  409. }
  410. int DispenserWriteStatusCode(int socket, unsigned char connector, unsigned char se)
  411. {
  412. int rxLen = 0, timeout = 0;
  413. struct PACKET_STRUCTURE receiveBuffer;
  414. int result = FAIL;
  415. sendCmdWriteDispenserStatus(socket, connector, se);
  416. while(1)
  417. {
  418. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  419. {
  420. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  421. {
  422. ShowSocketData(&receiveBuffer);
  423. if(receiveBuffer.Header.len == 2)
  424. {
  425. if(receiveBuffer.Payload.reg == _Reg_Dispenser_Status && receiveBuffer.Payload.data[0] == _R_OK)
  426. {
  427. PRINTF_FUNC("Write Dispenser Status Code OK");
  428. result = PASS;
  429. }
  430. }
  431. }
  432. }
  433. timeout += SOCKET_RECEIVE_INTERVAL;
  434. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  435. if(timeout >= CABINET_STATUS_REQUEST_RESEND)
  436. {
  437. //timeout
  438. break;
  439. }
  440. }
  441. return result;
  442. }
  443. int DispenserReadChargingCapability(int socket, unsigned char connector, unsigned char se)
  444. {
  445. int rxLen = 0, timeout = 0;
  446. struct PACKET_STRUCTURE receiveBuffer;
  447. int result = FAIL;
  448. unsigned short voltage = 0, current = 0, power = 0;
  449. sendCmdReadChargingCapability(socket, connector, se);
  450. while(1)
  451. {
  452. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  453. {
  454. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  455. {
  456. ShowSocketData(&receiveBuffer);
  457. if(receiveBuffer.Header.len == 8)
  458. {
  459. if(receiveBuffer.Payload.reg == _Reg_Charging_Capability && receiveBuffer.Payload.data[0] == _R_OK)
  460. {
  461. voltage = (receiveBuffer.Payload.data[1] << 8) + receiveBuffer.Payload.data[2];
  462. current = (receiveBuffer.Payload.data[3] << 8) + receiveBuffer.Payload.data[4];
  463. power = (receiveBuffer.Payload.data[5] << 8) + receiveBuffer.Payload.data[6];
  464. PRINTF_FUNC("Capability Voltage: %d.%dV, Current: %d.%dA, Power: %d.%dkW",
  465. voltage / 10, voltage % 10, current / 10, current % 10, power / 10, power % 10);
  466. result = PASS;
  467. }
  468. }
  469. }
  470. }
  471. timeout += SOCKET_RECEIVE_INTERVAL;
  472. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  473. if(timeout >= CHARGING_CAPABILITY_RESEND)
  474. {
  475. //timeout
  476. break;
  477. }
  478. }
  479. return result;
  480. }
  481. int DispenserWriteChargingTarget(int socket, unsigned char connector, unsigned char se)
  482. {
  483. int rxLen = 0, timeout = 0;
  484. struct PACKET_STRUCTURE receiveBuffer;
  485. int result = FAIL;
  486. sendCmdWriteChargingTarget(socket, connector, se, 7505, 406);
  487. while(1)
  488. {
  489. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  490. {
  491. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  492. {
  493. ShowSocketData(&receiveBuffer);
  494. if(receiveBuffer.Header.len == 2)
  495. {
  496. if(receiveBuffer.Payload.reg == _Reg_Charging_Target && receiveBuffer.Payload.data[0] == _R_OK)
  497. {
  498. PRINTF_FUNC("Write Charging Target OK");
  499. result = PASS;
  500. }
  501. }
  502. }
  503. }
  504. timeout += SOCKET_RECEIVE_INTERVAL;
  505. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  506. if(timeout >= CHARGING_CAPABILITY_RESEND)
  507. {
  508. //timeout
  509. break;
  510. }
  511. }
  512. return result;
  513. }
  514. int DispenserWritePlugInStatus(int socket, unsigned char connector, unsigned char se)
  515. {
  516. int rxLen = 0, timeout = 0;
  517. struct PACKET_STRUCTURE receiveBuffer;
  518. int result = FAIL;
  519. sendCmdWritePlugInStatus(socket, connector, se);
  520. while(1)
  521. {
  522. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  523. {
  524. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  525. {
  526. ShowSocketData(&receiveBuffer);
  527. if(receiveBuffer.Header.len == 2)
  528. {
  529. if(receiveBuffer.Payload.reg == _Reg_Plug_In_Status && receiveBuffer.Payload.data[0] == _R_OK)
  530. {
  531. PRINTF_FUNC("Write Plug In Status OK");
  532. result = PASS;
  533. }
  534. }
  535. }
  536. }
  537. timeout += SOCKET_RECEIVE_INTERVAL;
  538. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  539. if(timeout >= CHARGING_CAPABILITY_RESEND)
  540. {
  541. //timeout
  542. break;
  543. }
  544. }
  545. return result;
  546. }
  547. int DispenserWriteConnectorState(int socket, unsigned char connector, unsigned char se)
  548. {
  549. int rxLen = 0, timeout = 0;
  550. struct PACKET_STRUCTURE receiveBuffer;
  551. int result = FAIL;
  552. sendCmdWriteConnectorState(socket, connector, se);
  553. while(1)
  554. {
  555. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  556. {
  557. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  558. {
  559. ShowSocketData(&receiveBuffer);
  560. if(receiveBuffer.Header.len == 2)
  561. {
  562. if(receiveBuffer.Payload.reg == _Reg_Connector_State && receiveBuffer.Payload.data[0] == _R_OK)
  563. {
  564. PRINTF_FUNC("Write Connector State OK");
  565. result = PASS;
  566. }
  567. }
  568. }
  569. }
  570. timeout += SOCKET_RECEIVE_INTERVAL;
  571. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  572. if(timeout >= CHARGING_CAPABILITY_RESEND)
  573. {
  574. //timeout
  575. break;
  576. }
  577. }
  578. return result;
  579. }
  580. int DispenserWriteUserID(int socket, unsigned char connector, unsigned char se)
  581. {
  582. int rxLen = 0, timeout = 0;
  583. struct PACKET_STRUCTURE receiveBuffer;
  584. int result = FAIL;
  585. sendCmdWriteUserID(socket, connector, se);
  586. while(1)
  587. {
  588. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  589. {
  590. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  591. {
  592. ShowSocketData(&receiveBuffer);
  593. if(receiveBuffer.Header.len == 2)
  594. {
  595. if(receiveBuffer.Payload.reg == _Reg_User_ID && receiveBuffer.Payload.data[0] == _R_OK)
  596. {
  597. PRINTF_FUNC("Write User ID OK");
  598. result = PASS;
  599. }
  600. }
  601. }
  602. }
  603. timeout += SOCKET_RECEIVE_INTERVAL;
  604. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  605. if(timeout >= CHARGING_CAPABILITY_RESEND)
  606. {
  607. //timeout
  608. break;
  609. }
  610. }
  611. return result;
  612. }
  613. int DispenserReadChargingPermission(int socket, unsigned char connector, unsigned char se)
  614. {
  615. int rxLen = 0, timeout = 0;
  616. struct PACKET_STRUCTURE receiveBuffer;
  617. int result = FAIL;
  618. sendCmdReadChargingPermission(socket, connector, se);
  619. while(1)
  620. {
  621. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  622. {
  623. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  624. {
  625. ShowSocketData(&receiveBuffer);
  626. if(receiveBuffer.Header.len == 3)
  627. {
  628. if(receiveBuffer.Payload.reg == _Reg_Charging_Permission && receiveBuffer.Payload.data[0] == _R_OK)
  629. {
  630. PRINTF_FUNC("Charging Permission: %s", receiveBuffer.Payload.data[1] == _PS_Permitted ? "Permitted" : "Not Permitted");
  631. result = PASS;
  632. }
  633. }
  634. }
  635. }
  636. timeout += SOCKET_RECEIVE_INTERVAL;
  637. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  638. if(timeout >= CHARGING_CAPABILITY_RESEND)
  639. {
  640. //timeout
  641. break;
  642. }
  643. }
  644. return result;
  645. }
  646. void tcpSocketClientStart(void)
  647. {
  648. struct sockaddr_in info;
  649. struct timeval tv;
  650. uint8_t socketEnable;
  651. int sockfd = 0;
  652. unsigned char clientStatus = 0;
  653. unsigned char se = 0;
  654. //start dhcp client
  655. //udhcpc -i eth1 --script /root/dhcp_script/eth1.script
  656. //udhcpc -i eth1 -s /root/dhcp_script/eth1.script &
  657. //udhcpc -i eth0 -x hostname CSU3_DSYE601J0EW2PH-SERIALFORRD -s /root/dhcp_script/eth0.script
  658. for(;;)
  659. {
  660. bzero(&info,sizeof(info));
  661. info.sin_family = PF_INET;
  662. info.sin_addr.s_addr = inet_addr(SOCKET_SERVER_IP);
  663. info.sin_port = htons(TCP_LISTEN_PORT);
  664. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  665. if (sockfd == -1)
  666. {
  667. PRINTF_FUNC("Fail to create a socket.");
  668. return;
  669. }
  670. PRINTF_FUNC("TCP Client Start");
  671. if(connect(sockfd, (struct sockaddr *)&info, sizeof(info)) == -1)
  672. {
  673. PRINTF_FUNC("Connection error");
  674. }
  675. else
  676. {
  677. PRINTF_FUNC("Connect to %s success", inet_ntoa(info.sin_addr));
  678. tv.tv_sec = 0;
  679. tv.tv_usec = 500000;
  680. setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  681. socketEnable = ON;
  682. }
  683. while(socketEnable)
  684. {
  685. switch(clientStatus)
  686. {
  687. case 0:
  688. //5.1 Dispenser model name
  689. if(DispenserModelNameCheckIn(sockfd, se++) == PASS)
  690. {
  691. clientStatus = 1;
  692. }
  693. break;
  694. case 1:
  695. if(DispenserConnectorRequest(sockfd, se++) == PASS)
  696. {
  697. clientStatus = 2;
  698. }
  699. break;
  700. case 2:
  701. if(DispenserReadPowerCabinetStatus(sockfd, 0x01, se++) == PASS)
  702. {
  703. if(DispenserWriteStatusCode(sockfd, 0x01, se++) == PASS)
  704. {
  705. clientStatus = 3;
  706. }
  707. }
  708. break;
  709. case 3:
  710. if(DispenserWriteUserID(sockfd, 0x01, se++) == PASS)
  711. {
  712. clientStatus = 4;
  713. }
  714. break;
  715. case 4:
  716. if(DispenserReadChargingCapability(sockfd, 0x01, se++) == PASS)
  717. {
  718. }
  719. if(DispenserWriteChargingTarget(sockfd, 0x01, se++) == PASS)
  720. {
  721. }
  722. if(DispenserWritePlugInStatus(sockfd, 0x01, se++) == PASS)
  723. {
  724. }
  725. if(DispenserWriteConnectorState(sockfd, 0x01, se++) == PASS)
  726. {
  727. }
  728. if(DispenserReadChargingPermission(sockfd, 0x01, se++) == PASS)
  729. {
  730. }
  731. break;
  732. default:
  733. break;
  734. }
  735. }
  736. close(sockfd);
  737. sleep(5);
  738. return;
  739. }
  740. }
  741. //--------------------------------------------Socket Client End--------------------------------------------
  742. BOOL IsAvalibleGunType(char name, unsigned char *type)
  743. {
  744. char modelList[6] = {'J', 'U', 'E', 'G', 'V', 'F'};
  745. unsigned char typeList[6] = {0, 1, 1, 2, 1, 1}; // 0 : Chademo, 1: CCS, 2: GB
  746. for(int i = 0; i < 6; i++)
  747. {
  748. if(name == modelList[i])
  749. {
  750. *type = typeList[i];
  751. return true;
  752. }
  753. }
  754. return false;
  755. }
  756. unsigned char ConnectorQuantityTypeParsing(unsigned char *modelName, unsigned char *type, unsigned char *physical)
  757. {
  758. unsigned char quantity = 0;
  759. for(int i = 0; i < 3; i++)
  760. {
  761. if(IsAvalibleGunType(modelName[7 + i], &type[quantity]))
  762. {
  763. physical[quantity] = modelName[7 + i];
  764. if(quantity < 2)
  765. {
  766. quantity++;
  767. }
  768. else
  769. {
  770. quantity = 0;
  771. break;
  772. }
  773. }
  774. }
  775. return quantity;
  776. }
  777. void ModelNameResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  778. {
  779. struct PACKET_STRUCTURE sendBuffer;
  780. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  781. sendBuffer.Header.se = packet->Header.se;
  782. sendBuffer.Header.id = 0xFF;
  783. sendBuffer.Header.op = _Header_Response;
  784. sendBuffer.Header.len = 2;
  785. sendBuffer.Payload.reg = _Reg_Dispenser_Model_Name;
  786. sendBuffer.Payload.data[0] = result;
  787. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  788. }
  789. void ConnectorIDResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result, unsigned char dispenserIndex)
  790. {
  791. struct PACKET_STRUCTURE sendBuffer;
  792. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  793. sendBuffer.Header.se = packet->Header.se;
  794. sendBuffer.Header.id = 0xFF;
  795. sendBuffer.Header.op = _Header_Response;
  796. sendBuffer.Header.len = 4;
  797. sendBuffer.Payload.reg = _Reg_Connector_ID;
  798. sendBuffer.Payload.data[0] = result;
  799. sendBuffer.Payload.data[1] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0];
  800. sendBuffer.Payload.data[2] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[1];
  801. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  802. }
  803. void PowerCabinetStatusResponse(int socket, struct PACKET_STRUCTURE *packet)
  804. {
  805. struct PACKET_STRUCTURE sendBuffer;
  806. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  807. sendBuffer.Header.se = packet->Header.se;
  808. sendBuffer.Header.id = packet->Header.id;
  809. sendBuffer.Header.op = _Header_Response;
  810. sendBuffer.Header.len = 2;
  811. sendBuffer.Payload.reg = _Reg_Power_Cabinet_Status;
  812. sendBuffer.Payload.data[0] = _R_OK;
  813. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0)
  814. {
  815. sendBuffer.Header.len += (ShmSysConfigAndInfo->SysWarningInfo.WarningCount * 6);
  816. for(int i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++)
  817. {
  818. memcpy(&sendBuffer.Payload.data[1 + (i * 6)], &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], 6);
  819. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] == '1')
  820. {
  821. sendBuffer.Payload.data[1 + (i * 6) + 1] = '4';
  822. }
  823. }
  824. }
  825. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  826. }
  827. void DispenserStatusResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  828. {
  829. struct PACKET_STRUCTURE sendBuffer;
  830. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  831. sendBuffer.Header.se = packet->Header.se;
  832. sendBuffer.Header.id = packet->Header.id;
  833. sendBuffer.Header.op = _Header_Response;
  834. sendBuffer.Header.len = 2;
  835. sendBuffer.Payload.reg = _Reg_Dispenser_Status;
  836. sendBuffer.Payload.data[0] = result;
  837. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  838. }
  839. struct ChargingCapabilityResponse ConnectorCapability[GENERAL_GUN_QUANTITY];
  840. void GetPhysicalLimitVoltageAndCurrent(byte index, unsigned short *voltage, unsigned short *currrent)
  841. {
  842. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalVoltage != 0 &&
  843. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalVoltage <= *voltage)
  844. {
  845. *voltage = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalVoltage;
  846. }
  847. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalCurrent != 0 &&
  848. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalCurrent <= *currrent)
  849. {
  850. *currrent = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalCurrent;
  851. }
  852. }
  853. void GetConfigLimitVoltageAndCurrent(byte index, unsigned short *voltage, unsigned short *currrent)
  854. {
  855. unsigned short limitCurrent = ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX ?
  856. (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].MaxTotalChargingCurrent) :
  857. (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].MaxTotalChargingCurrent / 2);
  858. if(limitCurrent != 0 && limitCurrent <= *currrent)
  859. {
  860. *currrent = limitCurrent;
  861. }
  862. }
  863. void ChargingCapabilityResponse(int socket, struct PACKET_STRUCTURE *packet)
  864. {
  865. struct PACKET_STRUCTURE sendBuffer;
  866. unsigned short voltage = 0, current = 0, power = 0;
  867. unsigned char currency = 0;
  868. unsigned int price = 0, cost = 0, account = 0;
  869. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  870. voltage = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.MaximumChargingVoltage;
  871. current = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.AvailableChargingCurrent;
  872. power = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.AvailableChargingPower;
  873. GetPhysicalLimitVoltageAndCurrent(packet->Header.id - 1, &voltage, &current);
  874. GetConfigLimitVoltageAndCurrent(packet->Header.id - 1, &voltage, &current);
  875. currency = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency;
  876. price = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].UserPrice;
  877. cost = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].TotalCost;
  878. account = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AccountBalance;
  879. if(ConnectorCapability[packet->Header.id - 1].MaxOuputVoltage != voltage ||
  880. ConnectorCapability[packet->Header.id - 1].MaxOuputCurrent != current ||
  881. ConnectorCapability[packet->Header.id - 1].MaxOuputPower != power)
  882. {
  883. PRINTF_FUNC("Connector %d Capability Voltage: %d, Current: %d, Power: %d", packet->Header.id, (int)(voltage), (int)(current), (int)(power));
  884. }
  885. if(ConnectorCapability[packet->Header.id - 1].Currency != currency ||
  886. ConnectorCapability[packet->Header.id - 1].UserPrice != price ||
  887. ConnectorCapability[packet->Header.id - 1].TotalCost != cost ||
  888. ConnectorCapability[packet->Header.id - 1].AccountBalance != account)
  889. {
  890. PRINTF_FUNC("Connector %d Currency: %d, UserPrice: %d, Cost: %d, Account: %d", packet->Header.id, (int)(currency), (int)(price), (int)(cost), (int)(account));
  891. }
  892. ConnectorCapability[packet->Header.id - 1].MaxOuputVoltage = voltage;
  893. ConnectorCapability[packet->Header.id - 1].MaxOuputCurrent = current;
  894. ConnectorCapability[packet->Header.id - 1].MaxOuputPower = power;
  895. ConnectorCapability[packet->Header.id - 1].Currency = currency;
  896. ConnectorCapability[packet->Header.id - 1].UserPrice = price;
  897. ConnectorCapability[packet->Header.id - 1].TotalCost = cost;
  898. ConnectorCapability[packet->Header.id - 1].AccountBalance = account;
  899. sendBuffer.Header.se = packet->Header.se;
  900. sendBuffer.Header.id = packet->Header.id;
  901. sendBuffer.Header.op = _Header_Response;
  902. sendBuffer.Header.len = 21;
  903. sendBuffer.Payload.reg = _Reg_Charging_Capability;
  904. sendBuffer.Payload.data[0] = _R_OK;
  905. sendBuffer.Payload.data[1] = ((voltage >> 8) & 0xFF);
  906. sendBuffer.Payload.data[2] = (voltage & 0xFF);
  907. sendBuffer.Payload.data[3] = ((current >> 8) & 0xFF);
  908. sendBuffer.Payload.data[4] = (current & 0xFF);
  909. sendBuffer.Payload.data[5] = ((power >> 8) & 0xFF);
  910. sendBuffer.Payload.data[6] = (power & 0xFF);
  911. sendBuffer.Payload.data[7] = currency;
  912. sendBuffer.Payload.data[8] = ((price >> 24) & 0xFF);
  913. sendBuffer.Payload.data[9] = ((price >> 16) & 0xFF);
  914. sendBuffer.Payload.data[10] = ((price >> 8) & 0xFF);
  915. sendBuffer.Payload.data[11] = (price & 0xFF);
  916. sendBuffer.Payload.data[12] = ((cost >> 24) & 0xFF);
  917. sendBuffer.Payload.data[13] = ((cost >> 16) & 0xFF);
  918. sendBuffer.Payload.data[14] = ((cost >> 8) & 0xFF);
  919. sendBuffer.Payload.data[15] = (cost & 0xFF);
  920. sendBuffer.Payload.data[16] = ((account >> 24) & 0xFF);
  921. sendBuffer.Payload.data[17] = ((account >> 16) & 0xFF);
  922. sendBuffer.Payload.data[18] = ((account >> 8) & 0xFF);
  923. sendBuffer.Payload.data[19] = (account & 0xFF);
  924. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  925. }
  926. void ChargingTargetResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  927. {
  928. struct PACKET_STRUCTURE sendBuffer;
  929. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  930. sendBuffer.Header.se = packet->Header.se;
  931. sendBuffer.Header.id = packet->Header.id;
  932. sendBuffer.Header.op = _Header_Response;
  933. sendBuffer.Header.len = 2;
  934. sendBuffer.Payload.reg = _Reg_Charging_Target;
  935. sendBuffer.Payload.data[0] = result;
  936. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  937. }
  938. void FirmwareUpgradeResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  939. {
  940. struct PACKET_STRUCTURE sendBuffer;
  941. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  942. sendBuffer.Header.se = packet->Header.se;
  943. sendBuffer.Header.id = packet->Header.id;
  944. sendBuffer.Header.op = _Header_Response;
  945. sendBuffer.Header.len = 3;
  946. sendBuffer.Payload.reg = _Reg_Software_Update;
  947. sendBuffer.Payload.data[0] = _R_OK;
  948. sendBuffer.Payload.data[1] = result;
  949. if(result == _R_NeedUpgrade)
  950. {
  951. int length = strlen(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FwFileName);
  952. memcpy(&sendBuffer.Payload.data[2], ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FwFileName, length);
  953. sendBuffer.Header.len += length;
  954. }
  955. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  956. }
  957. void PlugInStatusResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  958. {
  959. struct PACKET_STRUCTURE sendBuffer;
  960. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  961. sendBuffer.Header.se = packet->Header.se;
  962. sendBuffer.Header.id = packet->Header.id;
  963. sendBuffer.Header.op = _Header_Response;
  964. sendBuffer.Header.len = 2;
  965. sendBuffer.Payload.reg = _Reg_Plug_In_Status;
  966. sendBuffer.Payload.data[0] = result;
  967. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  968. }
  969. void ConnectorStateResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  970. {
  971. struct PACKET_STRUCTURE sendBuffer;
  972. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  973. sendBuffer.Header.se = packet->Header.se;
  974. sendBuffer.Header.id = packet->Header.id;
  975. sendBuffer.Header.op = _Header_Response;
  976. sendBuffer.Header.len = 2;
  977. sendBuffer.Payload.reg = _Reg_Connector_State;
  978. sendBuffer.Payload.data[0] = result;
  979. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  980. }
  981. void UserIDResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  982. {
  983. struct PACKET_STRUCTURE sendBuffer;
  984. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  985. sendBuffer.Header.se = packet->Header.se;
  986. sendBuffer.Header.id = packet->Header.id;
  987. sendBuffer.Header.op = _Header_Response;
  988. sendBuffer.Header.len = 3;
  989. sendBuffer.Payload.reg = _Reg_User_ID;
  990. sendBuffer.Payload.data[0] = result == _DAS_Wait ? _R_NG : _R_OK;
  991. sendBuffer.Payload.data[1] = result == _DAS_Allowed ? _PS_Permitted : _PS_NotPermitted;
  992. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  993. }
  994. void ChargingPermissionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  995. {
  996. struct PACKET_STRUCTURE sendBuffer;
  997. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  998. sendBuffer.Header.se = packet->Header.se;
  999. sendBuffer.Header.id = packet->Header.id;
  1000. sendBuffer.Header.op = _Header_Response;
  1001. sendBuffer.Header.len = 3;
  1002. sendBuffer.Payload.reg = _Reg_Charging_Permission;
  1003. sendBuffer.Payload.data[0] = result == _DAS_Wait ? _R_NG : _R_OK;
  1004. sendBuffer.Payload.data[1] = result == _DAS_Allowed ? _PS_Permitted : _PS_NotPermitted;
  1005. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1006. }
  1007. void AddMiscCommand(struct PACKET_STRUCTURE *packet, struct MISC_COMMAND *misc)
  1008. {
  1009. packet->Payload.data[packet->Header.len - 1 + 0] = ((misc->Command >> 8) & 0xFF);
  1010. packet->Payload.data[packet->Header.len - 1 + 1] = ((misc->Command >> 0) & 0xFF);
  1011. packet->Payload.data[packet->Header.len - 1 + 2] = ((misc->Value >> 24) & 0xFF);
  1012. packet->Payload.data[packet->Header.len - 1 + 3] = ((misc->Value >> 16) & 0xFF);
  1013. packet->Payload.data[packet->Header.len - 1 + 4] = ((misc->Value >> 8) & 0xFF);
  1014. packet->Payload.data[packet->Header.len - 1 + 5] = ((misc->Value >> 0) & 0xFF);
  1015. packet->Header.len += 6;
  1016. }
  1017. void MiscControlResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1018. {
  1019. struct PACKET_STRUCTURE sendBuffer;
  1020. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1021. sendBuffer.Header.se = packet->Header.se;
  1022. sendBuffer.Header.id = packet->Header.id;
  1023. sendBuffer.Header.op = _Header_Response;
  1024. sendBuffer.Header.len = 2;
  1025. sendBuffer.Payload.reg = _Reg_Misc_Control;
  1026. sendBuffer.Payload.data[0] = result;
  1027. if(result == _R_OK)
  1028. {
  1029. struct MISC_COMMAND misc;
  1030. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AvailabilityRequest)
  1031. {
  1032. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AvailabilityRequest = false;
  1033. misc.Command = _MiscCmd_ChangeOperative;
  1034. misc.Value = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.IsAvailable;
  1035. PRINTF_FUNC("Announce Connector %d Availability: %d", packet->Header.id, misc.Value);
  1036. AddMiscCommand(&sendBuffer, &misc);
  1037. }
  1038. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStartRequest)
  1039. {
  1040. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStartRequest = false;
  1041. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStartConfirm = true;
  1042. misc.Command = _MiscCmd_RemoteStart;
  1043. misc.Value = true;
  1044. PRINTF_FUNC("Announce Connector %d Remote Start: %d", packet->Header.id, misc.Value);
  1045. AddMiscCommand(&sendBuffer, &misc);
  1046. }
  1047. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AccountBalanceRequest)
  1048. {
  1049. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AccountBalanceRequest = false;
  1050. misc.Command = _MiscCmd_AccountBalance;
  1051. misc.Value = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AccountBalance;
  1052. PRINTF_FUNC("Announce Connector %d Account Balance: %d", packet->Header.id, (misc.Value));
  1053. AddMiscCommand(&sendBuffer, &misc);
  1054. }
  1055. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] == packet->Header.id)
  1056. {
  1057. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.ConnectorTimeoutConfigRequest)
  1058. {
  1059. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.ConnectorTimeoutConfigRequest = false;
  1060. misc.Command = _MiscCmd_ConnectorTimeout;
  1061. misc.Value = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout;
  1062. PRINTF_FUNC("Announce Dispenser %d Connection Timeout: %d", dispenserIndex + 1, misc.Value);
  1063. AddMiscCommand(&sendBuffer, &misc);
  1064. }
  1065. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DefaultPriceConfigRequest)
  1066. {
  1067. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DefaultPriceConfigRequest = false;
  1068. misc.Command = _MiscCmd_DefaultPrice;
  1069. misc.Value = ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice;
  1070. PRINTF_FUNC("Announce Dispenser %d Default Price: %d", dispenserIndex + 1, misc.Value);
  1071. AddMiscCommand(&sendBuffer, &misc);
  1072. }
  1073. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.CurrencyConfigRequest)
  1074. {
  1075. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.CurrencyConfigRequest = false;
  1076. misc.Command = _MiscCmd_Currency;
  1077. misc.Value = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency;
  1078. PRINTF_FUNC("Announce Dispenser %d Currency Index: %d", dispenserIndex + 1, misc.Value);
  1079. AddMiscCommand(&sendBuffer, &misc);
  1080. }
  1081. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootRequest)
  1082. {
  1083. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootRequest = false;
  1084. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootConfirm = true;
  1085. misc.Command = _MiscCmd_HardwareReboot;
  1086. misc.Value = true;
  1087. PRINTF_FUNC("Announce Dispenser %d Hardware Reboot: %d", dispenserIndex + 1, misc.Value);
  1088. AddMiscCommand(&sendBuffer, &misc);
  1089. }
  1090. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetRequest)
  1091. {
  1092. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetRequest = false;
  1093. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetConfirm = true;
  1094. misc.Command = _MiscCmd_SoftwareRestart;
  1095. misc.Value = true;
  1096. PRINTF_FUNC("Announce Dispenser %d Software Reset: %d", dispenserIndex + 1, misc.Value);
  1097. AddMiscCommand(&sendBuffer, &misc);
  1098. }
  1099. }
  1100. }
  1101. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1102. }
  1103. void WriteCsuVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1104. {
  1105. struct PACKET_STRUCTURE sendBuffer;
  1106. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1107. sendBuffer.Header.se = packet->Header.se;
  1108. sendBuffer.Header.id = packet->Header.id;
  1109. sendBuffer.Header.op = _Header_Response;
  1110. sendBuffer.Header.len = 2;
  1111. sendBuffer.Payload.reg = _Reg_Report_Csu_Version;
  1112. sendBuffer.Payload.data[0] = result;
  1113. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1114. }
  1115. void WriteOtherVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1116. {
  1117. struct PACKET_STRUCTURE sendBuffer;
  1118. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1119. sendBuffer.Header.se = packet->Header.se;
  1120. sendBuffer.Header.id = packet->Header.id;
  1121. sendBuffer.Header.op = _Header_Response;
  1122. sendBuffer.Header.len = 2;
  1123. sendBuffer.Payload.reg = _Reg_Report_Other_Version;
  1124. sendBuffer.Payload.data[0] = result;
  1125. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1126. }
  1127. void WriteChargingInfoResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1128. {
  1129. struct PACKET_STRUCTURE sendBuffer;
  1130. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1131. sendBuffer.Header.se = packet->Header.se;
  1132. sendBuffer.Header.id = packet->Header.id;
  1133. sendBuffer.Header.op = _Header_Response;
  1134. sendBuffer.Header.len = 2;
  1135. sendBuffer.Payload.reg = _Reg_Charging_Info;
  1136. sendBuffer.Payload.data[0] = result;
  1137. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1138. }
  1139. void ChargerSystemIdResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1140. {
  1141. struct PACKET_STRUCTURE sendBuffer;
  1142. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1143. sendBuffer.Header.se = packet->Header.se;
  1144. sendBuffer.Header.id = packet->Header.id;
  1145. sendBuffer.Header.op = _Header_Response;
  1146. sendBuffer.Header.len = 2;
  1147. sendBuffer.Payload.reg = _Reg_Charger_System_Id;
  1148. sendBuffer.Payload.data[0] = result;
  1149. if(result == _R_OK)
  1150. {
  1151. int strLen = 0;
  1152. strLen = strlen((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1153. memcpy(&sendBuffer.Payload.data[sendBuffer.Header.len - 1], (char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, strLen);
  1154. sendBuffer.Header.len += strLen + 1;
  1155. char TimeSync[32];
  1156. struct timeb SeqEndTime;
  1157. struct tm *tm;
  1158. ftime(&SeqEndTime);
  1159. SeqEndTime.time = time(NULL);
  1160. tm=localtime(&SeqEndTime.time);
  1161. memset(TimeSync, 0x00, 32);
  1162. sprintf(TimeSync, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  1163. strLen = strlen(TimeSync);
  1164. memcpy(&sendBuffer.Payload.data[sendBuffer.Header.len - 1], TimeSync, strLen);
  1165. sendBuffer.Header.len += strLen + 1;
  1166. //PRINTF_FUNC("Dispenser %d Read System Id: %s", dispenserIndex + 1, ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1167. }
  1168. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1169. }
  1170. void WriteWaitPlugInResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1171. {
  1172. struct PACKET_STRUCTURE sendBuffer;
  1173. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1174. sendBuffer.Header.se = packet->Header.se;
  1175. sendBuffer.Header.id = packet->Header.id;
  1176. sendBuffer.Header.op = _Header_Response;
  1177. sendBuffer.Header.len = 2;
  1178. sendBuffer.Payload.reg = _Reg_WaitPlugIn;
  1179. sendBuffer.Payload.data[0] = result;
  1180. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1181. }
  1182. BOOL FindConnectorID(unsigned char dispenserIndex, unsigned char id)
  1183. {
  1184. BOOL find = false;
  1185. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1186. {
  1187. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] == id)
  1188. {
  1189. find = true;
  1190. break;
  1191. }
  1192. }
  1193. return find;
  1194. }
  1195. void ConnectorPhysicalLimitBindingHandler(unsigned char connectorIndex, unsigned char physical)
  1196. {
  1197. //char modelList[6] = {'J', 'U', 'E', 'G', 'V', 'F'};
  1198. //unsigned char typeList[6] = {0, 1, 1, 2, 1, 1}; // 0 : Chademo, 1: CCS, 2: GB
  1199. switch(physical)
  1200. {
  1201. case 'J':
  1202. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CHA_MAX_PHYSICAL_VOLTAGE;
  1203. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CHA_NATURAL_MAX_CURRENT;
  1204. break;
  1205. case 'U':
  1206. case 'E':
  1207. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CCS_MAX_PHYSICAL_VOLTAGE;
  1208. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CCS_NATURAL_MAX_CURRENT;
  1209. break;
  1210. case 'G':
  1211. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = GBT_MAX_PHYSICAL_VOLTAGE;
  1212. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = GBT_NATURAL_MAX_CURRENT;
  1213. break;
  1214. case 'V':
  1215. case 'F':
  1216. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CCS_MAX_PHYSICAL_VOLTAGE;
  1217. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CCS_LIQUID_MAX_CURRENT;
  1218. break;
  1219. default:
  1220. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = 0;
  1221. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = 0;
  1222. break;
  1223. }
  1224. }
  1225. void ConnectorTypeBindingHandler(unsigned char dispenserIndex, unsigned char *type, unsigned char *physicalType)
  1226. {
  1227. char *str_gun_type[] = {STR_GUN_TYPE_CHADEMO, STR_GUN_TYPE_CCS, STR_GUN_TYPE_GBT};
  1228. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1229. {
  1230. unsigned char gunIndex = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  1231. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex = dispenserIndex;
  1232. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.Index = gunIndex;
  1233. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.Type = type[i];
  1234. ConnectorPhysicalLimitBindingHandler(gunIndex, physicalType[i]);
  1235. PRINTF_FUNC("Dispenser %d Connector %d type %s, MaxVol: %4d, MaxCur: %4d",
  1236. dispenserIndex + 1, gunIndex + 1, str_gun_type[type[i]],
  1237. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalVoltage,
  1238. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalCurrent);
  1239. }
  1240. }
  1241. BOOL IsAvailableDispenserIndexSequence(unsigned char index)
  1242. {
  1243. if((ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status & (1 << index)) > 0)
  1244. {
  1245. // dispenser ever checkin
  1246. return true;
  1247. }
  1248. else
  1249. {
  1250. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  1251. {
  1252. if(i == index)
  1253. {
  1254. return true;
  1255. }
  1256. if((ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status & (1 << index)) == 0)
  1257. {
  1258. // i is not check in yet before index
  1259. return false;
  1260. }
  1261. }
  1262. }
  1263. return false;
  1264. }
  1265. void DispenserCheckInInfoUpdate(void)
  1266. {
  1267. unsigned char dispenser = 0, connector = 0;
  1268. BOOL change = false;
  1269. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  1270. {
  1271. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  1272. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  1273. {
  1274. dispenser++;
  1275. ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status |= (1 << i);
  1276. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[i] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity;
  1277. connector += ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity;
  1278. }
  1279. }
  1280. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity != dispenser ||
  1281. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity != connector)
  1282. {
  1283. change = true;
  1284. }
  1285. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity = dispenser;
  1286. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity = connector;
  1287. if(change)
  1288. {
  1289. PRINTF_FUNC("Total Dispenser: %d, Total Connector: %d\n",
  1290. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity,
  1291. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity);
  1292. }
  1293. }
  1294. void SetConnectorID(unsigned char dispenserIndex, unsigned char quantity)
  1295. {
  1296. unsigned char currentQuantity = 0;
  1297. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] = 0x00;
  1298. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[1] = 0x00;
  1299. for(int i = 0; i < dispenserIndex; i++)
  1300. {
  1301. currentQuantity += ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[i];
  1302. }
  1303. for(int i = 0; i < quantity; i++)
  1304. {
  1305. currentQuantity++;
  1306. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] = currentQuantity;
  1307. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[currentQuantity - 1].Enable = true;
  1308. }
  1309. }
  1310. BOOL DispenserIdentificationHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char *modelName)
  1311. {
  1312. unsigned char quantity = 0;
  1313. unsigned char connectorType[2] = {0, 0};
  1314. unsigned char physicalType[2] = {0, 0};
  1315. quantity = ConnectorQuantityTypeParsing(modelName, connectorType, physicalType);
  1316. if(quantity > 0)
  1317. {
  1318. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_Timeout &&
  1319. quantity != ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[dispenserIndex])
  1320. {
  1321. PRINTF_FUNC("The same dispenser id (%d) but connector quantity not match", dispenserIndex + 1);
  1322. return false;
  1323. }
  1324. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_None ||
  1325. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_Timeout)
  1326. {
  1327. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Identification;
  1328. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity = quantity;
  1329. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[dispenserIndex] = quantity;
  1330. SetConnectorID(dispenserIndex, quantity);
  1331. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName, packet->Payload.data, (packet->Header.len - 2));
  1332. ConnectorTypeBindingHandler(dispenserIndex, connectorType, physicalType);
  1333. PRINTF_FUNC("Dispenser id %d identified, Connector quantity: %d\n", dispenserIndex + 1, quantity);
  1334. DispenserCheckInInfoUpdate();
  1335. return true;
  1336. }
  1337. else
  1338. {
  1339. PRINTF_FUNC("Conflict dispenser ID: %d", dispenserIndex + 1);
  1340. }
  1341. }
  1342. else
  1343. {
  1344. PRINTF_FUNC("Connector quantity fail, model name: %s", modelName);
  1345. }
  1346. return false;
  1347. }
  1348. BOOL DispenserStatusCodeHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1349. {
  1350. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1351. if(find)
  1352. {
  1353. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount = (packet->Header.len - 1) / 6;
  1354. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount > 10)
  1355. {
  1356. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount = 10;
  1357. }
  1358. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  1359. {
  1360. memcpy(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i][0],
  1361. &packet->Payload.data[i * 6], 6);
  1362. }
  1363. /*
  1364. BOOL needUpdate = FALSE;
  1365. // check is there new status code
  1366. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  1367. {
  1368. if(strcmp(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i][0],
  1369. &LastWarningInfo[packet->Header.id - 1].WarningCode[i][0]) == 0)
  1370. {
  1371. needUpdate = TRUE;
  1372. break;
  1373. }
  1374. }
  1375. if(needUpdate)
  1376. {
  1377. memcpy(&LastWarningInfo[packet->Header.id - 1], &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo, sizeof(struct WARNING_CODE_INFO));
  1378. }
  1379. */
  1380. char strDispenserStatus[128];
  1381. sprintf(strDispenserStatus, "Dispenser id %d, Status Code Len: %d", packet->Header.id, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount);
  1382. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount > 0)
  1383. {
  1384. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  1385. {
  1386. char strTemp[16];
  1387. sprintf(strTemp, ", %s", ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i]);
  1388. strcat(strDispenserStatus, strTemp);
  1389. }
  1390. }
  1391. PRINTF_FUNC(strDispenserStatus);
  1392. }
  1393. else
  1394. {
  1395. }
  1396. return find;
  1397. }
  1398. BOOL ConnectorChargingCapabilityHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1399. {
  1400. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1401. BOOL done = false;
  1402. if(find)
  1403. {
  1404. done = true;
  1405. }
  1406. return done;
  1407. }
  1408. unsigned long GetTimeoutValue(struct timeval _sour_time)
  1409. {
  1410. struct timeval _end_time;
  1411. gettimeofday(&_end_time, NULL);
  1412. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  1413. }
  1414. BOOL ConnectorChargingTargetHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1415. {
  1416. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1417. BOOL done = false;
  1418. unsigned short voltage = 0, current = 0;
  1419. if(find)
  1420. {
  1421. voltage = ((packet->Payload.data[0] << 8) + packet->Payload.data[1]);
  1422. current = ((packet->Payload.data[2] << 8) + packet->Payload.data[3]);
  1423. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage != voltage ||
  1424. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent != current)
  1425. {
  1426. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage = voltage;
  1427. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent = current;
  1428. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable)
  1429. {
  1430. PRINTF_FUNC("Connector %d Remote Voltage: %d, Remote Current: %d", packet->Header.id, voltage, current);
  1431. }
  1432. }
  1433. GetPhysicalLimitVoltageAndCurrent(packet->Header.id - 1, &voltage, &current);
  1434. GetConfigLimitVoltageAndCurrent(packet->Header.id - 1, &voltage, &current);
  1435. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage != voltage ||
  1436. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent != current)
  1437. {
  1438. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable)
  1439. {
  1440. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable = true;
  1441. PRINTF_FUNC("Connector %d Remote Voltage: %d, Remote Current: %d", packet->Header.id,
  1442. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage,
  1443. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent);
  1444. PRINTF_FUNC("Connector %d Limit Voltage: %d, Limit Current: %d", packet->Header.id, voltage, current);
  1445. }
  1446. }
  1447. else
  1448. {
  1449. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable = false;
  1450. }
  1451. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitVoltage = voltage;
  1452. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitCurrent = current;
  1453. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus == S_CHARGING)
  1454. {
  1455. float targetVol = (float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitVoltage / 10);
  1456. float targetCur = (float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitCurrent / 10);
  1457. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage != targetVol ||
  1458. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent != targetCur)
  1459. {
  1460. PRINTF_FUNC("Connector %d Target Voltage: %d, Current: %d",
  1461. packet->Header.id, (int)(targetVol * 10), (int)(targetCur * 10));
  1462. }
  1463. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage = targetVol;
  1464. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent = targetCur;
  1465. done = true;
  1466. }
  1467. else
  1468. {
  1469. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage != 0 ||
  1470. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent != 0)
  1471. {
  1472. PRINTF_FUNC("Connector %d Not Charging Status, Set Voltage & Current Fail", packet->Header.id);
  1473. }
  1474. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage = 0;
  1475. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent = 0;
  1476. }
  1477. }
  1478. else
  1479. {
  1480. }
  1481. return done;
  1482. }
  1483. unsigned char FirmwareUpgradeHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1484. {
  1485. unsigned char UpgradeResponse = _R_NoUpgrade;
  1486. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] == packet->Header.id)
  1487. {
  1488. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateRequest)
  1489. {
  1490. if(packet->Payload.data[0] == 0)
  1491. {
  1492. UpgradeResponse = _R_NeedUpgrade;
  1493. }
  1494. else
  1495. {
  1496. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm)
  1497. {
  1498. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm = true;
  1499. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateRequest = false;
  1500. PRINTF_FUNC("Dispenser %d Updating...", dispenserIndex + 1);
  1501. }
  1502. }
  1503. }
  1504. else
  1505. {
  1506. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm)
  1507. {
  1508. if(packet->Payload.data[0] == 0)
  1509. {
  1510. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateCompleted)
  1511. {
  1512. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateCompleted = true;
  1513. PRINTF_FUNC("Dispenser %d Upgrade Completed", dispenserIndex + 1);
  1514. }
  1515. }
  1516. }
  1517. }
  1518. }
  1519. return UpgradeResponse;
  1520. }
  1521. BOOL ConnectorPlugInHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1522. {
  1523. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1524. if(find)
  1525. {
  1526. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn != packet->Payload.data[0])
  1527. {
  1528. PRINTF_FUNC("Connector %d %s", packet->Header.id, packet->Payload.data[0] == _PIS_PluggedIn ? "Pluged In" : "Un Plugged");
  1529. }
  1530. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn = packet->Payload.data[0];
  1531. }
  1532. else
  1533. {
  1534. }
  1535. return find;
  1536. }
  1537. BOOL ConnectorStateHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1538. {
  1539. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1540. if(find)
  1541. {
  1542. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus != packet->Payload.data[0])
  1543. {
  1544. PRINTF_FUNC("Connector %d Remote Status: %d", packet->Header.id, packet->Payload.data[0]);
  1545. if((packet->Payload.data[0] == _CRS_Idle && ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus != _CRS_Terminating) ||
  1546. packet->Payload.data[0] == _CRS_Terminating)
  1547. {
  1548. PRINTF_FUNC("*********** Connector id %d Set Stop Flag ***********\n", packet->Header.id);
  1549. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.StopChargeFlag = true;
  1550. }
  1551. }
  1552. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus = packet->Payload.data[0];
  1553. }
  1554. else
  1555. {
  1556. }
  1557. return true;
  1558. }
  1559. unsigned char UserIDHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1560. {
  1561. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1562. DispenserAck_Status authorize = _DAS_Wait;
  1563. if(find)
  1564. {
  1565. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AuthorizeStatus == _AuthorizeStatus_Idle)
  1566. {
  1567. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AuthorizeRequest &&
  1568. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].AuthorizeStatus == _AuthorizeStatus_Idle)
  1569. {
  1570. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId, 0x00, 32);
  1571. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId, packet->Payload.data, packet->Header.len - 1);
  1572. PRINTF_FUNC("Dispenser %d connector %d user id %s need authorize", dispenserIndex + 1, packet->Header.id, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId);
  1573. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AuthorizeRequest = true;
  1574. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.AuthorizeTargetID = packet->Header.id;
  1575. }
  1576. authorize = _DAS_Wait;
  1577. }
  1578. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AuthorizeStatus == _AuthorizeStatus_Fail)
  1579. {
  1580. PRINTF_FUNC("Dispenser %d connector %d user id %s authorizing fail", dispenserIndex + 1, packet->Header.id, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId);
  1581. authorize = _DAS_NotAllowed;
  1582. }
  1583. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AuthorizeStatus == _AuthorizeStatus_Pass)
  1584. {
  1585. PRINTF_FUNC("Dispenser %d connector %d user id %s authorizing ok", dispenserIndex + 1, packet->Header.id, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId);
  1586. authorize = _DAS_Allowed;
  1587. }
  1588. else
  1589. {
  1590. authorize = _DAS_Wait;
  1591. }
  1592. }
  1593. else
  1594. {
  1595. authorize = _DAS_NotAllowed;
  1596. }
  1597. return authorize;
  1598. }
  1599. unsigned char ChargingPermissionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1600. {
  1601. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1602. DispenserAck_Status permission = _DAS_Wait;
  1603. if(find)
  1604. {
  1605. switch(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus)
  1606. {
  1607. case S_IDLE:
  1608. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Idle ||
  1609. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing)
  1610. {
  1611. permission = _DAS_Wait;
  1612. }
  1613. else
  1614. {
  1615. permission = _DAS_NotAllowed;
  1616. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  1617. {
  1618. PRINTF_FUNC("Cabinet status idle, not allow dispenser %d connector %d charging",
  1619. dispenserIndex + 1, packet->Header.id);
  1620. }
  1621. }
  1622. break;
  1623. case S_PREPARING_FOR_EV:
  1624. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStopRequest ||
  1625. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopRequest)
  1626. {
  1627. permission = _DAS_NotAllowed;
  1628. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  1629. {
  1630. PRINTF_FUNC("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  1631. }
  1632. }
  1633. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus >= _CRS_Preparing &&
  1634. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus <= _CRS_Charging)
  1635. {
  1636. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.PermissionRequest)
  1637. {
  1638. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.PermissionRequest = true;
  1639. PRINTF_FUNC("Allow dispenser index %d connector id %d start preparing to charge", dispenserIndex, packet->Header.id);
  1640. }
  1641. permission = _DAS_Allowed;
  1642. }
  1643. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Terminating)
  1644. {
  1645. permission = _DAS_NotAllowed;
  1646. PRINTF_FUNC("Dispenser Terminating, Not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  1647. }
  1648. else
  1649. {
  1650. // keep wait
  1651. }
  1652. break;
  1653. case S_CHARGING:
  1654. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStopRequest ||
  1655. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopRequest)
  1656. {
  1657. permission = _DAS_NotAllowed;
  1658. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  1659. {
  1660. PRINTF_FUNC("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d charging", dispenserIndex, packet->Header.id);
  1661. }
  1662. }
  1663. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing ||
  1664. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Charging)
  1665. {
  1666. permission = _DAS_Allowed;
  1667. }
  1668. else
  1669. {
  1670. permission = _DAS_NotAllowed;
  1671. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  1672. {
  1673. PRINTF_FUNC("Dispenser index %d connector id %d remote status not in charging mode", dispenserIndex, packet->Header.id);
  1674. }
  1675. }
  1676. break;
  1677. case S_TERMINATING:
  1678. case S_COMPLETE:
  1679. case S_ALARM:
  1680. case S_RESERVATION:
  1681. case S_MAINTAIN:
  1682. permission = _DAS_NotAllowed;
  1683. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  1684. {
  1685. PRINTF_FUNC("Cabinet status(%d) not match, not allow dispenser index %d connector id %d charging",
  1686. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus,
  1687. dispenserIndex, packet->Header.id);
  1688. }
  1689. break;
  1690. default:
  1691. break;
  1692. }
  1693. }
  1694. else
  1695. {
  1696. PRINTF_FUNC("Dispenser index %d Connector id %d not found", dispenserIndex, packet->Header.id);
  1697. permission = _DAS_NotAllowed;
  1698. }
  1699. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission = permission;
  1700. // PRINTF_FUNC("Dispenser %d Permission %s, Gun Status: %d, Connector Status: %d, Wait Plug: %d, PlugIn: %d, Available: %d, Order: %d", dispenserIndex, permission == true ? "OK" : "NG",
  1701. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus,
  1702. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus,
  1703. // ShmSysConfigAndInfo->SysInfo.WaitForPlugit,
  1704. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn,
  1705. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.IsAvailable,
  1706. // ShmSysConfigAndInfo->SysInfo.OrderCharging);
  1707. return permission;
  1708. }
  1709. BOOL MiscControlHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1710. {
  1711. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1712. BOOL done = false;
  1713. if(find)
  1714. {
  1715. done = true;
  1716. }
  1717. return done;
  1718. }
  1719. unsigned char DispenserWriteCsuVersionInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1720. {
  1721. char version[32];
  1722. BOOL writeVersion = FALSE, showVersion = FALSE;;
  1723. memcpy(version, &packet->Payload.data[0], 32);
  1724. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev, version) != 0)
  1725. {
  1726. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev, version, 32);
  1727. showVersion = true;
  1728. }
  1729. memcpy(version, &packet->Payload.data[32], 32);
  1730. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev, version) != 0)
  1731. {
  1732. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev, version, 32);
  1733. showVersion = true;
  1734. }
  1735. memcpy(version, &packet->Payload.data[64], 32);
  1736. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev, version) != 0)
  1737. {
  1738. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev, version, 32);
  1739. showVersion = true;
  1740. }
  1741. memcpy(version, &packet->Payload.data[96], 32);
  1742. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev, version) != 0)
  1743. {
  1744. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev, version, 32);
  1745. showVersion = true;
  1746. }
  1747. if(showVersion)
  1748. {
  1749. PRINTF_FUNC("********** Dispenser id %d: Csu Version information **********", dispenserIndex + 1);
  1750. PRINTF_FUNC("********** CsuBootLoadRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev);
  1751. PRINTF_FUNC("********** CsuKernelFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev);
  1752. PRINTF_FUNC("********** CsuRootFsFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev);
  1753. PRINTF_FUNC("********** CsuPrimFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev);
  1754. }
  1755. writeVersion = TRUE;
  1756. return writeVersion;
  1757. }
  1758. unsigned char DispenserWriteOtherVersionInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1759. {
  1760. char version[32];
  1761. BOOL writeVersion = FALSE, showVersion = FALSE;;
  1762. memcpy(version, &packet->Payload.data[0], 32);
  1763. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev, version) != 0)
  1764. {
  1765. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev, version, 32);
  1766. showVersion = true;
  1767. }
  1768. memcpy(version, &packet->Payload.data[32], 32);
  1769. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev, version) != 0)
  1770. {
  1771. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev, version, 32);
  1772. showVersion = true;
  1773. }
  1774. memcpy(version, &packet->Payload.data[64], 32);
  1775. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev, version) != 0)
  1776. {
  1777. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev, version, 32);
  1778. showVersion = true;
  1779. }
  1780. memcpy(version, &packet->Payload.data[96], 32);
  1781. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev, version) != 0)
  1782. {
  1783. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev, version, 32);
  1784. showVersion = true;
  1785. }
  1786. memcpy(version, &packet->Payload.data[128], 32);
  1787. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev, version) != 0)
  1788. {
  1789. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev, version, 32);
  1790. showVersion = true;
  1791. }
  1792. if(showVersion)
  1793. {
  1794. PRINTF_FUNC("********** Dispenser id %d: Other Version information **********", dispenserIndex + 1);
  1795. PRINTF_FUNC("********** FanModuleFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev);
  1796. PRINTF_FUNC("********** RelayModuleRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev);
  1797. PRINTF_FUNC("********** Connector1Rev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev);
  1798. PRINTF_FUNC("********** Connector2Rev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev);
  1799. PRINTF_FUNC("********** LedModuleFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev);
  1800. }
  1801. writeVersion = TRUE;
  1802. return writeVersion;
  1803. }
  1804. unsigned char DispenserWriteChargingInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1805. {
  1806. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1807. if(find)
  1808. {
  1809. unsigned short voltage = (packet->Payload.data[0] << 8) + (packet->Payload.data[1]);
  1810. unsigned short current = (packet->Payload.data[2] << 8) + (packet->Payload.data[3]);
  1811. unsigned int time = (packet->Payload.data[4] << 24) + (packet->Payload.data[5] << 16) + (packet->Payload.data[6] << 8) + (packet->Payload.data[7]);
  1812. unsigned char soc = (packet->Payload.data[8]);
  1813. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteChargingVoltage = voltage;
  1814. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteChargingCurrent = current;
  1815. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteRemainChargingDuration = time;
  1816. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteSoc = soc;
  1817. }
  1818. else
  1819. {
  1820. }
  1821. return find;
  1822. }
  1823. unsigned char ReadChargerSystemIdHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1824. {
  1825. BOOL find = TRUE;
  1826. return find;
  1827. }
  1828. unsigned char WriteWaitPlugInHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1829. {
  1830. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1831. if(find)
  1832. {
  1833. unsigned char StartWait = packet->Payload.data[0];
  1834. if(StartWait)
  1835. {
  1836. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug)
  1837. {
  1838. PRINTF_FUNC("Connector %d Start Wait Plug In", packet->Header.id);
  1839. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.SwipeRfidConfirm = true;
  1840. }
  1841. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = true;
  1842. }
  1843. else
  1844. {
  1845. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug)
  1846. {
  1847. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus != S_IDLE)
  1848. {
  1849. PRINTF_FUNC("Connector %d Stop Wait Plug In", packet->Header.id);
  1850. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.NeedCleanAuthorizeInfo = true;
  1851. }
  1852. }
  1853. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = false;
  1854. }
  1855. }
  1856. else
  1857. {
  1858. }
  1859. return find;
  1860. }
  1861. void DisableConnector(unsigned char dispenserIndex)
  1862. {
  1863. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1864. {
  1865. unsigned char connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  1866. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Enable = false;
  1867. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.TimeoutStopRequest = true;
  1868. }
  1869. }
  1870. void DispenserSocketProcess(int socketFd, struct sockaddr_in clientInfo, unsigned char index)
  1871. {
  1872. int rxLen = 0, timeout = 0;
  1873. unsigned char dispenserID = 0, dispenserIndex = 0;
  1874. struct PACKET_STRUCTURE receiveBuffer;
  1875. unsigned char modelName[64];
  1876. unsigned char ackResult = _R_NG;
  1877. DispenserAck_Status ack = _DAS_Wait;
  1878. PRINTF_FUNC("IP %s connection(%d) is established, start dispenser process", (inet_ntoa(clientInfo.sin_addr)), index);
  1879. while(1)
  1880. {
  1881. if((rxLen = recv(socketFd, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  1882. {
  1883. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  1884. {
  1885. ackResult = _R_NG;
  1886. //ShowSocketData(&receiveBuffer);
  1887. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_WaitModelName)
  1888. {
  1889. // Reg: 0x01, Dispenser model name
  1890. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name)
  1891. {
  1892. memset(modelName, 0x00, 64);
  1893. dispenserID = receiveBuffer.Payload.data[receiveBuffer.Header.len - 2];
  1894. if(dispenserID > 0 && dispenserID <= GENERAL_GUN_QUANTITY)
  1895. {
  1896. dispenserIndex = dispenserID - 1;
  1897. memcpy(modelName, receiveBuffer.Payload.data, receiveBuffer.Header.len - 2);
  1898. if(IsAvailableDispenserIndexSequence(dispenserIndex))
  1899. {
  1900. if(DispenserIdentificationHandler(&receiveBuffer, dispenserIndex, modelName) == true)
  1901. {
  1902. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].DispenserIndex = dispenserIndex;
  1903. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status = _CNS_DispenserMatched;
  1904. PRINTF_FUNC("********** Dispenser id %d check in, model name: %s **********\n", dispenserID, modelName);
  1905. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName, modelName, 64);
  1906. ackResult = _R_OK;
  1907. }
  1908. else
  1909. {
  1910. // response NG: gun quantity fail
  1911. PRINTF_FUNC("Dispenser ID %d Identification Fail", dispenserID);
  1912. }
  1913. }
  1914. else
  1915. {
  1916. // response NG: dispenser Index fail
  1917. PRINTF_FUNC("Dispenser ID %d Fail, Need Wait Another Dispenser", dispenserID);
  1918. }
  1919. }
  1920. else
  1921. {
  1922. PRINTF_FUNC("Dispenser ID: %d Over Range", dispenserID);
  1923. }
  1924. ModelNameResponse(socketFd, &receiveBuffer, ackResult);
  1925. }
  1926. else
  1927. {
  1928. // do nothing
  1929. }
  1930. }
  1931. else if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_DispenserMatched)
  1932. {
  1933. // Reg: 0x01, Dispenser model name
  1934. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name)
  1935. {
  1936. ackResult = _R_OK;
  1937. ModelNameResponse(socketFd, &receiveBuffer, ackResult);
  1938. }
  1939. // Reg: 0x02, Connector ID
  1940. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Connector_ID)
  1941. {
  1942. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity > 0)
  1943. {
  1944. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Idle;
  1945. ackResult = _R_OK;
  1946. }
  1947. PRINTF_FUNC("Dispenser ID %d Get Connector ID %s", dispenserID, ackResult == _R_OK ? "OK" : "NG");
  1948. ConnectorIDResponse(socketFd, &receiveBuffer, ackResult, dispenserIndex);
  1949. if(ackResult == _R_OK)
  1950. {
  1951. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DispenserConfigSync)
  1952. {
  1953. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DispenserConfigSync = true;
  1954. PRINTF_FUNC("Dispenser ID %d Configuration Need To Synchronize", dispenserID);
  1955. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.ConnectorTimeoutConfigRequest = 1;
  1956. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DefaultPriceConfigRequest = 1;
  1957. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.CurrencyConfigRequest = 1;
  1958. }
  1959. }
  1960. }
  1961. // Reg: 0x03, Power cabinet status
  1962. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Power_Cabinet_Status)
  1963. {
  1964. PowerCabinetStatusResponse(socketFd, &receiveBuffer);
  1965. }
  1966. // Reg: 0x04, Dispenser status
  1967. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Status)
  1968. {
  1969. if(DispenserStatusCodeHandler(&receiveBuffer, dispenserIndex))
  1970. {
  1971. ackResult = _R_OK;
  1972. }
  1973. DispenserStatusResponse(socketFd, &receiveBuffer, ackResult);
  1974. }
  1975. // Reg: 0x05, Charging capability
  1976. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charging_Capability)
  1977. {
  1978. if(ConnectorChargingCapabilityHandler(&receiveBuffer, dispenserIndex))
  1979. {
  1980. ChargingCapabilityResponse(socketFd, &receiveBuffer);
  1981. }
  1982. }
  1983. // Reg: 0x06, Charging target
  1984. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Charging_Target)
  1985. {
  1986. if(receiveBuffer.Header.len == 5)
  1987. {
  1988. if(ConnectorChargingTargetHandler(&receiveBuffer, dispenserIndex))
  1989. {
  1990. ackResult = _R_OK;
  1991. }
  1992. }
  1993. ChargingTargetResponse(socketFd, &receiveBuffer, ackResult);
  1994. }
  1995. // Reg: 0x07, Software update
  1996. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Software_Update)
  1997. {
  1998. unsigned char NeedUpgrade = FirmwareUpgradeHandler(&receiveBuffer, dispenserIndex);
  1999. FirmwareUpgradeResponse(socketFd, &receiveBuffer, dispenserIndex, NeedUpgrade);
  2000. }
  2001. // Reg: 0x08, Plug-in status
  2002. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Plug_In_Status)
  2003. {
  2004. if(receiveBuffer.Header.len == 2)
  2005. {
  2006. if(ConnectorPlugInHandler(&receiveBuffer, dispenserIndex))
  2007. {
  2008. ackResult = _R_OK;
  2009. }
  2010. }
  2011. PlugInStatusResponse(socketFd, &receiveBuffer, ackResult);
  2012. }
  2013. // Reg: 0x09, Connector state
  2014. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Connector_State)
  2015. {
  2016. if(ConnectorStateHandler(&receiveBuffer, dispenserIndex))
  2017. {
  2018. ackResult = _R_OK;
  2019. }
  2020. ConnectorStateResponse(socketFd, &receiveBuffer, ackResult);
  2021. }
  2022. // Reg: 0x0A, User ID
  2023. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_User_ID)
  2024. {
  2025. ack = UserIDHandler(&receiveBuffer, dispenserIndex);
  2026. UserIDResponse(socketFd, &receiveBuffer, ack);
  2027. }
  2028. // Reg: 0x0B, Charging permission
  2029. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charging_Permission)
  2030. {
  2031. ack = ChargingPermissionHandler(&receiveBuffer, dispenserIndex);
  2032. ChargingPermissionResponse(socketFd, &receiveBuffer, ack);
  2033. }
  2034. // Reg: 0x0C, Misc Control
  2035. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Misc_Control)
  2036. {
  2037. if(MiscControlHandler(&receiveBuffer, dispenserIndex))
  2038. {
  2039. ackResult = _R_OK;
  2040. }
  2041. MiscControlResponse(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  2042. }
  2043. // Reg: 0x0D, Report csu version
  2044. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Report_Csu_Version)
  2045. {
  2046. if(DispenserWriteCsuVersionInfoHandler(&receiveBuffer, dispenserIndex))
  2047. {
  2048. ackResult = _R_OK;
  2049. }
  2050. WriteCsuVersionResponse(socketFd, &receiveBuffer, ackResult);
  2051. }
  2052. // Reg: 0x0E, Report other version
  2053. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Report_Other_Version)
  2054. {
  2055. if(DispenserWriteOtherVersionInfoHandler(&receiveBuffer, dispenserIndex))
  2056. {
  2057. ackResult = _R_OK;
  2058. }
  2059. WriteOtherVersionResponse(socketFd, &receiveBuffer, ackResult);
  2060. }
  2061. // Reg: 0x0F, Charging info
  2062. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Charging_Info)
  2063. {
  2064. if(DispenserWriteChargingInfoHandler(&receiveBuffer, dispenserIndex))
  2065. {
  2066. ackResult = _R_OK;
  2067. }
  2068. WriteChargingInfoResponse(socketFd, &receiveBuffer, ackResult);
  2069. }
  2070. // Reg: 0x10, Charger system id
  2071. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charger_System_Id)
  2072. {
  2073. if(ReadChargerSystemIdHandler(&receiveBuffer, dispenserIndex))
  2074. {
  2075. ackResult = _R_OK;
  2076. }
  2077. ChargerSystemIdResponse(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  2078. }
  2079. // Reg: 0x11, Wait plug in
  2080. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_WaitPlugIn)
  2081. {
  2082. if(WriteWaitPlugInHandler(&receiveBuffer, dispenserIndex))
  2083. {
  2084. ackResult = _R_OK;
  2085. }
  2086. WriteWaitPlugInResponse(socketFd, &receiveBuffer, ackResult);
  2087. }
  2088. }
  2089. // clean timeout
  2090. timeout = 0;
  2091. }
  2092. }
  2093. else
  2094. {
  2095. timeout += SOCKET_RECEIVE_INTERVAL;
  2096. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  2097. }
  2098. if(timeout >= DISPENSER_SOCKET_TIMEOUT)
  2099. {
  2100. //timeout
  2101. PRINTF_FUNC("IP: %s, Socket %d connection timeout", (inet_ntoa(clientInfo.sin_addr)), index);
  2102. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_DispenserMatched)
  2103. {
  2104. DisableConnector(dispenserIndex);
  2105. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex], 0x00, sizeof(struct DispenserModule));
  2106. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Timeout;
  2107. DispenserCheckInInfoUpdate();
  2108. }
  2109. break;
  2110. }
  2111. }
  2112. }
  2113. int GetMacAddress(int sockFd, unsigned char *mac)
  2114. {
  2115. struct arpreq arpReq;
  2116. struct sockaddr_in clientInfo;
  2117. socklen_t len = sizeof(struct sockaddr_in);
  2118. memset(mac, 0x00, 6);
  2119. if(getpeername(sockFd, (struct sockaddr*)&clientInfo, &len) < 0)
  2120. {
  2121. return 0;
  2122. }
  2123. else
  2124. {
  2125. memcpy(&arpReq.arp_pa, &clientInfo, sizeof(struct sockaddr_in));
  2126. strcpy(arpReq.arp_dev, "eth1");
  2127. arpReq.arp_pa.sa_family = AF_INET;
  2128. arpReq.arp_ha.sa_family = AF_UNSPEC;
  2129. if(ioctl(sockFd, SIOCGARP, &arpReq) < 0)
  2130. {
  2131. return 0;
  2132. }
  2133. else
  2134. {
  2135. memcpy(mac, arpReq.arp_ha.sa_data, 6);
  2136. }
  2137. }
  2138. return 1;
  2139. }
  2140. int FindFreeChannel(void)
  2141. {
  2142. for(int i = 0; i < MAXIMUM_CONNECT_QUANTITY; i++)
  2143. {
  2144. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_FREE)
  2145. {
  2146. return i;
  2147. }
  2148. }
  2149. return FAIL;
  2150. }
  2151. BOOL IsConflictIp(uint32_t ipAddress)
  2152. {
  2153. for(int i = 0; i < MAXIMUM_CONNECT_QUANTITY; i++)
  2154. {
  2155. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress == ipAddress)
  2156. {
  2157. return true;
  2158. }
  2159. }
  2160. return false;
  2161. }
  2162. void InitDispenserInfo(void)
  2163. {
  2164. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  2165. {
  2166. //memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i], 0x00, sizeof(struct ConnectorInfoData));
  2167. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteStatus = 0;
  2168. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Enable = 0;
  2169. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ReadyToCharge = 0;
  2170. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex = 0;
  2171. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.Value = 0;
  2172. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AuthorizeStatus = 0;
  2173. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].WarningInfo, 0x00, sizeof(struct WARNING_CODE_INFO));
  2174. //memset(&LastWarningInfo[i], 0x00, sizeof(struct WARNING_CODE_INFO));
  2175. }
  2176. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo, 0x00, sizeof(struct DispenserInfoData));
  2177. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  2178. }
  2179. void tcpSocketServerStart(void)
  2180. {
  2181. int sockFd = 0;
  2182. int clientSockFd = 0;
  2183. int connectIndex = 0;
  2184. pid_t forkId;
  2185. struct sockaddr_in serverInfo, clientInfo;
  2186. socklen_t addrlen = sizeof(clientInfo);
  2187. //struct timeval timeout = {3, 0};
  2188. sockFd = socket(AF_INET, SOCK_STREAM, 0);
  2189. if(sockFd == -1)
  2190. {
  2191. PRINTF_FUNC("InitSocketServer NG\n");
  2192. sleep(5);
  2193. return;
  2194. }
  2195. InitDispenserInfo();
  2196. bzero(&serverInfo,sizeof(serverInfo));
  2197. serverInfo.sin_family = PF_INET;
  2198. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  2199. serverInfo.sin_port = htons(TCP_LISTEN_PORT);
  2200. bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo));
  2201. listen(sockFd, CONNECTION_LIMIT);
  2202. PRINTF_FUNC("TCP Server Start\n");
  2203. signal(SIGCHLD, SIG_IGN);
  2204. // Main loop
  2205. while(1)
  2206. {
  2207. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  2208. /*
  2209. if((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == ENONET) ||
  2210. (errno == EPROTO) || (errno == ENOPROTOOPT) || (errno == EOPNOTSUPP) ||
  2211. (errno == ENETDOWN) || (errno == ENETUNREACH) || (errno == EHOSTDOWN) ||
  2212. (errno == EHOSTUNREACH) || (errno == ECONNABORTED))
  2213. {
  2214. printf("accept error: %s\n", strerror(errno));
  2215. //exit(EXIT_FAILURE);
  2216. //break;
  2217. }
  2218. else if ( errno == EINTR)
  2219. {
  2220. printf("accept EINTRn");
  2221. continue;
  2222. }
  2223. else
  2224. {
  2225. printf("AccepCb: accept error: %s\n", strerror(errno));
  2226. //break;
  2227. //exit(EXIT_FAILURE);
  2228. }*/
  2229. PRINTF_FUNC("Client ip: %s is accepted at port: %d", inet_ntoa(clientInfo.sin_addr), clientInfo.sin_port);
  2230. connectIndex = FindFreeChannel();
  2231. if(connectIndex >= 0)
  2232. {
  2233. if(IsConflictIp(clientInfo.sin_addr.s_addr) == false)
  2234. {
  2235. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex].Status = _CNS_WaitModelName;
  2236. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex].IpAddress = clientInfo.sin_addr.s_addr;
  2237. forkId = fork();
  2238. if(forkId == 0)
  2239. {
  2240. // child process
  2241. DispenserSocketProcess(clientSockFd, clientInfo, connectIndex);
  2242. close(clientSockFd);
  2243. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex], 0x00, sizeof(struct ConnectionInfoData));
  2244. return;
  2245. }
  2246. else if(forkId == -1)
  2247. {
  2248. PRINTF_FUNC("fork fail");
  2249. }
  2250. }
  2251. else
  2252. {
  2253. // conflict ip address
  2254. PRINTF_FUNC("Conflict IP address, close socket");
  2255. close(clientSockFd);
  2256. }
  2257. }
  2258. else
  2259. {
  2260. // no free channel
  2261. PRINTF_FUNC("No free channel, close socket");
  2262. close(clientSockFd);
  2263. }
  2264. usleep(10000);
  2265. }
  2266. return;
  2267. }
  2268. int main(void)
  2269. {
  2270. if(InitShareMemory() == FAIL)
  2271. {
  2272. #ifdef SystemLogMessage
  2273. DEBUG_ERROR("InitShareMemory NG\n");
  2274. #endif
  2275. sleep(5);
  2276. return 0;
  2277. }
  2278. // wait for self test completed
  2279. while(ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING);
  2280. tcpSocketServerStart();
  2281. //tcpSocketClientStart();
  2282. return 0;
  2283. }