Module_EvComm.c 120 KB

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