Module_EvComm.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  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)
  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. if(quantity < 2)
  764. {
  765. quantity++;
  766. }
  767. else
  768. {
  769. quantity = 0;
  770. break;
  771. }
  772. }
  773. }
  774. return quantity;
  775. }
  776. void ModelNameResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  777. {
  778. struct PACKET_STRUCTURE sendBuffer;
  779. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  780. sendBuffer.Header.se = packet->Header.se;
  781. sendBuffer.Header.id = 0xFF;
  782. sendBuffer.Header.op = _Header_Response;
  783. sendBuffer.Header.len = 2;
  784. sendBuffer.Payload.reg = _Reg_Dispenser_Model_Name;
  785. sendBuffer.Payload.data[0] = result;
  786. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  787. }
  788. void ConnectorIDResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result, unsigned char dispenserIndex)
  789. {
  790. struct PACKET_STRUCTURE sendBuffer;
  791. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  792. sendBuffer.Header.se = packet->Header.se;
  793. sendBuffer.Header.id = 0xFF;
  794. sendBuffer.Header.op = _Header_Response;
  795. sendBuffer.Header.len = 4;
  796. sendBuffer.Payload.reg = _Reg_Connector_ID;
  797. sendBuffer.Payload.data[0] = result;
  798. sendBuffer.Payload.data[1] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0];
  799. sendBuffer.Payload.data[2] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[1];
  800. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  801. }
  802. void PowerCabinetStatusResponse(int socket, struct PACKET_STRUCTURE *packet)
  803. {
  804. struct PACKET_STRUCTURE sendBuffer;
  805. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  806. sendBuffer.Header.se = packet->Header.se;
  807. sendBuffer.Header.id = packet->Header.id;
  808. sendBuffer.Header.op = _Header_Response;
  809. sendBuffer.Header.len = 2;
  810. sendBuffer.Payload.reg = _Reg_Power_Cabinet_Status;
  811. sendBuffer.Payload.data[0] = _R_OK;
  812. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0)
  813. {
  814. sendBuffer.Header.len += (ShmSysConfigAndInfo->SysWarningInfo.WarningCount * 6);
  815. for(int i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++)
  816. {
  817. memcpy(&sendBuffer.Payload.data[1 + (i * 6)], &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], 6);
  818. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] == '1')
  819. {
  820. sendBuffer.Payload.data[1 + (i * 6) + 1] = '4';
  821. }
  822. }
  823. }
  824. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  825. }
  826. void DispenserStatusResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  827. {
  828. struct PACKET_STRUCTURE sendBuffer;
  829. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  830. sendBuffer.Header.se = packet->Header.se;
  831. sendBuffer.Header.id = packet->Header.id;
  832. sendBuffer.Header.op = _Header_Response;
  833. sendBuffer.Header.len = 2;
  834. sendBuffer.Payload.reg = _Reg_Dispenser_Status;
  835. sendBuffer.Payload.data[0] = result;
  836. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  837. }
  838. struct ChargingCapabilityResponse ConnectorCapability[GENERAL_GUN_QUANTITY];
  839. void ChargingCapabilityResponse(int socket, struct PACKET_STRUCTURE *packet)
  840. {
  841. struct PACKET_STRUCTURE sendBuffer;
  842. unsigned short voltage = 0, current = 0, power = 0;
  843. unsigned char currency = 0;
  844. unsigned int price = 0, cost = 0, account = 0;
  845. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  846. voltage = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.MaximumChargingVoltage;
  847. current = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.AvailableChargingCurrent;
  848. power = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.AvailableChargingPower;
  849. currency = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency;
  850. price = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].UserPrice;
  851. cost = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].TotalCost;
  852. account = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AccountBalance;
  853. if(ConnectorCapability[packet->Header.id - 1].MaxOuputVoltage != voltage ||
  854. ConnectorCapability[packet->Header.id - 1].MaxOuputCurrent != current ||
  855. ConnectorCapability[packet->Header.id - 1].MaxOuputPower != power)
  856. {
  857. PRINTF_FUNC("Connector %d Capability Voltage: %d, Current: %d, Power: %d", packet->Header.id, (int)(voltage), (int)(current), (int)(power));
  858. }
  859. if(ConnectorCapability[packet->Header.id - 1].Currency != currency ||
  860. ConnectorCapability[packet->Header.id - 1].UserPrice != price ||
  861. ConnectorCapability[packet->Header.id - 1].TotalCost != cost ||
  862. ConnectorCapability[packet->Header.id - 1].AccountBalance != account)
  863. {
  864. PRINTF_FUNC("Connector %d Currency: %d, UserPrice: %d, Cost: %d, Account: %d", packet->Header.id, (int)(currency), (int)(price), (int)(cost), (int)(account));
  865. }
  866. ConnectorCapability[packet->Header.id - 1].MaxOuputVoltage = voltage;
  867. ConnectorCapability[packet->Header.id - 1].MaxOuputCurrent = current;
  868. ConnectorCapability[packet->Header.id - 1].MaxOuputPower = power;
  869. ConnectorCapability[packet->Header.id - 1].Currency = currency;
  870. ConnectorCapability[packet->Header.id - 1].UserPrice = price;
  871. ConnectorCapability[packet->Header.id - 1].TotalCost = cost;
  872. ConnectorCapability[packet->Header.id - 1].AccountBalance = account;
  873. sendBuffer.Header.se = packet->Header.se;
  874. sendBuffer.Header.id = packet->Header.id;
  875. sendBuffer.Header.op = _Header_Response;
  876. sendBuffer.Header.len = 21;
  877. sendBuffer.Payload.reg = _Reg_Charging_Capability;
  878. sendBuffer.Payload.data[0] = _R_OK;
  879. sendBuffer.Payload.data[1] = ((voltage >> 8) & 0xFF);
  880. sendBuffer.Payload.data[2] = (voltage & 0xFF);
  881. sendBuffer.Payload.data[3] = ((current >> 8) & 0xFF);
  882. sendBuffer.Payload.data[4] = (current & 0xFF);
  883. sendBuffer.Payload.data[5] = ((power >> 8) & 0xFF);
  884. sendBuffer.Payload.data[6] = (power & 0xFF);
  885. sendBuffer.Payload.data[7] = currency;
  886. sendBuffer.Payload.data[8] = ((price >> 24) & 0xFF);
  887. sendBuffer.Payload.data[9] = ((price >> 16) & 0xFF);
  888. sendBuffer.Payload.data[10] = ((price >> 8) & 0xFF);
  889. sendBuffer.Payload.data[11] = (price & 0xFF);
  890. sendBuffer.Payload.data[12] = ((cost >> 24) & 0xFF);
  891. sendBuffer.Payload.data[13] = ((cost >> 16) & 0xFF);
  892. sendBuffer.Payload.data[14] = ((cost >> 8) & 0xFF);
  893. sendBuffer.Payload.data[15] = (cost & 0xFF);
  894. sendBuffer.Payload.data[16] = ((account >> 24) & 0xFF);
  895. sendBuffer.Payload.data[17] = ((account >> 16) & 0xFF);
  896. sendBuffer.Payload.data[18] = ((account >> 8) & 0xFF);
  897. sendBuffer.Payload.data[19] = (account & 0xFF);
  898. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  899. }
  900. void ChargingTargetResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  901. {
  902. struct PACKET_STRUCTURE sendBuffer;
  903. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  904. sendBuffer.Header.se = packet->Header.se;
  905. sendBuffer.Header.id = packet->Header.id;
  906. sendBuffer.Header.op = _Header_Response;
  907. sendBuffer.Header.len = 2;
  908. sendBuffer.Payload.reg = _Reg_Charging_Target;
  909. sendBuffer.Payload.data[0] = result;
  910. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  911. }
  912. void FirmwareUpgradeResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  913. {
  914. struct PACKET_STRUCTURE sendBuffer;
  915. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  916. sendBuffer.Header.se = packet->Header.se;
  917. sendBuffer.Header.id = packet->Header.id;
  918. sendBuffer.Header.op = _Header_Response;
  919. sendBuffer.Header.len = 2;
  920. sendBuffer.Payload.reg = _Reg_Software_Update;
  921. sendBuffer.Payload.data[0] = _R_OK;
  922. sendBuffer.Payload.data[1] = result;
  923. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  924. }
  925. void PlugInStatusResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  926. {
  927. struct PACKET_STRUCTURE sendBuffer;
  928. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  929. sendBuffer.Header.se = packet->Header.se;
  930. sendBuffer.Header.id = packet->Header.id;
  931. sendBuffer.Header.op = _Header_Response;
  932. sendBuffer.Header.len = 2;
  933. sendBuffer.Payload.reg = _Reg_Plug_In_Status;
  934. sendBuffer.Payload.data[0] = result;
  935. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  936. }
  937. void ConnectorStateResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  938. {
  939. struct PACKET_STRUCTURE sendBuffer;
  940. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  941. sendBuffer.Header.se = packet->Header.se;
  942. sendBuffer.Header.id = packet->Header.id;
  943. sendBuffer.Header.op = _Header_Response;
  944. sendBuffer.Header.len = 2;
  945. sendBuffer.Payload.reg = _Reg_Connector_State;
  946. sendBuffer.Payload.data[0] = result;
  947. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  948. }
  949. void UserIDResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  950. {
  951. struct PACKET_STRUCTURE sendBuffer;
  952. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  953. sendBuffer.Header.se = packet->Header.se;
  954. sendBuffer.Header.id = packet->Header.id;
  955. sendBuffer.Header.op = _Header_Response;
  956. sendBuffer.Header.len = 3;
  957. sendBuffer.Payload.reg = _Reg_User_ID;
  958. sendBuffer.Payload.data[0] = result == _DAS_Wait ? _R_NG : _R_OK;
  959. sendBuffer.Payload.data[1] = result == _DAS_Allowed ? _PS_Permitted : _PS_NotPermitted;
  960. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  961. }
  962. void ChargingPermissionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  963. {
  964. struct PACKET_STRUCTURE sendBuffer;
  965. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  966. sendBuffer.Header.se = packet->Header.se;
  967. sendBuffer.Header.id = packet->Header.id;
  968. sendBuffer.Header.op = _Header_Response;
  969. sendBuffer.Header.len = 3;
  970. sendBuffer.Payload.reg = _Reg_Charging_Permission;
  971. sendBuffer.Payload.data[0] = result == _DAS_Wait ? _R_NG : _R_OK;
  972. sendBuffer.Payload.data[1] = result == _DAS_Allowed ? _PS_Permitted : _PS_NotPermitted;
  973. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  974. }
  975. void AddMiscCommand(struct PACKET_STRUCTURE *packet, struct MISC_COMMAND *misc)
  976. {
  977. packet->Payload.data[packet->Header.len - 1 + 0] = ((misc->Command >> 8) & 0xFF);
  978. packet->Payload.data[packet->Header.len - 1 + 1] = ((misc->Command >> 0) & 0xFF);
  979. packet->Payload.data[packet->Header.len - 1 + 2] = ((misc->Value >> 24) & 0xFF);
  980. packet->Payload.data[packet->Header.len - 1 + 3] = ((misc->Value >> 16) & 0xFF);
  981. packet->Payload.data[packet->Header.len - 1 + 4] = ((misc->Value >> 8) & 0xFF);
  982. packet->Payload.data[packet->Header.len - 1 + 5] = ((misc->Value >> 0) & 0xFF);
  983. packet->Header.len += 6;
  984. }
  985. void MiscControlResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  986. {
  987. struct PACKET_STRUCTURE sendBuffer;
  988. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  989. sendBuffer.Header.se = packet->Header.se;
  990. sendBuffer.Header.id = packet->Header.id;
  991. sendBuffer.Header.op = _Header_Response;
  992. sendBuffer.Header.len = 2;
  993. sendBuffer.Payload.reg = _Reg_Misc_Control;
  994. sendBuffer.Payload.data[0] = result;
  995. if(result == _R_OK)
  996. {
  997. struct MISC_COMMAND misc;
  998. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AvailabilityRequest)
  999. {
  1000. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AvailabilityRequest = false;
  1001. misc.Command = _MiscCmd_ChangeOperative;
  1002. misc.Value = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.IsAvailable;
  1003. PRINTF_FUNC("Announce Connector %d Availability: %d", packet->Header.id, misc.Value);
  1004. AddMiscCommand(&sendBuffer, &misc);
  1005. }
  1006. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStartRequest)
  1007. {
  1008. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStartRequest = false;
  1009. misc.Command = _MiscCmd_RemoteStart;
  1010. misc.Value = true;
  1011. PRINTF_FUNC("Announce Connector %d Remote Start: %d", packet->Header.id, misc.Value);
  1012. AddMiscCommand(&sendBuffer, &misc);
  1013. }
  1014. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AccountBalanceRequest)
  1015. {
  1016. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AccountBalanceRequest = false;
  1017. misc.Command = _MiscCmd_AccountBalance;
  1018. misc.Value = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AccountBalance;
  1019. PRINTF_FUNC("Announce Connector %d Account Balance: %d", packet->Header.id, (misc.Value));
  1020. AddMiscCommand(&sendBuffer, &misc);
  1021. }
  1022. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] == packet->Header.id)
  1023. {
  1024. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.ConnectorTimeoutConfigRequest)
  1025. {
  1026. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.ConnectorTimeoutConfigRequest = false;
  1027. misc.Command = _MiscCmd_ConnectorTimeout;
  1028. misc.Value = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout;
  1029. PRINTF_FUNC("Announce Dispenser %d Connection Timeout: %d", dispenserIndex + 1, misc.Value);
  1030. AddMiscCommand(&sendBuffer, &misc);
  1031. }
  1032. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DefaultPriceConfigRequest)
  1033. {
  1034. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DefaultPriceConfigRequest = false;
  1035. misc.Command = _MiscCmd_DefaultPrice;
  1036. misc.Value = ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice;
  1037. PRINTF_FUNC("Announce Dispenser %d Default Price: %d", dispenserIndex + 1, misc.Value);
  1038. AddMiscCommand(&sendBuffer, &misc);
  1039. }
  1040. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.CurrencyConfigRequest)
  1041. {
  1042. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.CurrencyConfigRequest = false;
  1043. misc.Command = _MiscCmd_Currency;
  1044. misc.Value = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency;
  1045. PRINTF_FUNC("Announce Dispenser %d Currency Index: %d", dispenserIndex + 1, misc.Value);
  1046. AddMiscCommand(&sendBuffer, &misc);
  1047. }
  1048. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootRequest)
  1049. {
  1050. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootRequest = false;
  1051. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootConfirm = true;
  1052. misc.Command = _MiscCmd_HardwareReboot;
  1053. misc.Value = true;
  1054. PRINTF_FUNC("Announce Dispenser %d Hardware Reboot: %d", dispenserIndex + 1, misc.Value);
  1055. AddMiscCommand(&sendBuffer, &misc);
  1056. }
  1057. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetRequest)
  1058. {
  1059. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetRequest = false;
  1060. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetConfirm = true;
  1061. misc.Command = _MiscCmd_SoftwareRestart;
  1062. misc.Value = true;
  1063. PRINTF_FUNC("Announce Dispenser %d Software Reset: %d", dispenserIndex + 1, misc.Value);
  1064. AddMiscCommand(&sendBuffer, &misc);
  1065. }
  1066. }
  1067. }
  1068. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1069. }
  1070. void WriteCsuVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1071. {
  1072. struct PACKET_STRUCTURE sendBuffer;
  1073. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1074. sendBuffer.Header.se = packet->Header.se;
  1075. sendBuffer.Header.id = packet->Header.id;
  1076. sendBuffer.Header.op = _Header_Response;
  1077. sendBuffer.Header.len = 2;
  1078. sendBuffer.Payload.reg = _Reg_Report_Csu_Version;
  1079. sendBuffer.Payload.data[0] = result;
  1080. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1081. }
  1082. void WriteOtherVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1083. {
  1084. struct PACKET_STRUCTURE sendBuffer;
  1085. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1086. sendBuffer.Header.se = packet->Header.se;
  1087. sendBuffer.Header.id = packet->Header.id;
  1088. sendBuffer.Header.op = _Header_Response;
  1089. sendBuffer.Header.len = 2;
  1090. sendBuffer.Payload.reg = _Reg_Report_Other_Version;
  1091. sendBuffer.Payload.data[0] = result;
  1092. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1093. }
  1094. void WriteChargingInfoResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1095. {
  1096. struct PACKET_STRUCTURE sendBuffer;
  1097. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1098. sendBuffer.Header.se = packet->Header.se;
  1099. sendBuffer.Header.id = packet->Header.id;
  1100. sendBuffer.Header.op = _Header_Response;
  1101. sendBuffer.Header.len = 2;
  1102. sendBuffer.Payload.reg = _Reg_Charging_Info;
  1103. sendBuffer.Payload.data[0] = result;
  1104. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1105. }
  1106. void ChargerSystemIdResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1107. {
  1108. struct PACKET_STRUCTURE sendBuffer;
  1109. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1110. sendBuffer.Header.se = packet->Header.se;
  1111. sendBuffer.Header.id = packet->Header.id;
  1112. sendBuffer.Header.op = _Header_Response;
  1113. sendBuffer.Header.len = 2;
  1114. sendBuffer.Payload.reg = _Reg_Charger_System_Id;
  1115. sendBuffer.Payload.data[0] = result;
  1116. if(result == _R_OK)
  1117. {
  1118. int strLen = 0;
  1119. strLen = strlen((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1120. memcpy(&sendBuffer.Payload.data[sendBuffer.Header.len - 1], (char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, strLen);
  1121. sendBuffer.Header.len += strLen + 1;
  1122. char TimeSync[32];
  1123. struct timeb SeqEndTime;
  1124. struct tm *tm;
  1125. ftime(&SeqEndTime);
  1126. SeqEndTime.time = time(NULL);
  1127. tm=localtime(&SeqEndTime.time);
  1128. memset(TimeSync, 0x00, 32);
  1129. 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);
  1130. strLen = strlen(TimeSync);
  1131. memcpy(&sendBuffer.Payload.data[sendBuffer.Header.len - 1], TimeSync, strLen);
  1132. sendBuffer.Header.len += strLen + 1;
  1133. //PRINTF_FUNC("Dispenser %d Read System Id: %s", dispenserIndex + 1, ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1134. }
  1135. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1136. }
  1137. void WriteWaitPlugInResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1138. {
  1139. struct PACKET_STRUCTURE sendBuffer;
  1140. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1141. sendBuffer.Header.se = packet->Header.se;
  1142. sendBuffer.Header.id = packet->Header.id;
  1143. sendBuffer.Header.op = _Header_Response;
  1144. sendBuffer.Header.len = 2;
  1145. sendBuffer.Payload.reg = _Reg_WaitPlugIn;
  1146. sendBuffer.Payload.data[0] = result;
  1147. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  1148. }
  1149. BOOL FindConnectorID(unsigned char dispenserIndex, unsigned char id)
  1150. {
  1151. BOOL find = false;
  1152. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1153. {
  1154. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] == id)
  1155. {
  1156. find = true;
  1157. break;
  1158. }
  1159. }
  1160. return find;
  1161. }
  1162. void ConnectorTypeBindingHandler(unsigned char dispenserIndex, unsigned char *type)
  1163. {
  1164. char *str_gun_type[] = {STR_GUN_TYPE_CHADEMO, STR_GUN_TYPE_CCS, STR_GUN_TYPE_GBT};
  1165. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1166. {
  1167. unsigned char gunIndex = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  1168. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex = dispenserIndex;
  1169. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.Index = gunIndex;
  1170. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.Type = type[i];
  1171. PRINTF_FUNC("Dispenser id %d Connector %d type %s", dispenserIndex + 1, gunIndex + 1, str_gun_type[type[i]]);
  1172. }
  1173. }
  1174. BOOL IsAvailableDispenserIndexSequence(unsigned char index)
  1175. {
  1176. if((ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status & (1 << index)) > 0)
  1177. {
  1178. // dispenser ever checkin
  1179. return true;
  1180. }
  1181. else
  1182. {
  1183. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  1184. {
  1185. if(i == index)
  1186. {
  1187. return true;
  1188. }
  1189. if((ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status & (1 << index)) == 0)
  1190. {
  1191. // i is not check in yet before index
  1192. return false;
  1193. }
  1194. }
  1195. }
  1196. return false;
  1197. }
  1198. void DispenserCheckInInfoUpdate(void)
  1199. {
  1200. unsigned char dispenser = 0, connector = 0;
  1201. BOOL change = false;
  1202. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  1203. {
  1204. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  1205. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  1206. {
  1207. dispenser++;
  1208. ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status |= (1 << i);
  1209. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[i] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity;
  1210. connector += ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity;
  1211. }
  1212. }
  1213. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity != dispenser ||
  1214. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity != connector)
  1215. {
  1216. change = true;
  1217. }
  1218. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity = dispenser;
  1219. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity = connector;
  1220. if(change)
  1221. {
  1222. PRINTF_FUNC("Total Dispenser: %d, Total Connector: %d\n",
  1223. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity,
  1224. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity);
  1225. }
  1226. }
  1227. void SetConnectorID(unsigned char dispenserIndex, unsigned char quantity)
  1228. {
  1229. unsigned char currentQuantity = 0;
  1230. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] = 0x00;
  1231. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[1] = 0x00;
  1232. for(int i = 0; i < dispenserIndex; i++)
  1233. {
  1234. currentQuantity += ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[i];
  1235. }
  1236. for(int i = 0; i < quantity; i++)
  1237. {
  1238. currentQuantity++;
  1239. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] = currentQuantity;
  1240. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[currentQuantity - 1].Enable = true;
  1241. }
  1242. }
  1243. BOOL DispenserIdentificationHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char *modelName)
  1244. {
  1245. unsigned char quantity = 0;
  1246. unsigned char connectorType[2] = {0, 0};
  1247. quantity = ConnectorQuantityTypeParsing(modelName, connectorType);
  1248. if(quantity > 0)
  1249. {
  1250. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_Timeout &&
  1251. quantity != ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[dispenserIndex])
  1252. {
  1253. PRINTF_FUNC("The same dispenser id (%d) but connector quantity not match", dispenserIndex + 1);
  1254. return false;
  1255. }
  1256. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_None ||
  1257. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_Timeout)
  1258. {
  1259. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Identification;
  1260. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity = quantity;
  1261. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[dispenserIndex] = quantity;
  1262. SetConnectorID(dispenserIndex, quantity);
  1263. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName, packet->Payload.data, (packet->Header.len - 2));
  1264. ConnectorTypeBindingHandler(dispenserIndex, connectorType);
  1265. PRINTF_FUNC("Dispenser id %d identified, Connector quantity: %d\n", dispenserIndex + 1, quantity);
  1266. DispenserCheckInInfoUpdate();
  1267. return true;
  1268. }
  1269. else
  1270. {
  1271. PRINTF_FUNC("Conflict dispenser ID: %d", dispenserIndex + 1);
  1272. }
  1273. }
  1274. else
  1275. {
  1276. PRINTF_FUNC("Connector quantity fail, model name: %s", modelName);
  1277. }
  1278. return false;
  1279. }
  1280. BOOL DispenserStatusCodeHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1281. {
  1282. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1283. if(find)
  1284. {
  1285. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount = (packet->Header.len - 1) / 6;
  1286. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount > 10)
  1287. {
  1288. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount = 10;
  1289. }
  1290. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  1291. {
  1292. memcpy(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i][0],
  1293. &packet->Payload.data[i * 6], 6);
  1294. }
  1295. /*
  1296. BOOL needUpdate = FALSE;
  1297. // check is there new status code
  1298. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  1299. {
  1300. if(strcmp(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i][0],
  1301. &LastWarningInfo[packet->Header.id - 1].WarningCode[i][0]) == 0)
  1302. {
  1303. needUpdate = TRUE;
  1304. break;
  1305. }
  1306. }
  1307. if(needUpdate)
  1308. {
  1309. memcpy(&LastWarningInfo[packet->Header.id - 1], &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo, sizeof(struct WARNING_CODE_INFO));
  1310. }
  1311. */
  1312. char strDispenserStatus[128];
  1313. sprintf(strDispenserStatus, "Dispenser id %d, Status Code Len: %d", packet->Header.id, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount);
  1314. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount > 0)
  1315. {
  1316. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  1317. {
  1318. char strTemp[16];
  1319. sprintf(strTemp, ", %s", ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i]);
  1320. strcat(strDispenserStatus, strTemp);
  1321. }
  1322. }
  1323. PRINTF_FUNC(strDispenserStatus);
  1324. }
  1325. else
  1326. {
  1327. }
  1328. return find;
  1329. }
  1330. BOOL ConnectorChargingCapabilityHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1331. {
  1332. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1333. BOOL done = false;
  1334. if(find)
  1335. {
  1336. done = true;
  1337. }
  1338. return done;
  1339. }
  1340. unsigned long GetTimeoutValue(struct timeval _sour_time)
  1341. {
  1342. struct timeval _end_time;
  1343. gettimeofday(&_end_time, NULL);
  1344. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  1345. }
  1346. BOOL ConnectorChargingTargetHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1347. {
  1348. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1349. BOOL done = false;
  1350. float voltage = 0, current = 0;
  1351. if(find)
  1352. {
  1353. voltage = (float)((packet->Payload.data[0] << 8) + packet->Payload.data[1]) / 10;
  1354. current = (float)((packet->Payload.data[2] << 8) + packet->Payload.data[3]) / 10;
  1355. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus == S_CHARGING)
  1356. {
  1357. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage != voltage ||
  1358. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent != current)
  1359. {
  1360. PRINTF_FUNC("Connector %d Target Voltage: %d, Current: %d", packet->Header.id, (int)(voltage * 10), (int)(current * 10));
  1361. }
  1362. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage = (float)voltage;
  1363. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent = (float)current;
  1364. done = true;
  1365. }
  1366. else
  1367. {
  1368. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage != 0 ||
  1369. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent != 0)
  1370. {
  1371. PRINTF_FUNC("Connector %d Not Charging Status, Set Voltage & Current Fail", packet->Header.id);
  1372. }
  1373. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage = 0;
  1374. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent = 0;
  1375. }
  1376. }
  1377. else
  1378. {
  1379. }
  1380. return done;
  1381. }
  1382. unsigned char FirmwareUpgradeHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1383. {
  1384. return _R_NoUpgrade;
  1385. }
  1386. BOOL ConnectorPlugInHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1387. {
  1388. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1389. if(find)
  1390. {
  1391. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn != packet->Payload.data[0])
  1392. {
  1393. PRINTF_FUNC("Connector %d %s", packet->Header.id, packet->Payload.data[0] == _PIS_PluggedIn ? "Pluged In" : "Un Plugged");
  1394. }
  1395. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn = packet->Payload.data[0];
  1396. }
  1397. else
  1398. {
  1399. }
  1400. return find;
  1401. }
  1402. BOOL ConnectorStateHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1403. {
  1404. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1405. if(find)
  1406. {
  1407. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus != packet->Payload.data[0])
  1408. {
  1409. PRINTF_FUNC("Connector %d Remote Status: %d", packet->Header.id, packet->Payload.data[0]);
  1410. if((packet->Payload.data[0] == _CRS_Idle && ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus != _CRS_Terminating) ||
  1411. packet->Payload.data[0] == _CRS_Terminating)
  1412. {
  1413. PRINTF_FUNC("*********** Connector id %d Set Stop Flag ***********\n", packet->Header.id);
  1414. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.StopChargeFlag = true;
  1415. }
  1416. }
  1417. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus = packet->Payload.data[0];
  1418. }
  1419. else
  1420. {
  1421. }
  1422. return true;
  1423. }
  1424. unsigned char UserIDHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1425. {
  1426. DispenserAck_Status authorize = _DAS_Wait;
  1427. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].AuthorizeStatus == _AuthorizeStatus_Idle)
  1428. {
  1429. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.AuthorizeRequest)
  1430. {
  1431. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.AuthorizeRequest = false;
  1432. }
  1433. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId, 0x00, 32);
  1434. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId, packet->Payload.data, packet->Header.len - 1);
  1435. PRINTF_FUNC("Dispenser id %d user id %s need authorize", dispenserIndex + 1, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId);
  1436. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.AuthorizeRequest = true;
  1437. authorize = _DAS_Wait;
  1438. }
  1439. else if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].AuthorizeStatus == _AuthorizeStatus_Fail)
  1440. {
  1441. PRINTF_FUNC("Dispenser id %d user id %s authorizing fail", dispenserIndex + 1, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId);
  1442. authorize = _DAS_NotAllowed;
  1443. }
  1444. else if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].AuthorizeStatus == _AuthorizeStatus_Pass)
  1445. {
  1446. PRINTF_FUNC("Dispenser id %d user id %s authorizing ok", dispenserIndex + 1, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].UserId);
  1447. authorize = _DAS_Allowed;
  1448. }
  1449. else
  1450. {
  1451. authorize = _DAS_Wait;
  1452. }
  1453. return authorize;
  1454. }
  1455. unsigned char ChargingPermissionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1456. {
  1457. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1458. DispenserAck_Status permission = _DAS_Wait;
  1459. if(find)
  1460. {
  1461. switch(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus)
  1462. {
  1463. case S_IDLE:
  1464. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Idle ||
  1465. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing)
  1466. {
  1467. permission = _DAS_Wait;
  1468. }
  1469. else
  1470. {
  1471. permission = _DAS_NotAllowed;
  1472. PRINTF_FUNC("Cabinet status idle, not allow dispenser id %d connector id %d charging",
  1473. dispenserIndex + 1, packet->Header.id);
  1474. PRINTF_FUNC("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  1475. }
  1476. break;
  1477. case S_PREPARING_FOR_EV:
  1478. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStopRequest ||
  1479. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopRequest)
  1480. {
  1481. permission = _DAS_NotAllowed;
  1482. PRINTF_FUNC("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  1483. }
  1484. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus >= _CRS_Preparing &&
  1485. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus <= _CRS_Charging)
  1486. {
  1487. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.PermissionRequest)
  1488. {
  1489. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.PermissionRequest = true;
  1490. PRINTF_FUNC("Allow dispenser index %d connector id %d start preparing to charge", dispenserIndex, packet->Header.id);
  1491. }
  1492. permission = _DAS_Allowed;
  1493. }
  1494. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Terminating)
  1495. {
  1496. permission = _DAS_NotAllowed;
  1497. PRINTF_FUNC("Dispenser Terminating, Not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  1498. }
  1499. else
  1500. {
  1501. // keep wait
  1502. }
  1503. break;
  1504. case S_CHARGING:
  1505. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStopRequest ||
  1506. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopRequest)
  1507. {
  1508. permission = _DAS_NotAllowed;
  1509. PRINTF_FUNC("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d charging", dispenserIndex, packet->Header.id);
  1510. }
  1511. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing ||
  1512. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Charging)
  1513. {
  1514. permission = _DAS_Allowed;
  1515. }
  1516. else
  1517. {
  1518. permission = _DAS_NotAllowed;
  1519. PRINTF_FUNC("Dispenser index %d connector id %d remote status not in charging mode", dispenserIndex, packet->Header.id);
  1520. }
  1521. break;
  1522. case S_TERMINATING:
  1523. case S_COMPLETE:
  1524. case S_ALARM:
  1525. case S_RESERVATION:
  1526. case S_MAINTAIN:
  1527. permission = _DAS_NotAllowed;
  1528. PRINTF_FUNC("Cabinet status(%d) not match, not allow dispenser index %d connector id %d charging",
  1529. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus,
  1530. dispenserIndex, packet->Header.id);
  1531. break;
  1532. default:
  1533. break;
  1534. }
  1535. }
  1536. else
  1537. {
  1538. PRINTF_FUNC("Dispenser index %d Connector id %d not found", dispenserIndex, packet->Header.id);
  1539. permission = _DAS_NotAllowed;
  1540. }
  1541. // 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",
  1542. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus,
  1543. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus,
  1544. // ShmSysConfigAndInfo->SysInfo.WaitForPlugit,
  1545. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn,
  1546. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.IsAvailable,
  1547. // ShmSysConfigAndInfo->SysInfo.OrderCharging);
  1548. return permission;
  1549. }
  1550. BOOL MiscControlHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1551. {
  1552. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1553. BOOL done = false;
  1554. if(find)
  1555. {
  1556. done = true;
  1557. }
  1558. return done;
  1559. }
  1560. unsigned char DispenserWriteCsuVersionInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1561. {
  1562. char version[32];
  1563. BOOL writeVersion = FALSE, showVersion = FALSE;;
  1564. memcpy(version, &packet->Payload.data[0], 32);
  1565. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev, version) != 0)
  1566. {
  1567. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev, version, 32);
  1568. showVersion = true;
  1569. }
  1570. memcpy(version, &packet->Payload.data[32], 32);
  1571. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev, version) != 0)
  1572. {
  1573. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev, version, 32);
  1574. showVersion = true;
  1575. }
  1576. memcpy(version, &packet->Payload.data[64], 32);
  1577. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev, version) != 0)
  1578. {
  1579. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev, version, 32);
  1580. showVersion = true;
  1581. }
  1582. memcpy(version, &packet->Payload.data[96], 32);
  1583. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev, version) != 0)
  1584. {
  1585. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev, version, 32);
  1586. showVersion = true;
  1587. }
  1588. if(showVersion)
  1589. {
  1590. PRINTF_FUNC("********** Dispenser id %d: Csu Version information **********", dispenserIndex + 1);
  1591. PRINTF_FUNC("********** CsuBootLoadRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev);
  1592. PRINTF_FUNC("********** CsuKernelFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev);
  1593. PRINTF_FUNC("********** CsuRootFsFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev);
  1594. PRINTF_FUNC("********** CsuPrimFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev);
  1595. }
  1596. writeVersion = TRUE;
  1597. return writeVersion;
  1598. }
  1599. unsigned char DispenserWriteOtherVersionInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1600. {
  1601. char version[32];
  1602. BOOL writeVersion = FALSE, showVersion = FALSE;;
  1603. memcpy(version, &packet->Payload.data[0], 32);
  1604. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev, version) != 0)
  1605. {
  1606. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev, version, 32);
  1607. showVersion = true;
  1608. }
  1609. memcpy(version, &packet->Payload.data[32], 32);
  1610. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev, version) != 0)
  1611. {
  1612. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev, version, 32);
  1613. showVersion = true;
  1614. }
  1615. memcpy(version, &packet->Payload.data[64], 32);
  1616. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev, version) != 0)
  1617. {
  1618. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev, version, 32);
  1619. showVersion = true;
  1620. }
  1621. memcpy(version, &packet->Payload.data[96], 32);
  1622. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev, version) != 0)
  1623. {
  1624. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev, version, 32);
  1625. showVersion = true;
  1626. }
  1627. memcpy(version, &packet->Payload.data[128], 32);
  1628. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev, version) != 0)
  1629. {
  1630. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev, version, 32);
  1631. showVersion = true;
  1632. }
  1633. if(showVersion)
  1634. {
  1635. PRINTF_FUNC("********** Dispenser id %d: Other Version information **********", dispenserIndex + 1);
  1636. PRINTF_FUNC("********** FanModuleFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev);
  1637. PRINTF_FUNC("********** RelayModuleRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev);
  1638. PRINTF_FUNC("********** Connector1Rev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev);
  1639. PRINTF_FUNC("********** Connector2Rev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev);
  1640. PRINTF_FUNC("********** LedModuleFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev);
  1641. }
  1642. writeVersion = TRUE;
  1643. return writeVersion;
  1644. }
  1645. unsigned char DispenserWriteChargingInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1646. {
  1647. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1648. if(find)
  1649. {
  1650. unsigned short voltage = (packet->Payload.data[0] << 8) + (packet->Payload.data[1]);
  1651. unsigned short current = (packet->Payload.data[2] << 8) + (packet->Payload.data[3]);
  1652. unsigned int time = (packet->Payload.data[4] << 24) + (packet->Payload.data[5] << 16) + (packet->Payload.data[6] << 8) + (packet->Payload.data[7]);
  1653. unsigned char soc = (packet->Payload.data[8]);
  1654. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteChargingVoltage = voltage;
  1655. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteChargingCurrent = current;
  1656. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteRemainChargingDuration = time;
  1657. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteSoc = soc;
  1658. }
  1659. else
  1660. {
  1661. }
  1662. return find;
  1663. }
  1664. unsigned char ReadChargerSystemIdHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1665. {
  1666. BOOL find = TRUE;
  1667. return find;
  1668. }
  1669. unsigned char WriteWaitPlugInHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  1670. {
  1671. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  1672. if(find)
  1673. {
  1674. unsigned char StartWait = packet->Payload.data[0];
  1675. if(StartWait)
  1676. {
  1677. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug)
  1678. {
  1679. PRINTF_FUNC("Connector %d Start Wait Plug In", packet->Header.id);
  1680. }
  1681. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = true;
  1682. }
  1683. else
  1684. {
  1685. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug)
  1686. {
  1687. PRINTF_FUNC("Connector %d Stop Wait Plug In", packet->Header.id);
  1688. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.NeedCleanAuthorizeInfo = true;
  1689. }
  1690. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = false;
  1691. }
  1692. }
  1693. else
  1694. {
  1695. }
  1696. return find;
  1697. }
  1698. void DisableConnector(unsigned char dispenserIndex)
  1699. {
  1700. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1701. {
  1702. unsigned char connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  1703. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Enable = false;
  1704. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.TimeoutStopRequest = true;
  1705. }
  1706. }
  1707. void DispenserSocketProcess(int socketFd, struct sockaddr_in clientInfo, unsigned char index)
  1708. {
  1709. int rxLen = 0, timeout = 0;
  1710. unsigned char dispenserID = 0, dispenserIndex = 0;
  1711. struct PACKET_STRUCTURE receiveBuffer;
  1712. unsigned char modelName[64];
  1713. unsigned char ackResult = _R_NG;
  1714. DispenserAck_Status ack = _DAS_Wait;
  1715. PRINTF_FUNC("IP %s connection(%d) is established, start dispenser process", (inet_ntoa(clientInfo.sin_addr)), index);
  1716. while(1)
  1717. {
  1718. if((rxLen = recv(socketFd, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  1719. {
  1720. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  1721. {
  1722. ackResult = _R_NG;
  1723. //ShowSocketData(&receiveBuffer);
  1724. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_WaitModelName)
  1725. {
  1726. // Reg: 0x01, Dispenser model name
  1727. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name)
  1728. {
  1729. memset(modelName, 0x00, 64);
  1730. dispenserID = receiveBuffer.Payload.data[receiveBuffer.Header.len - 2];
  1731. if(dispenserID > 0 && dispenserID <= GENERAL_GUN_QUANTITY)
  1732. {
  1733. dispenserIndex = dispenserID - 1;
  1734. memcpy(modelName, receiveBuffer.Payload.data, receiveBuffer.Header.len - 2);
  1735. if(IsAvailableDispenserIndexSequence(dispenserIndex))
  1736. {
  1737. if(DispenserIdentificationHandler(&receiveBuffer, dispenserIndex, modelName) == true)
  1738. {
  1739. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].DispenserIndex = dispenserIndex;
  1740. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status = _CNS_DispenserMatched;
  1741. PRINTF_FUNC("********** Dispenser id %d check in, model name: %s **********\n", dispenserID, modelName);
  1742. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName, modelName, 64);
  1743. ackResult = _R_OK;
  1744. }
  1745. else
  1746. {
  1747. // response NG: gun quantity fail
  1748. PRINTF_FUNC("Dispenser ID %d Identification Fail", dispenserID);
  1749. }
  1750. }
  1751. else
  1752. {
  1753. // response NG: dispenser Index fail
  1754. PRINTF_FUNC("Dispenser ID %d Fail, Need Wait Another Dispenser", dispenserID);
  1755. }
  1756. }
  1757. else
  1758. {
  1759. PRINTF_FUNC("Dispenser ID: %d Over Range", dispenserID);
  1760. }
  1761. ModelNameResponse(socketFd, &receiveBuffer, ackResult);
  1762. }
  1763. else
  1764. {
  1765. // do nothing
  1766. }
  1767. }
  1768. else if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_DispenserMatched)
  1769. {
  1770. // Reg: 0x01, Dispenser model name
  1771. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name)
  1772. {
  1773. ackResult = _R_OK;
  1774. ModelNameResponse(socketFd, &receiveBuffer, ackResult);
  1775. }
  1776. // Reg: 0x02, Connector ID
  1777. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Connector_ID)
  1778. {
  1779. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity > 0)
  1780. {
  1781. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Idle;
  1782. ackResult = _R_OK;
  1783. }
  1784. PRINTF_FUNC("Dispenser ID %d Get Connector ID %s", dispenserID, ackResult == _R_OK ? "OK" : "NG");
  1785. ConnectorIDResponse(socketFd, &receiveBuffer, ackResult, dispenserIndex);
  1786. if(ackResult == _R_OK)
  1787. {
  1788. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DispenserConfigSync)
  1789. {
  1790. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DispenserConfigSync = true;
  1791. PRINTF_FUNC("Dispenser ID %d Configuration Need To Synchronize", dispenserID);
  1792. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.ConnectorTimeoutConfigRequest = 1;
  1793. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DefaultPriceConfigRequest = 1;
  1794. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.CurrencyConfigRequest = 1;
  1795. }
  1796. }
  1797. }
  1798. // Reg: 0x03, Power cabinet status
  1799. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Power_Cabinet_Status)
  1800. {
  1801. PowerCabinetStatusResponse(socketFd, &receiveBuffer);
  1802. }
  1803. // Reg: 0x04, Dispenser status
  1804. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Status)
  1805. {
  1806. if(DispenserStatusCodeHandler(&receiveBuffer, dispenserIndex))
  1807. {
  1808. ackResult = _R_OK;
  1809. }
  1810. DispenserStatusResponse(socketFd, &receiveBuffer, ackResult);
  1811. }
  1812. // Reg: 0x05, Charging capability
  1813. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charging_Capability)
  1814. {
  1815. if(ConnectorChargingCapabilityHandler(&receiveBuffer, dispenserIndex))
  1816. {
  1817. ChargingCapabilityResponse(socketFd, &receiveBuffer);
  1818. }
  1819. }
  1820. // Reg: 0x06, Charging target
  1821. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Charging_Target)
  1822. {
  1823. if(receiveBuffer.Header.len == 5)
  1824. {
  1825. if(ConnectorChargingTargetHandler(&receiveBuffer, dispenserIndex))
  1826. {
  1827. ackResult = _R_OK;
  1828. }
  1829. }
  1830. ChargingTargetResponse(socketFd, &receiveBuffer, ackResult);
  1831. }
  1832. // Reg: 0x07, Software update
  1833. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Software_Update)
  1834. {
  1835. unsigned char NeedUpgrade = FirmwareUpgradeHandler(&receiveBuffer, dispenserIndex);
  1836. FirmwareUpgradeResponse(socketFd, &receiveBuffer, NeedUpgrade);
  1837. }
  1838. // Reg: 0x08, Plug-in status
  1839. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Plug_In_Status)
  1840. {
  1841. if(receiveBuffer.Header.len == 2)
  1842. {
  1843. if(ConnectorPlugInHandler(&receiveBuffer, dispenserIndex))
  1844. {
  1845. ackResult = _R_OK;
  1846. }
  1847. }
  1848. PlugInStatusResponse(socketFd, &receiveBuffer, ackResult);
  1849. }
  1850. // Reg: 0x09, Connector state
  1851. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Connector_State)
  1852. {
  1853. if(ConnectorStateHandler(&receiveBuffer, dispenserIndex))
  1854. {
  1855. ackResult = _R_OK;
  1856. }
  1857. ConnectorStateResponse(socketFd, &receiveBuffer, ackResult);
  1858. }
  1859. // Reg: 0x0A, User ID
  1860. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_User_ID)
  1861. {
  1862. ack = UserIDHandler(&receiveBuffer, dispenserIndex);
  1863. UserIDResponse(socketFd, &receiveBuffer, ack);
  1864. }
  1865. // Reg: 0x0B, Charging permission
  1866. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charging_Permission)
  1867. {
  1868. ack = ChargingPermissionHandler(&receiveBuffer, dispenserIndex);
  1869. ChargingPermissionResponse(socketFd, &receiveBuffer, ack);
  1870. }
  1871. // Reg: 0x0C, Misc Control
  1872. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Misc_Control)
  1873. {
  1874. if(MiscControlHandler(&receiveBuffer, dispenserIndex))
  1875. {
  1876. ackResult = _R_OK;
  1877. }
  1878. MiscControlResponse(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  1879. }
  1880. // Reg: 0x0D, Report csu version
  1881. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Report_Csu_Version)
  1882. {
  1883. if(DispenserWriteCsuVersionInfoHandler(&receiveBuffer, dispenserIndex))
  1884. {
  1885. ackResult = _R_OK;
  1886. }
  1887. WriteCsuVersionResponse(socketFd, &receiveBuffer, ackResult);
  1888. }
  1889. // Reg: 0x0E, Report other version
  1890. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Report_Other_Version)
  1891. {
  1892. if(DispenserWriteOtherVersionInfoHandler(&receiveBuffer, dispenserIndex))
  1893. {
  1894. ackResult = _R_OK;
  1895. }
  1896. WriteOtherVersionResponse(socketFd, &receiveBuffer, ackResult);
  1897. }
  1898. // Reg: 0x0F, Charging info
  1899. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Charging_Info)
  1900. {
  1901. if(DispenserWriteChargingInfoHandler(&receiveBuffer, dispenserIndex))
  1902. {
  1903. ackResult = _R_OK;
  1904. }
  1905. WriteChargingInfoResponse(socketFd, &receiveBuffer, ackResult);
  1906. }
  1907. // Reg: 0x10, Charger system id
  1908. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charger_System_Id)
  1909. {
  1910. if(ReadChargerSystemIdHandler(&receiveBuffer, dispenserIndex))
  1911. {
  1912. ackResult = _R_OK;
  1913. }
  1914. ChargerSystemIdResponse(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  1915. }
  1916. // Reg: 0x11, Wait plug in
  1917. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_WaitPlugIn)
  1918. {
  1919. if(WriteWaitPlugInHandler(&receiveBuffer, dispenserIndex))
  1920. {
  1921. ackResult = _R_OK;
  1922. }
  1923. WriteWaitPlugInResponse(socketFd, &receiveBuffer, ackResult);
  1924. }
  1925. }
  1926. // clean timeout
  1927. timeout = 0;
  1928. }
  1929. }
  1930. else
  1931. {
  1932. timeout += SOCKET_RECEIVE_INTERVAL;
  1933. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  1934. }
  1935. if(timeout >= DISPENSER_SOCKET_TIMEOUT)
  1936. {
  1937. //timeout
  1938. PRINTF_FUNC("IP: %s, Socket %d connection timeout", (inet_ntoa(clientInfo.sin_addr)), index);
  1939. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_DispenserMatched)
  1940. {
  1941. DisableConnector(dispenserIndex);
  1942. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex], 0x00, sizeof(struct DispenserModule));
  1943. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Timeout;
  1944. DispenserCheckInInfoUpdate();
  1945. }
  1946. break;
  1947. }
  1948. }
  1949. }
  1950. int GetMacAddress(int sockFd, unsigned char *mac)
  1951. {
  1952. struct arpreq arpReq;
  1953. struct sockaddr_in clientInfo;
  1954. socklen_t len = sizeof(struct sockaddr_in);
  1955. memset(mac, 0x00, 6);
  1956. if(getpeername(sockFd, (struct sockaddr*)&clientInfo, &len) < 0)
  1957. {
  1958. return 0;
  1959. }
  1960. else
  1961. {
  1962. memcpy(&arpReq.arp_pa, &clientInfo, sizeof(struct sockaddr_in));
  1963. strcpy(arpReq.arp_dev, "eth1");
  1964. arpReq.arp_pa.sa_family = AF_INET;
  1965. arpReq.arp_ha.sa_family = AF_UNSPEC;
  1966. if(ioctl(sockFd, SIOCGARP, &arpReq) < 0)
  1967. {
  1968. return 0;
  1969. }
  1970. else
  1971. {
  1972. memcpy(mac, arpReq.arp_ha.sa_data, 6);
  1973. }
  1974. }
  1975. return 1;
  1976. }
  1977. int FindFreeChannel(void)
  1978. {
  1979. for(int i = 0; i < MAXIMUM_CONNECT_QUANTITY; i++)
  1980. {
  1981. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_FREE)
  1982. {
  1983. return i;
  1984. }
  1985. }
  1986. return FAIL;
  1987. }
  1988. BOOL IsConflictIp(uint32_t ipAddress)
  1989. {
  1990. for(int i = 0; i < MAXIMUM_CONNECT_QUANTITY; i++)
  1991. {
  1992. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress == ipAddress)
  1993. {
  1994. return true;
  1995. }
  1996. }
  1997. return false;
  1998. }
  1999. void InitDispenserInfo(void)
  2000. {
  2001. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  2002. {
  2003. //memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i], 0x00, sizeof(struct ConnectorInfoData));
  2004. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteStatus = 0;
  2005. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Enable = 0;
  2006. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ReadyToCharge = 0;
  2007. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex = 0;
  2008. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.Value = 0;
  2009. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AuthorizeStatus = 0;
  2010. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].WarningInfo, 0x00, sizeof(struct WARNING_CODE_INFO));
  2011. //memset(&LastWarningInfo[i], 0x00, sizeof(struct WARNING_CODE_INFO));
  2012. }
  2013. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo, 0x00, sizeof(struct DispenserInfoData));
  2014. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  2015. }
  2016. void tcpSocketServerStart(void)
  2017. {
  2018. int sockFd = 0;
  2019. int clientSockFd = 0;
  2020. int connectIndex = 0;
  2021. pid_t forkId;
  2022. struct sockaddr_in serverInfo, clientInfo;
  2023. socklen_t addrlen = sizeof(clientInfo);
  2024. //struct timeval timeout = {3, 0};
  2025. sockFd = socket(AF_INET, SOCK_STREAM, 0);
  2026. if(sockFd == -1)
  2027. {
  2028. PRINTF_FUNC("InitSocketServer NG\n");
  2029. sleep(5);
  2030. return;
  2031. }
  2032. InitDispenserInfo();
  2033. bzero(&serverInfo,sizeof(serverInfo));
  2034. serverInfo.sin_family = PF_INET;
  2035. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  2036. serverInfo.sin_port = htons(TCP_LISTEN_PORT);
  2037. bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo));
  2038. listen(sockFd, CONNECTION_LIMIT);
  2039. PRINTF_FUNC("TCP Server Start\n");
  2040. signal(SIGCHLD, SIG_IGN);
  2041. // Main loop
  2042. while(1)
  2043. {
  2044. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  2045. /*
  2046. if((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == ENONET) ||
  2047. (errno == EPROTO) || (errno == ENOPROTOOPT) || (errno == EOPNOTSUPP) ||
  2048. (errno == ENETDOWN) || (errno == ENETUNREACH) || (errno == EHOSTDOWN) ||
  2049. (errno == EHOSTUNREACH) || (errno == ECONNABORTED))
  2050. {
  2051. printf("accept error: %s\n", strerror(errno));
  2052. //exit(EXIT_FAILURE);
  2053. //break;
  2054. }
  2055. else if ( errno == EINTR)
  2056. {
  2057. printf("accept EINTRn");
  2058. continue;
  2059. }
  2060. else
  2061. {
  2062. printf("AccepCb: accept error: %s\n", strerror(errno));
  2063. //break;
  2064. //exit(EXIT_FAILURE);
  2065. }*/
  2066. PRINTF_FUNC("Client ip: %s is accepted at port: %d", inet_ntoa(clientInfo.sin_addr), clientInfo.sin_port);
  2067. connectIndex = FindFreeChannel();
  2068. if(connectIndex >= 0)
  2069. {
  2070. if(IsConflictIp(clientInfo.sin_addr.s_addr) == false)
  2071. {
  2072. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex].Status = _CNS_WaitModelName;
  2073. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex].IpAddress = clientInfo.sin_addr.s_addr;
  2074. forkId = fork();
  2075. if(forkId == 0)
  2076. {
  2077. // child process
  2078. DispenserSocketProcess(clientSockFd, clientInfo, connectIndex);
  2079. close(clientSockFd);
  2080. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex], 0x00, sizeof(struct ConnectionInfoData));
  2081. return;
  2082. }
  2083. else if(forkId == -1)
  2084. {
  2085. PRINTF_FUNC("fork fail");
  2086. }
  2087. }
  2088. else
  2089. {
  2090. // conflict ip address
  2091. PRINTF_FUNC("Conflict IP address, close socket");
  2092. close(clientSockFd);
  2093. }
  2094. }
  2095. else
  2096. {
  2097. // no free channel
  2098. PRINTF_FUNC("No free channel, close socket");
  2099. close(clientSockFd);
  2100. }
  2101. usleep(10000);
  2102. }
  2103. return;
  2104. }
  2105. int main(void)
  2106. {
  2107. if(InitShareMemory() == FAIL)
  2108. {
  2109. #ifdef SystemLogMessage
  2110. DEBUG_ERROR("InitShareMemory NG\n");
  2111. #endif
  2112. sleep(5);
  2113. return 0;
  2114. }
  2115. // wait for self test completed
  2116. while(ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING);
  2117. tcpSocketServerStart();
  2118. //tcpSocketClientStart();
  2119. return 0;
  2120. }