Module_EvComm.c 127 KB

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