Module_EvComm.c 210 KB

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