Module_EvComm.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702
  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 / 10) != (voltage / 10) ||
  880. (ConnectorCapability[packet->Header.id - 1].MaxOuputCurrent / 10) != (current / 10) ||
  881. (ConnectorCapability[packet->Header.id - 1].MaxOuputPower / 10) != (power / 10))
  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.RemoteStopRequest)
  1048. // {
  1049. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStopRequest = false;
  1050. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStopConfirm = true;
  1051. // misc.Command = _MiscCmd_RemoteStop;
  1052. // misc.Value = true;
  1053. // PRINTF_FUNC("Announce Connector %d Remote Stop: %d", packet->Header.id, misc.Value);
  1054. // AddMiscCommand(&sendBuffer, &misc);
  1055. // }
  1056. // if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopRequest)
  1057. // {
  1058. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopRequest = false;
  1059. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopConfirm = true;
  1060. // misc.Command = _MiscCmd_UnlockStop;
  1061. // misc.Value = true;
  1062. // PRINTF_FUNC("Announce Connector %d Unlock Stop: %d", packet->Header.id, misc.Value);
  1063. // AddMiscCommand(&sendBuffer, &misc);
  1064. // }
  1065. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AccountBalanceRequest)
  1066. {
  1067. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AccountBalanceRequest = false;
  1068. misc.Command = _MiscCmd_AccountBalance;
  1069. misc.Value = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AccountBalance;
  1070. PRINTF_FUNC("Announce Connector %d Account Balance: %d", packet->Header.id, (misc.Value));
  1071. AddMiscCommand(&sendBuffer, &misc);
  1072. }
  1073. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] == packet->Header.id)
  1074. {
  1075. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.ConnectorTimeoutConfigRequest)
  1076. {
  1077. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.ConnectorTimeoutConfigRequest = false;
  1078. misc.Command = _MiscCmd_ConnectorTimeout;
  1079. misc.Value = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout;
  1080. PRINTF_FUNC("Announce Dispenser %d Connection Timeout: %d", dispenserIndex + 1, misc.Value);
  1081. AddMiscCommand(&sendBuffer, &misc);
  1082. }
  1083. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DefaultPriceConfigRequest)
  1084. {
  1085. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DefaultPriceConfigRequest = false;
  1086. misc.Command = _MiscCmd_DefaultPrice;
  1087. misc.Value = ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice;
  1088. PRINTF_FUNC("Announce Dispenser %d Default Price: %d", dispenserIndex + 1, misc.Value);
  1089. AddMiscCommand(&sendBuffer, &misc);
  1090. }
  1091. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.CurrencyConfigRequest)
  1092. {
  1093. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.CurrencyConfigRequest = false;
  1094. misc.Command = _MiscCmd_Currency;
  1095. misc.Value = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency;
  1096. PRINTF_FUNC("Announce Dispenser %d Currency Index: %d", dispenserIndex + 1, misc.Value);
  1097. AddMiscCommand(&sendBuffer, &misc);
  1098. }
  1099. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootRequest)
  1100. {
  1101. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootRequest = false;
  1102. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootConfirm = true;
  1103. misc.Command = _MiscCmd_HardwareReboot;
  1104. misc.Value = true;
  1105. PRINTF_FUNC("Announce Dispenser %d Hardware Reboot: %d", dispenserIndex + 1, misc.Value);
  1106. AddMiscCommand(&sendBuffer, &misc);
  1107. }
  1108. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetRequest)
  1109. {
  1110. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetRequest = false;
  1111. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetConfirm = true;
  1112. misc.Command = _MiscCmd_SoftwareRestart;
  1113. misc.Value = true;
  1114. PRINTF_FUNC("Announce Dispenser %d Software Reset: %d", dispenserIndex + 1, misc.Value);
  1115. AddMiscCommand(&sendBuffer, &misc);
  1116. }
  1117. }
  1118. }
  1119. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1120. }
  1121. void WriteCsuVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1122. {
  1123. struct PACKET_STRUCTURE sendBuffer;
  1124. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1125. sendBuffer.Header.se = packet->Header.se;
  1126. sendBuffer.Header.id = packet->Header.id;
  1127. sendBuffer.Header.op = _Header_Response;
  1128. sendBuffer.Header.len = 2;
  1129. sendBuffer.Payload.reg = _Reg_Report_Csu_Version;
  1130. sendBuffer.Payload.data[0] = result;
  1131. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1132. }
  1133. void WriteOtherVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1134. {
  1135. struct PACKET_STRUCTURE sendBuffer;
  1136. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1137. sendBuffer.Header.se = packet->Header.se;
  1138. sendBuffer.Header.id = packet->Header.id;
  1139. sendBuffer.Header.op = _Header_Response;
  1140. sendBuffer.Header.len = 2;
  1141. sendBuffer.Payload.reg = _Reg_Report_Other_Version;
  1142. sendBuffer.Payload.data[0] = result;
  1143. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1144. }
  1145. void WriteChargingInfoResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1146. {
  1147. struct PACKET_STRUCTURE sendBuffer;
  1148. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1149. sendBuffer.Header.se = packet->Header.se;
  1150. sendBuffer.Header.id = packet->Header.id;
  1151. sendBuffer.Header.op = _Header_Response;
  1152. sendBuffer.Header.len = 2;
  1153. sendBuffer.Payload.reg = _Reg_Charging_Info;
  1154. sendBuffer.Payload.data[0] = result;
  1155. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1156. }
  1157. void ChargerSystemIdResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1158. {
  1159. struct PACKET_STRUCTURE sendBuffer;
  1160. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1161. sendBuffer.Header.se = packet->Header.se;
  1162. sendBuffer.Header.id = packet->Header.id;
  1163. sendBuffer.Header.op = _Header_Response;
  1164. sendBuffer.Header.len = 2;
  1165. sendBuffer.Payload.reg = _Reg_Charger_System_Id;
  1166. sendBuffer.Payload.data[0] = result;
  1167. if(result == _R_OK)
  1168. {
  1169. int strLen = 0;
  1170. strLen = strlen((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1171. memcpy(&sendBuffer.Payload.data[sendBuffer.Header.len - 1], (char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, strLen);
  1172. sendBuffer.Header.len += strLen + 1;
  1173. char TimeSync[32];
  1174. struct timeb SeqEndTime;
  1175. struct tm *tm;
  1176. ftime(&SeqEndTime);
  1177. SeqEndTime.time = time(NULL);
  1178. tm=localtime(&SeqEndTime.time);
  1179. memset(TimeSync, 0x00, 32);
  1180. 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);
  1181. strLen = strlen(TimeSync);
  1182. memcpy(&sendBuffer.Payload.data[sendBuffer.Header.len - 1], TimeSync, strLen);
  1183. sendBuffer.Header.len += strLen + 1;
  1184. //PRINTF_FUNC("Dispenser %d Read System Id: %s", dispenserIndex + 1, ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1185. }
  1186. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1187. }
  1188. void WriteWaitPlugInResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1189. {
  1190. struct PACKET_STRUCTURE sendBuffer;
  1191. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1192. sendBuffer.Header.se = packet->Header.se;
  1193. sendBuffer.Header.id = packet->Header.id;
  1194. sendBuffer.Header.op = _Header_Response;
  1195. sendBuffer.Header.len = 2;
  1196. sendBuffer.Payload.reg = _Reg_WaitPlugIn;
  1197. sendBuffer.Payload.data[0] = result;
  1198. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1199. }
  1200. BOOL FindConnectorID(unsigned char dispenserIndex, unsigned char id)
  1201. {
  1202. BOOL find = false;
  1203. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1204. {
  1205. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] == id)
  1206. {
  1207. find = true;
  1208. break;
  1209. }
  1210. }
  1211. return find;
  1212. }
  1213. void ConnectorPhysicalLimitBindingHandler(unsigned char connectorIndex, unsigned char physical)
  1214. {
  1215. //char modelList[6] = {'J', 'U', 'E', 'G', 'V', 'F'};
  1216. //unsigned char typeList[6] = {0, 1, 1, 2, 1, 1}; // 0 : Chademo, 1: CCS, 2: GB
  1217. switch(physical)
  1218. {
  1219. case 'J':
  1220. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CHA_MAX_PHYSICAL_VOLTAGE;
  1221. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CHA_NATURAL_MAX_CURRENT;
  1222. break;
  1223. case 'U':
  1224. case 'E':
  1225. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CCS_MAX_PHYSICAL_VOLTAGE;
  1226. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CCS_NATURAL_MAX_CURRENT;
  1227. break;
  1228. case 'G':
  1229. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = GBT_MAX_PHYSICAL_VOLTAGE;
  1230. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = GBT_NATURAL_MAX_CURRENT;
  1231. break;
  1232. case 'V':
  1233. case 'F':
  1234. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CCS_MAX_PHYSICAL_VOLTAGE;
  1235. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CCS_LIQUID_MAX_CURRENT;
  1236. break;
  1237. default:
  1238. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = 0;
  1239. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = 0;
  1240. break;
  1241. }
  1242. }
  1243. void ConnectorTypeBindingHandler(unsigned char dispenserIndex, unsigned char *type, unsigned char *physicalType)
  1244. {
  1245. char *str_gun_type[] = {STR_GUN_TYPE_CHADEMO, STR_GUN_TYPE_CCS, STR_GUN_TYPE_GBT};
  1246. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1247. {
  1248. unsigned char gunIndex = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  1249. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex = dispenserIndex;
  1250. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.Index = gunIndex;
  1251. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.Type = type[i];
  1252. ConnectorPhysicalLimitBindingHandler(gunIndex, physicalType[i]);
  1253. PRINTF_FUNC("Dispenser %d Connector %d type %s, MaxVol: %4d, MaxCur: %4d",
  1254. dispenserIndex + 1, gunIndex + 1, str_gun_type[type[i]],
  1255. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalVoltage,
  1256. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalCurrent);
  1257. }
  1258. }
  1259. BOOL IsAvailableDispenserIndexSequence(unsigned char index)
  1260. {
  1261. if((ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status & (1 << index)) > 0)
  1262. {
  1263. // dispenser ever checkin
  1264. return true;
  1265. }
  1266. else
  1267. {
  1268. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  1269. {
  1270. if(i == index)
  1271. {
  1272. return true;
  1273. }
  1274. if((ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status & (1 << index)) == 0)
  1275. {
  1276. // i is not check in yet before index
  1277. return false;
  1278. }
  1279. }
  1280. }
  1281. return false;
  1282. }
  1283. void DispenserCheckInInfoUpdate(void)
  1284. {
  1285. unsigned char dispenser = 0, connector = 0;
  1286. BOOL change = false;
  1287. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  1288. {
  1289. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  1290. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  1291. {
  1292. dispenser++;
  1293. ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status |= (1 << i);
  1294. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[i] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity;
  1295. connector += ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity;
  1296. }
  1297. }
  1298. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity != dispenser ||
  1299. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity != connector)
  1300. {
  1301. change = true;
  1302. }
  1303. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity = dispenser;
  1304. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity = connector;
  1305. if(change)
  1306. {
  1307. PRINTF_FUNC("Total Dispenser: %d, Total Connector: %d\n",
  1308. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity,
  1309. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity);
  1310. }
  1311. }
  1312. void SetConnectorID(unsigned char dispenserIndex, unsigned char quantity)
  1313. {
  1314. unsigned char currentQuantity = 0;
  1315. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] = 0x00;
  1316. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[1] = 0x00;
  1317. for(int i = 0; i < dispenserIndex; i++)
  1318. {
  1319. currentQuantity += ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[i];
  1320. }
  1321. for(int i = 0; i < quantity; i++)
  1322. {
  1323. currentQuantity++;
  1324. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] = currentQuantity;
  1325. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[currentQuantity - 1].Enable = true;
  1326. }
  1327. }
  1328. BOOL DispenserIdentificationHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char *modelName)
  1329. {
  1330. unsigned char quantity = 0;
  1331. unsigned char connectorType[2] = {0, 0};
  1332. unsigned char physicalType[2] = {0, 0};
  1333. quantity = ConnectorQuantityTypeParsing(modelName, connectorType, physicalType);
  1334. if(quantity > 0)
  1335. {
  1336. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_Timeout &&
  1337. quantity != ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[dispenserIndex])
  1338. {
  1339. PRINTF_FUNC("The same dispenser id (%d) but connector quantity not match", dispenserIndex + 1);
  1340. return false;
  1341. }
  1342. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_None ||
  1343. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_Timeout)
  1344. {
  1345. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Identification;
  1346. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity = quantity;
  1347. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[dispenserIndex] = quantity;
  1348. SetConnectorID(dispenserIndex, quantity);
  1349. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName, packet->Payload.data, (packet->Header.len - 2));
  1350. ConnectorTypeBindingHandler(dispenserIndex, connectorType, physicalType);
  1351. PRINTF_FUNC("Dispenser id %d identified, Connector quantity: %d\n", dispenserIndex + 1, quantity);
  1352. DispenserCheckInInfoUpdate();
  1353. return true;
  1354. }
  1355. else
  1356. {
  1357. PRINTF_FUNC("Conflict dispenser ID: %d", dispenserIndex + 1);
  1358. }
  1359. }
  1360. else
  1361. {
  1362. PRINTF_FUNC("Connector quantity fail, model name: %s", modelName);
  1363. }
  1364. return false;
  1365. }
  1366. BOOL DispenserStatusCodeHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1367. {
  1368. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1369. if(find)
  1370. {
  1371. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount = (packet->Header.len - 1) / 6;
  1372. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount > 10)
  1373. {
  1374. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount = 10;
  1375. }
  1376. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  1377. {
  1378. memcpy(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i][0],
  1379. &packet->Payload.data[i * 6], 6);
  1380. }
  1381. /*
  1382. BOOL needUpdate = FALSE;
  1383. // check is there new status code
  1384. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  1385. {
  1386. if(strcmp(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i][0],
  1387. &LastWarningInfo[packet->Header.id - 1].WarningCode[i][0]) == 0)
  1388. {
  1389. needUpdate = TRUE;
  1390. break;
  1391. }
  1392. }
  1393. if(needUpdate)
  1394. {
  1395. memcpy(&LastWarningInfo[packet->Header.id - 1], &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo, sizeof(struct WARNING_CODE_INFO));
  1396. }
  1397. */
  1398. char strDispenserStatus[128];
  1399. sprintf(strDispenserStatus, "Dispenser id %d, Status Code Len: %d", packet->Header.id, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount);
  1400. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount > 0)
  1401. {
  1402. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  1403. {
  1404. char strTemp[16];
  1405. sprintf(strTemp, ", %s", ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i]);
  1406. strcat(strDispenserStatus, strTemp);
  1407. }
  1408. }
  1409. PRINTF_FUNC(strDispenserStatus);
  1410. }
  1411. else
  1412. {
  1413. }
  1414. return find;
  1415. }
  1416. BOOL ConnectorChargingCapabilityHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1417. {
  1418. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1419. BOOL done = false;
  1420. if(find)
  1421. {
  1422. done = true;
  1423. }
  1424. return done;
  1425. }
  1426. unsigned long GetTimeoutValue(struct timeval _sour_time)
  1427. {
  1428. struct timeval _end_time;
  1429. gettimeofday(&_end_time, NULL);
  1430. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  1431. }
  1432. BOOL ConnectorChargingTargetHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1433. {
  1434. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1435. BOOL done = false;
  1436. unsigned short voltage = 0, current = 0;
  1437. if(find)
  1438. {
  1439. voltage = ((packet->Payload.data[0] << 8) + packet->Payload.data[1]);
  1440. current = ((packet->Payload.data[2] << 8) + packet->Payload.data[3]);
  1441. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage != voltage ||
  1442. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent != current)
  1443. {
  1444. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage = voltage;
  1445. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent = current;
  1446. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable)
  1447. {
  1448. PRINTF_FUNC("Connector %d Remote Voltage: %d, Remote Current: %d", packet->Header.id, voltage, current);
  1449. }
  1450. }
  1451. GetPhysicalLimitVoltageAndCurrent(packet->Header.id - 1, &voltage, &current);
  1452. GetConfigLimitVoltageAndCurrent(packet->Header.id - 1, &voltage, &current);
  1453. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage != voltage ||
  1454. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent != current)
  1455. {
  1456. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable)
  1457. {
  1458. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable = true;
  1459. PRINTF_FUNC("Connector %d Remote Voltage: %d, Remote Current: %d", packet->Header.id,
  1460. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage,
  1461. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent);
  1462. PRINTF_FUNC("Connector %d Limit Voltage: %d, Limit Current: %d", packet->Header.id, voltage, current);
  1463. }
  1464. }
  1465. else
  1466. {
  1467. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable = false;
  1468. }
  1469. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitVoltage = voltage;
  1470. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitCurrent = current;
  1471. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus == S_PREPARING_FOR_EVSE ||
  1472. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus == S_CHARGING)
  1473. {
  1474. float targetVol = (float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitVoltage / 10);
  1475. float targetCur = (float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitCurrent / 10);
  1476. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage != targetVol ||
  1477. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent != targetCur)
  1478. {
  1479. PRINTF_FUNC("Connector %d Target Voltage: %d, Current: %d",
  1480. packet->Header.id, (int)(targetVol * 10), (int)(targetCur * 10));
  1481. }
  1482. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage = targetVol;
  1483. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent = targetCur;
  1484. done = true;
  1485. }
  1486. else
  1487. {
  1488. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage != 0 ||
  1489. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent != 0)
  1490. {
  1491. PRINTF_FUNC("Connector %d Not Charging Status, Set Voltage & Current Fail", packet->Header.id);
  1492. }
  1493. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage = 0;
  1494. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent = 0;
  1495. }
  1496. }
  1497. else
  1498. {
  1499. }
  1500. return done;
  1501. }
  1502. unsigned char FirmwareUpgradeHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1503. {
  1504. unsigned char UpgradeResponse = _R_NoUpgrade;
  1505. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] == packet->Header.id)
  1506. {
  1507. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateRequest)
  1508. {
  1509. if(packet->Payload.data[0] == 0)
  1510. {
  1511. UpgradeResponse = _R_NeedUpgrade;
  1512. }
  1513. else
  1514. {
  1515. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm)
  1516. {
  1517. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm = true;
  1518. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateRequest = false;
  1519. PRINTF_FUNC("Dispenser %d Updating...", dispenserIndex + 1);
  1520. }
  1521. }
  1522. }
  1523. else
  1524. {
  1525. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm)
  1526. {
  1527. if(packet->Payload.data[0] == 0)
  1528. {
  1529. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateCompleted)
  1530. {
  1531. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateCompleted = true;
  1532. PRINTF_FUNC("Dispenser %d Upgrade Completed", dispenserIndex + 1);
  1533. }
  1534. }
  1535. }
  1536. }
  1537. }
  1538. return UpgradeResponse;
  1539. }
  1540. BOOL ConnectorPlugInHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1541. {
  1542. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1543. if(find)
  1544. {
  1545. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn != packet->Payload.data[0])
  1546. {
  1547. PRINTF_FUNC("Connector %d %s", packet->Header.id, packet->Payload.data[0] == _PIS_PluggedIn ? "Pluged In" : "Un Plugged");
  1548. }
  1549. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn = packet->Payload.data[0];
  1550. }
  1551. else
  1552. {
  1553. }
  1554. return find;
  1555. }
  1556. BOOL ConnectorStateHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1557. {
  1558. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1559. if(find)
  1560. {
  1561. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus != packet->Payload.data[0])
  1562. {
  1563. PRINTF_FUNC("Connector %d Remote Status: %d", packet->Header.id, packet->Payload.data[0]);
  1564. if((packet->Payload.data[0] == _CRS_Idle && ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus != _CRS_Terminating) ||
  1565. packet->Payload.data[0] == _CRS_Terminating)
  1566. {
  1567. PRINTF_FUNC("*********** Connector id %d Set Stop Flag ***********\n", packet->Header.id);
  1568. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ChargingStopFlag.bits.NormalStop = true;
  1569. }
  1570. }
  1571. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus = packet->Payload.data[0];
  1572. }
  1573. else
  1574. {
  1575. }
  1576. return true;
  1577. }
  1578. unsigned char UserIDHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1579. {
  1580. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1581. DispenserAck_Status authorize = _DAS_Wait;
  1582. if(find)
  1583. {
  1584. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AuthorizeStatus == _AuthorizeStatus_Idle)
  1585. {
  1586. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AuthorizeRequest &&
  1587. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].AuthorizeStatus == _AuthorizeStatus_Idle)
  1588. {
  1589. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId, 0x00, 32);
  1590. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId, packet->Payload.data, packet->Header.len - 1);
  1591. PRINTF_FUNC("Dispenser %d connector %d user id %s need authorize", dispenserIndex + 1, packet->Header.id, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId);
  1592. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AuthorizeRequest = true;
  1593. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.AuthorizeTargetID = packet->Header.id;
  1594. }
  1595. authorize = _DAS_Wait;
  1596. }
  1597. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AuthorizeStatus == _AuthorizeStatus_Fail)
  1598. {
  1599. PRINTF_FUNC("Dispenser %d connector %d user id %s authorizing fail", dispenserIndex + 1, packet->Header.id, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId);
  1600. authorize = _DAS_NotAllowed;
  1601. }
  1602. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AuthorizeStatus == _AuthorizeStatus_Pass)
  1603. {
  1604. PRINTF_FUNC("Dispenser %d connector %d user id %s authorizing ok", dispenserIndex + 1, packet->Header.id, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId);
  1605. authorize = _DAS_Allowed;
  1606. }
  1607. else
  1608. {
  1609. authorize = _DAS_Wait;
  1610. }
  1611. }
  1612. else
  1613. {
  1614. authorize = _DAS_NotAllowed;
  1615. }
  1616. return authorize;
  1617. }
  1618. unsigned char ChargingPermissionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1619. {
  1620. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1621. DispenserAck_Status permission = _DAS_Wait;
  1622. if(find)
  1623. {
  1624. switch(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus)
  1625. {
  1626. case S_IDLE:
  1627. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Idle ||
  1628. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing)
  1629. {
  1630. permission = _DAS_Wait;
  1631. }
  1632. else
  1633. {
  1634. permission = _DAS_NotAllowed;
  1635. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  1636. {
  1637. PRINTF_FUNC("Cabinet status idle, not allow dispenser %d connector %d charging",
  1638. dispenserIndex + 1, packet->Header.id);
  1639. }
  1640. }
  1641. break;
  1642. case S_AUTHORIZING:
  1643. case S_REASSIGN_CHECK:
  1644. case S_REASSIGN:
  1645. case S_PREPARNING:
  1646. permission = _DAS_Wait;
  1647. break;
  1648. case S_PREPARING_FOR_EV:
  1649. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStopRequest ||
  1650. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopRequest)
  1651. {
  1652. permission = _DAS_NotAllowed;
  1653. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  1654. {
  1655. PRINTF_FUNC("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  1656. }
  1657. }
  1658. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus >= _CRS_Preparing &&
  1659. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus <= _CRS_Charging)
  1660. {
  1661. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.PermissionRequest)
  1662. {
  1663. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.PermissionRequest = true;
  1664. PRINTF_FUNC("Allow dispenser index %d connector id %d start preparing to charge", dispenserIndex, packet->Header.id);
  1665. }
  1666. permission = _DAS_Allowed;
  1667. }
  1668. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Terminating)
  1669. {
  1670. permission = _DAS_NotAllowed;
  1671. PRINTF_FUNC("Dispenser Terminating, Not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  1672. }
  1673. else
  1674. {
  1675. // keep wait
  1676. }
  1677. break;
  1678. case S_PREPARING_FOR_EVSE:
  1679. permission = _DAS_Allowed;
  1680. break;
  1681. case S_CHARGING:
  1682. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStopRequest ||
  1683. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopRequest)
  1684. {
  1685. permission = _DAS_NotAllowed;
  1686. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  1687. {
  1688. PRINTF_FUNC("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d charging", dispenserIndex, packet->Header.id);
  1689. }
  1690. }
  1691. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing ||
  1692. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Charging)
  1693. {
  1694. permission = _DAS_Allowed;
  1695. }
  1696. else
  1697. {
  1698. permission = _DAS_NotAllowed;
  1699. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  1700. {
  1701. PRINTF_FUNC("Dispenser index %d connector id %d remote status not in charging mode", dispenserIndex, packet->Header.id);
  1702. }
  1703. }
  1704. break;
  1705. case S_TERMINATING:
  1706. case S_COMPLETE:
  1707. case S_ALARM:
  1708. case S_FAULT:
  1709. case S_RESERVATION:
  1710. case S_MAINTAIN:
  1711. permission = _DAS_NotAllowed;
  1712. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  1713. {
  1714. PRINTF_FUNC("Cabinet status(%d) not match, not allow dispenser index %d connector id %d charging",
  1715. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus,
  1716. dispenserIndex, packet->Header.id);
  1717. }
  1718. break;
  1719. default:
  1720. break;
  1721. }
  1722. }
  1723. else
  1724. {
  1725. PRINTF_FUNC("Dispenser index %d Connector id %d not found", dispenserIndex, packet->Header.id);
  1726. permission = _DAS_NotAllowed;
  1727. }
  1728. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission = permission;
  1729. // 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",
  1730. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus,
  1731. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus,
  1732. // ShmSysConfigAndInfo->SysInfo.WaitForPlugit,
  1733. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn,
  1734. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.IsAvailable,
  1735. // ShmSysConfigAndInfo->SysInfo.OrderCharging);
  1736. return permission;
  1737. }
  1738. BOOL MiscControlHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1739. {
  1740. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1741. BOOL done = false;
  1742. if(find)
  1743. {
  1744. done = true;
  1745. }
  1746. return done;
  1747. }
  1748. unsigned char DispenserWriteCsuVersionInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1749. {
  1750. char version[32];
  1751. BOOL writeVersion = FALSE, showVersion = FALSE;;
  1752. memcpy(version, &packet->Payload.data[0], 32);
  1753. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev, version) != 0)
  1754. {
  1755. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev, version, 32);
  1756. showVersion = true;
  1757. }
  1758. memcpy(version, &packet->Payload.data[32], 32);
  1759. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev, version) != 0)
  1760. {
  1761. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev, version, 32);
  1762. showVersion = true;
  1763. }
  1764. memcpy(version, &packet->Payload.data[64], 32);
  1765. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev, version) != 0)
  1766. {
  1767. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev, version, 32);
  1768. showVersion = true;
  1769. }
  1770. memcpy(version, &packet->Payload.data[96], 32);
  1771. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev, version) != 0)
  1772. {
  1773. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev, version, 32);
  1774. showVersion = true;
  1775. }
  1776. if(showVersion)
  1777. {
  1778. PRINTF_FUNC("********** Dispenser id %d: Csu Version information **********", dispenserIndex + 1);
  1779. PRINTF_FUNC("********** CsuBootLoadRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev);
  1780. PRINTF_FUNC("********** CsuKernelFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev);
  1781. PRINTF_FUNC("********** CsuRootFsFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev);
  1782. PRINTF_FUNC("********** CsuPrimFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev);
  1783. }
  1784. writeVersion = TRUE;
  1785. return writeVersion;
  1786. }
  1787. unsigned char DispenserWriteOtherVersionInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1788. {
  1789. char version[32];
  1790. BOOL writeVersion = FALSE, showVersion = FALSE;;
  1791. memcpy(version, &packet->Payload.data[0], 32);
  1792. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev, version) != 0)
  1793. {
  1794. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev, version, 32);
  1795. showVersion = true;
  1796. }
  1797. memcpy(version, &packet->Payload.data[32], 32);
  1798. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev, version) != 0)
  1799. {
  1800. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev, version, 32);
  1801. showVersion = true;
  1802. }
  1803. memcpy(version, &packet->Payload.data[64], 32);
  1804. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev, version) != 0)
  1805. {
  1806. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev, version, 32);
  1807. showVersion = true;
  1808. }
  1809. memcpy(version, &packet->Payload.data[96], 32);
  1810. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev, version) != 0)
  1811. {
  1812. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev, version, 32);
  1813. showVersion = true;
  1814. }
  1815. memcpy(version, &packet->Payload.data[128], 32);
  1816. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev, version) != 0)
  1817. {
  1818. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev, version, 32);
  1819. showVersion = true;
  1820. }
  1821. if(showVersion)
  1822. {
  1823. PRINTF_FUNC("********** Dispenser id %d: Other Version information **********", dispenserIndex + 1);
  1824. PRINTF_FUNC("********** FanModuleFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev);
  1825. PRINTF_FUNC("********** RelayModuleRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev);
  1826. PRINTF_FUNC("********** Connector1Rev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev);
  1827. PRINTF_FUNC("********** Connector2Rev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev);
  1828. PRINTF_FUNC("********** LedModuleFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev);
  1829. }
  1830. writeVersion = TRUE;
  1831. return writeVersion;
  1832. }
  1833. unsigned char DispenserWriteChargingInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1834. {
  1835. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1836. if(find)
  1837. {
  1838. unsigned short voltage = (packet->Payload.data[0] << 8) + (packet->Payload.data[1]);
  1839. unsigned short current = (packet->Payload.data[2] << 8) + (packet->Payload.data[3]);
  1840. //unsigned int time = (packet->Payload.data[4] << 24) + (packet->Payload.data[5] << 16) + (packet->Payload.data[6] << 8) + (packet->Payload.data[7]);
  1841. unsigned char soc = (packet->Payload.data[8]);
  1842. if((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteChargingVoltage / 10) != (voltage / 10) ||
  1843. (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteChargingCurrent / 10) != (current / 10) ||
  1844. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteSoc != soc)
  1845. {
  1846. PRINTF_FUNC("Connector %d Charging Info: Voltage %4dV Current %4dA Soc %d\% ", packet->Header.id, voltage, current, soc);
  1847. }
  1848. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteChargingVoltage = voltage;
  1849. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteChargingCurrent = current;
  1850. //ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteRemainChargingDuration = time;
  1851. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteSoc = soc;
  1852. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterySoc = soc;
  1853. }
  1854. else
  1855. {
  1856. }
  1857. return find;
  1858. }
  1859. unsigned char ReadChargerSystemIdHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1860. {
  1861. BOOL find = TRUE;
  1862. return find;
  1863. }
  1864. unsigned char WriteWaitPlugInHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1865. {
  1866. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1867. if(find)
  1868. {
  1869. unsigned char StartWait = packet->Payload.data[0];
  1870. if(StartWait)
  1871. {
  1872. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug)
  1873. {
  1874. PRINTF_FUNC("Connector %d Start Wait Plug In", packet->Header.id);
  1875. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.SwipeRfidConfirm = true;
  1876. }
  1877. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = true;
  1878. }
  1879. else
  1880. {
  1881. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug)
  1882. {
  1883. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus != S_IDLE)
  1884. {
  1885. PRINTF_FUNC("Connector %d Stop Wait Plug In", packet->Header.id);
  1886. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.NeedCleanAuthorizeInfo = true;
  1887. }
  1888. }
  1889. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = false;
  1890. }
  1891. }
  1892. else
  1893. {
  1894. }
  1895. return find;
  1896. }
  1897. void DisableConnector(unsigned char dispenserIndex)
  1898. {
  1899. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1900. {
  1901. unsigned char connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  1902. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Enable = false;
  1903. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.TimeoutStopRequest = true;
  1904. }
  1905. }
  1906. void DispenserSocketProcess(int socketFd, struct sockaddr_in clientInfo, unsigned char index)
  1907. {
  1908. int rxLen = 0, timeout = 0;
  1909. unsigned char dispenserID = 0, dispenserIndex = 0;
  1910. struct PACKET_STRUCTURE receiveBuffer;
  1911. unsigned char modelName[64];
  1912. unsigned char ackResult = _R_NG;
  1913. DispenserAck_Status ack = _DAS_Wait;
  1914. PRINTF_FUNC("IP %s connection(%d) is established, start dispenser process", (inet_ntoa(clientInfo.sin_addr)), index);
  1915. while(1)
  1916. {
  1917. if((rxLen = recv(socketFd, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  1918. {
  1919. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  1920. {
  1921. ackResult = _R_NG;
  1922. //ShowSocketData(&receiveBuffer);
  1923. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_WaitModelName)
  1924. {
  1925. // Reg: 0x01, Dispenser model name
  1926. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name)
  1927. {
  1928. memset(modelName, 0x00, 64);
  1929. dispenserID = receiveBuffer.Payload.data[receiveBuffer.Header.len - 2];
  1930. if(dispenserID > 0 && dispenserID <= GENERAL_GUN_QUANTITY)
  1931. {
  1932. dispenserIndex = dispenserID - 1;
  1933. memcpy(modelName, receiveBuffer.Payload.data, receiveBuffer.Header.len - 2);
  1934. if(IsAvailableDispenserIndexSequence(dispenserIndex))
  1935. {
  1936. if(DispenserIdentificationHandler(&receiveBuffer, dispenserIndex, modelName) == true)
  1937. {
  1938. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].DispenserIndex = dispenserIndex;
  1939. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status = _CNS_DispenserMatched;
  1940. PRINTF_FUNC("********** Dispenser id %d check in, model name: %s **********\n", dispenserID, modelName);
  1941. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName, modelName, 64);
  1942. ackResult = _R_OK;
  1943. }
  1944. else
  1945. {
  1946. // response NG: gun quantity fail
  1947. PRINTF_FUNC("Dispenser ID %d Identification Fail", dispenserID);
  1948. }
  1949. }
  1950. else
  1951. {
  1952. // response NG: dispenser Index fail
  1953. PRINTF_FUNC("Dispenser ID %d Fail, Need Wait Another Dispenser", dispenserID);
  1954. }
  1955. }
  1956. else
  1957. {
  1958. PRINTF_FUNC("Dispenser ID: %d Over Range", dispenserID);
  1959. }
  1960. ModelNameResponse(socketFd, &receiveBuffer, ackResult);
  1961. }
  1962. else
  1963. {
  1964. // do nothing
  1965. }
  1966. }
  1967. else if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_DispenserMatched)
  1968. {
  1969. // Reg: 0x01, Dispenser model name
  1970. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name)
  1971. {
  1972. ackResult = _R_OK;
  1973. ModelNameResponse(socketFd, &receiveBuffer, ackResult);
  1974. }
  1975. // Reg: 0x02, Connector ID
  1976. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Connector_ID)
  1977. {
  1978. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity > 0)
  1979. {
  1980. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Idle;
  1981. ackResult = _R_OK;
  1982. }
  1983. PRINTF_FUNC("Dispenser ID %d Get Connector ID %s", dispenserID, ackResult == _R_OK ? "OK" : "NG");
  1984. ConnectorIDResponse(socketFd, &receiveBuffer, ackResult, dispenserIndex);
  1985. if(ackResult == _R_OK)
  1986. {
  1987. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DispenserConfigSync)
  1988. {
  1989. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DispenserConfigSync = true;
  1990. PRINTF_FUNC("Dispenser ID %d Configuration Need To Synchronize", dispenserID);
  1991. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.ConnectorTimeoutConfigRequest = 1;
  1992. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DefaultPriceConfigRequest = 1;
  1993. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.CurrencyConfigRequest = 1;
  1994. }
  1995. }
  1996. }
  1997. // Reg: 0x03, Power cabinet status
  1998. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Power_Cabinet_Status)
  1999. {
  2000. PowerCabinetStatusResponse(socketFd, &receiveBuffer);
  2001. }
  2002. // Reg: 0x04, Dispenser status
  2003. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Status)
  2004. {
  2005. if(DispenserStatusCodeHandler(&receiveBuffer, dispenserIndex))
  2006. {
  2007. ackResult = _R_OK;
  2008. }
  2009. DispenserStatusResponse(socketFd, &receiveBuffer, ackResult);
  2010. }
  2011. // Reg: 0x05, Charging capability
  2012. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charging_Capability)
  2013. {
  2014. if(ConnectorChargingCapabilityHandler(&receiveBuffer, dispenserIndex))
  2015. {
  2016. ChargingCapabilityResponse(socketFd, &receiveBuffer);
  2017. }
  2018. }
  2019. // Reg: 0x06, Charging target
  2020. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Charging_Target)
  2021. {
  2022. if(receiveBuffer.Header.len == 5)
  2023. {
  2024. if(ConnectorChargingTargetHandler(&receiveBuffer, dispenserIndex))
  2025. {
  2026. ackResult = _R_OK;
  2027. }
  2028. }
  2029. ChargingTargetResponse(socketFd, &receiveBuffer, ackResult);
  2030. }
  2031. // Reg: 0x07, Software update
  2032. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Software_Update)
  2033. {
  2034. unsigned char NeedUpgrade = FirmwareUpgradeHandler(&receiveBuffer, dispenserIndex);
  2035. FirmwareUpgradeResponse(socketFd, &receiveBuffer, dispenserIndex, NeedUpgrade);
  2036. }
  2037. // Reg: 0x08, Plug-in status
  2038. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Plug_In_Status)
  2039. {
  2040. if(receiveBuffer.Header.len == 2)
  2041. {
  2042. if(ConnectorPlugInHandler(&receiveBuffer, dispenserIndex))
  2043. {
  2044. ackResult = _R_OK;
  2045. }
  2046. }
  2047. PlugInStatusResponse(socketFd, &receiveBuffer, ackResult);
  2048. }
  2049. // Reg: 0x09, Connector state
  2050. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Connector_State)
  2051. {
  2052. if(ConnectorStateHandler(&receiveBuffer, dispenserIndex))
  2053. {
  2054. ackResult = _R_OK;
  2055. }
  2056. ConnectorStateResponse(socketFd, &receiveBuffer, ackResult);
  2057. }
  2058. // Reg: 0x0A, User ID
  2059. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_User_ID)
  2060. {
  2061. ack = UserIDHandler(&receiveBuffer, dispenserIndex);
  2062. UserIDResponse(socketFd, &receiveBuffer, ack);
  2063. }
  2064. // Reg: 0x0B, Charging permission
  2065. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charging_Permission)
  2066. {
  2067. ack = ChargingPermissionHandler(&receiveBuffer, dispenserIndex);
  2068. ChargingPermissionResponse(socketFd, &receiveBuffer, ack);
  2069. }
  2070. // Reg: 0x0C, Misc Control
  2071. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Misc_Control)
  2072. {
  2073. if(MiscControlHandler(&receiveBuffer, dispenserIndex))
  2074. {
  2075. ackResult = _R_OK;
  2076. }
  2077. MiscControlResponse(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  2078. }
  2079. // Reg: 0x0D, Report csu version
  2080. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Report_Csu_Version)
  2081. {
  2082. if(DispenserWriteCsuVersionInfoHandler(&receiveBuffer, dispenserIndex))
  2083. {
  2084. ackResult = _R_OK;
  2085. }
  2086. WriteCsuVersionResponse(socketFd, &receiveBuffer, ackResult);
  2087. }
  2088. // Reg: 0x0E, Report other version
  2089. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Report_Other_Version)
  2090. {
  2091. if(DispenserWriteOtherVersionInfoHandler(&receiveBuffer, dispenserIndex))
  2092. {
  2093. ackResult = _R_OK;
  2094. }
  2095. WriteOtherVersionResponse(socketFd, &receiveBuffer, ackResult);
  2096. }
  2097. // Reg: 0x0F, Charging info
  2098. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Charging_Info)
  2099. {
  2100. if(DispenserWriteChargingInfoHandler(&receiveBuffer, dispenserIndex))
  2101. {
  2102. ackResult = _R_OK;
  2103. }
  2104. WriteChargingInfoResponse(socketFd, &receiveBuffer, ackResult);
  2105. }
  2106. // Reg: 0x10, Charger system id
  2107. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charger_System_Id)
  2108. {
  2109. if(ReadChargerSystemIdHandler(&receiveBuffer, dispenserIndex))
  2110. {
  2111. ackResult = _R_OK;
  2112. }
  2113. ChargerSystemIdResponse(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  2114. }
  2115. // Reg: 0x11, Wait plug in
  2116. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_WaitPlugIn)
  2117. {
  2118. if(WriteWaitPlugInHandler(&receiveBuffer, dispenserIndex))
  2119. {
  2120. ackResult = _R_OK;
  2121. }
  2122. WriteWaitPlugInResponse(socketFd, &receiveBuffer, ackResult);
  2123. }
  2124. }
  2125. // clean timeout
  2126. timeout = 0;
  2127. }
  2128. }
  2129. else
  2130. {
  2131. timeout += SOCKET_RECEIVE_INTERVAL;
  2132. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  2133. }
  2134. if(timeout >= DISPENSER_SOCKET_TIMEOUT)
  2135. {
  2136. //timeout
  2137. PRINTF_FUNC("IP: %s, Socket %d connection timeout", (inet_ntoa(clientInfo.sin_addr)), index);
  2138. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_DispenserMatched)
  2139. {
  2140. DisableConnector(dispenserIndex);
  2141. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex], 0x00, sizeof(struct DispenserModule));
  2142. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Timeout;
  2143. DispenserCheckInInfoUpdate();
  2144. }
  2145. break;
  2146. }
  2147. }
  2148. }
  2149. int GetMacAddress(int sockFd, unsigned char *mac)
  2150. {
  2151. struct arpreq arpReq;
  2152. struct sockaddr_in clientInfo;
  2153. socklen_t len = sizeof(struct sockaddr_in);
  2154. memset(mac, 0x00, 6);
  2155. if(getpeername(sockFd, (struct sockaddr*)&clientInfo, &len) < 0)
  2156. {
  2157. return 0;
  2158. }
  2159. else
  2160. {
  2161. memcpy(&arpReq.arp_pa, &clientInfo, sizeof(struct sockaddr_in));
  2162. strcpy(arpReq.arp_dev, "eth1");
  2163. arpReq.arp_pa.sa_family = AF_INET;
  2164. arpReq.arp_ha.sa_family = AF_UNSPEC;
  2165. if(ioctl(sockFd, SIOCGARP, &arpReq) < 0)
  2166. {
  2167. return 0;
  2168. }
  2169. else
  2170. {
  2171. memcpy(mac, arpReq.arp_ha.sa_data, 6);
  2172. }
  2173. }
  2174. return 1;
  2175. }
  2176. int FindFreeChannel(void)
  2177. {
  2178. for(int i = 0; i < MAXIMUM_CONNECT_QUANTITY; i++)
  2179. {
  2180. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_FREE)
  2181. {
  2182. return i;
  2183. }
  2184. }
  2185. return FAIL;
  2186. }
  2187. BOOL IsConflictIp(uint32_t ipAddress)
  2188. {
  2189. for(int i = 0; i < MAXIMUM_CONNECT_QUANTITY; i++)
  2190. {
  2191. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress == ipAddress)
  2192. {
  2193. return true;
  2194. }
  2195. }
  2196. return false;
  2197. }
  2198. void InitDispenserInfo(void)
  2199. {
  2200. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  2201. {
  2202. //memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i], 0x00, sizeof(struct ConnectorInfoData));
  2203. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteStatus = 0;
  2204. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Enable = 0;
  2205. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ReadyToCharge = 0;
  2206. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex = 0;
  2207. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.Value = 0;
  2208. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AuthorizeStatus = 0;
  2209. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].WarningInfo, 0x00, sizeof(struct WARNING_CODE_INFO));
  2210. //memset(&LastWarningInfo[i], 0x00, sizeof(struct WARNING_CODE_INFO));
  2211. }
  2212. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo, 0x00, sizeof(struct DispenserInfoData));
  2213. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  2214. }
  2215. void tcpSocketServerStart(void)
  2216. {
  2217. int sockFd = 0;
  2218. int clientSockFd = 0;
  2219. int connectIndex = 0;
  2220. pid_t forkId;
  2221. struct sockaddr_in serverInfo, clientInfo;
  2222. socklen_t addrlen = sizeof(clientInfo);
  2223. //struct timeval timeout = {3, 0};
  2224. sockFd = socket(AF_INET, SOCK_STREAM, 0);
  2225. if(sockFd == -1)
  2226. {
  2227. PRINTF_FUNC("InitSocketServer NG\n");
  2228. sleep(5);
  2229. return;
  2230. }
  2231. InitDispenserInfo();
  2232. bzero(&serverInfo,sizeof(serverInfo));
  2233. serverInfo.sin_family = PF_INET;
  2234. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  2235. serverInfo.sin_port = htons(TCP_LISTEN_PORT);
  2236. bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo));
  2237. listen(sockFd, CONNECTION_LIMIT);
  2238. PRINTF_FUNC("TCP Server Start");
  2239. signal(SIGCHLD, SIG_IGN);
  2240. // Main loop
  2241. while(1)
  2242. {
  2243. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  2244. /*
  2245. if((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == ENONET) ||
  2246. (errno == EPROTO) || (errno == ENOPROTOOPT) || (errno == EOPNOTSUPP) ||
  2247. (errno == ENETDOWN) || (errno == ENETUNREACH) || (errno == EHOSTDOWN) ||
  2248. (errno == EHOSTUNREACH) || (errno == ECONNABORTED))
  2249. {
  2250. printf("accept error: %s\n", strerror(errno));
  2251. //exit(EXIT_FAILURE);
  2252. //break;
  2253. }
  2254. else if ( errno == EINTR)
  2255. {
  2256. printf("accept EINTRn");
  2257. continue;
  2258. }
  2259. else
  2260. {
  2261. printf("AccepCb: accept error: %s\n", strerror(errno));
  2262. //break;
  2263. //exit(EXIT_FAILURE);
  2264. }*/
  2265. PRINTF_FUNC("Client ip: %s is accepted at port: %d", inet_ntoa(clientInfo.sin_addr), clientInfo.sin_port);
  2266. connectIndex = FindFreeChannel();
  2267. if(connectIndex >= 0)
  2268. {
  2269. if(IsConflictIp(clientInfo.sin_addr.s_addr) == false)
  2270. {
  2271. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex].Status = _CNS_WaitModelName;
  2272. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex].IpAddress = clientInfo.sin_addr.s_addr;
  2273. forkId = fork();
  2274. if(forkId == 0)
  2275. {
  2276. // child process
  2277. DispenserSocketProcess(clientSockFd, clientInfo, connectIndex);
  2278. close(clientSockFd);
  2279. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex], 0x00, sizeof(struct ConnectionInfoData));
  2280. return;
  2281. }
  2282. else if(forkId == -1)
  2283. {
  2284. PRINTF_FUNC("fork fail");
  2285. }
  2286. }
  2287. else
  2288. {
  2289. // conflict ip address
  2290. PRINTF_FUNC("Conflict IP address, close socket");
  2291. close(clientSockFd);
  2292. }
  2293. }
  2294. else
  2295. {
  2296. // no free channel
  2297. PRINTF_FUNC("No free channel, close socket");
  2298. close(clientSockFd);
  2299. }
  2300. usleep(10000);
  2301. }
  2302. return;
  2303. }
  2304. int main(void)
  2305. {
  2306. if(InitShareMemory() == FAIL)
  2307. {
  2308. #ifdef SystemLogMessage
  2309. DEBUG_ERROR("InitShareMemory NG\n");
  2310. #endif
  2311. sleep(5);
  2312. return 0;
  2313. }
  2314. // wait for self test completed
  2315. while(ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING);
  2316. tcpSocketServerStart();
  2317. //tcpSocketClientStart();
  2318. return 0;
  2319. }