Module_EvComm.c 115 KB

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