Module_EvComm.c 190 KB

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