Module_EvComm.c 156 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160
  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. ChargingCapabilityResponseInfo ConnectorCapability[GENERAL_GUN_QUANTITY];
  47. RefundResponse ConnectorRefund[GENERAL_GUN_QUANTITY];
  48. PrepaymentResponseInfo ConnectorPrepayment[GENERAL_GUN_QUANTITY];
  49. PaymentFailResponse ConnectorPaymentFailReason[GENERAL_GUN_QUANTITY];
  50. char _ConnectorQRCode[GENERAL_GUN_QUANTITY][128];
  51. StationInfoData _stationInfo[GENERAL_GUN_QUANTITY];
  52. void ShowSocketData(struct PACKET_STRUCTURE *packet)
  53. {
  54. printf("se: %02X, id: %02X, op: %d, len: %3d, reg: %02X",
  55. packet->Header.se, packet->Header.id, packet->Header.op, packet->Header.len, packet->Payload.reg);
  56. if(packet->Header.len > 1)
  57. {
  58. printf(", Data:");
  59. for(int i = 0; i < packet->Header.len - 1; i++)
  60. {
  61. printf(" %02X", packet->Payload.data[i]);
  62. }
  63. }
  64. printf("\n");
  65. }
  66. //==========================================
  67. // Init all share memory
  68. //==========================================
  69. int InitShareMemory()
  70. {
  71. int result = PASS;
  72. int MeterSMId;
  73. //creat ShmSysConfigAndInfo
  74. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  75. {
  76. #ifdef SystemLogMessage
  77. LOG_ERROR("shmget ShmSysConfigAndInfo NG");
  78. #endif
  79. result = FAIL;
  80. }
  81. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  82. {
  83. #ifdef SystemLogMessage
  84. LOG_ERROR("shmat ShmSysConfigAndInfo NG");
  85. #endif
  86. result = FAIL;
  87. }
  88. else
  89. {
  90. }
  91. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
  92. {
  93. #ifdef SystemLogMessage
  94. LOG_ERROR("shmat ChargerInfoData NG");
  95. #endif
  96. result = FAIL;
  97. }
  98. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  99. {
  100. #ifdef SystemLogMessage
  101. LOG_ERROR("shmat ChargerInfoData NG");
  102. #endif
  103. result = FAIL;
  104. }
  105. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  106. {
  107. #ifdef SystemLogMessage
  108. LOG_ERROR("shmget ShmPsuData NG");
  109. #endif
  110. result = FAIL;
  111. }
  112. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  113. {
  114. #ifdef SystemLogMessage
  115. LOG_ERROR("shmat ShmPsuData NG");
  116. #endif
  117. result = FAIL;
  118. }
  119. return result;
  120. }
  121. //-------------------------------------------Socket Client Start-------------------------------------------
  122. unsigned char seqNum = 0;
  123. void sendCmdDispenserModelName(int socket, unsigned char se)
  124. {
  125. struct PACKET_STRUCTURE sendBuffer;
  126. char *modelName = "DSYE601J0EW2PH";
  127. //char *modelName = "DSYE601J00W2PH";
  128. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  129. sendBuffer.Header.se = se;
  130. sendBuffer.Header.id = 0xFF;
  131. sendBuffer.Header.op = _Header_Write;
  132. sendBuffer.Header.len = strlen(modelName) + 2;
  133. sendBuffer.Payload.reg = _Reg_Dispenser_Model_Name;
  134. memcpy(&sendBuffer.Payload.data[0], modelName, strlen(modelName));
  135. sendBuffer.Payload.data[strlen(modelName)] = 1;
  136. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  137. }
  138. void sendCmdDispenserConnectorIDRequest(int socket, unsigned char se)
  139. {
  140. struct PACKET_STRUCTURE sendBuffer;
  141. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  142. sendBuffer.Header.se = se;
  143. sendBuffer.Header.id = 0xFF;
  144. sendBuffer.Header.op = _Header_Read;
  145. sendBuffer.Header.len = 1;
  146. sendBuffer.Payload.reg = _Reg_Connector_ID;
  147. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  148. }
  149. void sendCmdPowerCabinetStatusRequest(int socket, unsigned char connector, unsigned char se)
  150. {
  151. struct PACKET_STRUCTURE sendBuffer;
  152. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  153. sendBuffer.Header.se = se;
  154. sendBuffer.Header.id = connector;
  155. sendBuffer.Header.op = _Header_Read;
  156. sendBuffer.Header.len = 1;
  157. sendBuffer.Payload.reg = _Reg_Power_Cabinet_Status;
  158. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  159. }
  160. void sendCmdWriteDispenserStatus(int socket, unsigned char connector, unsigned char se)
  161. {
  162. struct PACKET_STRUCTURE sendBuffer;
  163. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  164. sendBuffer.Header.se = se;
  165. sendBuffer.Header.id = connector;
  166. sendBuffer.Header.op = _Header_Write;
  167. sendBuffer.Header.len = 1;
  168. sendBuffer.Payload.reg = _Reg_Dispenser_Status;
  169. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  170. }
  171. void sendCmdReadChargingCapability(int socket, unsigned char connector, unsigned char se)
  172. {
  173. struct PACKET_STRUCTURE sendBuffer;
  174. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  175. sendBuffer.Header.se = se;
  176. sendBuffer.Header.id = connector;
  177. sendBuffer.Header.op = _Header_Read;
  178. sendBuffer.Header.len = 1;
  179. sendBuffer.Payload.reg = _Reg_Charging_Capability;
  180. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  181. }
  182. void sendCmdWriteChargingTarget(int socket, unsigned char connector, unsigned char se, unsigned short voltage, unsigned short current)
  183. {
  184. struct PACKET_STRUCTURE sendBuffer;
  185. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  186. sendBuffer.Header.se = se;
  187. sendBuffer.Header.id = connector;
  188. sendBuffer.Header.op = _Header_Write;
  189. sendBuffer.Header.len = 5;
  190. sendBuffer.Payload.reg = _Reg_Charging_Target;
  191. sendBuffer.Payload.data[0] = (voltage >> 8) & 0xFF;
  192. sendBuffer.Payload.data[1] = voltage & 0xFF;
  193. sendBuffer.Payload.data[2] = (current >> 8) & 0xFF;
  194. sendBuffer.Payload.data[3] = current & 0xFF;
  195. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  196. }
  197. void sendCmdWritePlugInStatus(int socket, unsigned char connector, unsigned char se)
  198. {
  199. struct PACKET_STRUCTURE sendBuffer;
  200. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  201. sendBuffer.Header.se = se;
  202. sendBuffer.Header.id = connector;
  203. sendBuffer.Header.op = _Header_Write;
  204. sendBuffer.Header.len = 2;
  205. sendBuffer.Payload.reg = _Reg_Plug_In_Status;
  206. sendBuffer.Payload.data[0] = se % 2 == 0 ?_PIS_UnPlugged : _PIS_PluggedIn;
  207. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  208. }
  209. void sendCmdWriteConnectorState(int socket, unsigned char connector, unsigned char se)
  210. {
  211. struct PACKET_STRUCTURE sendBuffer;
  212. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  213. sendBuffer.Header.se = se;
  214. sendBuffer.Header.id = connector;
  215. sendBuffer.Header.op = _Header_Write;
  216. sendBuffer.Header.len = 2;
  217. sendBuffer.Payload.reg = _Reg_Connector_State;
  218. sendBuffer.Payload.data[0] = _CRS_Idle;
  219. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  220. }
  221. void sendCmdWriteUserID(int socket, unsigned char connector, unsigned char se)
  222. {
  223. struct PACKET_STRUCTURE sendBuffer;
  224. char *userID = "010203040506";
  225. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  226. sendBuffer.Header.se = se;
  227. sendBuffer.Header.id = connector;
  228. sendBuffer.Header.op = _Header_Write;
  229. sendBuffer.Header.len = strlen(userID) + 1;
  230. sendBuffer.Payload.reg = _Reg_User_ID;
  231. memcpy(sendBuffer.Payload.data, userID, strlen(userID));
  232. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  233. }
  234. void sendCmdReadChargingPermission(int socket, unsigned char connector, unsigned char se)
  235. {
  236. struct PACKET_STRUCTURE sendBuffer;
  237. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  238. sendBuffer.Header.se = se;
  239. sendBuffer.Header.id = connector;
  240. sendBuffer.Header.op = _Header_Read;
  241. sendBuffer.Header.len = 1;
  242. sendBuffer.Payload.reg = _Reg_Charging_Permission;
  243. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  244. }
  245. int DispenserModelNameCheckIn(int socket, unsigned char se)
  246. {
  247. int rxLen = 0, timeout = 0;
  248. struct PACKET_STRUCTURE receiveBuffer;
  249. int result = FAIL;
  250. sendCmdDispenserModelName(socket, se);
  251. while(1)
  252. {
  253. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  254. {
  255. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  256. {
  257. ShowSocketData(&receiveBuffer);
  258. if(receiveBuffer.Header.len == 2)
  259. {
  260. if(receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name && receiveBuffer.Payload.data[0] == _R_OK)
  261. {
  262. LOG_INFO("Write Model Name OK");
  263. result = PASS;
  264. break;
  265. }
  266. }
  267. }
  268. }
  269. timeout += SOCKET_RECEIVE_INTERVAL;
  270. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  271. if(timeout >= DISPENSER_MODEL_NAME_RESEND)
  272. {
  273. //timeout
  274. break;
  275. }
  276. }
  277. return result;
  278. }
  279. int DispenserConnectorRequest(int socket, unsigned char se)
  280. {
  281. int rxLen = 0, timeout = 0;
  282. struct PACKET_STRUCTURE receiveBuffer;
  283. int result = FAIL;
  284. sendCmdDispenserConnectorIDRequest(socket, se);
  285. while(1)
  286. {
  287. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  288. {
  289. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  290. {
  291. ShowSocketData(&receiveBuffer);
  292. if(receiveBuffer.Header.len == 4)
  293. {
  294. if(receiveBuffer.Payload.reg == _Reg_Connector_ID && receiveBuffer.Payload.data[0] == _R_OK)
  295. {
  296. LOG_INFO("Get Connector ID: %02X, %02X", receiveBuffer.Payload.data[1], receiveBuffer.Payload.data[2]);
  297. result = PASS;
  298. break;
  299. }
  300. }
  301. }
  302. }
  303. timeout += SOCKET_RECEIVE_INTERVAL;
  304. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  305. if(timeout >= DISPENSER_CONNECTOR_RESEND)
  306. {
  307. //timeout
  308. break;
  309. }
  310. }
  311. return result;
  312. }
  313. int DispenserReadPowerCabinetStatus(int socket, unsigned char connector, unsigned char se)
  314. {
  315. int rxLen = 0, timeout = 0;
  316. struct PACKET_STRUCTURE receiveBuffer;
  317. int result = FAIL;
  318. sendCmdPowerCabinetStatusRequest(socket, connector, se);
  319. while(1)
  320. {
  321. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  322. {
  323. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  324. {
  325. ShowSocketData(&receiveBuffer);
  326. if(receiveBuffer.Payload.reg == _Reg_Power_Cabinet_Status && receiveBuffer.Payload.data[0] == _R_OK)
  327. {
  328. LOG_INFO("Get Cabinet Status Code, Len: %d", (receiveBuffer.Header.len - 2) / 6);
  329. result = PASS;
  330. }
  331. }
  332. }
  333. timeout += SOCKET_RECEIVE_INTERVAL;
  334. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  335. if(timeout >= CABINET_STATUS_REQUEST_RESEND)
  336. {
  337. //timeout
  338. break;
  339. }
  340. }
  341. return result;
  342. }
  343. int DispenserWriteStatusCode(int socket, unsigned char connector, unsigned char se)
  344. {
  345. int rxLen = 0, timeout = 0;
  346. struct PACKET_STRUCTURE receiveBuffer;
  347. int result = FAIL;
  348. sendCmdWriteDispenserStatus(socket, connector, se);
  349. while(1)
  350. {
  351. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  352. {
  353. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  354. {
  355. ShowSocketData(&receiveBuffer);
  356. if(receiveBuffer.Header.len == 2)
  357. {
  358. if(receiveBuffer.Payload.reg == _Reg_Dispenser_Status && receiveBuffer.Payload.data[0] == _R_OK)
  359. {
  360. LOG_INFO("Write Dispenser Status Code OK");
  361. result = PASS;
  362. }
  363. }
  364. }
  365. }
  366. timeout += SOCKET_RECEIVE_INTERVAL;
  367. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  368. if(timeout >= CABINET_STATUS_REQUEST_RESEND)
  369. {
  370. //timeout
  371. break;
  372. }
  373. }
  374. return result;
  375. }
  376. int DispenserReadChargingCapability(int socket, unsigned char connector, unsigned char se)
  377. {
  378. int rxLen = 0, timeout = 0;
  379. struct PACKET_STRUCTURE receiveBuffer;
  380. int result = FAIL;
  381. unsigned short voltage = 0, current = 0, power = 0;
  382. sendCmdReadChargingCapability(socket, connector, se);
  383. while(1)
  384. {
  385. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  386. {
  387. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  388. {
  389. ShowSocketData(&receiveBuffer);
  390. if(receiveBuffer.Header.len == 8)
  391. {
  392. if(receiveBuffer.Payload.reg == _Reg_Charging_Capability && receiveBuffer.Payload.data[0] == _R_OK)
  393. {
  394. voltage = (receiveBuffer.Payload.data[1] << 8) + receiveBuffer.Payload.data[2];
  395. current = (receiveBuffer.Payload.data[3] << 8) + receiveBuffer.Payload.data[4];
  396. power = (receiveBuffer.Payload.data[5] << 8) + receiveBuffer.Payload.data[6];
  397. LOG_INFO("Capability Voltage: %d.%dV, Current: %d.%dA, Power: %d.%dkW",
  398. voltage / 10, voltage % 10, current / 10, current % 10, power / 10, power % 10);
  399. result = PASS;
  400. }
  401. }
  402. }
  403. }
  404. timeout += SOCKET_RECEIVE_INTERVAL;
  405. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  406. if(timeout >= CHARGING_CAPABILITY_RESEND)
  407. {
  408. //timeout
  409. break;
  410. }
  411. }
  412. return result;
  413. }
  414. int DispenserWriteChargingTarget(int socket, unsigned char connector, unsigned char se)
  415. {
  416. int rxLen = 0, timeout = 0;
  417. struct PACKET_STRUCTURE receiveBuffer;
  418. int result = FAIL;
  419. sendCmdWriteChargingTarget(socket, connector, se, 7505, 406);
  420. while(1)
  421. {
  422. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  423. {
  424. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  425. {
  426. ShowSocketData(&receiveBuffer);
  427. if(receiveBuffer.Header.len == 2)
  428. {
  429. if(receiveBuffer.Payload.reg == _Reg_Charging_Target && receiveBuffer.Payload.data[0] == _R_OK)
  430. {
  431. LOG_INFO("Write Charging Target OK");
  432. result = PASS;
  433. }
  434. }
  435. }
  436. }
  437. timeout += SOCKET_RECEIVE_INTERVAL;
  438. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  439. if(timeout >= CHARGING_CAPABILITY_RESEND)
  440. {
  441. //timeout
  442. break;
  443. }
  444. }
  445. return result;
  446. }
  447. int DispenserWritePlugInStatus(int socket, unsigned char connector, unsigned char se)
  448. {
  449. int rxLen = 0, timeout = 0;
  450. struct PACKET_STRUCTURE receiveBuffer;
  451. int result = FAIL;
  452. sendCmdWritePlugInStatus(socket, connector, se);
  453. while(1)
  454. {
  455. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  456. {
  457. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  458. {
  459. ShowSocketData(&receiveBuffer);
  460. if(receiveBuffer.Header.len == 2)
  461. {
  462. if(receiveBuffer.Payload.reg == _Reg_Plug_In_Status && receiveBuffer.Payload.data[0] == _R_OK)
  463. {
  464. LOG_INFO("Write Plug In Status OK");
  465. result = PASS;
  466. }
  467. }
  468. }
  469. }
  470. timeout += SOCKET_RECEIVE_INTERVAL;
  471. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  472. if(timeout >= CHARGING_CAPABILITY_RESEND)
  473. {
  474. //timeout
  475. break;
  476. }
  477. }
  478. return result;
  479. }
  480. int DispenserWriteConnectorState(int socket, unsigned char connector, unsigned char se)
  481. {
  482. int rxLen = 0, timeout = 0;
  483. struct PACKET_STRUCTURE receiveBuffer;
  484. int result = FAIL;
  485. sendCmdWriteConnectorState(socket, connector, se);
  486. while(1)
  487. {
  488. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  489. {
  490. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  491. {
  492. ShowSocketData(&receiveBuffer);
  493. if(receiveBuffer.Header.len == 2)
  494. {
  495. if(receiveBuffer.Payload.reg == _Reg_Connector_State && receiveBuffer.Payload.data[0] == _R_OK)
  496. {
  497. LOG_INFO("Write Connector State OK");
  498. result = PASS;
  499. }
  500. }
  501. }
  502. }
  503. timeout += SOCKET_RECEIVE_INTERVAL;
  504. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  505. if(timeout >= CHARGING_CAPABILITY_RESEND)
  506. {
  507. //timeout
  508. break;
  509. }
  510. }
  511. return result;
  512. }
  513. int DispenserWriteUserID(int socket, unsigned char connector, unsigned char se)
  514. {
  515. int rxLen = 0, timeout = 0;
  516. struct PACKET_STRUCTURE receiveBuffer;
  517. int result = FAIL;
  518. sendCmdWriteUserID(socket, connector, se);
  519. while(1)
  520. {
  521. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  522. {
  523. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  524. {
  525. ShowSocketData(&receiveBuffer);
  526. if(receiveBuffer.Header.len == 2)
  527. {
  528. if(receiveBuffer.Payload.reg == _Reg_User_ID && receiveBuffer.Payload.data[0] == _R_OK)
  529. {
  530. LOG_INFO("Write User ID OK");
  531. result = PASS;
  532. }
  533. }
  534. }
  535. }
  536. timeout += SOCKET_RECEIVE_INTERVAL;
  537. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  538. if(timeout >= CHARGING_CAPABILITY_RESEND)
  539. {
  540. //timeout
  541. break;
  542. }
  543. }
  544. return result;
  545. }
  546. int DispenserReadChargingPermission(int socket, unsigned char connector, unsigned char se)
  547. {
  548. int rxLen = 0, timeout = 0;
  549. struct PACKET_STRUCTURE receiveBuffer;
  550. int result = FAIL;
  551. sendCmdReadChargingPermission(socket, connector, se);
  552. while(1)
  553. {
  554. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  555. {
  556. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  557. {
  558. ShowSocketData(&receiveBuffer);
  559. if(receiveBuffer.Header.len == 3)
  560. {
  561. if(receiveBuffer.Payload.reg == _Reg_Charging_Permission && receiveBuffer.Payload.data[0] == _R_OK)
  562. {
  563. LOG_INFO("Charging Permission: %s", receiveBuffer.Payload.data[1] == _PS_Permitted ? "Permitted" : "Not Permitted");
  564. result = PASS;
  565. }
  566. }
  567. }
  568. }
  569. timeout += SOCKET_RECEIVE_INTERVAL;
  570. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  571. if(timeout >= CHARGING_CAPABILITY_RESEND)
  572. {
  573. //timeout
  574. break;
  575. }
  576. }
  577. return result;
  578. }
  579. void tcpSocketClientStart(void)
  580. {
  581. struct sockaddr_in info;
  582. struct timeval tv;
  583. uint8_t socketEnable;
  584. int sockfd = 0;
  585. unsigned char clientStatus = 0;
  586. unsigned char se = 0;
  587. //start dhcp client
  588. //udhcpc -i eth1 --script /root/dhcp_script/eth1.script
  589. //udhcpc -i eth1 -s /root/dhcp_script/eth1.script &
  590. //udhcpc -i eth0 -x hostname CSU3_DSYE601J0EW2PH-SERIALFORRD -s /root/dhcp_script/eth0.script
  591. for(;;)
  592. {
  593. bzero(&info,sizeof(info));
  594. info.sin_family = PF_INET;
  595. info.sin_addr.s_addr = inet_addr(SOCKET_SERVER_IP);
  596. info.sin_port = htons(TCP_LISTEN_PORT);
  597. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  598. if (sockfd == -1)
  599. {
  600. LOG_INFO("Fail to create a socket.");
  601. return;
  602. }
  603. LOG_INFO("TCP Client Start");
  604. if(connect(sockfd, (struct sockaddr *)&info, sizeof(info)) == -1)
  605. {
  606. LOG_INFO("Connection error");
  607. }
  608. else
  609. {
  610. LOG_INFO("Connect to %s success", inet_ntoa(info.sin_addr));
  611. tv.tv_sec = 0;
  612. tv.tv_usec = 500000;
  613. setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  614. socketEnable = ON;
  615. }
  616. while(socketEnable)
  617. {
  618. switch(clientStatus)
  619. {
  620. case 0:
  621. //5.1 Dispenser model name
  622. if(DispenserModelNameCheckIn(sockfd, se++) == PASS)
  623. {
  624. clientStatus = 1;
  625. }
  626. break;
  627. case 1:
  628. if(DispenserConnectorRequest(sockfd, se++) == PASS)
  629. {
  630. clientStatus = 2;
  631. }
  632. break;
  633. case 2:
  634. if(DispenserReadPowerCabinetStatus(sockfd, 0x01, se++) == PASS)
  635. {
  636. if(DispenserWriteStatusCode(sockfd, 0x01, se++) == PASS)
  637. {
  638. clientStatus = 3;
  639. }
  640. }
  641. break;
  642. case 3:
  643. if(DispenserWriteUserID(sockfd, 0x01, se++) == PASS)
  644. {
  645. clientStatus = 4;
  646. }
  647. break;
  648. case 4:
  649. if(DispenserReadChargingCapability(sockfd, 0x01, se++) == PASS)
  650. {
  651. }
  652. if(DispenserWriteChargingTarget(sockfd, 0x01, se++) == PASS)
  653. {
  654. }
  655. if(DispenserWritePlugInStatus(sockfd, 0x01, se++) == PASS)
  656. {
  657. }
  658. if(DispenserWriteConnectorState(sockfd, 0x01, se++) == PASS)
  659. {
  660. }
  661. if(DispenserReadChargingPermission(sockfd, 0x01, se++) == PASS)
  662. {
  663. }
  664. break;
  665. default:
  666. break;
  667. }
  668. }
  669. close(sockfd);
  670. sleep(5);
  671. return;
  672. }
  673. }
  674. //--------------------------------------------Socket Client End--------------------------------------------
  675. BOOL IsAvalibleGunType(char name, unsigned char *type)
  676. {
  677. // 20210817 remove type "R"
  678. char modelList[10] = {'J', 'U', 'V', 'E', 'F', 'G', 'T', 'D', 'K', 'P'};
  679. unsigned char typeList[10] = { 0, 1, 1, 1, 1, 2, 1, 1, 0, 1}; // 0 : Chademo, 1: CCS, 2: GB
  680. for(int i = 0; i < 11; i++)
  681. {
  682. if(name == modelList[i])
  683. {
  684. *type = typeList[i];
  685. return true;
  686. }
  687. }
  688. return false;
  689. }
  690. unsigned char ConnectorQuantityTypeParsing(unsigned char *modelName, unsigned char *type, unsigned char *physical)
  691. {
  692. unsigned char quantity = 0;
  693. for(int i = 0; i < 3; i++)
  694. {
  695. if(IsAvalibleGunType(modelName[7 + i], &type[quantity]))
  696. {
  697. physical[quantity] = modelName[7 + i];
  698. if(quantity < 2)
  699. {
  700. quantity++;
  701. }
  702. else
  703. {
  704. quantity = 0;
  705. break;
  706. }
  707. }
  708. }
  709. return quantity;
  710. }
  711. void SendPacket(int socket, struct PACKET_STRUCTURE *packet)
  712. {
  713. //ShowSocketData(packet);
  714. send(socket, packet, packet->Header.len + 4, 0);
  715. }
  716. void ModelNameResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  717. {
  718. struct PACKET_STRUCTURE sendBuffer;
  719. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  720. sendBuffer.Header.se = packet->Header.se;
  721. sendBuffer.Header.id = 0xFF;
  722. sendBuffer.Header.op = _Header_Response;
  723. sendBuffer.Header.len = 2;
  724. sendBuffer.Payload.reg = _Reg_Dispenser_Model_Name;
  725. sendBuffer.Payload.data[0] = result;
  726. SendPacket(socket, &sendBuffer);
  727. }
  728. void ConnectorIDResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result, unsigned char dispenserIndex)
  729. {
  730. struct PACKET_STRUCTURE sendBuffer;
  731. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  732. sendBuffer.Header.se = packet->Header.se;
  733. sendBuffer.Header.id = 0xFF;
  734. sendBuffer.Header.op = _Header_Response;
  735. sendBuffer.Header.len = 4;
  736. sendBuffer.Payload.reg = _Reg_Connector_ID;
  737. sendBuffer.Payload.data[0] = result;
  738. sendBuffer.Payload.data[1] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0];
  739. sendBuffer.Payload.data[2] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[1];
  740. SendPacket(socket, &sendBuffer);
  741. }
  742. // more message for debug
  743. struct PACKET_STRUCTURE CabinetStatusToDispenser[4];
  744. void PowerCabinetStatusResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  745. {
  746. struct PACKET_STRUCTURE sendBuffer;
  747. unsigned char MiscEventCode[7];
  748. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  749. sendBuffer.Header.se = packet->Header.se;
  750. sendBuffer.Header.id = packet->Header.id;
  751. sendBuffer.Header.op = _Header_Response;
  752. sendBuffer.Header.len = 2;
  753. sendBuffer.Payload.reg = _Reg_Power_Cabinet_Status;
  754. sendBuffer.Payload.data[0] = _R_OK;
  755. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0)
  756. {
  757. sendBuffer.Header.len += (ShmSysConfigAndInfo->SysWarningInfo.WarningCount * 6);
  758. for(int i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++)
  759. {
  760. memcpy(&sendBuffer.Payload.data[1 + (i * 6)], &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], 6);
  761. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] == '1')
  762. {
  763. sendBuffer.Payload.data[1 + (i * 6) + 1] = '4';
  764. }
  765. }
  766. }
  767. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.MiscNeedAnnouncement)
  768. {
  769. sendBuffer.Header.len += 6;
  770. memset(MiscEventCode, 0, sizeof(MiscEventCode));
  771. memcpy(MiscEventCode, "B40001", sizeof(MiscEventCode) - 1);
  772. memcpy(&sendBuffer.Payload.data[1 + (ShmSysConfigAndInfo->SysWarningInfo.WarningCount * 6)], &MiscEventCode[0], 6);
  773. }
  774. bool need_copy = false;
  775. if(sendBuffer.Header.len != CabinetStatusToDispenser[dispenserIndex].Header.len ||
  776. sendBuffer.Header.op != CabinetStatusToDispenser[dispenserIndex].Header.op ||
  777. sendBuffer.Payload.reg != CabinetStatusToDispenser[dispenserIndex].Payload.reg ||
  778. sendBuffer.Payload.data[0] != CabinetStatusToDispenser[dispenserIndex].Payload.data[0])
  779. {
  780. need_copy = true;
  781. }
  782. else
  783. {
  784. for(int i = 0; i < sendBuffer.Header.len - 2; i++)
  785. {
  786. if(sendBuffer.Payload.data[1 + i] != CabinetStatusToDispenser[dispenserIndex].Payload.data[1 + i])
  787. {
  788. need_copy = true;
  789. }
  790. }
  791. }
  792. if(need_copy)
  793. {
  794. char str[256];
  795. memcpy(&CabinetStatusToDispenser[dispenserIndex], &sendBuffer, sizeof(struct PACKET_STRUCTURE));
  796. sprintf(str, "Cabinet >> Dispenser %d Status Code:", dispenserIndex + 1);
  797. for(int i = 0; i < CabinetStatusToDispenser[dispenserIndex].Header.len - 2; i++)
  798. {
  799. char temp[8];
  800. sprintf(temp, " %02X", CabinetStatusToDispenser[dispenserIndex].Payload.data[1 + i]);
  801. strcat(str, temp);
  802. }
  803. LOG_INFO("%s", str);
  804. }
  805. SendPacket(socket, &sendBuffer);
  806. }
  807. void DispenserStatusResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  808. {
  809. struct PACKET_STRUCTURE sendBuffer;
  810. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  811. sendBuffer.Header.se = packet->Header.se;
  812. sendBuffer.Header.id = packet->Header.id;
  813. sendBuffer.Header.op = _Header_Response;
  814. sendBuffer.Header.len = 2;
  815. sendBuffer.Payload.reg = _Reg_Dispenser_Status;
  816. sendBuffer.Payload.data[0] = result;
  817. SendPacket(socket, &sendBuffer);
  818. }
  819. void GetPhysicalLimitVoltageAndCurrent(byte index, unsigned short *voltage, unsigned short *currrent)
  820. {
  821. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalVoltage != 0 &&
  822. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalVoltage <= *voltage)
  823. {
  824. *voltage = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalVoltage;
  825. }
  826. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalCurrent != 0 &&
  827. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalCurrent <= *currrent)
  828. {
  829. *currrent = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalCurrent;
  830. }
  831. }
  832. void GetModelNameLimitPower(byte index, unsigned short *power)
  833. {
  834. unsigned short limitPower = 0;
  835. unsigned char inUsingCnt = 0;
  836. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  837. {
  838. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  839. {
  840. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  841. }
  842. }
  843. if(ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  844. ShmChargerInfo->Control.ChargerRatingPower != 0)
  845. {
  846. limitPower = ShmChargerInfo->Control.ChargerRatingPower;
  847. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  848. {
  849. limitPower += ShmChargerInfo->ParallelCabinet.TotalParallelPower;
  850. }
  851. limitPower = (limitPower * ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity) / inUsingCnt;
  852. }
  853. if(limitPower != 0 && limitPower <= *power)
  854. {
  855. *power = limitPower;
  856. }
  857. }
  858. void GetConfigLimitPowerAndCurrent(byte index, unsigned short *power, unsigned short *currrent)
  859. {
  860. unsigned short limitPower = 0, limitCurrent = 0;
  861. unsigned char inUsingCnt = 0;
  862. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  863. {
  864. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  865. {
  866. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  867. }
  868. }
  869. if(ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  870. ShmSysConfigAndInfo->SysConfig.MaxChargingPower != 0)
  871. {
  872. limitPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  873. limitPower = (limitPower * ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity) / inUsingCnt;
  874. }
  875. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].MaxTotalChargingPower = limitPower;
  876. if(ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  877. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent != 0)
  878. {
  879. limitCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  880. limitCurrent = (limitCurrent * ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity) / inUsingCnt;
  881. }
  882. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].MaxTotalChargingCurrent = limitCurrent;
  883. if(limitPower != 0 && limitPower <= *power)
  884. {
  885. *power = limitPower;
  886. }
  887. if(limitCurrent != 0 && limitCurrent <= *currrent)
  888. {
  889. *currrent = limitCurrent;
  890. }
  891. }
  892. void GetChargingProfileLimit(byte index, unsigned short *power, unsigned short *currrent)
  893. {
  894. if((chargingInfo[index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[index]->SystemStatus <= S_CHARGING) ||
  895. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  896. {
  897. if(chargingInfo[index]->ChargingProfilePower >= 0 && *power > ((int)chargingInfo[index]->ChargingProfilePower / 100))
  898. {
  899. *power = (int)chargingInfo[index]->ChargingProfilePower / 100;
  900. }
  901. if(chargingInfo[index]->ChargingProfileCurrent >= 0 && *currrent > (int)chargingInfo[index]->ChargingProfileCurrent)
  902. {
  903. *currrent = (int)chargingInfo[index]->ChargingProfileCurrent;
  904. }
  905. }
  906. }
  907. void GetMaxChargingProfileLimit(byte index, unsigned short *power, unsigned short *currrent)
  908. {
  909. unsigned short limitPower = 0, limitCurrent = 0;
  910. unsigned char inUsingCnt = 0;
  911. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  912. {
  913. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  914. {
  915. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  916. }
  917. }
  918. // max charging profile
  919. if(ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  920. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower >= 0)
  921. {
  922. limitPower = (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 100;
  923. limitPower = (limitPower * ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity) / inUsingCnt;
  924. if(*power > limitPower)
  925. {
  926. *power = limitPower;
  927. }
  928. if (chargingInfo[index]->EvBatterytargetVoltage > 0 &&
  929. (int)chargingInfo[index]->PresentChargingVoltage > 0)
  930. {
  931. limitCurrent = (limitPower * 100 / chargingInfo[index]->PresentChargingVoltage) * 10;
  932. if(*currrent > limitCurrent)
  933. {
  934. *currrent = limitCurrent;
  935. }
  936. }
  937. }
  938. }
  939. void ChargingCapabilityResponse(int socket, struct PACKET_STRUCTURE *packet)
  940. {
  941. struct PACKET_STRUCTURE sendBuffer;
  942. unsigned short voltage = 0, current = 0, power = 0;
  943. unsigned char currency = 0;
  944. unsigned int price = 0, cost = 0, account = 0, discount = 0;
  945. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  946. voltage = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.MaximumChargingVoltage;
  947. current = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.AvailableChargingCurrent;
  948. power = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.AvailableChargingPower;
  949. GetPhysicalLimitVoltageAndCurrent(packet->Header.id - 1, &voltage, &current);
  950. GetModelNameLimitPower(packet->Header.id - 1, &power);
  951. GetConfigLimitPowerAndCurrent(packet->Header.id - 1, &power, &current);
  952. GetChargingProfileLimit(packet->Header.id - 1, &power, &current);
  953. GetMaxChargingProfileLimit(packet->Header.id - 1, &power, &current);
  954. currency = ShmChargerInfo->CabinetMiscValue.Currency;
  955. price = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].UserPrice;
  956. cost = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].TotalCost;
  957. account = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AccountBalance;
  958. if((ConnectorCapability[packet->Header.id - 1].MaxOuputVoltage / 10) != (voltage / 10) ||
  959. (ConnectorCapability[packet->Header.id - 1].MaxOuputCurrent / 10) != (current / 10) ||
  960. (ConnectorCapability[packet->Header.id - 1].MaxOuputPower / 10) != (power / 10))
  961. {
  962. LOG_INFO("Connector %d Capability Voltage: %4d.%d V, Current: %3d.%d A, Power: %d kW", packet->Header.id,
  963. (voltage / 10), (voltage % 10), (current / 10), (current % 10), (power / 10));
  964. }
  965. if(ConnectorCapability[packet->Header.id - 1].Currency != currency ||
  966. ConnectorCapability[packet->Header.id - 1].UserPrice != price ||
  967. ConnectorCapability[packet->Header.id - 1].TotalCost != cost ||
  968. ConnectorCapability[packet->Header.id - 1].AccountBalance != account ||
  969. ConnectorCapability[packet->Header.id - 1].CostDiscount != discount)
  970. {
  971. LOG_INFO("Connector %d Currency: %d, UserPrice: %d, Cost: %d, Account: %d, Discount: %d", packet->Header.id,
  972. (int)(currency), (int)(price), (int)(cost), (int)(account), (int)(discount));
  973. }
  974. ConnectorCapability[packet->Header.id - 1].MaxOuputVoltage = voltage;
  975. ConnectorCapability[packet->Header.id - 1].MaxOuputCurrent = current;
  976. ConnectorCapability[packet->Header.id - 1].MaxOuputPower = power;
  977. ConnectorCapability[packet->Header.id - 1].Currency = currency;
  978. ConnectorCapability[packet->Header.id - 1].UserPrice = price;
  979. ConnectorCapability[packet->Header.id - 1].TotalCost = cost;
  980. ConnectorCapability[packet->Header.id - 1].AccountBalance = account;
  981. ConnectorCapability[packet->Header.id - 1].CostDiscount = discount;
  982. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].CapabilityVoltage = voltage;
  983. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].CapabilityCurrent = current;
  984. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].CapabilityPower = power;
  985. sendBuffer.Header.se = packet->Header.se;
  986. sendBuffer.Header.id = packet->Header.id;
  987. sendBuffer.Header.op = _Header_Response;
  988. sendBuffer.Header.len = 25;
  989. sendBuffer.Payload.reg = _Reg_Charging_Capability;
  990. sendBuffer.Payload.data[0] = _R_OK;
  991. sendBuffer.Payload.data[1] = ((voltage >> 8) & 0xFF);
  992. sendBuffer.Payload.data[2] = (voltage & 0xFF);
  993. sendBuffer.Payload.data[3] = ((current >> 8) & 0xFF);
  994. sendBuffer.Payload.data[4] = (current & 0xFF);
  995. sendBuffer.Payload.data[5] = ((power >> 8) & 0xFF);
  996. sendBuffer.Payload.data[6] = (power & 0xFF);
  997. sendBuffer.Payload.data[7] = currency;
  998. sendBuffer.Payload.data[8] = ((price >> 24) & 0xFF);
  999. sendBuffer.Payload.data[9] = ((price >> 16) & 0xFF);
  1000. sendBuffer.Payload.data[10] = ((price >> 8) & 0xFF);
  1001. sendBuffer.Payload.data[11] = (price & 0xFF);
  1002. sendBuffer.Payload.data[12] = ((cost >> 24) & 0xFF);
  1003. sendBuffer.Payload.data[13] = ((cost >> 16) & 0xFF);
  1004. sendBuffer.Payload.data[14] = ((cost >> 8) & 0xFF);
  1005. sendBuffer.Payload.data[15] = (cost & 0xFF);
  1006. sendBuffer.Payload.data[16] = ((account >> 24) & 0xFF);
  1007. sendBuffer.Payload.data[17] = ((account >> 16) & 0xFF);
  1008. sendBuffer.Payload.data[18] = ((account >> 8) & 0xFF);
  1009. sendBuffer.Payload.data[19] = (account & 0xFF);
  1010. sendBuffer.Payload.data[20] = ((discount >> 24) & 0xFF);
  1011. sendBuffer.Payload.data[21] = ((discount >> 16) & 0xFF);
  1012. sendBuffer.Payload.data[22] = ((discount >> 8) & 0xFF);
  1013. sendBuffer.Payload.data[23] = (discount & 0xFF);
  1014. SendPacket(socket, &sendBuffer);
  1015. }
  1016. void ChargingTargetResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1017. {
  1018. struct PACKET_STRUCTURE sendBuffer;
  1019. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1020. sendBuffer.Header.se = packet->Header.se;
  1021. sendBuffer.Header.id = packet->Header.id;
  1022. sendBuffer.Header.op = _Header_Response;
  1023. sendBuffer.Header.len = 2;
  1024. sendBuffer.Payload.reg = _Reg_Charging_Target;
  1025. sendBuffer.Payload.data[0] = result;
  1026. SendPacket(socket, &sendBuffer);
  1027. }
  1028. void FirmwareUpgradeResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1029. {
  1030. struct PACKET_STRUCTURE sendBuffer;
  1031. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1032. sendBuffer.Header.se = packet->Header.se;
  1033. sendBuffer.Header.id = packet->Header.id;
  1034. sendBuffer.Header.op = _Header_Response;
  1035. sendBuffer.Header.len = 3;
  1036. sendBuffer.Payload.reg = _Reg_Software_Update;
  1037. sendBuffer.Payload.data[0] = _R_OK;
  1038. sendBuffer.Payload.data[1] = result;
  1039. if(result == _R_NeedUpgrade)
  1040. {
  1041. int length = strlen(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FwFileName);
  1042. memcpy(&sendBuffer.Payload.data[2], ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FwFileName, length);
  1043. sendBuffer.Header.len += length;
  1044. }
  1045. SendPacket(socket, &sendBuffer);
  1046. }
  1047. void PlugInStatusResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1048. {
  1049. struct PACKET_STRUCTURE sendBuffer;
  1050. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1051. sendBuffer.Header.se = packet->Header.se;
  1052. sendBuffer.Header.id = packet->Header.id;
  1053. sendBuffer.Header.op = _Header_Response;
  1054. sendBuffer.Header.len = 2;
  1055. sendBuffer.Payload.reg = _Reg_Plug_In_Status;
  1056. sendBuffer.Payload.data[0] = result;
  1057. SendPacket(socket, &sendBuffer);
  1058. }
  1059. void ConnectorStateResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1060. {
  1061. struct PACKET_STRUCTURE sendBuffer;
  1062. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1063. sendBuffer.Header.se = packet->Header.se;
  1064. sendBuffer.Header.id = packet->Header.id;
  1065. sendBuffer.Header.op = _Header_Response;
  1066. sendBuffer.Header.len = 2;
  1067. sendBuffer.Payload.reg = _Reg_Connector_State;
  1068. sendBuffer.Payload.data[0] = result;
  1069. SendPacket(socket, &sendBuffer);
  1070. }
  1071. void UserIDResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1072. {
  1073. struct PACKET_STRUCTURE sendBuffer;
  1074. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1075. sendBuffer.Header.se = packet->Header.se;
  1076. sendBuffer.Header.id = packet->Header.id;
  1077. sendBuffer.Header.op = _Header_Response;
  1078. sendBuffer.Header.len = 3;
  1079. sendBuffer.Payload.reg = _Reg_User_ID;
  1080. sendBuffer.Payload.data[0] = result == _DAS_Wait ? _R_NG : _R_OK;
  1081. sendBuffer.Payload.data[1] = result == _DAS_Allowed ? _PS_Permitted : _PS_NotPermitted;
  1082. SendPacket(socket, &sendBuffer);
  1083. }
  1084. void ChargingPermissionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1085. {
  1086. struct PACKET_STRUCTURE sendBuffer;
  1087. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1088. sendBuffer.Header.se = packet->Header.se;
  1089. sendBuffer.Header.id = packet->Header.id;
  1090. sendBuffer.Header.op = _Header_Response;
  1091. sendBuffer.Header.len = 3;
  1092. sendBuffer.Payload.reg = _Reg_Charging_Permission;
  1093. sendBuffer.Payload.data[0] = result == _DAS_Wait ? _R_NG : _R_OK;
  1094. sendBuffer.Payload.data[1] = result == _DAS_Allowed ? _PS_Permitted : _PS_NotPermitted;
  1095. SendPacket(socket, &sendBuffer);
  1096. }
  1097. bool Is_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1098. {
  1099. return (ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue & misc_cmd) > 0 ? true : false;
  1100. }
  1101. void Set_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1102. {
  1103. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue |= misc_cmd;
  1104. }
  1105. void Clean_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1106. {
  1107. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue &= ~misc_cmd;
  1108. }
  1109. bool Is_Connector_MiscCommand(int connector, int misc_cmd)
  1110. {
  1111. return (ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue & misc_cmd) > 0 ? true : false;
  1112. }
  1113. void Set_Connector_MiscCommand(int connector, int misc_cmd)
  1114. {
  1115. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue |= misc_cmd;
  1116. }
  1117. void Clean_Connector_MiscCommand(int connector, int misc_cmd)
  1118. {
  1119. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue &= ~misc_cmd;
  1120. }
  1121. void AddMiscCommand(struct PACKET_STRUCTURE *packet, struct MISC_COMMAND *misc)
  1122. {
  1123. packet->Payload.data[packet->Header.len - 1 + 0] = ((misc->Command >> 8) & 0xFF);
  1124. packet->Payload.data[packet->Header.len - 1 + 1] = ((misc->Command >> 0) & 0xFF);
  1125. packet->Payload.data[packet->Header.len - 1 + 2] = ((misc->Value >> 24) & 0xFF);
  1126. packet->Payload.data[packet->Header.len - 1 + 3] = ((misc->Value >> 16) & 0xFF);
  1127. packet->Payload.data[packet->Header.len - 1 + 4] = ((misc->Value >> 8) & 0xFF);
  1128. packet->Payload.data[packet->Header.len - 1 + 5] = ((misc->Value >> 0) & 0xFF);
  1129. packet->Header.len += 6;
  1130. }
  1131. bool Is_LcmPageChange_Req(int gun_index, int lcm_req)
  1132. {
  1133. if((ShmChargerInfo->ConnectorPageReq[gun_index].CtrlValue & lcm_req) > 0)
  1134. {
  1135. return true;
  1136. }
  1137. return false;
  1138. }
  1139. void Clean_LcmPage_Req(int gun_index, int lcm_req)
  1140. {
  1141. ShmChargerInfo->ConnectorPageReq[gun_index].CtrlValue &= ~lcm_req;
  1142. }
  1143. // LOG_INFO("Cabinet >> Connector %d Availability [SelfTestFail]", packet->Header.id);
  1144. // LOG_INFO("Cabinet >> Connector %d Availability [Disable]", packet->Header.id);
  1145. // LOG_INFO("Cabinet >> Connector %d Availability [Inoperative]", packet->Header.id);
  1146. // LOG_INFO("Cabinet >> Connector %d Availability [Enable]", packet->Header.id);
  1147. // LOG_INFO("Cabinet >> Connector %d RemoteStart [%d]", packet->Header.id, misc.Value);
  1148. // LOG_INFO("Cabinet >> Connector %d Balance [%d]", packet->Header.id, (misc.Value));
  1149. // LOG_INFO("Cabinet >> Connector %d Reservation [Enable]", packet->Header.id);
  1150. // LOG_INFO("Cabinet >> Connector %d QR Code [Enable]", packet->Header.id);
  1151. // LOG_INFO("Cabinet >> Dispenser %d PlugTimeout [%d]", dispenserIndex + 1, misc.Value);
  1152. // LOG_INFO("Cabinet >> Dispenser %d DefaultPrice [%d]", dispenserIndex + 1, misc.Value);
  1153. // LOG_INFO("Cabinet >> Dispenser %d Currency [%d]", dispenserIndex + 1, misc.Value);
  1154. // LOG_INFO("Cabinet >> Dispenser %d BackendConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1155. // LOG_INFO("Cabinet >> Dispenser %d BackendConn [%d]", dispenserIndex + 1, (misc.Value));
  1156. // LOG_INFO("Cabinet >> Dispenser %d EthernetConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1157. // LOG_INFO("Cabinet >> Dispenser %d EthernetConn [%d]", dispenserIndex + 1, (misc.Value));
  1158. // LOG_INFO("Cabinet >> Dispenser %d WiFiConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1159. // LOG_INFO("Cabinet >> Dispenser %d WiFiConn [%d]", dispenserIndex + 1, (misc.Value));
  1160. // LOG_INFO("Cabinet >> Dispenser %d TelModemConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1161. // LOG_INFO("Cabinet >> Dispenser %d TelModemConn [%d]", dispenserIndex + 1, (misc.Value));
  1162. // LOG_INFO("Cabinet >> Dispenser %d BillingInfo [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1163. // LOG_INFO("Cabinet >> Dispenser %d StopButton [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1164. // LOG_INFO("Cabinet >> Dispenser %d HardReboot [%d]", dispenserIndex + 1, misc.Value);
  1165. // LOG_INFO("Cabinet >> Dispenser %d SoftReset [%d]", dispenserIndex + 1, misc.Value);
  1166. // LOG_INFO("Cabinet >> Dispenser %d AuthMode [%s]", dispenserIndex + 1, misc.Value ? "Disable" : "Enable");
  1167. // LOG_INFO("Cabinet >> Dispenser %d EVCCID [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1168. // LOG_INFO("Cabinet >> Dispenser %d LEDIntensity [%s]", dispenserIndex + 1, strIntensity[misc.Value]);
  1169. // LOG_INFO("Cabinet >> Dispenser %d LEDIntensity [%d]", dispenserIndex + 1, misc.Value);
  1170. // LOG_INFO("Cabinet >> Dispenser %d Station Info [%d]", dispenserIndex + 1, misc.Value);
  1171. void MiscCmdRes(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1172. {
  1173. char *strConnection[] = {STR_CONN_DISABLE, STR_CONN_CONNECTED, STR_CONN_DISCONNECTED};
  1174. char *strIntensity[] = {STR_DARKEST, STR_MEDIUM, STR_BRIGHTEST};
  1175. char *strLcmPage[] = {STR_PAGE_NONE, STR_REMOTE_NO_ID, STR_REFUND_AMOUNT, STR_PREPAYMENT, STR_PAYMENT_FAIL};
  1176. struct PACKET_STRUCTURE sendBuffer;
  1177. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1178. sendBuffer.Header.se = packet->Header.se;
  1179. sendBuffer.Header.id = packet->Header.id;
  1180. sendBuffer.Header.op = _Header_Response;
  1181. sendBuffer.Header.len = 2;
  1182. sendBuffer.Payload.reg = _Reg_Misc_Control;
  1183. sendBuffer.Payload.data[0] = result;
  1184. if(result == _R_OK)
  1185. {
  1186. struct MISC_COMMAND misc;
  1187. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_AVAILABILITY))
  1188. {
  1189. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_AVAILABILITY);
  1190. misc.Command = _MiscCmd_ChangeOperative;
  1191. unsigned char IsAvailable = true;
  1192. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  1193. {
  1194. IsAvailable = false;
  1195. LOG_INFO("Cabinet >> Connector %d Availability [SelfTestFail]", packet->Header.id);
  1196. }
  1197. else if(!ShmChargerInfo->Control.GunAvailable[packet->Header.id - 1])
  1198. {
  1199. IsAvailable = false;
  1200. LOG_INFO("Cabinet >> Connector %d Availability [Disable]", packet->Header.id);
  1201. }
  1202. else if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.IsAvailable)
  1203. {
  1204. IsAvailable = false;
  1205. LOG_INFO("Cabinet >> Connector %d Availability [Inoperative]", packet->Header.id);
  1206. }
  1207. else
  1208. {
  1209. LOG_INFO("Cabinet >> Connector %d Availability [Enable]", packet->Header.id);
  1210. }
  1211. misc.Value = IsAvailable;
  1212. AddMiscCommand(&sendBuffer, &misc);
  1213. }
  1214. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_REMOTE_START))
  1215. {
  1216. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_REMOTE_START);
  1217. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStartConfirm = true;
  1218. misc.Command = _MiscCmd_RemoteStart;
  1219. misc.Value = true;
  1220. LOG_INFO("Cabinet >> Connector %d RemoteStart [%d]", packet->Header.id, misc.Value);
  1221. AddMiscCommand(&sendBuffer, &misc);
  1222. }
  1223. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_REMOTE_STOP))
  1224. {
  1225. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_REMOTE_STOP);
  1226. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStopConfirm = true;
  1227. misc.Command = _MiscCmd_RemoteStop;
  1228. misc.Value = true;
  1229. LOG_INFO("Cabinet >> Connector %d Remote Stop: %d", packet->Header.id, misc.Value);
  1230. AddMiscCommand(&sendBuffer, &misc);
  1231. }
  1232. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_UNLOCK))
  1233. {
  1234. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_UNLOCK);
  1235. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopConfirm = true;
  1236. misc.Command = _MiscCmd_UnlockStop;
  1237. misc.Value = true;
  1238. LOG_INFO("Cabinet >> Connector %d Unlock Stop: %d", packet->Header.id, misc.Value);
  1239. AddMiscCommand(&sendBuffer, &misc);
  1240. }
  1241. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_ACCOUNT_BALANCE))
  1242. {
  1243. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_ACCOUNT_BALANCE);
  1244. misc.Command = _MiscCmd_AccountBalance;
  1245. misc.Value = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AccountBalance;
  1246. LOG_INFO("Cabinet >> Connector %d Balance [%d]", packet->Header.id, (misc.Value));
  1247. AddMiscCommand(&sendBuffer, &misc);
  1248. }
  1249. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_RESERVATION))
  1250. {
  1251. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_RESERVATION);
  1252. misc.Command = _MiscCmd_Reservation;
  1253. misc.Value = true;
  1254. LOG_INFO("Cabinet >> Connector %d Reservation [Enable]", packet->Header.id);
  1255. AddMiscCommand(&sendBuffer, &misc);
  1256. }
  1257. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_QRCODE_INFO))
  1258. {
  1259. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_QRCODE_INFO);
  1260. misc.Command = _MiscCmd_QRCodeRequest;
  1261. misc.Value = true;
  1262. LOG_INFO("Cabinet >> Connector %d QR Code [Enable]", packet->Header.id);
  1263. AddMiscCommand(&sendBuffer, &misc);
  1264. }
  1265. // announce misc command to first connector of the dispenser
  1266. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] == packet->Header.id)
  1267. {
  1268. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CONNECTION_TIMEOUT))
  1269. {
  1270. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CONNECTION_TIMEOUT);
  1271. misc.Command = _MiscCmd_ConnectorTimeout;
  1272. misc.Value = ShmChargerInfo->CabinetMiscValue.ConnectionTimeout;
  1273. LOG_INFO("Cabinet >> Dispenser %d PlugTimeout [%d]", dispenserIndex + 1, misc.Value);
  1274. AddMiscCommand(&sendBuffer, &misc);
  1275. }
  1276. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_DEFAULT_PRICE))
  1277. {
  1278. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_DEFAULT_PRICE);
  1279. misc.Command = _MiscCmd_DefaultPrice;
  1280. misc.Value = ShmChargerInfo->CabinetMiscValue.DefaultPrice;
  1281. LOG_INFO("Cabinet >> Dispenser %d DefaultPrice [%d]", dispenserIndex + 1, misc.Value);
  1282. AddMiscCommand(&sendBuffer, &misc);
  1283. }
  1284. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CURRENCY))
  1285. {
  1286. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CURRENCY);
  1287. misc.Command = _MiscCmd_Currency;
  1288. misc.Value = ShmChargerInfo->CabinetMiscValue.Currency;
  1289. LOG_INFO("Cabinet >> Dispenser %d Currency [%d]", dispenserIndex + 1, misc.Value);
  1290. AddMiscCommand(&sendBuffer, &misc);
  1291. }
  1292. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BACKEND_STATUS))
  1293. {
  1294. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BACKEND_STATUS);
  1295. misc.Command = _MiscCmd_BackendStatus;
  1296. misc.Value = ShmChargerInfo->CabinetMiscValue.BackendStatus;
  1297. if(misc.Value <= _Connnection_Disconnected)
  1298. {
  1299. LOG_INFO("Cabinet >> Dispenser %d BackendConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1300. }
  1301. else
  1302. {
  1303. LOG_INFO("Cabinet >> Dispenser %d BackendConn [%d]", dispenserIndex + 1, (misc.Value));
  1304. }
  1305. AddMiscCommand(&sendBuffer, &misc);
  1306. }
  1307. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_ETHERNET_STATUS))
  1308. {
  1309. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_ETHERNET_STATUS);
  1310. misc.Command = _MiscCmd_EthernetStatus;
  1311. misc.Value = ShmChargerInfo->CabinetMiscValue.EthernetStatus;
  1312. if(misc.Value <= _Connnection_Disconnected)
  1313. {
  1314. LOG_INFO("Cabinet >> Dispenser %d EthernetConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1315. }
  1316. else
  1317. {
  1318. LOG_INFO("Cabinet >> Dispenser %d EthernetConn [%d]", dispenserIndex + 1, (misc.Value));
  1319. }
  1320. AddMiscCommand(&sendBuffer, &misc);
  1321. }
  1322. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_WIFI_STATUS))
  1323. {
  1324. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_WIFI_STATUS);
  1325. misc.Command = _MiscCmd_WiFiStatus;
  1326. misc.Value = ShmChargerInfo->CabinetMiscValue.WiFiStatus;
  1327. if(misc.Value <= _Connnection_Disconnected)
  1328. {
  1329. LOG_INFO("Cabinet >> Dispenser %d WiFiConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1330. }
  1331. else
  1332. {
  1333. LOG_INFO("Cabinet >> Dispenser %d WiFiConn [%d]", dispenserIndex + 1, (misc.Value));
  1334. }
  1335. AddMiscCommand(&sendBuffer, &misc);
  1336. }
  1337. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_TELCOM_MODEM_STATUS))
  1338. {
  1339. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_TELCOM_MODEM_STATUS);
  1340. misc.Command = _MiscCmd_4GStatus;
  1341. misc.Value = ShmChargerInfo->CabinetMiscValue.TelcomModemStatus;
  1342. if(misc.Value <= _Connnection_Disconnected)
  1343. {
  1344. LOG_INFO("Cabinet >> Dispenser %d TelModemConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1345. }
  1346. else
  1347. {
  1348. LOG_INFO("Cabinet >> Dispenser %d TelModemConn [%d]", dispenserIndex + 1, (misc.Value));
  1349. }
  1350. AddMiscCommand(&sendBuffer, &misc);
  1351. }
  1352. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BILLING_STATUS))
  1353. {
  1354. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BILLING_STATUS);
  1355. misc.Command = _MiscCmd_Billing;
  1356. misc.Value = ShmChargerInfo->CabinetMiscValue.BillingStatus;
  1357. LOG_INFO("Cabinet >> Dispenser %d BillingInfo [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1358. AddMiscCommand(&sendBuffer, &misc);
  1359. }
  1360. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STOP_BUTTON_STATUS))
  1361. {
  1362. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STOP_BUTTON_STATUS);
  1363. misc.Command = _MiscCmd_StopButton;
  1364. misc.Value = ShmChargerInfo->CabinetMiscValue.StopChargingButton;
  1365. LOG_INFO("Cabinet >> Dispenser %d StopButton [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1366. AddMiscCommand(&sendBuffer, &misc);
  1367. }
  1368. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_HARDWARE_REBOOT))
  1369. {
  1370. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_HARDWARE_REBOOT);
  1371. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootConfirm = true;
  1372. misc.Command = _MiscCmd_HardwareReboot;
  1373. misc.Value = true;
  1374. LOG_INFO("Cabinet >> Dispenser %d HardReboot [%d]", dispenserIndex + 1, misc.Value);
  1375. AddMiscCommand(&sendBuffer, &misc);
  1376. }
  1377. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_SOFTWARE_RESET))
  1378. {
  1379. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_SOFTWARE_RESET);
  1380. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetConfirm = true;
  1381. misc.Command = _MiscCmd_SoftwareRestart;
  1382. misc.Value = true;
  1383. LOG_INFO("Cabinet >> Dispenser %d SoftReset [%d]", dispenserIndex + 1, misc.Value);
  1384. AddMiscCommand(&sendBuffer, &misc);
  1385. }
  1386. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_AUTH_MODE_CONFIG))
  1387. {
  1388. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_AUTH_MODE_CONFIG);
  1389. misc.Command = _MiscCmd_AuthDisable;
  1390. misc.Value = ShmChargerInfo->CabinetMiscValue.AuthModeConfig;
  1391. LOG_INFO("Cabinet >> Dispenser %d AuthMode [%s]", dispenserIndex + 1, misc.Value ? "Disable" : "Enable");
  1392. AddMiscCommand(&sendBuffer, &misc);
  1393. }
  1394. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_EVCCID_CONFIG))
  1395. {
  1396. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_EVCCID_CONFIG);
  1397. misc.Command = _MiscCmd_EVCCIDEnable;
  1398. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName[0] == 'D' &&
  1399. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName[0] == 'X')
  1400. {
  1401. misc.Value = ShmChargerInfo->CabinetMiscValue.AuthModeConfig > 0 ? 0 : 1;
  1402. }
  1403. else
  1404. {
  1405. misc.Value = ShmChargerInfo->CabinetMiscValue.EVCCIDConfig;
  1406. }
  1407. LOG_INFO("Cabinet >> Dispenser %d EVCCID [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1408. AddMiscCommand(&sendBuffer, &misc);
  1409. }
  1410. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_LED_INTENSITY))
  1411. {
  1412. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_LED_INTENSITY);
  1413. misc.Command = _MiscCmd_LEDIntensity;
  1414. misc.Value = ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity;
  1415. if(misc.Value <= _LED_INTENSITY_BRIGHTEST)
  1416. {
  1417. LOG_INFO("Cabinet >> Dispenser %d LEDIntensity [%s]", dispenserIndex + 1, strIntensity[misc.Value]);
  1418. }
  1419. else
  1420. {
  1421. LOG_INFO("Cabinet >> Dispenser %d LEDIntensity [%d]", dispenserIndex + 1, misc.Value);
  1422. }
  1423. AddMiscCommand(&sendBuffer, &misc);
  1424. }
  1425. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STATION_INFO))
  1426. {
  1427. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STATION_INFO);
  1428. misc.Command = _MiscCmd_StationInfo;
  1429. misc.Value = true;
  1430. LOG_INFO("Cabinet >> Dispenser %d Station Info [%d]", dispenserIndex + 1, misc.Value);
  1431. AddMiscCommand(&sendBuffer, &misc);
  1432. }
  1433. }
  1434. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CHANGE_LCM_INFO))
  1435. {
  1436. int gun = packet->Header.id - 1;
  1437. for(int i = 0; i < _LCM_Page_PaymentFail; i++)
  1438. {
  1439. if(Is_LcmPageChange_Req(gun, i + 1))
  1440. {
  1441. Clean_LcmPage_Req(gun, i + 1);
  1442. misc.Command = _MiscCmd_ChangeLcmPage;
  1443. misc.Value = i + 1;
  1444. LOG_INFO("Cabinet >> Connector %d LCM_Page [%s]", packet->Header.id, strLcmPage[misc.Value]);
  1445. AddMiscCommand(&sendBuffer, &misc);
  1446. }
  1447. }
  1448. }
  1449. }
  1450. SendPacket(socket, &sendBuffer);
  1451. }
  1452. void WriteCsuVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1453. {
  1454. struct PACKET_STRUCTURE sendBuffer;
  1455. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1456. sendBuffer.Header.se = packet->Header.se;
  1457. sendBuffer.Header.id = packet->Header.id;
  1458. sendBuffer.Header.op = _Header_Response;
  1459. sendBuffer.Header.len = 2;
  1460. sendBuffer.Payload.reg = _Reg_Report_Csu_Version;
  1461. sendBuffer.Payload.data[0] = result;
  1462. SendPacket(socket, &sendBuffer);
  1463. }
  1464. void WriteOtherVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1465. {
  1466. struct PACKET_STRUCTURE sendBuffer;
  1467. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1468. sendBuffer.Header.se = packet->Header.se;
  1469. sendBuffer.Header.id = packet->Header.id;
  1470. sendBuffer.Header.op = _Header_Response;
  1471. sendBuffer.Header.len = 2;
  1472. sendBuffer.Payload.reg = _Reg_Report_Other_Version;
  1473. sendBuffer.Payload.data[0] = result;
  1474. SendPacket(socket, &sendBuffer);
  1475. }
  1476. void WriteChargingInfoResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1477. {
  1478. struct PACKET_STRUCTURE sendBuffer;
  1479. unsigned short voltage = 0, current = 0;
  1480. unsigned char gun = packet->Header.id - 1;
  1481. #if 0
  1482. // for safety test
  1483. voltage = (unsigned short)(chargingInfo[gun]->PresentChargingVoltage * 10);
  1484. current = (unsigned short)(chargingInfo[gun]->PresentChargingCurrent * 10);
  1485. voltage -= ((current * 8 * 10) / 10000);
  1486. #endif
  1487. if(chargingInfo[gun]->PantographFlag == YES)
  1488. {
  1489. voltage = (unsigned short)(chargingInfo[gun]->PresentChargingVoltage * 10);
  1490. current = (unsigned short)(chargingInfo[gun]->PresentChargingCurrent * 10);
  1491. }
  1492. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1493. sendBuffer.Header.se = packet->Header.se;
  1494. sendBuffer.Header.id = packet->Header.id;
  1495. sendBuffer.Header.op = _Header_Response;
  1496. sendBuffer.Header.len = 6;
  1497. sendBuffer.Payload.reg = _Reg_Charging_Info;
  1498. sendBuffer.Payload.data[0] = result;
  1499. sendBuffer.Payload.data[1] = (voltage >> 8) & 0xFF;
  1500. sendBuffer.Payload.data[2] = (voltage >> 0) & 0xFF;
  1501. sendBuffer.Payload.data[3] = (current >> 8) & 0xFF;
  1502. sendBuffer.Payload.data[4] = (current >> 0) & 0xFF;
  1503. SendPacket(socket, &sendBuffer);
  1504. }
  1505. int GetChargerSystemId(char *id)
  1506. {
  1507. int len = 0;
  1508. strcpy((char *)id, "");
  1509. if(ShmChargerInfo->Control.CustomerCode != _CUSTOMER_CODE_Audi)
  1510. {
  1511. ShmChargerInfo->AuthInfo.QRCodeMode = ShmSysConfigAndInfo->SysConfig.QRCodeMadeMode;
  1512. }
  1513. switch(ShmChargerInfo->AuthInfo.QRCodeMode)
  1514. {
  1515. case _QR_MODE_Customized:
  1516. strcpy((char *)id, (char *)ShmSysConfigAndInfo->SysConfig.QRCodeContent);
  1517. break;
  1518. case _QR_MODE_ChargeBoxId:
  1519. strcpy((char *)id, (char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1520. break;
  1521. case _QR_MODE_Default:
  1522. default:
  1523. strcat((char *)id, (char *)ShmSysConfigAndInfo->SysConfig.SystemId);
  1524. break;
  1525. }
  1526. len = strlen((char *)id);
  1527. return len;
  1528. }
  1529. void ChargerSystemIdResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1530. {
  1531. struct PACKET_STRUCTURE sendBuffer;
  1532. char system_id[128];
  1533. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1534. sendBuffer.Header.se = packet->Header.se;
  1535. sendBuffer.Header.id = packet->Header.id;
  1536. sendBuffer.Header.op = _Header_Response;
  1537. sendBuffer.Header.len = 2;
  1538. sendBuffer.Payload.reg = _Reg_Charger_System_Id;
  1539. sendBuffer.Payload.data[0] = result;
  1540. if(result == _R_OK)
  1541. {
  1542. int strLen = 0;
  1543. strLen = GetChargerSystemId(system_id);
  1544. memcpy(&sendBuffer.Payload.data[sendBuffer.Header.len - 1], (char *)system_id, strLen);
  1545. sendBuffer.Header.len += strLen + 1;
  1546. char TimeSync[32];
  1547. struct timeb SeqEndTime;
  1548. struct tm *tm;
  1549. ftime(&SeqEndTime);
  1550. SeqEndTime.time = time(NULL);
  1551. tm=localtime(&SeqEndTime.time);
  1552. memset(TimeSync, 0x00, 32);
  1553. 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);
  1554. strLen = strlen(TimeSync);
  1555. memcpy(&sendBuffer.Payload.data[sendBuffer.Header.len - 1], TimeSync, strLen);
  1556. sendBuffer.Header.len += strLen + 1;
  1557. //LOG_INFO("Dispenser %d Read System Id: %s", dispenserIndex + 1, ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1558. }
  1559. SendPacket(socket, &sendBuffer);
  1560. }
  1561. void WriteWaitPlugInResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1562. {
  1563. struct PACKET_STRUCTURE sendBuffer;
  1564. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1565. sendBuffer.Header.se = packet->Header.se;
  1566. sendBuffer.Header.id = packet->Header.id;
  1567. sendBuffer.Header.op = _Header_Response;
  1568. sendBuffer.Header.len = 2;
  1569. sendBuffer.Payload.reg = _Reg_WaitPlugIn;
  1570. sendBuffer.Payload.data[0] = result;
  1571. SendPacket(socket, &sendBuffer);
  1572. }
  1573. void GroundFaultDetectionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1574. {
  1575. struct PACKET_STRUCTURE sendBuffer;
  1576. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1577. sendBuffer.Header.se = packet->Header.se;
  1578. sendBuffer.Header.id = packet->Header.id;
  1579. sendBuffer.Header.op = _Header_Response;
  1580. sendBuffer.Header.len = 3;
  1581. sendBuffer.Payload.reg = _Reg_GroundFaultDetection;
  1582. sendBuffer.Payload.data[0] = result;
  1583. sendBuffer.Payload.data[1] = result == _R_OK ? chargingInfo[packet->Header.id - 1]->GroundFaultStatus : GFD_WAIT;
  1584. SendPacket(socket, &sendBuffer);
  1585. }
  1586. void CabinetCSUVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1587. {
  1588. struct PACKET_STRUCTURE sendBuffer;
  1589. char version[64];
  1590. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1591. sendBuffer.Header.se = packet->Header.se;
  1592. sendBuffer.Header.id = packet->Header.id;
  1593. sendBuffer.Header.op = _Header_Response;
  1594. sendBuffer.Header.len = 194;
  1595. sendBuffer.Payload.reg = _Reg_Get_Cabinet_CSU_Version;
  1596. sendBuffer.Payload.data[0] = result;
  1597. memset(version, 0x00, sizeof(version));
  1598. strcpy(version, (char *)&ShmSysConfigAndInfo->SysConfig.ModelName[0]);
  1599. memcpy(&sendBuffer.Payload.data[1], version, 32);
  1600. memset(version, 0x00, sizeof(version));
  1601. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev[0]);
  1602. memcpy(&sendBuffer.Payload.data[33], version, 32);
  1603. memset(version, 0x00, sizeof(version));
  1604. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev[0]);
  1605. memcpy(&sendBuffer.Payload.data[65], version, 32);
  1606. memset(version, 0x00, sizeof(version));
  1607. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[0]);
  1608. memcpy(&sendBuffer.Payload.data[97], version, 32);
  1609. memset(version, 0x00, sizeof(version));
  1610. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[0]);
  1611. memcpy(&sendBuffer.Payload.data[129], version, 32);
  1612. memset(version, 0x00, sizeof(version));
  1613. strcpy(version, (char *)&ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress[0]);
  1614. memcpy(&sendBuffer.Payload.data[161], version, 32);
  1615. SendPacket(socket, &sendBuffer);
  1616. }
  1617. void CabinetOtherVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1618. {
  1619. struct PACKET_STRUCTURE sendBuffer;
  1620. char version[64];
  1621. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1622. sendBuffer.Header.se = packet->Header.se;
  1623. sendBuffer.Header.id = packet->Header.id;
  1624. sendBuffer.Header.op = _Header_Response;
  1625. sendBuffer.Header.len = 98;
  1626. sendBuffer.Payload.reg = _Reg_Get_Cabinet_Other_Version;
  1627. sendBuffer.Payload.data[0] = result;
  1628. memset(version, 0x00, sizeof(version));
  1629. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0]);
  1630. memcpy(&sendBuffer.Payload.data[1], version, 32);
  1631. memset(version, 0x00, sizeof(version));
  1632. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev[0]);
  1633. memcpy(&sendBuffer.Payload.data[33], version, 32);
  1634. memset(version, 0x00, sizeof(version));
  1635. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0]);
  1636. memcpy(&sendBuffer.Payload.data[65], version, 32);
  1637. SendPacket(socket, &sendBuffer);
  1638. }
  1639. void PsuQuantityResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1640. {
  1641. struct PACKET_STRUCTURE sendBuffer;
  1642. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1643. sendBuffer.Header.se = packet->Header.se;
  1644. sendBuffer.Header.id = packet->Header.id;
  1645. sendBuffer.Header.op = _Header_Response;
  1646. sendBuffer.Header.len = 3;
  1647. sendBuffer.Payload.reg = _Reg_Get_Psu_Quantity;
  1648. sendBuffer.Payload.data[0] = result;
  1649. sendBuffer.Payload.data[1] = ShmPsuData->SystemPresentPsuQuantity;
  1650. SendPacket(socket, &sendBuffer);
  1651. }
  1652. void PsuVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result, int psuIndex)
  1653. {
  1654. struct PACKET_STRUCTURE sendBuffer;
  1655. char version[64];
  1656. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1657. sendBuffer.Header.se = packet->Header.se;
  1658. sendBuffer.Header.id = packet->Header.id;
  1659. sendBuffer.Header.op = _Header_Response;
  1660. sendBuffer.Header.len = 3 + 64;
  1661. sendBuffer.Payload.reg = _Reg_Get_Psu_Version;
  1662. sendBuffer.Payload.data[0] = result;
  1663. sendBuffer.Payload.data[1] = psuIndex;
  1664. memset(version, 0x00, sizeof(version));
  1665. strcpy(version, (char *)&ShmPsuData->PsuVersion[psuIndex].FwPrimaryVersion[0]);
  1666. memcpy(&sendBuffer.Payload.data[2], version, 32);
  1667. memset(version, 0x00, sizeof(version));
  1668. strcpy(version, (char *)&ShmPsuData->PsuVersion[psuIndex].FwSecondVersion[0]);
  1669. memcpy(&sendBuffer.Payload.data[34], version, 32);
  1670. SendPacket(socket, &sendBuffer);
  1671. }
  1672. void ReservationResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result, unsigned char _reservation)
  1673. {
  1674. struct PACKET_STRUCTURE sendBuffer;
  1675. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1676. sendBuffer.Header.se = packet->Header.se;
  1677. sendBuffer.Header.id = packet->Header.id;
  1678. sendBuffer.Header.op = _Header_Response;
  1679. sendBuffer.Header.len = 3;
  1680. sendBuffer.Payload.reg = _Reg_Get_Reservation;
  1681. sendBuffer.Payload.data[0] = result;
  1682. sendBuffer.Payload.data[1] = _reservation;
  1683. if(_reservation)
  1684. {
  1685. strcpy((char *)&sendBuffer.Payload.data[2], &ShmChargerInfo->Control.ResevedIdTag[packet->Header.id - 1][0]);
  1686. sendBuffer.Header.len += strlen(&ShmChargerInfo->Control.ResevedIdTag[packet->Header.id - 1][0]);
  1687. }
  1688. // '\0'
  1689. sendBuffer.Header.len += 1;
  1690. SendPacket(socket, &sendBuffer);
  1691. }
  1692. void DispenserReqHandlerResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1693. {
  1694. struct PACKET_STRUCTURE sendBuffer;
  1695. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1696. sendBuffer.Header.se = packet->Header.se;
  1697. sendBuffer.Header.id = packet->Header.id;
  1698. sendBuffer.Header.op = _Header_Response;
  1699. sendBuffer.Header.len = 2;
  1700. sendBuffer.Payload.reg = _Reg_Dispenser_Request;
  1701. sendBuffer.Payload.data[0] = result;
  1702. SendPacket(socket, &sendBuffer);
  1703. }
  1704. void RemoteStarNoIDResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result, unsigned char state)
  1705. {
  1706. struct PACKET_STRUCTURE sendBuffer;
  1707. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1708. sendBuffer.Header.se = packet->Header.se;
  1709. sendBuffer.Header.id = packet->Header.id;
  1710. sendBuffer.Header.op = _Header_Response;
  1711. sendBuffer.Header.len = 3;
  1712. sendBuffer.Payload.reg = _Reg_RemoteStartNoIDState;
  1713. sendBuffer.Payload.data[0] = result;
  1714. sendBuffer.Payload.data[1] = state;
  1715. SendPacket(socket, &sendBuffer);
  1716. }
  1717. void RefundAmountResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1718. {
  1719. struct PACKET_STRUCTURE sendBuffer;
  1720. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1721. sendBuffer.Header.se = packet->Header.se;
  1722. sendBuffer.Header.id = packet->Header.id;
  1723. sendBuffer.Header.op = _Header_Response;
  1724. sendBuffer.Header.len = 2 + 128;
  1725. sendBuffer.Payload.reg = _Reg_RefundAmount;
  1726. sendBuffer.Payload.data[0] = result;
  1727. memcpy(&sendBuffer.Payload.data[1], &ConnectorRefund[packet->Header.id - 1].ChargingDate, 32);
  1728. memcpy(&sendBuffer.Payload.data[33], &ConnectorRefund[packet->Header.id - 1].ChargingDate, 32);
  1729. memcpy(&sendBuffer.Payload.data[65], &ConnectorRefund[packet->Header.id - 1].ChargingDate, 32);
  1730. memcpy(&sendBuffer.Payload.data[97], &ConnectorRefund[packet->Header.id - 1].ChargingDate, 32);
  1731. SendPacket(socket, &sendBuffer);
  1732. }
  1733. void PrepaymentResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1734. {
  1735. struct PACKET_STRUCTURE sendBuffer;
  1736. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1737. sendBuffer.Header.se = packet->Header.se;
  1738. sendBuffer.Header.id = packet->Header.id;
  1739. sendBuffer.Header.op = _Header_Response;
  1740. sendBuffer.Header.len = 3 + 128;
  1741. sendBuffer.Payload.reg = _Reg_PrepaymentInfo;
  1742. sendBuffer.Payload.data[0] = result;
  1743. sendBuffer.Payload.data[1] = ConnectorPrepayment[packet->Header.id - 1].PaymentType;
  1744. memcpy(&sendBuffer.Payload.data[2], &ConnectorPrepayment[packet->Header.id - 1].TopUp, 32);
  1745. memcpy(&sendBuffer.Payload.data[34], &ConnectorPrepayment[packet->Header.id - 1].AccountAmount, 32);
  1746. memcpy(&sendBuffer.Payload.data[66], &ConnectorPrepayment[packet->Header.id - 1].Prepayment, 32);
  1747. memcpy(&sendBuffer.Payload.data[98], &ConnectorPrepayment[packet->Header.id - 1].Balance, 32);
  1748. SendPacket(socket, &sendBuffer);
  1749. }
  1750. void PaymentFailReasonResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1751. {
  1752. struct PACKET_STRUCTURE sendBuffer;
  1753. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1754. sendBuffer.Header.se = packet->Header.se;
  1755. sendBuffer.Header.id = packet->Header.id;
  1756. sendBuffer.Header.op = _Header_Response;
  1757. sendBuffer.Header.len = 3;
  1758. sendBuffer.Payload.reg = _Reg_PaymentFailReason;
  1759. sendBuffer.Payload.data[0] = result;
  1760. sendBuffer.Payload.data[1] = ConnectorPaymentFailReason[packet->Header.id - 1].ReasonIndex;
  1761. if(ConnectorPaymentFailReason[packet->Header.id - 1].ReasonIndex == 0)
  1762. {
  1763. int len = strlen(ConnectorPaymentFailReason[packet->Header.id - 1].Reason) + 1;
  1764. memcpy(&sendBuffer.Payload.data[2], &ConnectorPaymentFailReason[packet->Header.id - 1].Reason, len);
  1765. sendBuffer.Header.len += len;
  1766. }
  1767. SendPacket(socket, &sendBuffer);
  1768. }
  1769. void QRCodeResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1770. {
  1771. struct PACKET_STRUCTURE sendBuffer;
  1772. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1773. sendBuffer.Header.se = packet->Header.se;
  1774. sendBuffer.Header.id = packet->Header.id;
  1775. sendBuffer.Header.op = _Header_Response;
  1776. sendBuffer.Header.len = 2;
  1777. sendBuffer.Payload.reg = _Reg_ConnectorQRCode;
  1778. sendBuffer.Payload.data[0] = result;
  1779. if(result == _R_OK)
  1780. {
  1781. char qrCode[128];
  1782. int qrCodeLen = 0;
  1783. memset(qrCode, 0x00, sizeof(qrCode));
  1784. qrCodeLen = strlen(ShmChargerInfo->StationInfo.QRCode[sendBuffer.Header.id - 1]);
  1785. if(qrCodeLen > 0)
  1786. {
  1787. memcpy(qrCode, ShmChargerInfo->StationInfo.QRCode[sendBuffer.Header.id - 1], qrCodeLen);
  1788. }
  1789. else
  1790. {
  1791. qrCodeLen = GetChargerSystemId(qrCode);
  1792. sprintf(&qrCode[qrCodeLen], "_%d", packet->Header.id - 1);
  1793. qrCodeLen = strlen(qrCode);
  1794. }
  1795. if(strcmp((char *)&_ConnectorQRCode[packet->Header.id - 1][0], qrCode) != 0)
  1796. {
  1797. strcpy((char *)&_ConnectorQRCode[packet->Header.id - 1][0], qrCode);
  1798. LOG_INFO("Gun %d QR Code: [%s]", packet->Header.id, &_ConnectorQRCode[packet->Header.id - 1][0]);
  1799. }
  1800. memcpy(&sendBuffer.Payload.data[1], (char *)qrCode, qrCodeLen);
  1801. sendBuffer.Header.len += qrCodeLen;
  1802. }
  1803. // '\0'
  1804. sendBuffer.Header.len += 1;
  1805. SendPacket(socket, &sendBuffer);
  1806. }
  1807. void StationInfoResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1808. {
  1809. char strWeather[16];
  1810. char *str_weather_type[] = {STR_WEATHER_NONE, STR_WEATHER_SUNNY, STR_WEATHER_CLOUDY, STR_WEATHER_RAIN, STR_WEATHER_THUNDERSTORM, STR_WEATHER_SNOW, STR_WEATHER_MIST};
  1811. struct PACKET_STRUCTURE sendBuffer;
  1812. memset(strWeather, 0x00, sizeof(strWeather));
  1813. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1814. sendBuffer.Header.se = packet->Header.se;
  1815. sendBuffer.Header.id = packet->Header.id;
  1816. sendBuffer.Header.op = _Header_Response;
  1817. sendBuffer.Header.len = 90;
  1818. sendBuffer.Payload.reg = _Reg_StationInfo;
  1819. sendBuffer.Payload.data[0] = result;
  1820. memcpy(&sendBuffer.Payload.data[1], ShmChargerInfo->StationInfo.StationName, 64);
  1821. memcpy(&sendBuffer.Payload.data[65], ShmChargerInfo->StationInfo.StationID, 16);
  1822. sendBuffer.Payload.data[81] = (ShmChargerInfo->StationInfo.WeatherID >> 24) & 0xFF;
  1823. sendBuffer.Payload.data[82] = (ShmChargerInfo->StationInfo.WeatherID >> 16) & 0xFF;
  1824. sendBuffer.Payload.data[83] = (ShmChargerInfo->StationInfo.WeatherID >> 8) & 0xFF;
  1825. sendBuffer.Payload.data[84] = ShmChargerInfo->StationInfo.WeatherID & 0xFF;
  1826. char *ptr = (char *)&ShmChargerInfo->StationInfo.Temperature;
  1827. sendBuffer.Payload.data[85] = *(ptr + 3);
  1828. sendBuffer.Payload.data[86] = *(ptr + 2);
  1829. sendBuffer.Payload.data[87] = *(ptr + 1);
  1830. sendBuffer.Payload.data[88] = *ptr;
  1831. if(strcmp(_stationInfo[dispenserIndex].StationName, ShmChargerInfo->StationInfo.StationName) != 0 ||
  1832. strcmp(_stationInfo[dispenserIndex].StationID, ShmChargerInfo->StationInfo.StationID) != 0 ||
  1833. _stationInfo[dispenserIndex].WeatherID != ShmChargerInfo->StationInfo.WeatherID ||
  1834. _stationInfo[dispenserIndex].Temperature != ShmChargerInfo->StationInfo.Temperature)
  1835. {
  1836. memcpy((char *)&_stationInfo[dispenserIndex], (char *)&ShmChargerInfo->StationInfo, sizeof(StationInfoData));
  1837. if(ShmChargerInfo->StationInfo.WeatherID <= _WEATHER_Mist)
  1838. {
  1839. strcpy(strWeather, str_weather_type[ShmChargerInfo->StationInfo.WeatherID]);
  1840. }
  1841. else
  1842. {
  1843. sprintf(strWeather, "%d", ShmChargerInfo->StationInfo.WeatherID);
  1844. }
  1845. LOG_INFO("Dispenser %d StationID: %s, Name: %s, WeatherID: %s, Temperature: %4.1f",
  1846. dispenserIndex + 1,
  1847. ShmChargerInfo->StationInfo.StationID,
  1848. ShmChargerInfo->StationInfo.StationName,
  1849. strWeather,
  1850. ShmChargerInfo->StationInfo.Temperature);
  1851. }
  1852. SendPacket(socket, &sendBuffer);
  1853. }
  1854. BOOL FindConnectorID(unsigned char dispenserIndex, unsigned char id)
  1855. {
  1856. BOOL find = false;
  1857. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1858. {
  1859. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] == id)
  1860. {
  1861. find = true;
  1862. break;
  1863. }
  1864. }
  1865. return find;
  1866. }
  1867. void ConnectorPhysicalLimitBindingHandler(unsigned char connectorIndex, unsigned char physical)
  1868. {
  1869. // 20210817 remove type "R"
  1870. //char modelList[11] = {'J', 'U', 'V', 'E', 'F', 'G', 'T', 'D', 'K', 'P'};
  1871. //unsigned char typeList[11] = { 0, 1, 1, 1, 1, 2, 1, 1, 0, 1}; // 0 : Chademo, 1: CCS, 2: GB
  1872. switch(physical)
  1873. {
  1874. case 'J':
  1875. case 'K':
  1876. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CHA_MAX_PHYSICAL_VOLTAGE;
  1877. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CHA_NATURAL_MAX_CURRENT;
  1878. break;
  1879. case 'U':
  1880. case 'E':
  1881. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CCS_MAX_PHYSICAL_VOLTAGE;
  1882. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CCS_NATURAL_MAX_CURRENT;
  1883. break;
  1884. case 'G':
  1885. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = GBT_MAX_PHYSICAL_VOLTAGE;
  1886. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = GBT_NATURAL_MAX_CURRENT;
  1887. break;
  1888. case 'V':
  1889. case 'F':
  1890. case 'P':
  1891. //case 'R': // 20210817 remove type "R"
  1892. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CCS_MAX_PHYSICAL_VOLTAGE;
  1893. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CCS_LIQUID_MAX_CURRENT;
  1894. break;
  1895. case 'T':
  1896. case 'D':
  1897. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CCS_MAX_PHYSICAL_VOLTAGE;
  1898. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CCS_NATURAL_REMA_MAX_CURRENT;
  1899. break;
  1900. default:
  1901. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = 0;
  1902. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = 0;
  1903. break;
  1904. }
  1905. }
  1906. void ConnectorPantographBindingHandler(unsigned char connectorIndex, unsigned char physical)
  1907. {
  1908. switch(physical)
  1909. {
  1910. case 'P':
  1911. chargingInfo[connectorIndex]->PantographFlag = YES;
  1912. break;
  1913. default:
  1914. chargingInfo[connectorIndex]->PantographFlag = NO;
  1915. break;
  1916. }
  1917. }
  1918. void ConnectorTypeBindingHandler(unsigned char dispenserIndex, unsigned char *type, unsigned char *physicalType)
  1919. {
  1920. char *str_gun_type[] = {STR_GUN_TYPE_CHADEMO, STR_GUN_TYPE_CCS, STR_GUN_TYPE_GBT};
  1921. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1922. {
  1923. unsigned char gunIndex = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  1924. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex = dispenserIndex;
  1925. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.Index = gunIndex;
  1926. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.Type = type[i];
  1927. ConnectorPhysicalLimitBindingHandler(gunIndex, physicalType[i]);
  1928. ConnectorPantographBindingHandler(gunIndex, physicalType[i]);
  1929. LOG_INFO("Dispenser %d Connector %d type: %s, MaxVol: %4d, MaxCur: %4d, %s",
  1930. dispenserIndex + 1, gunIndex + 1, str_gun_type[type[i]],
  1931. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalVoltage,
  1932. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalCurrent,
  1933. chargingInfo[gunIndex]->PantographFlag == YES ? "Pantograph" : "Normal");
  1934. }
  1935. }
  1936. BOOL IsAvailableDispenserIndexSequence(unsigned char index)
  1937. {
  1938. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  1939. {
  1940. if(i == index)
  1941. {
  1942. return true;
  1943. }
  1944. if((ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status & (1 << i)) == 0)
  1945. {
  1946. // i is not check in yet before index
  1947. return false;
  1948. }
  1949. }
  1950. return false;
  1951. }
  1952. void DispenserCheckInInfoUpdate(void)
  1953. {
  1954. unsigned char dispenser = 0, connector = 0;
  1955. BOOL change = false;
  1956. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  1957. {
  1958. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  1959. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  1960. {
  1961. dispenser++;
  1962. connector += ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity;
  1963. }
  1964. }
  1965. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentDispenserQuantity != dispenser ||
  1966. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentConnectorQuantity != connector)
  1967. {
  1968. change = true;
  1969. }
  1970. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentDispenserQuantity = dispenser;
  1971. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentConnectorQuantity = connector;
  1972. if(change)
  1973. {
  1974. LOG_INFO("Total Dispenser: %d, Total Connector: %d\n",
  1975. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentDispenserQuantity,
  1976. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentConnectorQuantity);
  1977. }
  1978. }
  1979. void SetConnectorID(unsigned char dispenserIndex, unsigned char quantity)
  1980. {
  1981. unsigned char currentQuantity = 0;
  1982. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] = 0x00;
  1983. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[1] = 0x00;
  1984. for(int i = 0; i < dispenserIndex; i++)
  1985. {
  1986. currentQuantity += ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[i];
  1987. }
  1988. for(int i = 0; i < quantity; i++)
  1989. {
  1990. currentQuantity++;
  1991. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] = currentQuantity;
  1992. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[currentQuantity - 1].Enable = true;
  1993. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[currentQuantity - 1].GeneralChargingData.Index = currentQuantity - 1;
  1994. }
  1995. }
  1996. BOOL CheckNewDispenserSequence(unsigned char index, unsigned char quantity)
  1997. {
  1998. if(index == ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence)
  1999. {
  2000. LOG_INFO("Get New Dispenser %d, Connector Quantity: %d\n", index + 1, quantity);
  2001. ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status |= (1 << index);
  2002. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[index] = quantity;
  2003. ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence = index + 1;
  2004. ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity += quantity;
  2005. ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[index] = quantity;
  2006. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.FlashConfigChanged = true;
  2007. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity = ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence;
  2008. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity = ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity;
  2009. return true;
  2010. }
  2011. return false;
  2012. }
  2013. BOOL DispenserIdentificationHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char *modelName)
  2014. {
  2015. unsigned char quantity = 0;
  2016. unsigned char connectorType[2] = {0, 0};
  2017. unsigned char physicalType[2] = {0, 0};
  2018. quantity = ConnectorQuantityTypeParsing(modelName, connectorType, physicalType);
  2019. if(quantity > 0)
  2020. {
  2021. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[dispenserIndex] != 0 &&
  2022. quantity != ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[dispenserIndex])
  2023. {
  2024. LOG_INFO("The same dispenser id (%d) but connector quantity not match", dispenserIndex + 1);
  2025. return false;
  2026. }
  2027. if(dispenserIndex == ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence &&
  2028. (quantity + ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity) > ShmChargerInfo->Control.MaxConnector)
  2029. {
  2030. LOG_INFO("Dispenser %d connector quantity %d over range, total quantity: %d",
  2031. dispenserIndex + 1, quantity, ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity);
  2032. return false;
  2033. }
  2034. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_None ||
  2035. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_Timeout)
  2036. {
  2037. CheckNewDispenserSequence(dispenserIndex, quantity);
  2038. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Identification;
  2039. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity = quantity;
  2040. SetConnectorID(dispenserIndex, quantity);
  2041. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName, packet->Payload.data, (packet->Header.len - 2));
  2042. ConnectorTypeBindingHandler(dispenserIndex, connectorType, physicalType);
  2043. LOG_INFO("Dispenser id %d identified, Connector quantity: %d\n", dispenserIndex + 1, quantity);
  2044. DispenserCheckInInfoUpdate();
  2045. return true;
  2046. }
  2047. else
  2048. {
  2049. LOG_INFO("Conflict dispenser ID: %d", dispenserIndex + 1);
  2050. }
  2051. }
  2052. else
  2053. {
  2054. LOG_INFO("Connector quantity fail, model name: %s", modelName);
  2055. }
  2056. return false;
  2057. }
  2058. BOOL DispenserStatusCodeHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2059. {
  2060. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2061. if(find)
  2062. {
  2063. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount = (packet->Header.len - 1) / 6;
  2064. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount > 10)
  2065. {
  2066. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount = 10;
  2067. }
  2068. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  2069. {
  2070. memcpy(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i][0],
  2071. &packet->Payload.data[i * 6], 6);
  2072. }
  2073. /*
  2074. BOOL needUpdate = FALSE;
  2075. // check is there new status code
  2076. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  2077. {
  2078. if(strcmp(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i][0],
  2079. &LastWarningInfo[packet->Header.id - 1].WarningCode[i][0]) == 0)
  2080. {
  2081. needUpdate = TRUE;
  2082. break;
  2083. }
  2084. }
  2085. if(needUpdate)
  2086. {
  2087. memcpy(&LastWarningInfo[packet->Header.id - 1], &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo, sizeof(struct WARNING_CODE_INFO));
  2088. }
  2089. */
  2090. char strDispenserStatus[128];
  2091. sprintf(strDispenserStatus, "Dispenser id %d, Status Code Len: %d", packet->Header.id, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount);
  2092. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount > 0)
  2093. {
  2094. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  2095. {
  2096. char strTemp[16];
  2097. sprintf(strTemp, ", %s", ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i]);
  2098. strcat(strDispenserStatus, strTemp);
  2099. }
  2100. }
  2101. LOG_INFO("%s", strDispenserStatus);
  2102. }
  2103. else
  2104. {
  2105. }
  2106. return find;
  2107. }
  2108. BOOL ConnectorChargingCapabilityHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2109. {
  2110. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2111. BOOL done = false;
  2112. if(find)
  2113. {
  2114. done = true;
  2115. }
  2116. return done;
  2117. }
  2118. BOOL ConnectorChargingTargetHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2119. {
  2120. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2121. BOOL done = false;
  2122. unsigned short voltage = 0, current = 0, power = 0;
  2123. if(find)
  2124. {
  2125. voltage = ((packet->Payload.data[0] << 8) + packet->Payload.data[1]);
  2126. current = ((packet->Payload.data[2] << 8) + packet->Payload.data[3]);
  2127. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage != voltage ||
  2128. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent != current)
  2129. {
  2130. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage = voltage;
  2131. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent = current;
  2132. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable)
  2133. {
  2134. LOG_INFO("Connector %d Remote Voltage: %d, Remote Current: %d", packet->Header.id, voltage, current);
  2135. }
  2136. }
  2137. GetPhysicalLimitVoltageAndCurrent(packet->Header.id - 1, &voltage, &current);
  2138. GetModelNameLimitPower(packet->Header.id - 1, &power);
  2139. GetConfigLimitPowerAndCurrent(packet->Header.id - 1, &power, &current);
  2140. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage != voltage ||
  2141. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent != current)
  2142. {
  2143. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable)
  2144. {
  2145. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable = true;
  2146. LOG_INFO("Connector %d Remote Voltage: %d, Remote Current: %d", packet->Header.id,
  2147. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage,
  2148. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent);
  2149. LOG_INFO("Connector %d Limit Voltage: %d, Limit Current: %d", packet->Header.id, voltage, current);
  2150. }
  2151. }
  2152. else
  2153. {
  2154. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable = false;
  2155. }
  2156. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitVoltage = voltage;
  2157. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitCurrent = current;
  2158. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus == S_PREPARING_FOR_EVSE ||
  2159. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus == S_CHARGING)
  2160. {
  2161. float targetVol = (float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitVoltage / 10);
  2162. float targetCur = (float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitCurrent / 10);
  2163. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage != targetVol ||
  2164. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent != targetCur)
  2165. {
  2166. LOG_INFO("Connector %d Target Voltage: %d V, Current: %d A",
  2167. packet->Header.id, (int)(targetVol), (int)(targetCur));
  2168. }
  2169. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage = targetVol;
  2170. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent = targetCur;
  2171. done = true;
  2172. }
  2173. else
  2174. {
  2175. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage != 0 ||
  2176. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent != 0)
  2177. {
  2178. LOG_INFO("Connector %d Not Charging Status, Set Voltage & Current Fail", packet->Header.id);
  2179. }
  2180. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage = 0;
  2181. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent = 0;
  2182. }
  2183. }
  2184. else
  2185. {
  2186. }
  2187. return done;
  2188. }
  2189. unsigned char FirmwareUpgradeHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2190. {
  2191. unsigned char UpgradeResponse = _R_NoUpgrade;
  2192. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] == packet->Header.id)
  2193. {
  2194. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateRequest)
  2195. {
  2196. if(packet->Payload.data[0] == 0)
  2197. {
  2198. UpgradeResponse = _R_NeedUpgrade;
  2199. }
  2200. else
  2201. {
  2202. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm)
  2203. {
  2204. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm = true;
  2205. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateRequest = false;
  2206. LOG_INFO("Dispenser %d Updating...", dispenserIndex + 1);
  2207. }
  2208. }
  2209. }
  2210. else
  2211. {
  2212. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm)
  2213. {
  2214. if(packet->Payload.data[0] == 0)
  2215. {
  2216. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateCompleted)
  2217. {
  2218. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateCompleted = true;
  2219. LOG_INFO("Dispenser %d Upgrade Completed", dispenserIndex + 1);
  2220. }
  2221. }
  2222. }
  2223. }
  2224. }
  2225. return UpgradeResponse;
  2226. }
  2227. BOOL ConnectorPlugInHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2228. {
  2229. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2230. if(find)
  2231. {
  2232. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn != packet->Payload.data[0])
  2233. {
  2234. LOG_INFO("Connector %d %s", packet->Header.id, packet->Payload.data[0] == _PIS_PluggedIn ? "Pluged In" : "Un Plugged");
  2235. }
  2236. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn = packet->Payload.data[0];
  2237. }
  2238. else
  2239. {
  2240. }
  2241. return find;
  2242. }
  2243. BOOL ConnectorStateHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2244. {
  2245. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2246. unsigned char ConnectionState;
  2247. unsigned char *AlarmCode;
  2248. unsigned char GunTemp = 0;
  2249. unsigned char ChillerTemp = 0;
  2250. unsigned char plugin = 0;
  2251. unsigned char noAlarm[6];
  2252. memset(noAlarm, 0x00, 6);
  2253. if(find)
  2254. {
  2255. ConnectionState = packet->Payload.data[0];
  2256. AlarmCode = packet->Header.len >= 8 ? &packet->Payload.data[1] : &noAlarm[0];
  2257. GunTemp = packet->Header.len >= 10 ? packet->Payload.data[7] : GunTemp;
  2258. ChillerTemp = packet->Header.len >= 10 ? packet->Payload.data[8] : ChillerTemp;
  2259. chargingInfo[packet->Header.id - 1]->ConnectorTemp = GunTemp;
  2260. chargingInfo[packet->Header.id - 1]->ChillerTemp = ChillerTemp;
  2261. plugin = packet->Header.len >= 11 ? packet->Payload.data[9] : plugin;
  2262. if(packet->Header.len >= 11)
  2263. {
  2264. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn != plugin)
  2265. {
  2266. LOG_INFO("Gun %d %s", packet->Header.id, plugin == _PIS_PluggedIn ? "Pluged In" : "Un Plugged");
  2267. }
  2268. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn = plugin;
  2269. }
  2270. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus != ConnectionState)
  2271. {
  2272. LOG_INFO("Connector %d Remote Status: %d", packet->Header.id, ConnectionState);
  2273. switch(ConnectionState)
  2274. {
  2275. case _CRS_Idle:
  2276. case _CRS_Terminating:
  2277. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing ||
  2278. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Charging)
  2279. {
  2280. if(chargingInfo[packet->Header.id - 1]->SystemStatus != S_IDLE)
  2281. {
  2282. LOG_INFO("*********** Connector id %d Set Normal Stop Flag ***********\n", packet->Header.id);
  2283. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.NormalStopRequest = true;
  2284. }
  2285. }
  2286. break;
  2287. case _CRS_Alarm:
  2288. memcpy(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemotenAlarmCode, AlarmCode, 6);
  2289. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Idle)
  2290. {
  2291. LOG_INFO("*********** Connector id %d Set Fault Status [%s] ***********", packet->Header.id,
  2292. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemotenAlarmCode);
  2293. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.FaultStatusRequest = true;
  2294. }
  2295. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing ||
  2296. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Charging)
  2297. {
  2298. LOG_INFO("*********** Connector id %d Set Alarm Stop Flag [%s] ***********", packet->Header.id,
  2299. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemotenAlarmCode);
  2300. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AlarmStopRequest = true;
  2301. }
  2302. break;
  2303. default:
  2304. break;
  2305. }
  2306. }
  2307. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus = ConnectionState;
  2308. }
  2309. else
  2310. {
  2311. }
  2312. return true;
  2313. }
  2314. AuthorizingInfoData preGunAuthInfo[GENERAL_GUN_QUANTITY];
  2315. AuthorizingInfoData preDispenserAuthInfo[GENERAL_GUN_QUANTITY];
  2316. unsigned char UserIDHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2317. {
  2318. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2319. DispenserAck_Status authorize = _DAS_Wait;
  2320. unsigned char CardNumber[32];
  2321. //unsigned char *result;
  2322. AuthorizingInfoData *AuthInfo;
  2323. AuthorizingInfoData *preAuthInfo;
  2324. int length = 0;
  2325. if(find || packet->Header.id == AUTO_GUN_SELECTION)
  2326. {
  2327. length = packet->Header.len - 1;
  2328. memset(CardNumber, 0x00, sizeof(CardNumber));
  2329. memcpy(CardNumber, packet->Payload.data, length);
  2330. if(packet->Header.id != AUTO_GUN_SELECTION)
  2331. {
  2332. //result = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AuthorizingResult;
  2333. AuthInfo = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AuthInfo;
  2334. preAuthInfo = &preGunAuthInfo[packet->Header.id - 1];
  2335. }
  2336. else
  2337. {
  2338. //result = &ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].AuthResult;
  2339. AuthInfo = &ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].AuthInfo;
  2340. preAuthInfo = &preDispenserAuthInfo[dispenserIndex];
  2341. }
  2342. //if(*result == _AuthResult_Valid)
  2343. if(AuthInfo->AuthResult == _AuthResult_Valid)
  2344. {
  2345. if(preAuthInfo->AuthResult != AuthInfo->AuthResult)
  2346. {
  2347. LOG_INFO("Dispenser %d Target %d user id %s authorizing ok", dispenserIndex + 1, packet->Header.id, CardNumber);
  2348. }
  2349. authorize = _DAS_Allowed;
  2350. }
  2351. //else if(*result == _AuthResult_Invalid)
  2352. else if(AuthInfo->AuthResult == _AuthResult_Invalid)
  2353. {
  2354. if(preAuthInfo->AuthResult != AuthInfo->AuthResult)
  2355. {
  2356. LOG_INFO("Dispenser %d Target %d user id %s authorizing fail", dispenserIndex + 1, packet->Header.id, CardNumber);
  2357. }
  2358. authorize = _DAS_NotAllowed;
  2359. }
  2360. else
  2361. {
  2362. if(AuthInfo->AuthStatus == _AuthorizeStatus_Idle && AuthInfo->AuthRequest == NO)
  2363. {
  2364. memcpy(AuthInfo->AuthId, CardNumber, sizeof(CardNumber));
  2365. AuthInfo->AuthTarget = packet->Header.id;
  2366. AuthInfo->AuthRequest = YES;
  2367. if(AuthInfo->AuthTarget != AUTO_GUN_SELECTION)
  2368. {
  2369. LOG_INFO("Gun %d user id [%s] need authorize", packet->Header.id, CardNumber);
  2370. }
  2371. else
  2372. {
  2373. LOG_INFO("Dispenser %d user id [%s] need authorize", dispenserIndex + 1, CardNumber);
  2374. }
  2375. authorize = _DAS_Wait;
  2376. }
  2377. else
  2378. {
  2379. authorize = _DAS_Wait;
  2380. }
  2381. }
  2382. preAuthInfo->AuthResult = AuthInfo->AuthResult;
  2383. }
  2384. else
  2385. {
  2386. authorize = _DAS_NotAllowed;
  2387. }
  2388. return authorize;
  2389. }
  2390. unsigned char ChargingPermissionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2391. {
  2392. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2393. DispenserAck_Status permission = _DAS_Wait;
  2394. if(find)
  2395. {
  2396. switch(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus)
  2397. {
  2398. case S_IDLE:
  2399. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Idle ||
  2400. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing)
  2401. {
  2402. permission = _DAS_Wait;
  2403. }
  2404. else
  2405. {
  2406. permission = _DAS_NotAllowed;
  2407. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  2408. {
  2409. LOG_INFO("Cabinet status idle, not allow dispenser %d connector %d charging",
  2410. dispenserIndex + 1, packet->Header.id);
  2411. }
  2412. }
  2413. break;
  2414. case S_AUTHORIZING:
  2415. case S_REASSIGN_CHECK:
  2416. case S_REASSIGN:
  2417. case S_PREPARNING:
  2418. permission = _DAS_Wait;
  2419. break;
  2420. case S_PREPARING_FOR_EV:
  2421. if(chargingInfo[packet->Header.id - 1]->ChargingStopFlag.bits.RemoteStop ||
  2422. chargingInfo[packet->Header.id - 1]->ChargingStopFlag.bits.UnlockStop)
  2423. {
  2424. permission = _DAS_NotAllowed;
  2425. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  2426. {
  2427. LOG_INFO("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  2428. }
  2429. }
  2430. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus >= _CRS_Preparing &&
  2431. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus <= _CRS_Charging)
  2432. {
  2433. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.PermissionRequest)
  2434. {
  2435. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.PermissionRequest = true;
  2436. LOG_INFO("Allow dispenser index %d connector id %d start preparing to charge", dispenserIndex, packet->Header.id);
  2437. }
  2438. permission = _DAS_Allowed;
  2439. }
  2440. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Terminating)
  2441. {
  2442. permission = _DAS_NotAllowed;
  2443. LOG_INFO("Dispenser Terminating, Not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  2444. }
  2445. else
  2446. {
  2447. // keep wait
  2448. }
  2449. break;
  2450. case S_PREPARING_FOR_EVSE:
  2451. permission = _DAS_Allowed;
  2452. break;
  2453. case S_CHARGING:
  2454. if(chargingInfo[packet->Header.id - 1]->ChargingStopFlag.bits.RemoteStop ||
  2455. chargingInfo[packet->Header.id - 1]->ChargingStopFlag.bits.UnlockStop)
  2456. {
  2457. permission = _DAS_NotAllowed;
  2458. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  2459. {
  2460. LOG_INFO("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d charging", dispenserIndex, packet->Header.id);
  2461. }
  2462. }
  2463. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing ||
  2464. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Charging)
  2465. {
  2466. permission = _DAS_Allowed;
  2467. }
  2468. else
  2469. {
  2470. permission = _DAS_NotAllowed;
  2471. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  2472. {
  2473. LOG_INFO("Dispenser index %d connector id %d remote status not in charging mode", dispenserIndex, packet->Header.id);
  2474. }
  2475. }
  2476. break;
  2477. case S_TERMINATING:
  2478. case S_COMPLETE:
  2479. case S_ALARM:
  2480. case S_FAULT:
  2481. case S_RESERVATION:
  2482. case S_MAINTAIN:
  2483. permission = _DAS_NotAllowed;
  2484. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  2485. {
  2486. LOG_INFO("Cabinet status(%d) not match, not allow dispenser index %d connector id %d charging",
  2487. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus,
  2488. dispenserIndex, packet->Header.id);
  2489. }
  2490. break;
  2491. default:
  2492. break;
  2493. }
  2494. }
  2495. else
  2496. {
  2497. LOG_INFO("Dispenser index %d Connector id %d not found", dispenserIndex, packet->Header.id);
  2498. permission = _DAS_NotAllowed;
  2499. }
  2500. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission = permission;
  2501. // 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",
  2502. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus,
  2503. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus,
  2504. // ShmSysConfigAndInfo->SysInfo.WaitForPlugit,
  2505. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn,
  2506. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.IsAvailable,
  2507. // ShmSysConfigAndInfo->SysInfo.OrderCharging);
  2508. return permission;
  2509. }
  2510. BOOL MiscControlHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2511. {
  2512. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2513. BOOL done = false;
  2514. if(find)
  2515. {
  2516. done = true;
  2517. }
  2518. return done;
  2519. }
  2520. unsigned char DispenserWriteCsuVersionInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2521. {
  2522. char version[32];
  2523. BOOL writeVersion = FALSE, showVersion = FALSE;;
  2524. memcpy(version, &packet->Payload.data[0], 32);
  2525. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev, version) != 0)
  2526. {
  2527. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev, version, 32);
  2528. showVersion = true;
  2529. }
  2530. memcpy(version, &packet->Payload.data[32], 32);
  2531. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev, version) != 0)
  2532. {
  2533. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev, version, 32);
  2534. showVersion = true;
  2535. }
  2536. memcpy(version, &packet->Payload.data[64], 32);
  2537. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev, version) != 0)
  2538. {
  2539. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev, version, 32);
  2540. showVersion = true;
  2541. }
  2542. memcpy(version, &packet->Payload.data[96], 32);
  2543. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev, version) != 0)
  2544. {
  2545. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev, version, 32);
  2546. showVersion = true;
  2547. }
  2548. if(showVersion)
  2549. {
  2550. LOG_INFO("********** Dispenser id %d: Csu Version information **********", dispenserIndex + 1);
  2551. LOG_INFO("********** CsuBootLoadRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev);
  2552. LOG_INFO("********** CsuKernelFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev);
  2553. LOG_INFO("********** CsuRootFsFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev);
  2554. LOG_INFO("********** CsuPrimFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev);
  2555. }
  2556. writeVersion = TRUE;
  2557. return writeVersion;
  2558. }
  2559. unsigned char DispenserWriteOtherVersionInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2560. {
  2561. char version[32];
  2562. BOOL writeVersion = FALSE, showVersion = FALSE;;
  2563. memcpy(version, &packet->Payload.data[0], 32);
  2564. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev, version) != 0)
  2565. {
  2566. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev, version, 32);
  2567. showVersion = true;
  2568. }
  2569. memcpy(version, &packet->Payload.data[32], 32);
  2570. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev, version) != 0)
  2571. {
  2572. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev, version, 32);
  2573. showVersion = true;
  2574. }
  2575. memcpy(version, &packet->Payload.data[64], 32);
  2576. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev, version) != 0)
  2577. {
  2578. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev, version, 32);
  2579. showVersion = true;
  2580. }
  2581. memcpy(version, &packet->Payload.data[96], 32);
  2582. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev, version) != 0)
  2583. {
  2584. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev, version, 32);
  2585. showVersion = true;
  2586. }
  2587. memcpy(version, &packet->Payload.data[128], 32);
  2588. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev, version) != 0)
  2589. {
  2590. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev, version, 32);
  2591. showVersion = true;
  2592. }
  2593. if(showVersion)
  2594. {
  2595. LOG_INFO("********** Dispenser id %d: Other Version information **********", dispenserIndex + 1);
  2596. LOG_INFO("********** FanModuleFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev);
  2597. LOG_INFO("********** RelayModuleRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev);
  2598. LOG_INFO("********** Connector1Rev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev);
  2599. LOG_INFO("********** Connector2Rev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev);
  2600. LOG_INFO("********** LedModuleFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev);
  2601. }
  2602. writeVersion = TRUE;
  2603. return writeVersion;
  2604. }
  2605. unsigned char DispenserWriteChargingInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2606. {
  2607. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2608. unsigned char gun = 0;
  2609. if(find)
  2610. {
  2611. gun = packet->Header.id - 1;
  2612. unsigned short diffVoltage = 0, diffCurrent = 0;
  2613. unsigned short voltage = (packet->Payload.data[0] << 8) + (packet->Payload.data[1]);
  2614. unsigned short current = (packet->Payload.data[2] << 8) + (packet->Payload.data[3]);
  2615. //unsigned int time = (packet->Payload.data[4] << 24) + (packet->Payload.data[5] << 16) + (packet->Payload.data[6] << 8) + (packet->Payload.data[7]);
  2616. unsigned char soc = (packet->Payload.data[8]);
  2617. diffVoltage = voltage >= ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingVoltage ?
  2618. voltage - ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingVoltage :
  2619. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingVoltage - voltage;
  2620. diffCurrent = current >= ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingCurrent ?
  2621. current - ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingCurrent :
  2622. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingCurrent - current;
  2623. if(diffVoltage > 10 || diffCurrent > 10 || ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteSoc != soc)
  2624. {
  2625. LOG_INFO("Connector %d Charging Info: Voltage: %4d.%d V, Current: %3d.%d A, Soc: %3d, Temp: %3d, Chiller: %3d",
  2626. packet->Header.id, (voltage / 10), (voltage % 10), (current / 10), (current % 10), soc,
  2627. chargingInfo[gun]->ConnectorTemp - 60, chargingInfo[gun]->ChillerTemp - 60);
  2628. }
  2629. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingVoltage = voltage;
  2630. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingCurrent = current;
  2631. //ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteRemainChargingDuration = time;
  2632. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteSoc = soc;
  2633. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].GeneralChargingData.EvBatterySoc = soc;
  2634. }
  2635. else
  2636. {
  2637. }
  2638. return find;
  2639. }
  2640. unsigned char ReadChargerSystemIdHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2641. {
  2642. BOOL find = TRUE;
  2643. return find;
  2644. }
  2645. unsigned char WriteWaitPlugInHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2646. {
  2647. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2648. if(find)
  2649. {
  2650. unsigned char StartWait = packet->Payload.data[0];
  2651. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus == S_AUTHORIZING)
  2652. {
  2653. if(StartWait)
  2654. {
  2655. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug)
  2656. {
  2657. LOG_INFO("Connector %d Start Wait Plug In", packet->Header.id);
  2658. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.SwipeRfidConfirm = true;
  2659. }
  2660. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = true;
  2661. }
  2662. else
  2663. {
  2664. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug)
  2665. {
  2666. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus != S_IDLE)
  2667. {
  2668. LOG_INFO("Connector %d Stop Wait Plug In", packet->Header.id);
  2669. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.NeedCleanAuthorizeInfo = true;
  2670. }
  2671. }
  2672. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = false;
  2673. }
  2674. }
  2675. else
  2676. {
  2677. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = false;
  2678. }
  2679. }
  2680. else
  2681. {
  2682. }
  2683. return find;
  2684. }
  2685. unsigned char GroundFaultDetectionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2686. {
  2687. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2688. unsigned char gun = 0;
  2689. if(find)
  2690. {
  2691. gun = packet->Header.id - 1;
  2692. unsigned char enable = packet->Payload.data[0];
  2693. if(chargingInfo[gun]->PantographFlag == YES)
  2694. {
  2695. if(enable == _GFD_Enable &&
  2696. (chargingInfo[gun]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun]->SystemStatus <= S_CHARGING))
  2697. {
  2698. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].Parameter.bits.GfdDetection != _GFD_Enable)
  2699. {
  2700. LOG_INFO("Connector %d Gfd Detection Start", gun + 1);
  2701. }
  2702. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].Parameter.bits.GfdDetection = _GFD_Enable;
  2703. }
  2704. else
  2705. {
  2706. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].Parameter.bits.GfdDetection != _GFD_Disable)
  2707. {
  2708. LOG_INFO("Connector %d Gfd Detection Stop", gun + 1);
  2709. }
  2710. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].Parameter.bits.GfdDetection = _GFD_Disable;
  2711. }
  2712. }
  2713. }
  2714. else
  2715. {
  2716. }
  2717. return find;
  2718. }
  2719. unsigned char GetCabinetCSUVersionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2720. {
  2721. return true;
  2722. }
  2723. unsigned char GetCabinetOtherVersionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2724. {
  2725. return true;
  2726. }
  2727. unsigned char GetPsuQuantityHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2728. {
  2729. return true;
  2730. }
  2731. unsigned char GetPsuVersionHandler(struct PACKET_STRUCTURE *packet, int *psuIndex)
  2732. {
  2733. *psuIndex = 0;
  2734. if(packet->Payload.data[0] <= ShmPsuData->SystemPresentPsuQuantity && packet->Payload.data[0] > 0)
  2735. {
  2736. *psuIndex = packet->Payload.data[0] - 1;
  2737. }
  2738. return true;
  2739. }
  2740. unsigned char GetReservationHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex, unsigned char *reservation)
  2741. {
  2742. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2743. unsigned char gun = 0;
  2744. if(find)
  2745. {
  2746. gun = packet->Header.id - 1;
  2747. *reservation = chargingInfo[gun]->SystemStatus == S_RESERVATION ? YES : NO;
  2748. }
  2749. else
  2750. {
  2751. }
  2752. return find;
  2753. }
  2754. unsigned char DispenserReqHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2755. {
  2756. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2757. unsigned char gun = 0;
  2758. if(find)
  2759. {
  2760. gun = packet->Header.id - 1;
  2761. struct MISC_COMMAND connector_req;
  2762. memset(&connector_req, 0x00, sizeof(struct MISC_COMMAND));
  2763. connector_req.Command = (packet->Payload.data[0] << 8) | packet->Payload.data[1];
  2764. connector_req.Value = (packet->Payload.data[2] << 24) | (packet->Payload.data[3] << 16) | (packet->Payload.data[4] << 8) | packet->Payload.data[5];
  2765. switch(connector_req.Command)
  2766. {
  2767. case _DisReq_RefoundRequest:
  2768. if(connector_req.Value == YES)
  2769. {
  2770. ShmChargerInfo->ConnectorActReq[gun].Flag.bits.RefundRequest = true;
  2771. }
  2772. break;
  2773. case _DisReq_RefoundCancel:
  2774. if(connector_req.Value == YES)
  2775. {
  2776. ShmChargerInfo->ConnectorActReq[gun].Flag.bits.RefundCancel = true;
  2777. }
  2778. break;
  2779. case _DisReq_InvoiceRequest:
  2780. if(connector_req.Value > 0)
  2781. {
  2782. ShmChargerInfo->ConnectorActReq[gun].Flag.bits.InvoiceRequest = true;
  2783. ShmChargerInfo->ConnectorActReq[gun].ActionValue.bits.InvoiceIndex = connector_req.Value;
  2784. }
  2785. break;
  2786. default:
  2787. break;
  2788. }
  2789. }
  2790. else
  2791. {
  2792. }
  2793. return find;
  2794. }
  2795. unsigned char GetRemoteStartNoIDHandler(struct PACKET_STRUCTURE *packet, unsigned char *state)
  2796. {
  2797. *state = ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId ? YES : NO;
  2798. return true;
  2799. }
  2800. unsigned char GetRefundAmountHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2801. {
  2802. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2803. if(find)
  2804. {
  2805. }
  2806. else
  2807. {
  2808. }
  2809. return find;
  2810. }
  2811. unsigned char GetPrepaymentHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2812. {
  2813. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2814. if(find)
  2815. {
  2816. }
  2817. else
  2818. {
  2819. }
  2820. return find;
  2821. }
  2822. unsigned char GetPaymentFailReasonHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2823. {
  2824. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2825. if(find)
  2826. {
  2827. }
  2828. else
  2829. {
  2830. }
  2831. return find;
  2832. }
  2833. unsigned char GetQRCodeRequestHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2834. {
  2835. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2836. if(find)
  2837. {
  2838. }
  2839. else
  2840. {
  2841. }
  2842. return find;
  2843. }
  2844. unsigned char GetStationInfoHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2845. {
  2846. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2847. if(find)
  2848. {
  2849. }
  2850. else
  2851. {
  2852. if(packet->Header.id == 0xFF)
  2853. {
  2854. find = true;
  2855. }
  2856. }
  2857. return find;
  2858. }
  2859. void DisableConnector(unsigned char dispenserIndex)
  2860. {
  2861. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  2862. {
  2863. unsigned char connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  2864. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Enable = false;
  2865. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.TimeoutStopRequest = true;
  2866. }
  2867. }
  2868. void DispenserSocketProcess(int socketFd, struct sockaddr_in clientInfo, unsigned char index)
  2869. {
  2870. int rxLen = 0, timeout = 0;
  2871. unsigned char dispenserID = 0, dispenserIndex = 0;
  2872. struct PACKET_STRUCTURE receiveBuffer;
  2873. unsigned char modelName[64];
  2874. unsigned char ackResult = _R_NG;
  2875. DispenserAck_Status ack = _DAS_Wait;
  2876. LOG_INFO("IP %s connection(%d) is established, start dispenser process, PID = %d", (inet_ntoa(clientInfo.sin_addr)), index, getpid());
  2877. while(1)
  2878. {
  2879. if((rxLen = recv(socketFd, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  2880. {
  2881. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  2882. {
  2883. ackResult = _R_NG;
  2884. //ShowSocketData(&receiveBuffer);
  2885. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_WaitModelName)
  2886. {
  2887. // Reg: 0x01, Dispenser model name
  2888. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name)
  2889. {
  2890. memset(modelName, 0x00, 64);
  2891. dispenserID = receiveBuffer.Payload.data[receiveBuffer.Header.len - 2];
  2892. if(dispenserID > 0 && dispenserID <= GENERAL_GUN_QUANTITY &&
  2893. dispenserID <= ShmChargerInfo->Control.MaxDispenser)
  2894. {
  2895. dispenserIndex = dispenserID - 1;
  2896. memcpy(modelName, receiveBuffer.Payload.data, receiveBuffer.Header.len - 2);
  2897. if(IsAvailableDispenserIndexSequence(dispenserIndex))
  2898. {
  2899. if(DispenserIdentificationHandler(&receiveBuffer, dispenserIndex, modelName) == true)
  2900. {
  2901. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].DispenserIndex = dispenserIndex;
  2902. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status = _CNS_DispenserMatched;
  2903. LOG_INFO("********** Dispenser id %d check in, model name: %s **********\n", dispenserID, modelName);
  2904. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName, modelName, 64);
  2905. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectionChannel = index;
  2906. ackResult = _R_OK;
  2907. }
  2908. else
  2909. {
  2910. // response NG: gun quantity fail
  2911. LOG_INFO("Dispenser ID %d Identification Fail", dispenserID);
  2912. }
  2913. }
  2914. else
  2915. {
  2916. // response NG: dispenser Index fail
  2917. LOG_INFO("Dispenser ID %d Fail, Need Wait Another Dispenser", dispenserID);
  2918. }
  2919. }
  2920. else
  2921. {
  2922. LOG_INFO("Dispenser ID: %d Over Range", dispenserID);
  2923. }
  2924. ModelNameResponse(socketFd, &receiveBuffer, ackResult);
  2925. }
  2926. else
  2927. {
  2928. // do nothing
  2929. }
  2930. }
  2931. else if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_DispenserMatched)
  2932. {
  2933. // Reg: 0x01, Dispenser model name
  2934. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name)
  2935. {
  2936. ackResult = _R_OK;
  2937. ModelNameResponse(socketFd, &receiveBuffer, ackResult);
  2938. }
  2939. // Reg: 0x02, Connector ID
  2940. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Connector_ID)
  2941. {
  2942. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity > 0)
  2943. {
  2944. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Idle;
  2945. ackResult = _R_OK;
  2946. }
  2947. LOG_INFO("Dispenser ID %d Get Connector ID %s", dispenserID, ackResult == _R_OK ? "OK" : "NG");
  2948. ConnectorIDResponse(socketFd, &receiveBuffer, ackResult, dispenserIndex);
  2949. if(ackResult == _R_OK)
  2950. {
  2951. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DispenserConfigSync)
  2952. {
  2953. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DispenserConfigSync = true;
  2954. LOG_INFO("Dispenser ID %d Configuration Need To Synchronize", dispenserID);
  2955. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CONNECTION_TIMEOUT);
  2956. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_DEFAULT_PRICE);
  2957. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CURRENCY);
  2958. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BACKEND_STATUS);
  2959. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_ETHERNET_STATUS);
  2960. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_WIFI_STATUS);
  2961. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_TELCOM_MODEM_STATUS);
  2962. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BILLING_STATUS);
  2963. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STOP_BUTTON_STATUS);
  2964. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_AUTH_MODE_CONFIG);
  2965. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_EVCCID_CONFIG);
  2966. if(strlen(ShmChargerInfo->StationInfo.StationName) > 0 ||
  2967. strlen(ShmChargerInfo->StationInfo.StationID) > 0 ||
  2968. ShmChargerInfo->StationInfo.WeatherID != 0 ||
  2969. ShmChargerInfo->StationInfo.Temperature != 0)
  2970. {
  2971. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STATION_INFO);
  2972. }
  2973. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  2974. {
  2975. unsigned char gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  2976. Set_Connector_MiscCommand(gun, MISC_CONN_AVAILABILITY);
  2977. Set_Connector_MiscCommand(gun, MISC_CONN_QRCODE_INFO);
  2978. }
  2979. }
  2980. }
  2981. }
  2982. // Reg: 0x03, Power cabinet status
  2983. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Power_Cabinet_Status)
  2984. {
  2985. PowerCabinetStatusResponse(socketFd, &receiveBuffer, dispenserIndex);
  2986. }
  2987. // Reg: 0x04, Dispenser status
  2988. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Status)
  2989. {
  2990. if(DispenserStatusCodeHandler(&receiveBuffer, dispenserIndex))
  2991. {
  2992. ackResult = _R_OK;
  2993. }
  2994. DispenserStatusResponse(socketFd, &receiveBuffer, ackResult);
  2995. }
  2996. // Reg: 0x05, Charging capability
  2997. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charging_Capability)
  2998. {
  2999. if(ConnectorChargingCapabilityHandler(&receiveBuffer, dispenserIndex))
  3000. {
  3001. ChargingCapabilityResponse(socketFd, &receiveBuffer);
  3002. }
  3003. }
  3004. // Reg: 0x06, Charging target
  3005. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Charging_Target)
  3006. {
  3007. if(receiveBuffer.Header.len == 5)
  3008. {
  3009. if(ConnectorChargingTargetHandler(&receiveBuffer, dispenserIndex))
  3010. {
  3011. ackResult = _R_OK;
  3012. }
  3013. }
  3014. ChargingTargetResponse(socketFd, &receiveBuffer, ackResult);
  3015. }
  3016. // Reg: 0x07, Software update
  3017. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Software_Update)
  3018. {
  3019. unsigned char NeedUpgrade = FirmwareUpgradeHandler(&receiveBuffer, dispenserIndex);
  3020. FirmwareUpgradeResponse(socketFd, &receiveBuffer, dispenserIndex, NeedUpgrade);
  3021. }
  3022. // Reg: 0x08, Plug-in status
  3023. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Plug_In_Status)
  3024. {
  3025. if(receiveBuffer.Header.len == 2)
  3026. {
  3027. if(ConnectorPlugInHandler(&receiveBuffer, dispenserIndex))
  3028. {
  3029. ackResult = _R_OK;
  3030. }
  3031. }
  3032. PlugInStatusResponse(socketFd, &receiveBuffer, ackResult);
  3033. }
  3034. // Reg: 0x09, Connector state
  3035. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Connector_State)
  3036. {
  3037. if(ConnectorStateHandler(&receiveBuffer, dispenserIndex))
  3038. {
  3039. ackResult = _R_OK;
  3040. }
  3041. ConnectorStateResponse(socketFd, &receiveBuffer, ackResult);
  3042. }
  3043. // Reg: 0x0A, User ID
  3044. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_User_ID)
  3045. {
  3046. ack = UserIDHandler(&receiveBuffer, dispenserIndex);
  3047. UserIDResponse(socketFd, &receiveBuffer, ack);
  3048. }
  3049. // Reg: 0x0B, Charging permission
  3050. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charging_Permission)
  3051. {
  3052. ack = ChargingPermissionHandler(&receiveBuffer, dispenserIndex);
  3053. ChargingPermissionResponse(socketFd, &receiveBuffer, ack);
  3054. }
  3055. // Reg: 0x0C, Misc Control
  3056. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Misc_Control)
  3057. {
  3058. if(MiscControlHandler(&receiveBuffer, dispenserIndex))
  3059. {
  3060. ackResult = _R_OK;
  3061. }
  3062. MiscCmdRes(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  3063. }
  3064. // Reg: 0x0D, Report csu version
  3065. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Report_Csu_Version)
  3066. {
  3067. if(DispenserWriteCsuVersionInfoHandler(&receiveBuffer, dispenserIndex))
  3068. {
  3069. ackResult = _R_OK;
  3070. }
  3071. WriteCsuVersionResponse(socketFd, &receiveBuffer, ackResult);
  3072. }
  3073. // Reg: 0x0E, Report other version
  3074. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Report_Other_Version)
  3075. {
  3076. if(DispenserWriteOtherVersionInfoHandler(&receiveBuffer, dispenserIndex))
  3077. {
  3078. ackResult = _R_OK;
  3079. }
  3080. WriteOtherVersionResponse(socketFd, &receiveBuffer, ackResult);
  3081. }
  3082. // Reg: 0x0F, Charging info
  3083. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Charging_Info)
  3084. {
  3085. if(DispenserWriteChargingInfoHandler(&receiveBuffer, dispenserIndex))
  3086. {
  3087. ackResult = _R_OK;
  3088. }
  3089. WriteChargingInfoResponse(socketFd, &receiveBuffer, ackResult);
  3090. }
  3091. // Reg: 0x10, Charger system id
  3092. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charger_System_Id)
  3093. {
  3094. if(ReadChargerSystemIdHandler(&receiveBuffer, dispenserIndex))
  3095. {
  3096. ackResult = _R_OK;
  3097. }
  3098. ChargerSystemIdResponse(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  3099. }
  3100. // Reg: 0x11, Wait plug in
  3101. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_WaitPlugIn)
  3102. {
  3103. if(WriteWaitPlugInHandler(&receiveBuffer, dispenserIndex))
  3104. {
  3105. ackResult = _R_OK;
  3106. }
  3107. WriteWaitPlugInResponse(socketFd, &receiveBuffer, ackResult);
  3108. }
  3109. // Reg: 0x12, Ground Fault Detection
  3110. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_GroundFaultDetection)
  3111. {
  3112. if(GroundFaultDetectionHandler(&receiveBuffer, dispenserIndex))
  3113. {
  3114. ackResult = _R_OK;
  3115. }
  3116. GroundFaultDetectionResponse(socketFd, &receiveBuffer, ackResult);
  3117. }
  3118. // Reg: 0x13, Get Cabinet CSU Version
  3119. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Get_Cabinet_CSU_Version)
  3120. {
  3121. if(GetCabinetCSUVersionHandler(&receiveBuffer, dispenserIndex))
  3122. {
  3123. ackResult = _R_OK;
  3124. }
  3125. CabinetCSUVersionResponse(socketFd, &receiveBuffer, ackResult);
  3126. }
  3127. // Reg: 0x14, Get Cabinet Other Version
  3128. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Get_Cabinet_Other_Version)
  3129. {
  3130. if(GetCabinetOtherVersionHandler(&receiveBuffer, dispenserIndex))
  3131. {
  3132. ackResult = _R_OK;
  3133. }
  3134. CabinetOtherVersionResponse(socketFd, &receiveBuffer, ackResult);
  3135. }
  3136. // Reg: 0x15, Get Psu Quantity
  3137. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Get_Psu_Quantity)
  3138. {
  3139. if(GetPsuQuantityHandler(&receiveBuffer, dispenserIndex))
  3140. {
  3141. ackResult = _R_OK;
  3142. }
  3143. PsuQuantityResponse(socketFd, &receiveBuffer, ackResult);
  3144. }
  3145. // Reg: 0x16, Get Psu Version
  3146. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Get_Psu_Version)
  3147. {
  3148. int _PsuIndex = 0;
  3149. if(GetPsuVersionHandler(&receiveBuffer, &_PsuIndex))
  3150. {
  3151. ackResult = _R_OK;
  3152. }
  3153. PsuVersionResponse(socketFd, &receiveBuffer, ackResult, _PsuIndex);
  3154. }
  3155. // Reg: 0x17, Get Reservation
  3156. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Get_Reservation)
  3157. {
  3158. unsigned char _reservation = NO;
  3159. if(GetReservationHandler(&receiveBuffer, dispenserIndex, &_reservation))
  3160. {
  3161. ackResult = _R_OK;
  3162. }
  3163. ReservationResponse(socketFd, &receiveBuffer, ackResult, _reservation);
  3164. }
  3165. // Reg: 0x18, Dispenser Request
  3166. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Request)
  3167. {
  3168. if(DispenserReqHandler(&receiveBuffer, dispenserIndex))
  3169. {
  3170. ackResult = _R_OK;
  3171. }
  3172. DispenserReqHandlerResponse(socketFd, &receiveBuffer, ackResult);
  3173. }
  3174. // Reg: 0x19, State Of Remote Start Without Connector ID
  3175. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_RemoteStartNoIDState)
  3176. {
  3177. unsigned char _NoConnectorIDRemoteStart = 0;
  3178. if(GetRemoteStartNoIDHandler(&receiveBuffer, &_NoConnectorIDRemoteStart))
  3179. {
  3180. ackResult = _R_OK;
  3181. }
  3182. RemoteStarNoIDResponse(socketFd, &receiveBuffer, ackResult, _NoConnectorIDRemoteStart);
  3183. }
  3184. // Reg: 0x1A, Refund Amount
  3185. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_RefundAmount)
  3186. {
  3187. if(GetRefundAmountHandler(&receiveBuffer, dispenserIndex))
  3188. {
  3189. ackResult = _R_OK;
  3190. }
  3191. RefundAmountResponse(socketFd, &receiveBuffer, ackResult);
  3192. }
  3193. // Reg: 0x1B, Prepayment Info
  3194. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_PrepaymentInfo)
  3195. {
  3196. if(GetPrepaymentHandler(&receiveBuffer, dispenserIndex))
  3197. {
  3198. ackResult = _R_OK;
  3199. }
  3200. PrepaymentResponse(socketFd, &receiveBuffer, ackResult);
  3201. }
  3202. // Reg: 0x1C, Payment Fail Reason
  3203. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_PaymentFailReason)
  3204. {
  3205. if(GetPaymentFailReasonHandler(&receiveBuffer, dispenserIndex))
  3206. {
  3207. ackResult = _R_OK;
  3208. }
  3209. PaymentFailReasonResponse(socketFd, &receiveBuffer, ackResult);
  3210. }
  3211. // Reg: 0x1D, QR Code Request
  3212. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_ConnectorQRCode)
  3213. {
  3214. if(GetQRCodeRequestHandler(&receiveBuffer, dispenserIndex))
  3215. {
  3216. ackResult = _R_OK;
  3217. }
  3218. QRCodeResponse(socketFd, &receiveBuffer, ackResult);
  3219. }
  3220. // Reg: 0x1E, Station Info
  3221. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_StationInfo)
  3222. {
  3223. if(GetStationInfoHandler(&receiveBuffer, dispenserIndex))
  3224. {
  3225. ackResult = _R_OK;
  3226. }
  3227. StationInfoResponse(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  3228. }
  3229. }
  3230. // clean timeout
  3231. timeout = 0;
  3232. }
  3233. }
  3234. else
  3235. {
  3236. timeout += SOCKET_RECEIVE_INTERVAL;
  3237. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  3238. }
  3239. if(timeout >= DISPENSER_SOCKET_TIMEOUT || ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DuplicateIp)
  3240. {
  3241. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DuplicateIp)
  3242. {
  3243. LOG_INFO("Dispenser %d has been kick", dispenserIndex + 1);
  3244. }
  3245. else
  3246. {
  3247. //timeout
  3248. LOG_INFO("IP: %s, Socket %d connection timeout", (inet_ntoa(clientInfo.sin_addr)), index);
  3249. }
  3250. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_DispenserMatched)
  3251. {
  3252. DisableConnector(dispenserIndex);
  3253. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex], 0x00, sizeof(struct DispenserModule));
  3254. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Timeout;
  3255. DispenserCheckInInfoUpdate();
  3256. }
  3257. break;
  3258. }
  3259. }
  3260. }
  3261. int GetMacAddress(int sockFd, unsigned char *mac)
  3262. {
  3263. struct arpreq arpReq;
  3264. struct sockaddr_in clientInfo;
  3265. socklen_t len = sizeof(struct sockaddr_in);
  3266. memset(mac, 0x00, 6);
  3267. if(getpeername(sockFd, (struct sockaddr*)&clientInfo, &len) < 0)
  3268. {
  3269. return 0;
  3270. }
  3271. else
  3272. {
  3273. memcpy(&arpReq.arp_pa, &clientInfo, sizeof(struct sockaddr_in));
  3274. strcpy(arpReq.arp_dev, "eth1");
  3275. arpReq.arp_pa.sa_family = AF_INET;
  3276. arpReq.arp_ha.sa_family = AF_UNSPEC;
  3277. if(ioctl(sockFd, SIOCGARP, &arpReq) < 0)
  3278. {
  3279. return 0;
  3280. }
  3281. else
  3282. {
  3283. memcpy(mac, arpReq.arp_ha.sa_data, 6);
  3284. }
  3285. }
  3286. return 1;
  3287. }
  3288. int FindFreeChannel(void)
  3289. {
  3290. for(int i = 0; i < MAXIMUM_CONNECT_QUANTITY; i++)
  3291. {
  3292. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_FREE)
  3293. {
  3294. return i;
  3295. }
  3296. }
  3297. return FAIL;
  3298. }
  3299. BOOL IsConflictIp(uint32_t ipAddress)
  3300. {
  3301. for(int i = 0; i < MAXIMUM_CONNECT_QUANTITY; i++)
  3302. {
  3303. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress == ipAddress)
  3304. {
  3305. int dispenser = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex;
  3306. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Setting.bits.DuplicateIp == false)
  3307. {
  3308. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Setting.bits.DuplicateIp = true;
  3309. }
  3310. return true;
  3311. }
  3312. }
  3313. return false;
  3314. }
  3315. void InitDispenserInfo(void)
  3316. {
  3317. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  3318. {
  3319. bool disconnect = false;
  3320. //memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i], 0x00, sizeof(struct ConnectorInfoData));
  3321. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteStatus = 0;
  3322. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Enable = 0;
  3323. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ReadyToCharge = 0;
  3324. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex = 0;
  3325. disconnect = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection;
  3326. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.Value = 0;
  3327. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection = disconnect;
  3328. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].WarningInfo, 0x00, sizeof(struct WARNING_CODE_INFO));
  3329. //memset(&LastWarningInfo[i], 0x00, sizeof(struct WARNING_CODE_INFO));
  3330. }
  3331. //memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo, 0x00, sizeof(struct DispenserInfoData));
  3332. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity = ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence;
  3333. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity = ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity;
  3334. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentDispenserQuantity = 0;
  3335. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentConnectorQuantity = 0;
  3336. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  3337. {
  3338. unsigned char localStatus = 0;
  3339. localStatus = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus;
  3340. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i], 0x00, sizeof(struct DispenserModule));
  3341. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus = localStatus;
  3342. }
  3343. // initial CheckInLog from WiringInfo.DispenserSequence
  3344. ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status = 0;
  3345. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  3346. {
  3347. ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status |= (1 << i);
  3348. }
  3349. // initial ConnectorLog from WiringInfo.WiringSetting
  3350. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog, 0x00, sizeof(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog));
  3351. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  3352. {
  3353. if(i < GENERAL_GUN_QUANTITY)
  3354. {
  3355. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[i] = ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[i];
  3356. }
  3357. }
  3358. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo, 0x00, sizeof(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo));
  3359. ShmChargerInfo->CabinetMiscValue.Currency = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  3360. }
  3361. struct timespec _IpConflicted_time[CONNECTOR_QUANTITY];
  3362. void DispenserIpConflictedCheck(void)
  3363. {
  3364. while(1)
  3365. {
  3366. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3367. {
  3368. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DuplicateIp)
  3369. {
  3370. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DuplicateIpConfirm == NO)
  3371. {
  3372. GetClockTime(&_IpConflicted_time[i]);
  3373. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DuplicateIpConfirm = true;
  3374. LOG_INFO("Dispenser %d IP Conflicted Confirm", i + 1);
  3375. }
  3376. else
  3377. {
  3378. if((GetTimeoutValue(_IpConflicted_time[i]) / mSEC_VAL) >= IP_CONFLICTED_TIME)
  3379. {
  3380. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  3381. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  3382. {
  3383. unsigned char channel = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectionChannel;
  3384. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[channel].Status != _CNS_FREE)
  3385. {
  3386. LOG_INFO("Dispenser %d IP Conflicted Stop", i + 1);
  3387. DisableConnector(i);
  3388. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i], 0x00, sizeof(struct DispenserModule));
  3389. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus = _DS_Timeout;
  3390. DispenserCheckInInfoUpdate();
  3391. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[channel], 0x00, sizeof(struct ConnectionInfoData));
  3392. }
  3393. }
  3394. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DuplicateIp = false;
  3395. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DuplicateIpConfirm = false;
  3396. LOG_INFO("Dispenser %d IP Conflicted Clean", i + 1);
  3397. }
  3398. }
  3399. }
  3400. }
  3401. usleep(500000);
  3402. }
  3403. }
  3404. void ForkIpConflictedCheck(void)
  3405. {
  3406. pid_t forkId;
  3407. forkId = fork();
  3408. if(forkId == 0)
  3409. {
  3410. DispenserIpConflictedCheck();
  3411. return;
  3412. }
  3413. else if(forkId == -1)
  3414. {
  3415. LOG_INFO("fork fail");
  3416. }
  3417. }
  3418. int tcpSocketServerStart(void)
  3419. {
  3420. int sockFd = 0;
  3421. int clientSockFd = 0;
  3422. int connectIndex = 0;
  3423. int bindStatus = 0;
  3424. int reuseaddr = 1;
  3425. pid_t forkId;
  3426. struct sockaddr_in serverInfo, clientInfo;
  3427. socklen_t addrlen = sizeof(clientInfo);
  3428. //struct timeval timeout = {3, 0};
  3429. sockFd = socket(AF_INET, SOCK_STREAM, 0);
  3430. if(sockFd == -1)
  3431. {
  3432. LOG_INFO("InitSocketServer NG\n");
  3433. sleep(5);
  3434. return FAIL;
  3435. }
  3436. setsockopt(sockFd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
  3437. InitDispenserInfo();
  3438. bzero(&serverInfo,sizeof(serverInfo));
  3439. serverInfo.sin_family = PF_INET;
  3440. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  3441. serverInfo.sin_port = htons(TCP_LISTEN_PORT);
  3442. bindStatus = bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo));
  3443. if(bindStatus < 0)
  3444. {
  3445. LOG_INFO("socket bind fail");
  3446. return FAIL;
  3447. }
  3448. listen(sockFd, CONNECTION_LIMIT);
  3449. LOG_INFO("Dispenser TCP Server Start");
  3450. signal(SIGCHLD, SIG_IGN);
  3451. ForkIpConflictedCheck();
  3452. // Main loop
  3453. while(1)
  3454. {
  3455. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  3456. /*
  3457. if((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == ENONET) ||
  3458. (errno == EPROTO) || (errno == ENOPROTOOPT) || (errno == EOPNOTSUPP) ||
  3459. (errno == ENETDOWN) || (errno == ENETUNREACH) || (errno == EHOSTDOWN) ||
  3460. (errno == EHOSTUNREACH) || (errno == ECONNABORTED))
  3461. {
  3462. printf("accept error: %s\n", strerror(errno));
  3463. //exit(EXIT_FAILURE);
  3464. //break;
  3465. }
  3466. else if ( errno == EINTR)
  3467. {
  3468. printf("accept EINTRn");
  3469. continue;
  3470. }
  3471. else
  3472. {
  3473. printf("AccepCb: accept error: %s\n", strerror(errno));
  3474. //break;
  3475. //exit(EXIT_FAILURE);
  3476. }*/
  3477. LOG_INFO("Client ip: %s is accepted at port: %d", inet_ntoa(clientInfo.sin_addr), clientInfo.sin_port);
  3478. connectIndex = FindFreeChannel();
  3479. if(connectIndex >= 0)
  3480. {
  3481. if(IsConflictIp(clientInfo.sin_addr.s_addr) == false)
  3482. {
  3483. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex].Status = _CNS_WaitModelName;
  3484. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex].IpAddress = clientInfo.sin_addr.s_addr;
  3485. forkId = fork();
  3486. if(forkId == 0)
  3487. {
  3488. // child process
  3489. DispenserSocketProcess(clientSockFd, clientInfo, connectIndex);
  3490. close(clientSockFd);
  3491. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex], 0x00, sizeof(struct ConnectionInfoData));
  3492. return 0;
  3493. }
  3494. else if(forkId == -1)
  3495. {
  3496. LOG_INFO("fork fail");
  3497. }
  3498. }
  3499. else
  3500. {
  3501. // conflict ip address
  3502. LOG_INFO("Conflict IP address, close socket");
  3503. close(clientSockFd);
  3504. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex], 0x00, sizeof(struct ConnectionInfoData));
  3505. }
  3506. }
  3507. else
  3508. {
  3509. // no free channel
  3510. LOG_INFO("No free channel, close socket");
  3511. close(clientSockFd);
  3512. }
  3513. usleep(10000);
  3514. }
  3515. return 0;
  3516. }
  3517. void InitialConnector(void)
  3518. {
  3519. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3520. {
  3521. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  3522. }
  3523. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  3524. {
  3525. memset(&ConnectorCapability[i], 0x00, sizeof(ChargingCapabilityResponseInfo));
  3526. memset(&ConnectorRefund[i], 0x00, sizeof(RefundResponse));
  3527. memset(&ConnectorPrepayment[i], 0x00, sizeof(PrepaymentResponseInfo));
  3528. memset(&ConnectorPaymentFailReason[i], 0x00, sizeof(PaymentFailResponse));
  3529. memset(&_ConnectorQRCode[i][0], 0x00, 128);
  3530. memset(&_stationInfo[i], 0x00, sizeof(StationInfoData));
  3531. }
  3532. }
  3533. int main(void)
  3534. {
  3535. if(InitShareMemory() == FAIL)
  3536. {
  3537. #ifdef SystemLogMessage
  3538. LOG_ERROR("InitShareMemory NG");
  3539. #endif
  3540. sleep(5);
  3541. return 0;
  3542. }
  3543. InitialConnector();
  3544. // wait for self test completed
  3545. while(ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING ||
  3546. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  3547. {
  3548. sleep(1);
  3549. }
  3550. tcpSocketServerStart();
  3551. //tcpSocketClientStart();
  3552. return 0;
  3553. }