Module_EvComm.c 114 KB

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