main.c 233 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <stdint.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "../../define.h"
  30. #include "Config.h"
  31. #include <stdbool.h>
  32. #include <dirent.h>
  33. #include "timeout.h"
  34. #include "AudiCust.h"
  35. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  36. #define PASS 1
  37. #define FAIL -1
  38. #define BUFFER_SIZE 128
  39. #define YES 1
  40. #define NO 0
  41. #define NORMAL 0
  42. #define ABNORMAL 1
  43. #define EQUAL 0
  44. #define BTN_RELEASE 0
  45. #define BTN_PRESS 1
  46. #define MAX_BUF 64
  47. #define MtdBlockSize 0x600000
  48. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  49. #define UPGRADE_FAN 0x02
  50. #define UPGRADE_RB 0x09 //0x09 for dispenser //0x03 old module use
  51. #define UPGRADE_PRI 0x04
  52. #define UPGRADE_AC 0x05
  53. #define UPGRADE_LED 0x06
  54. #define SYSTEM_MIN_VOL 150
  55. #define MIN_OUTPUT_CUR 0
  56. #define AC_OUTPUT_VOL 220
  57. #define NO_DEFINE 255
  58. #define DEFAULT_AC_INDEX 2
  59. #define PSU_MIN_CUR 100
  60. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  61. #define uSEC_VAL 1000000
  62. #define SELFTEST_TIMEOUT 45
  63. #define AUTHORIZE_TIMEOUT 30
  64. #define AUTHORIZE_COMP_TIMEOUT 3
  65. #define AUTHORIZE_FAIL_TIMEOUT 3
  66. #define AUTHORIZE_STOP_TIMEOUT 30
  67. #define RETURN_TO_CHARGING_PAGE 30
  68. #define GUN_PREPARE_TIMEOUT 30
  69. #define GUN_EV_WAIT_TIMEOUT 120
  70. #define GUN_EVSE_WAIT_TIMEOUT 60
  71. #define GUN_COMP_WAIT_TIMEOUT 10
  72. #define GUN_PRECHARGING_TIMEOUT 60
  73. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  74. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  75. int whileLoopTime = 10000; // 10 ms
  76. int wtdFd = -1;
  77. byte _authorizeIndex = NO_DEFINE;
  78. bool IsAuthorizingMode();
  79. void ClearAuthorizedFlag();
  80. bool isDetectPlugin();
  81. void ClearDetectPluginFlag();
  82. int mystrcmp(unsigned char *p1, unsigned char *p2);
  83. long long DiffTimebWithNow(struct timeb ST);
  84. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  85. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  86. void ChargingTerminalProcess(byte gunIndex);
  87. void ChkPrimaryStatus();
  88. void StartSystemTimeoutDet(unsigned char flag);
  89. void StopSystemTimeoutDet();
  90. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  91. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  92. int StoreLogMsg_1(const char *fmt, ...);
  93. unsigned long GetTimeoutValue(struct timeval _sour_time);
  94. void gpio_set_value(unsigned int gpio, unsigned int value);
  95. void PRINTF_FUNC(char *string, ...);
  96. void ChangeGunSelectByIndex(byte sel);
  97. void InformOcppErrOccur(byte codeType);
  98. void RecordAlarmCode(byte gunIndex, char *code);
  99. void RecordWarningCode(byte gunIndex, char *code);
  100. void ReleaseWarningCodeByString(byte gunIndex, char *code);
  101. void ReleaseAlarmCode(byte gunIndex);
  102. int DB_Open(sqlite3 *db);
  103. int DB_Insert_Record(sqlite3 *db, int gun_index);
  104. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  105. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  106. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  107. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  108. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  109. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  110. struct StatusCodeData *ShmStatusCodeData;
  111. struct PsuData *ShmPsuData;
  112. struct CHAdeMOData *ShmCHAdeMOData;
  113. struct GBTData *ShmGBTData;
  114. struct CcsData *ShmCcsData;
  115. struct PrimaryMcuData *ShmPrimaryMcuData;
  116. struct FanModuleData *ShmFanModuleData;
  117. struct RelayModuleData *ShmRelayModuleData;
  118. struct LedModuleData *ShmLedModuleData;
  119. struct OCPP16Data *ShmOCPP16Data;
  120. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  121. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  122. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  123. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  124. static AudiCustInfo *gAudiCustInfo = NULL; //Jerry add
  125. // for initial index to check EV board type is correct
  126. byte _gunIndex = 0;
  127. byte _acgunIndex = 0;
  128. byte _chademoIndex = 0;
  129. byte _ccsIndex = 0;
  130. byte _gb_Index = 0;
  131. byte _ac_Index = 0;
  132. byte bd0_1_status = 0;
  133. byte bd0_2_status = 0;
  134. byte bd1_1_status = 0;
  135. byte bd1_2_status = 0;
  136. static uint8_t restartFlag = 0;
  137. bool isCardScan = false;
  138. bool isModelNameMatch = true;
  139. int rfidFd = -1;
  140. char *rfidPortName = "/dev/ttyS2";
  141. char *fwVersion = "V1.05.00.0000.00"; // "V0.16.00.0000.00";
  142. sqlite3 *localDb;
  143. bool isDb_ready;
  144. //------------------------------------------------------------------------------
  145. //--- share memory value ---
  146. //------------------------------------------------------------------------------
  147. static void changeLcmPage(uint8_t index)
  148. {
  149. ShmSysConfigAndInfo->SysInfo.SystemPage = index;
  150. }
  151. static uint8_t getCurLcmPage(void)
  152. {
  153. return ShmSysConfigAndInfo->SysInfo.SystemPage;
  154. }
  155. //------------------------------------------------------------------------------
  156. static void destroySelGun(uint8_t curGun)
  157. {
  158. uint8_t i = 0;
  159. uint8_t totalGun = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  160. //for status timeout
  161. if (curGun == DESTROY_ALL_SEL) {
  162. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  163. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  164. DEBUG_INFO_MSG("destroy all gun = %d, %d\r\n",
  165. gAudiCustInfo->SelGunInfo.LeftGun,
  166. gAudiCustInfo->SelGunInfo.RightGun);
  167. for (i = 0; i < totalGun; i++) {
  168. StopGunInfoTimeoutDet(i);
  169. memset(&gAudiCustInfo->PricesInfo[i], 0, sizeof(PricesInfo));
  170. }
  171. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  172. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  173. //changeLcmPage(_LCM_SELECT_GUN);
  174. return;
  175. }
  176. //for charging timeout or complete
  177. if ((curGun == LEFT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.LeftGun != SEL_GUN_RELEASE)) {
  178. if (gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  179. gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
  180. changeLcmPage(_LCM_SELECT_GUN);
  181. }
  182. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  183. StopGunInfoTimeoutDet(LEFT_GUN_NUM);
  184. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  185. ClearAuthorizedFlag();
  186. }
  187. DEBUG_INFO_MSG("destroy left gun, cur page = %d\r\n", getCurLcmPage());
  188. if (getCurLcmPage() == ShmSysConfigAndInfo->SysInfo.SystemPage) {
  189. DEBUG_INFO_MSG("clear left balance\r\n");
  190. memset(&gAudiCustInfo->PricesInfo[curGun], 0, sizeof(PricesInfo));
  191. gAudiCustInfo->PricesInfo[curGun].Balance = FAIL_BALANCE_PRICES;
  192. }
  193. }
  194. if ((curGun == RIGHT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.RightGun != SEL_GUN_RELEASE)) {
  195. if (gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  196. gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
  197. changeLcmPage(_LCM_SELECT_GUN);
  198. }
  199. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  200. StopGunInfoTimeoutDet(RIGHT_GUN_NUM);
  201. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  202. ClearAuthorizedFlag();
  203. }
  204. DEBUG_INFO_MSG("destroy right gun, cur page = %d\r\n", getCurLcmPage());
  205. if (getCurLcmPage() == ShmSysConfigAndInfo->SysInfo.SystemPage) {
  206. DEBUG_INFO_MSG("clear right balance\r\n");
  207. memset(&gAudiCustInfo->PricesInfo[curGun], 0, sizeof(PricesInfo));
  208. gAudiCustInfo->PricesInfo[curGun].Balance = FAIL_BALANCE_PRICES;
  209. }
  210. }
  211. }
  212. static int waitRightGunPlugIt(uint8_t curGun)
  213. {
  214. if ((curGun == RIGHT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  215. return PASS;
  216. }
  217. return FAIL;
  218. }
  219. static int waitLeftGunPlugIt(uint8_t curGun)
  220. {
  221. if ((curGun == LEFT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  222. return PASS;
  223. }
  224. return FAIL;
  225. }
  226. #if 0
  227. static void selGunEntryPlugIt(uint8_t curGun)
  228. {
  229. if (curGun == LEFT_GUN_NUM) {
  230. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_PLUG_IT;
  231. } else if (curGun == LEFT_GUN_NUM) {
  232. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_PLUG_IT;
  233. }
  234. }
  235. static int getSelGunAuthor(uint8_t selGun)
  236. {
  237. if ((selGun == LEFT_GUN_NUM) &&
  238. ((gAudiCustInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM) &&
  239. (gAudiCustInfo->SelGunInfo.LeftGun <= SEL_GUN_PLUG_IT))) {
  240. printf("getSelGunAuthor left = %d\r\n", gAudiCustInfo->SelGunInfo.LeftGun);
  241. return FAIL;
  242. }
  243. if ((selGun == RIGHT_GUN_NUM) &&
  244. ((gAudiCustInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM) &&
  245. (gAudiCustInfo->SelGunInfo.RightGun <= SEL_GUN_PLUG_IT))) {
  246. printf("getSelGunAuthor right = %d\r\n", gAudiCustInfo->SelGunInfo.RightGun);
  247. return FAIL;
  248. }
  249. return PASS;
  250. }
  251. #endif //0
  252. static void setSelGunWaitToAuthor(uint8_t curSel)
  253. {
  254. if (curSel == LEFT_GUN_NUM && gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM) {
  255. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_ATHOR;
  256. //printf("setSelGunWaitToAuthor left\r\n");
  257. StopGunInfoTimeoutDet(curSel);
  258. } else if (curSel == RIGHT_GUN_NUM && gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM) {
  259. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_ATHOR;
  260. //printf("setSelGunWaitToAuthor right\r\n");
  261. StopGunInfoTimeoutDet(curSel);
  262. }
  263. }
  264. static int getConfirmSelectedGun(uint8_t curSel)
  265. {
  266. if (((curSel == LEFT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
  267. ((curSel == RIGHT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
  268. return PASS;
  269. }
  270. return FAIL;
  271. }
  272. static void confirmSelGun(uint8_t selGun)
  273. {
  274. if (selGun == LEFT_GUN_NUM) {
  275. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
  276. StopGunInfoTimeoutDet(selGun);
  277. //printf("confirmSelGun left\r\n");
  278. } else if (selGun == RIGHT_GUN_NUM) {
  279. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
  280. StopGunInfoTimeoutDet(selGun);
  281. //printf("confirmSelGun right\r\n");
  282. }
  283. changeLcmPage(_LCM_IDLE);
  284. StartGunInfoTimeoutDet(selGun, Timeout_SelectGun);
  285. }
  286. //================================================
  287. // initial can-bus
  288. //================================================
  289. int InitCanBus()
  290. {
  291. int s0, nbytes;
  292. struct timeval tv;
  293. struct ifreq ifr0;
  294. struct sockaddr_can addr0;
  295. system("/sbin/ip link set can0 down");
  296. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  297. system("/sbin/ip link set can0 up");
  298. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  299. tv.tv_sec = 0;
  300. tv.tv_usec = 10000;
  301. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
  302. #ifdef SystemLogMessage
  303. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  304. #endif
  305. }
  306. nbytes = 40960;
  307. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0) {
  308. #ifdef SystemLogMessage
  309. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  310. #endif
  311. }
  312. nbytes = 40960;
  313. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0) {
  314. #ifdef SystemLogMessage
  315. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  316. #endif
  317. }
  318. strcpy(ifr0.ifr_name, "can0" );
  319. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  320. addr0.can_family = AF_CAN;
  321. addr0.can_ifindex = ifr0.ifr_ifindex;
  322. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  323. return s0;
  324. }
  325. //================================================
  326. // initial uart port
  327. //================================================
  328. char *_priPortName = "/dev/ttyS1";
  329. char *_485PortName = "/dev/ttyS5";
  330. int InitComPort(byte target)
  331. {
  332. int fd;
  333. struct termios tios;
  334. if (target == UPGRADE_PRI) {
  335. fd = open(_priPortName, O_RDWR);
  336. } else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED) {
  337. fd = open(_485PortName, O_RDWR);
  338. }
  339. if (fd <= 0) {
  340. #ifdef SystemLogMessage
  341. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  342. #endif
  343. return -1;
  344. }
  345. ioctl (fd, TCGETS, &tios);
  346. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  347. tios.c_lflag = 0;
  348. tios.c_iflag = 0;
  349. tios.c_oflag = 0;
  350. tios.c_cc[VMIN] = 0;
  351. tios.c_cc[VTIME] = (unsigned char)1;
  352. tios.c_lflag = 0;
  353. tcflush(fd, TCIFLUSH);
  354. ioctl (fd, TCSETS, &tios);
  355. return fd;
  356. }
  357. //=================================
  358. // Common routine
  359. //=================================
  360. void substr(char *dest, const char *src, unsigned int start, unsigned int cnt)
  361. {
  362. strncpy(dest, src + start, cnt);
  363. dest[cnt] = 0;
  364. }
  365. int InitWatchDog()
  366. {
  367. int fd;
  368. system("/usr/bin/fuser -k /dev/watchdog");
  369. sleep(1);
  370. system("echo V > /dev/watchdog");
  371. sleep(1);
  372. fd = open("/dev/watchdog", O_RDWR);
  373. if (fd <= 0) {
  374. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  375. }
  376. return fd;
  377. }
  378. int StoreLogMsg_1(const char *fmt, ...)
  379. {
  380. char Buf[4096 + 256];
  381. char buffer[4096];
  382. va_list args;
  383. struct timeb SeqEndTime;
  384. struct tm *tm;
  385. va_start(args, fmt);
  386. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  387. va_end(args);
  388. memset(Buf, 0, sizeof(Buf));
  389. ftime(&SeqEndTime);
  390. SeqEndTime.time = time(NULL);
  391. tm = localtime(&SeqEndTime.time);
  392. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES) {
  393. sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
  394. tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer);
  395. printf("%s \n", Buf);
  396. } else {
  397. sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  398. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm,
  399. buffer,
  400. tm->tm_year + 1900, tm->tm_mon + 1);
  401. system(Buf);
  402. }
  403. return rc;
  404. }
  405. unsigned long GetTimeoutValue(struct timeval _sour_time)
  406. {
  407. struct timeval _end_time;
  408. gettimeofday(&_end_time, NULL);
  409. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  410. }
  411. int mystrcmp(unsigned char *p1, unsigned char *p2)
  412. {
  413. while (*p1 == *p2) {
  414. if (*p1 == '\0' || *p2 == '\0') {
  415. break;
  416. }
  417. p1++;
  418. p2++;
  419. }
  420. if (*p1 == '\0' && *p2 == '\0') {
  421. return (PASS);
  422. } else {
  423. return (FAIL);
  424. }
  425. }
  426. int DiffTimeb(struct timeb ST, struct timeb ET)
  427. {
  428. //return milli-second
  429. unsigned int StartTime, StopTime;
  430. StartTime = (unsigned int)ST.time;
  431. StopTime = (unsigned int)ET.time;
  432. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  433. return (StopTime - StartTime);
  434. }
  435. bool CheckTimeOut(struct timeb ST)
  436. {
  437. struct timeb ET;
  438. unsigned int StartTime, StopTime;
  439. ftime(&ET);
  440. StartTime = (unsigned int) ST.time;
  441. StopTime = (unsigned int) ET.time;
  442. return (StopTime > StartTime) ? YES : NO;
  443. }
  444. void setChargerMode(byte gun_index, byte mode)
  445. {
  446. chargingInfo[gun_index]->SystemStatus = mode;
  447. }
  448. void PRINTF_FUNC(char *string, ...)
  449. {
  450. va_list args;
  451. char buffer[4096];
  452. va_start(args, string);
  453. vsnprintf(buffer, sizeof(buffer), string, args);
  454. va_end(args);
  455. DEBUG_INFO_MSG("%s \n", buffer);
  456. }
  457. long long DiffTimebWithNow(struct timeb ST)
  458. {
  459. //return milli-second
  460. struct timeb ET;
  461. long long StartTime, StopTime;
  462. ftime(&ET);
  463. StartTime = (long long)ST.time;
  464. StopTime = (long long)ET.time;
  465. return ((StopTime - StartTime) * 1000) + (ET.millitm - ST.millitm);
  466. }
  467. //==========================================
  468. // Check interface status
  469. //==========================================
  470. int isInterfaceUp(const char *interface)
  471. {
  472. int result = FAIL;
  473. FILE *fp;
  474. char cmd[256];
  475. char buf[512];
  476. strcpy(cmd, "ifconfig");
  477. fp = popen(cmd, "r");
  478. if (fp != NULL) {
  479. while (fgets(buf, sizeof(buf), fp) != NULL) {
  480. if (strstr(buf, interface) > 0) {
  481. result = PASS;
  482. }
  483. }
  484. }
  485. pclose(fp);
  486. return result;
  487. }
  488. //=================================
  489. // Create all share memory
  490. //=================================
  491. int CreateShareMemory()
  492. {
  493. int MeterSMId;
  494. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0) {
  495. #ifdef SystemLogMessage
  496. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  497. #endif
  498. return 0;
  499. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  500. #ifdef SystemLogMessage
  501. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  502. #endif
  503. return 0;
  504. }
  505. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  506. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0) {
  507. #ifdef SystemLogMessage
  508. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  509. #endif
  510. return 0;
  511. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  512. #ifdef SystemLogMessage
  513. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  514. #endif
  515. return 0;
  516. }
  517. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  518. //creat ShmPsuData
  519. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0) {
  520. #ifdef SystemLogMessage
  521. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  522. #endif
  523. return 0;
  524. } else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  525. #ifdef SystemLogMessage
  526. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  527. #endif
  528. return 0;
  529. }
  530. memset(ShmPsuData, 0, sizeof(struct PsuData));
  531. if (CHAdeMO_QUANTITY > 0) {
  532. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0) {
  533. #ifdef SystemLogMessage
  534. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  535. #endif
  536. return 0;
  537. } else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  538. #ifdef SystemLogMessage
  539. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  540. #endif
  541. return 0;
  542. }
  543. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  544. }
  545. if (GB_QUANTITY > 0) {
  546. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0) {
  547. #ifdef SystemLogMessage
  548. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  549. #endif
  550. return 0;
  551. } else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  552. #ifdef SystemLogMessage
  553. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  554. #endif
  555. return 0;
  556. }
  557. memset(ShmGBTData, 0, sizeof(struct GBTData));
  558. }
  559. //creat ShmCcsData
  560. if (CCS_QUANTITY > 0) {
  561. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0) {
  562. #ifdef SystemLogMessage
  563. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  564. #endif
  565. return 0;
  566. } else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  567. #ifdef SystemLogMessage
  568. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  569. #endif
  570. return 0;
  571. }
  572. memset(ShmCcsData, 0, sizeof(struct CcsData));
  573. }
  574. //creat ShmPrimaryMcuData
  575. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0) {
  576. #ifdef SystemLogMessage
  577. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  578. #endif
  579. return 0;
  580. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  581. #ifdef SystemLogMessage
  582. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  583. #endif
  584. return 0;
  585. }
  586. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  587. //creat ShmFanModuleData
  588. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0) {
  589. #ifdef SystemLogMessage
  590. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  591. #endif
  592. return 0;
  593. } else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  594. #ifdef SystemLogMessage
  595. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  596. #endif
  597. return 0;
  598. }
  599. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  600. //creat ShmRelayModuleData
  601. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0) {
  602. #ifdef SystemLogMessage
  603. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  604. #endif
  605. return 0;
  606. } else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  607. #ifdef SystemLogMessage
  608. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  609. #endif
  610. return 0;
  611. }
  612. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  613. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0) {
  614. #ifdef SystemLogMessage
  615. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmLedModuleData NG \n");
  616. #endif
  617. return 0;
  618. } else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  619. #ifdef SystemLogMessage
  620. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmLedModuleData NG \n");
  621. #endif
  622. return 0;
  623. }
  624. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  625. //creat ShmOCPP16Data
  626. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0) {
  627. #ifdef SystemLogMessage
  628. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  629. #endif
  630. return 0;
  631. } else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  632. #ifdef SystemLogMessage
  633. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  634. #endif
  635. return 0;
  636. }
  637. // memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  638. //creat Audi customization info
  639. if ((MeterSMId = shmget(ShmAudiCustInfoKey, sizeof(AudiCustInfo), IPC_CREAT | 0777)) < 0) {
  640. #ifdef SystemLogMessage
  641. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget select gun info NG \n");
  642. #endif
  643. return 0;
  644. } else if ((gAudiCustInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  645. #ifdef SystemLogMessage
  646. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat shmget select gun info \n");
  647. #endif
  648. return 0;
  649. }
  650. memset(gAudiCustInfo, 0, sizeof(AudiCustInfo));
  651. return 1;
  652. }
  653. //=================================
  654. // LCM Page
  655. //=================================
  656. void ChangeLcmByIndex(byte page_index)
  657. {
  658. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2 ||
  659. page_index == _LCM_COMPLETE || page_index == _LCM_FIX) {
  660. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  661. }
  662. }
  663. //======================================================
  664. // Peripheral initial
  665. //======================================================
  666. void InitGPIO()
  667. {
  668. /*****************0~3, 4 bank, bank x 32+ num*********************/
  669. /***************************************************************/
  670. /*************** GPIO 0 ***************************************/
  671. /***************************************************************/
  672. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  673. system("echo 22 > /sys/class/gpio/export");
  674. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  675. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  676. system("echo 23 > /sys/class/gpio/export");
  677. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  678. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  679. system("echo 26 > /sys/class/gpio/export");
  680. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  681. system("echo 1 > /sys/class/gpio/gpio26/value");
  682. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  683. system("echo 27 > /sys/class/gpio/export");
  684. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  685. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  686. system("echo 29 > /sys/class/gpio/export");
  687. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  688. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  689. system("echo 19 > /sys/class/gpio/export");
  690. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  691. system("echo 1 > /sys/class/gpio/gpio19/value");
  692. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  693. system("echo 20 > /sys/class/gpio/export");
  694. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  695. /***************************************************************/
  696. /*************** GPIO 1 ***************************************/
  697. /***************************************************************/
  698. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  699. system("echo 44 > /sys/class/gpio/export");
  700. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  701. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  702. system("echo 45 > /sys/class/gpio/export");
  703. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  704. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  705. system("echo 46 > /sys/class/gpio/export");
  706. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  707. system("echo 0 > /sys/class/gpio/gpio46/value");
  708. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  709. system("echo 47 > /sys/class/gpio/export");
  710. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  711. /***************************************************************/
  712. /*************** GPIO 2 ***************************************/
  713. /***************************************************************/
  714. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  715. system("echo 89 > /sys/class/gpio/export");
  716. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  717. system("echo 1 > /sys/class/gpio/gpio89/value");
  718. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  719. system("echo 87 > /sys/class/gpio/export");
  720. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  721. system("echo 0 > /sys/class/gpio/gpio87/value");
  722. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  723. system("echo 88 > /sys/class/gpio/export");
  724. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  725. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  726. system("echo 86 > /sys/class/gpio/export");
  727. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  728. /***************************************************************/
  729. /*************** GPIO 3 ***************************************/
  730. /***************************************************************/
  731. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  732. system("echo 111 > /sys/class/gpio/export");
  733. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  734. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  735. system("echo 114 > /sys/class/gpio/export");
  736. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  737. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  738. system("echo 113 > /sys/class/gpio/export");
  739. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  740. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  741. system("echo 110 > /sys/class/gpio/export");
  742. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  743. system("echo 0 > /sys/class/gpio/gpio110/value");
  744. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  745. system("echo 115 > /sys/class/gpio/export");
  746. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  747. system("echo 0 > /sys/class/gpio/gpio115/value");
  748. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  749. system("echo 112 > /sys/class/gpio/export");
  750. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  751. system("echo 1 > /sys/class/gpio/gpio112/value");
  752. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  753. system("echo 116 > /sys/class/gpio/export");
  754. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  755. system("echo 0 > /sys/class/gpio/gpio116/value");
  756. #ifdef SystemLogMessage
  757. //DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK");
  758. #endif
  759. }
  760. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  761. {
  762. int fd, wrd;
  763. unsigned char *buf;
  764. unsigned int ChkSum, ChkSumOrg;
  765. if ((buf = malloc(MtdBlockSize)) == NULL) {
  766. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  767. if (ShmStatusCodeData != NULL) {
  768. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  769. }
  770. sleep(5);
  771. system("reboot -f");
  772. sleep(5);
  773. system("reboot -f");
  774. }
  775. memset(buf, 0, MtdBlockSize);
  776. //================================================
  777. // Load configuration from mtdblock10
  778. //================================================
  779. fd = open("/dev/mtdblock10", O_RDWR);
  780. if (fd < 0) {
  781. free(buf);
  782. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  783. if (ShmStatusCodeData != NULL) {
  784. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  785. }
  786. sleep(5);
  787. system("reboot -f");
  788. sleep(5);
  789. system("reboot -f");
  790. }
  791. wrd = read(fd, buf, MtdBlockSize);
  792. close(fd);
  793. if (wrd < MtdBlockSize) {
  794. free(buf);
  795. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  796. if (ShmStatusCodeData != NULL) {
  797. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  798. }
  799. sleep(5);
  800. system("reboot -f");
  801. sleep(5);
  802. system("reboot -f");
  803. }
  804. ChkSum = 0;
  805. for (wrd = 0; wrd < MtdBlockSize - 4; wrd++) {
  806. ChkSum += buf[wrd];
  807. }
  808. memcpy(&ChkSumOrg, buf + (0x00600000 - 4), sizeof(ChkSumOrg));
  809. //================================================
  810. // Load configuration from mtdblock11
  811. //================================================
  812. if (ChkSum != ChkSumOrg) {
  813. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  814. fd = open("/dev/mtdblock11", O_RDWR);
  815. if (fd < 0) {
  816. free(buf);
  817. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  818. if (ShmStatusCodeData != NULL) {
  819. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  820. }
  821. sleep(5);
  822. system("reboot -f");
  823. sleep(5);
  824. system("reboot -f");
  825. }
  826. memset(buf, 0, MtdBlockSize);
  827. wrd = read(fd, buf, MtdBlockSize);
  828. close(fd);
  829. if (wrd < MtdBlockSize) {
  830. free(buf);
  831. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  832. if (ShmStatusCodeData != NULL) {
  833. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  834. }
  835. sleep(5);
  836. system("reboot -f");
  837. sleep(5);
  838. system("reboot -f");
  839. }
  840. ChkSum = 0;
  841. for (wrd = 0; wrd < MtdBlockSize - 4; wrd++) {
  842. ChkSum += buf[wrd];
  843. }
  844. memcpy(&ChkSumOrg, buf + (0x00600000 - 4), sizeof(ChkSumOrg));
  845. //================================================
  846. // Load configuration from mtdblock12 (Factory default)
  847. //================================================
  848. if (ChkSum != ChkSumOrg) {
  849. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  850. fd = open("/dev/mtdblock12", O_RDWR);
  851. if (fd < 0) {
  852. free(buf);
  853. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  854. if (ShmStatusCodeData != NULL) {
  855. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  856. }
  857. sleep(5);
  858. system("reboot -f");
  859. sleep(5);
  860. system("reboot -f");
  861. }
  862. memset(buf, 0, MtdBlockSize);
  863. wrd = read(fd, buf, MtdBlockSize);
  864. close(fd);
  865. if (wrd < MtdBlockSize) {
  866. free(buf);
  867. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  868. if (ShmStatusCodeData != NULL) {
  869. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  870. }
  871. sleep(5);
  872. system("reboot -f");
  873. sleep(5);
  874. system("reboot -f");
  875. }
  876. ChkSum = 0;
  877. for (wrd = 0; wrd < MtdBlockSize - 4; wrd++) {
  878. ChkSum += buf[wrd];
  879. }
  880. memcpy(&ChkSumOrg, buf + (0x00600000 - 4), sizeof(ChkSumOrg));
  881. if (ChkSum != ChkSumOrg) {
  882. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  883. free(buf);
  884. system("cd /root;./FactoryConfig -m");
  885. system("sync");
  886. sleep(5);
  887. system("reboot -f");
  888. sleep(5);
  889. system("reboot -f");
  890. return FAIL;
  891. }
  892. }
  893. }
  894. //load OK
  895. memcpy((struct SysConfigData *)ptr, buf, sizeof(struct SysConfigData));
  896. free(buf);
  897. //DEBUG_INFO_MSG("Load SysConfigData OK\n");
  898. return PASS;
  899. }
  900. int isReachableInternet()
  901. {
  902. int result = FAIL;
  903. FILE *fp;
  904. char cmd[256];
  905. char buf[512];
  906. char tmp[512];
  907. #if defined DD360 || defined DD360Audi
  908. strcpy(cmd, "ifconfig eth0");
  909. fp = popen(cmd, "r");
  910. if (fp != NULL) {
  911. while (fgets(buf, sizeof(buf), fp) != NULL) {
  912. if (strstr(buf, "inet addr:") > 0) {
  913. sscanf(buf, "%*s%s", tmp);
  914. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf) - strspn(tmp, "addr:"));
  915. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL) {
  916. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  917. }
  918. }
  919. }
  920. }
  921. pclose(fp);
  922. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo == NORMAL) {
  923. result = FAIL;
  924. } else {
  925. result = PASS;
  926. }
  927. #else
  928. if (ShmOCPP16Data->OcppConnStatus == PASS) {
  929. result = PASS;
  930. } else {
  931. strcpy(cmd, "ifconfig eth0");
  932. fp = popen(cmd, "r");
  933. if (fp != NULL) {
  934. while (fgets(buf, sizeof(buf), fp) != NULL) {
  935. if (strstr(buf, "inet addr:") > 0) {
  936. sscanf(buf, "%*s%s", tmp);
  937. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf) - strspn(tmp, "addr:"));
  938. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL) {
  939. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  940. }
  941. }
  942. }
  943. }
  944. pclose(fp);
  945. memset(buf, 0x00, sizeof(buf));
  946. #if !defined DD360 && !defined DD360Audi
  947. for (int idx = 0; idx < ARRAY_SIZE(valid_Internet); idx++) {
  948. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  949. fp = popen(cmd, "r");
  950. if (fp != NULL) {
  951. while (fgets(buf, sizeof(buf), fp) != NULL) {
  952. if (strstr(buf, "transmitted") > 0) {
  953. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  954. if (strstr(buf, "100%") != NULL) {
  955. } else {
  956. result = PASS;
  957. }
  958. //DEBUG_INFO("%s",buf);
  959. //DEBUG_INFO("%s\n",tmp);
  960. }
  961. }
  962. }
  963. pclose(fp);
  964. }
  965. #endif //!defined DD360 && !defined DD360Audi
  966. }
  967. #endif //defined DD360 || defined DD360Audi
  968. return result;
  969. }
  970. void InitEthernet()
  971. {
  972. char tmpbuf[256];
  973. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  974. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  975. sleep(2);
  976. //Init Eth0 for internet
  977. memset(tmpbuf, 0, 256);
  978. sprintf(tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up",
  979. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  980. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  981. //sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.10 netmask 255.255.255.0 up");
  982. system(tmpbuf);
  983. memset(tmpbuf, 0, 256);
  984. sprintf(tmpbuf, "route add default gw %s eth0 ",
  985. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  986. //sprintf(tmpbuf,"route add default gw 192.168.100.1 eth0 ");
  987. system(tmpbuf);
  988. //system("ifconfig lo up");
  989. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  990. //Init Eth1 for administrator tool
  991. memset(tmpbuf, 0, 256);
  992. sprintf(tmpbuf, "/sbin/ifconfig eth1 %s netmask %s up",
  993. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  994. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  995. system(tmpbuf);
  996. //Run DHCP client if enabled
  997. system("killall udhcpc");
  998. system("rm -rf /etc/resolv.conf");
  999. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1000. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  1001. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0) {
  1002. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1003. system(tmpbuf);
  1004. }
  1005. //Upgrade system id to /etc/hostname
  1006. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1007. system(tmpbuf);
  1008. pid_t pid = fork();
  1009. DEBUG_INFO_MSG("InitEthernet = %d\r\n", pid);
  1010. if (pid == 0) {
  1011. for (;;) {
  1012. #if !defined DD360 && !defined DD360Audi
  1013. bool ethResult = YES;
  1014. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet) {
  1015. ethResult = NO;
  1016. }
  1017. if (!ethResult && ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') {
  1018. ethResult = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi;
  1019. }
  1020. if (!ethResult && ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') {
  1021. ethResult = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi;
  1022. }
  1023. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1024. #else
  1025. isReachableInternet();
  1026. #endif //!defined DD360 && !defined DD360Audi
  1027. sleep(5);
  1028. }
  1029. }
  1030. /*#ifdef SystemLogMessage
  1031. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK");
  1032. #endif*/
  1033. }
  1034. int InitialRfidPort()
  1035. {
  1036. int uartO2 = open(rfidPortName, O_RDWR);
  1037. struct termios tios;
  1038. if (uartO2 != FAIL) {
  1039. ioctl (uartO2, TCGETS, &tios);
  1040. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1041. tios.c_lflag = 0;
  1042. tios.c_iflag = 0;
  1043. tios.c_oflag = 0;
  1044. tios.c_cc[VMIN] = 0;
  1045. tios.c_cc[VTIME] = (unsigned char) 1;
  1046. tios.c_lflag = 0;
  1047. tcflush(uartO2, TCIFLUSH);
  1048. ioctl(uartO2, TCSETS, &tios);
  1049. }
  1050. if (uartO2 < 0) {
  1051. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1052. }
  1053. return uartO2;
  1054. }
  1055. void GetMacAddress()
  1056. {
  1057. for (byte index = 0; index < 2; index++) {
  1058. int fd;
  1059. struct ifreq ifr;
  1060. char tarEth[5];
  1061. char Mac[18];
  1062. sprintf(tarEth, "eth%d", index);
  1063. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1064. ifr.ifr_addr.sa_family = AF_INET;
  1065. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1066. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1067. close(fd);
  1068. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1069. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1070. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1071. if (index == 0) {
  1072. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1073. } else {
  1074. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1075. }
  1076. }
  1077. }
  1078. void GetFirmwareVersion()
  1079. {
  1080. // Get CSU root file system version
  1081. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1082. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1083. for (uint8_t idx = 0; idx < 3; idx++) {
  1084. if (ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'J') {
  1085. chademo++;
  1086. count++;
  1087. } else if (ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'G') {
  1088. gb++;
  1089. count++;
  1090. } else if (ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'U' ||
  1091. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'E') {
  1092. ccs++;
  1093. count++;
  1094. }
  1095. }
  1096. if (count == 1) {
  1097. if (chademo > 0) {
  1098. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1099. } else if (ccs > 0) {
  1100. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1101. } else if (gb > 0) {
  1102. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1103. }
  1104. } else {
  1105. if (chademo > 0 && ccs > 0) {
  1106. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1107. } else if (chademo > 0 && gb > 0) {
  1108. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1109. } else if (ccs > 0 && gb > 0) {
  1110. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1111. }
  1112. }
  1113. // Get network option from model name
  1114. switch (ShmSysConfigAndInfo->SysConfig.ModelName[10]) {
  1115. case 'B':
  1116. case 'U':
  1117. //Blue tooth
  1118. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1119. break;
  1120. case 'W':
  1121. // WIFI
  1122. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1123. break;
  1124. case 'T':
  1125. // 3G/4G
  1126. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1127. break;
  1128. default:
  1129. // LAN
  1130. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1131. break;
  1132. }
  1133. // Get rating power from model name
  1134. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1135. // Get IEC or UL
  1136. char _buf[3] = {0};
  1137. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1138. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL) {
  1139. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1140. DEBUG_INFO_MSG("IEC model");
  1141. } else if (strcmp(_buf, "WU") == EQUAL) {
  1142. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1143. DEBUG_INFO_MSG("UL model");
  1144. }
  1145. }
  1146. void InitialShareMemoryInfo()
  1147. {
  1148. FILE *fp;
  1149. char cmd[512];
  1150. char buf[512];
  1151. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1152. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1153. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1154. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1155. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1156. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1157. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1158. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1159. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1160. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1161. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1162. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1163. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1164. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1165. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1166. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1167. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1168. sprintf(cmd, "/bin/uname -r");
  1169. fp = popen(cmd, "r");
  1170. if (fp == NULL) {
  1171. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1172. } else {
  1173. while (fgets(buf, sizeof(buf), fp) != NULL) {
  1174. strcpy((char *)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1175. }
  1176. }
  1177. // 雙槍 CCS + Chademo
  1178. GetFirmwareVersion();
  1179. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1180. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1181. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1182. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1183. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1184. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1185. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1186. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1187. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1188. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1189. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1190. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1191. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1192. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1193. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1194. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1195. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1196. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1197. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  1198. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  1199. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1200. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1201. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1202. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1203. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1204. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1205. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1206. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1207. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1208. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1209. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1210. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1211. ShmRelayModuleData->SelfTest_Comp = NO;
  1212. ShmFanModuleData->SelfTest_Comp = NO;
  1213. ShmLedModuleData->SelfTest_Comp = NO;
  1214. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;//_LCM_NONE; //Jerry add
  1215. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1216. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1217. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1218. ShmFanModuleData->TestFanSpeed = 0;
  1219. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1220. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1221. char EvsePower[2];
  1222. EvsePower[2] = '\0';
  1223. unsigned short buf_pow = 0;
  1224. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 6) {
  1225. strncpy(EvsePower, (char *)(ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  1226. if (strcmp(EvsePower, "15") == EQUAL) {
  1227. buf_pow = 150;
  1228. } else if (strcmp(EvsePower, "30") == EQUAL) {
  1229. buf_pow = 30;
  1230. } else if (strcmp(EvsePower, "60") == EQUAL) {
  1231. buf_pow = 60;
  1232. } else if (strcmp(EvsePower, "12") == EQUAL) {
  1233. buf_pow = 120;
  1234. } else if (strcmp(EvsePower, "18") == EQUAL) {
  1235. buf_pow = 180;
  1236. } else if (strcmp(EvsePower, "36") == EQUAL) {
  1237. buf_pow = 360;
  1238. }
  1239. ShmSysConfigAndInfo->SysConfig.RatingCurrent = (buf_pow / 30) * PSU_MIN_CUR;
  1240. if (ShmSysConfigAndInfo->SysConfig.MaxChargingPower == 0 ||
  1241. ShmSysConfigAndInfo->SysConfig.MaxChargingPower > buf_pow) {
  1242. ShmSysConfigAndInfo->SysConfig.MaxChargingPower = buf_pow;
  1243. }
  1244. }
  1245. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1246. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1247. }
  1248. int Initialization()
  1249. {
  1250. // 初始化卡號驗證的 Flag
  1251. ClearAuthorizedFlag();
  1252. // 初始化插槍驗證的 Flag
  1253. ClearDetectPluginFlag();
  1254. // UART 2 for Rfid
  1255. rfidFd = InitialRfidPort();
  1256. int pinOut[2] = { 116, 115 };
  1257. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++) {
  1258. chargingInfo[count]->RemoteStartFlag = NO;
  1259. if (chargingInfo[count]->Type == _Type_Chademo) {
  1260. gpio_set_value(pinOut[count], 0x00);
  1261. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1262. } else if (chargingInfo[count]->Type == _Type_GB) {
  1263. gpio_set_value(pinOut[count], 0x00);
  1264. ShmGBTData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1265. } else if (chargingInfo[count]->Type == _Type_CCS_2) {
  1266. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1267. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  1268. gpio_set_value(pinOut[1], 0x01);
  1269. } else {
  1270. gpio_set_value(pinOut[count], 0x01);
  1271. }
  1272. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1273. }
  1274. }
  1275. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  1276. }
  1277. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++) {
  1278. ac_chargingInfo[count]->RemoteStartFlag = NO;
  1279. if (ac_chargingInfo[count]->Type == _Type_AC) {
  1280. ac_chargingInfo[count]->SelfTest_Comp = NO;
  1281. strcpy((char *)ShmOCPP16Data->StatusNotification[count + ShmSysConfigAndInfo->SysConfig.TotalConnectorCount].ErrorCode, "NoError");
  1282. }
  1283. }
  1284. //PRINTF_FUNC("Initialization OK \n");
  1285. return PASS;
  1286. }
  1287. bool InitialSystemDefaultConfig()
  1288. {
  1289. bool result = true;
  1290. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1291. InitGPIO();
  1292. InitEthernet();
  1293. GetMacAddress();
  1294. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1295. // sleep(3);
  1296. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1297. // sleep(1);
  1298. // system("/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up");
  1299. return result;
  1300. }
  1301. void DisplaySelfTestFailReason()
  1302. {
  1303. // RB、FB、407、EV 小板中有些板子無回應
  1304. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1305. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; }
  1306. if (ShmFanModuleData->SelfTest_Comp == NO)
  1307. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; }
  1308. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1309. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; }
  1310. if (ShmLedModuleData->SelfTest_Comp == NO)
  1311. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES; }
  1312. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  1313. if (chargingInfo[index]->Type == _Type_Chademo) {
  1314. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1315. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; }
  1316. } else if (chargingInfo[index]->Type == _Type_GB) {
  1317. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1318. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; }
  1319. } else if (chargingInfo[index]->Type == _Type_CCS_2) {
  1320. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1321. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1322. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; }
  1323. }
  1324. }
  1325. }
  1326. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++) {
  1327. // 先借 GBT 顯示
  1328. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  1329. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; }
  1330. }
  1331. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO) {
  1332. #if !defined DD360 && !defined DD360Audi
  1333. // AC Contact 未搭上
  1334. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1335. #endif //!defined DD360 && !defined DD360Audi
  1336. } else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0) {
  1337. // PSU 通訊問題
  1338. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1339. }
  1340. }
  1341. void SelfTestRun()
  1342. {
  1343. bool evInitFlag = false;
  1344. StartSystemTimeoutDet(Timeout_SelftestChk);
  1345. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1346. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE) {
  1347. ChkPrimaryStatus();
  1348. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2) {
  1349. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1350. return;
  1351. }
  1352. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) {
  1353. if (ShmPsuData->Work_Step == _NO_WORKING ||
  1354. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL) {
  1355. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1356. return;
  1357. }
  1358. switch (ShmSysConfigAndInfo->SysInfo.SelfTestSeq) {
  1359. case _STEST_VERSION: {
  1360. if ((strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 || ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  1361. && (ShmRelayModuleData->SelfTest_Comp != YES)
  1362. ) {
  1363. DEBUG_INFO_MSG("Relay Board FW Rev = %s", ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev);
  1364. ShmRelayModuleData->SelfTest_Comp = YES;
  1365. }
  1366. #ifndef NO_FAN_BOARD
  1367. if ((strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 || ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  1368. && (ShmFanModuleData->SelfTest_Comp != YES)
  1369. ) {
  1370. DEBUG_INFO_MSG("Fan Board FW Rev = %s", ShmSysConfigAndInfo->SysInfo.FanModuleFwRev);
  1371. ShmFanModuleData->SelfTest_Comp = YES;
  1372. }
  1373. #else
  1374. ShmFanModuleData->SelfTest_Comp = YES;
  1375. #endif
  1376. if ((strlen((char *)ShmPrimaryMcuData->version) != 0 || ShmPrimaryMcuData->version[0] != '\0')
  1377. && (ShmPrimaryMcuData->SelfTest_Comp != YES)
  1378. ) {
  1379. DEBUG_INFO_MSG("Primary FW Rev = %s", ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev);
  1380. ShmPrimaryMcuData->SelfTest_Comp = YES;
  1381. }
  1382. if ((strlen((char *)ShmSysConfigAndInfo->SysInfo.LedModuleFwRev) != 0 || ShmSysConfigAndInfo->SysInfo.LedModuleFwRev[0] != '\0')
  1383. && (ShmLedModuleData->SelfTest_Comp != YES)
  1384. ) {
  1385. DEBUG_INFO_MSG("LED Board FW Rev = %s", ShmSysConfigAndInfo->SysInfo.LedModuleFwRev);
  1386. ShmLedModuleData->SelfTest_Comp = YES;
  1387. }
  1388. // EV 小板
  1389. if (!evInitFlag) {
  1390. evInitFlag = YES;
  1391. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  1392. if (chargingInfo[index]->Type == _Type_Chademo) {
  1393. if ((strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 || ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1394. && (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp != YES)
  1395. ) {
  1396. DEBUG_INFO_MSG("CHAdeMO[%d] FW Rev = %s", chargingInfo[index]->type_index, ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1397. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1398. } else {
  1399. //PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1400. evInitFlag = NO;
  1401. }
  1402. } else if (chargingInfo[index]->Type == _Type_GB) {
  1403. if ((strlen((char *)ShmGBTData->evse[chargingInfo[index]->type_index].version) != 0 || ShmGBTData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1404. && (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp != YES)
  1405. ) {
  1406. DEBUG_INFO_MSG("GBT[%d] FW Rev = %s", chargingInfo[index]->type_index, ShmGBTData->evse[chargingInfo[index]->type_index].version);
  1407. ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1408. } else {
  1409. //PRINTF_FUNC("GBT fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1410. evInitFlag = NO;
  1411. }
  1412. } else if (chargingInfo[index]->Type == _Type_CCS_2) {
  1413. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1414. if ((strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 || ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  1415. /*&&(ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp != YES)*/
  1416. ) {
  1417. DEBUG_INFO_MSG("CCS[%d] FW Rev = %s", chargingInfo[index]->type_index, ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1418. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1419. } else {
  1420. //PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1421. evInitFlag = NO;
  1422. }
  1423. }
  1424. }
  1425. }
  1426. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++) {
  1427. if (ac_chargingInfo[index]->Type == _Type_AC) {
  1428. if ((strlen((char *)ac_chargingInfo[index]->version) != 0 || ac_chargingInfo[index]->version[0] != '\0')
  1429. && (ac_chargingInfo[index]->SelfTest_Comp != YES)
  1430. ) {
  1431. DEBUG_INFO_MSG("AC connector[%d] FW Rev = %s", index, ac_chargingInfo[index]->version);
  1432. ac_chargingInfo[index]->SelfTest_Comp = YES;
  1433. } else {
  1434. evInitFlag = NO;
  1435. }
  1436. }
  1437. }
  1438. }
  1439. if ( ShmFanModuleData->SelfTest_Comp &&
  1440. ShmRelayModuleData->SelfTest_Comp &&
  1441. ShmPrimaryMcuData->SelfTest_Comp &&
  1442. ShmLedModuleData->SelfTest_Comp &&
  1443. evInitFlag
  1444. ) {
  1445. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  1446. }
  1447. }
  1448. break;
  1449. case _STEST_AC_CONTACTOR: {
  1450. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  1451. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  1452. #if !defined DD360 && !defined DD360Audi
  1453. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES) {
  1454. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1455. DEBUG_INFO_MSG("AC contactor self test OK");
  1456. }
  1457. #else
  1458. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1459. DEBUG_INFO_MSG("Waiting for DO communication");
  1460. #endif //!defined DD360 && !defined DD360Audi
  1461. }
  1462. break;
  1463. case _STEST_PSU_DETECT: {
  1464. /*if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  1465. {
  1466. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1467. }*/
  1468. /*+++ 20200908, vern, check ethernet connection between DD and DO +++*/
  1469. //if connection is establish
  1470. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1471. }
  1472. break;
  1473. case _STEST_PSU_CAP: {
  1474. // 此測試是要確認當前總輸出能力
  1475. // 如果沒有 PSU 模組請 bypass
  1476. /*if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  1477. {
  1478. sleep(1);
  1479. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1480. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1481. }*/
  1482. //check the power limit from DO
  1483. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1484. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1485. DEBUG_INFO_MSG("Successful Self Test");
  1486. }
  1487. break;
  1488. }
  1489. } else {
  1490. break;
  1491. }
  1492. usleep(100000);
  1493. }
  1494. }
  1495. int SpawnTask()
  1496. {
  1497. sleep(2);
  1498. system("/root/Module_EventLogging &");
  1499. system("/root/Module_PrimaryComm &");
  1500. system("/root/Module_EvComm &");
  1501. system("/root/Module_LcmControl &");
  1502. system("/root/Module_InternalComm &");
  1503. /*system("/root/Module_PsuComm &");*//*+++ 20200908, vern, disable it for DD360 ---*/
  1504. system("/root/Module_ProduceUtils &");
  1505. system("/root/DoComm &");/*+++ 20200908, vern, Add DO communication task ---*/
  1506. /*+++ 20200908, vern, disable it for DD360 +++*/
  1507. #if 0
  1508. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  1509. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL) {
  1510. system("/root/OcppBackend &");
  1511. }
  1512. if (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') {
  1513. system("/root/Module_4g &");
  1514. }
  1515. if (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') {
  1516. system("/root/Module_Wifi &");
  1517. }
  1518. if (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D') {
  1519. system("/root/Module_4g &");
  1520. system("/root/Module_Wifi &");
  1521. }
  1522. #endif
  1523. /*--- 20200908, vern, disable it for DD360 ---*/
  1524. return PASS;
  1525. }
  1526. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1527. {
  1528. int result = PASS;
  1529. int fd, wrd;
  1530. unsigned int i, Chk;
  1531. unsigned char *ptr, *BufTmp;
  1532. Chk = 0;
  1533. ptr = (unsigned char *)UsrData;
  1534. if ((BufTmp = malloc(MtdBlockSize)) != NULL) {
  1535. memset(BufTmp, 0, MtdBlockSize);
  1536. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  1537. for (i = 0; i < MtdBlockSize - 4; i++) {
  1538. Chk += *(ptr + i);
  1539. }
  1540. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  1541. fd = open("/dev/mtdblock10", O_RDWR);
  1542. if (fd > 0) {
  1543. wrd = write(fd, BufTmp, MtdBlockSize);
  1544. close(fd);
  1545. if (wrd >= MtdBlockSize) {
  1546. fd = open("/dev/mtdblock11", O_RDWR);
  1547. if (fd > 0) {
  1548. wrd = write(fd, BufTmp, MtdBlockSize);
  1549. close(fd);
  1550. if (wrd < MtdBlockSize) {
  1551. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  1552. result = FAIL;
  1553. }
  1554. } else {
  1555. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  1556. result = FAIL;
  1557. }
  1558. } else {
  1559. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  1560. result = FAIL;
  1561. }
  1562. } else {
  1563. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  1564. result = FAIL;
  1565. }
  1566. } else {
  1567. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  1568. result = FAIL;
  1569. }
  1570. if (BufTmp != NULL) {
  1571. free(BufTmp);
  1572. }
  1573. return result;
  1574. }
  1575. //===============================================
  1576. // Common Detect Chk - Stop Charging ?
  1577. //===============================================
  1578. bool isEvBoardStopChargeFlag(byte gunIndex)
  1579. {
  1580. return chargingInfo[gunIndex]->StopChargeFlag;
  1581. }
  1582. bool isEvBoardNormalStopChargeFlag(byte gunIndex)
  1583. {
  1584. return chargingInfo[gunIndex]->UnKnowStopChargeFlag;
  1585. }
  1586. //===============================================
  1587. // 掃描插槍狀況
  1588. //===============================================
  1589. void ClearDetectPluginFlag()
  1590. {
  1591. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  1592. }
  1593. void DetectPluginStart()
  1594. {
  1595. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  1596. }
  1597. bool isDetectPlugin()
  1598. {
  1599. if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES) {
  1600. return YES;
  1601. }
  1602. return NO;
  1603. }
  1604. //===============================================
  1605. // Common Detect Chk - Chademo
  1606. //===============================================
  1607. bool isEvGunLocked_chademo(byte gunIndex)
  1608. {
  1609. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0) ? NO : YES;
  1610. }
  1611. bool isEvContactorWelding_chademo(byte gunIndex)
  1612. {
  1613. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  1614. }
  1615. bool isEvStopReq_chademo(byte gunIndex)
  1616. {
  1617. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  1618. }
  1619. bool isEvStopCharging_chademo(byte gunIndex)
  1620. {
  1621. if (isEvGunLocked_chademo(gunIndex) == NO) {
  1622. // 無鎖槍 = 停止
  1623. PRINTF_FUNC("gun locked none (%d) \n", gunIndex);
  1624. return YES;
  1625. }
  1626. return NO;
  1627. }
  1628. byte isPrechargeStatus_chademo(byte gunIndex)
  1629. {
  1630. byte result = 0x00;
  1631. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1632. return result;
  1633. }
  1634. //===============================================
  1635. // Common Detect Chk - GB
  1636. //===============================================
  1637. bool isEvGunLocked_gb(byte gunIndex)
  1638. {
  1639. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0) ? NO : YES;
  1640. }
  1641. bool isEvStopCharging_gb(byte gunIndex)
  1642. {
  1643. if (isEvGunLocked_gb(gunIndex) == NO) {
  1644. // 無鎖槍 = 停止
  1645. PRINTF_FUNC("gun locked none. \n");
  1646. return YES;
  1647. }
  1648. return NO;
  1649. }
  1650. byte isPrechargeStatus_gb(byte gunIndex)
  1651. {
  1652. byte result = 0x00;
  1653. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1654. return result;
  1655. }
  1656. //===============================================
  1657. // Common Detect Chk - CCS
  1658. //===============================================
  1659. bool isEvGunLocked_ccs(byte gunIndex)
  1660. {
  1661. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0) ? NO : YES;
  1662. }
  1663. byte isPrechargeStatus_ccs(byte gunIndex)
  1664. {
  1665. byte result = 0x00;
  1666. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1667. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1668. }
  1669. return result;
  1670. }
  1671. bool isEvStopCharging_ccs(byte gunIndex)
  1672. {
  1673. if (isEvGunLocked_ccs(gunIndex) == NO) {
  1674. // 無鎖槍 = 停止
  1675. PRINTF_FUNC("gun locked none. \n");
  1676. return YES;
  1677. }
  1678. return NO;
  1679. }
  1680. //===============================================
  1681. // Callback
  1682. //===============================================
  1683. void DisplayChargingInfo()
  1684. {
  1685. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  1686. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  1687. if (chargingInfo[i]->SystemStatus != S_IDLE &&
  1688. chargingInfo[i]->SystemStatus != S_RESERVATION) {
  1689. ChangeGunSelectByIndex(i);
  1690. return;
  1691. }
  1692. }
  1693. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  1694. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  1695. ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE) {
  1696. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1697. }
  1698. usleep(50000);
  1699. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;//_LCM_NONE; //Jerry add
  1700. }
  1701. void _AutoReturnTimeout()
  1702. {
  1703. PRINTF_FUNC("*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  1704. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG) {
  1705. ClearDetectPluginFlag();
  1706. } else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP) {
  1707. DetectPluginStart();
  1708. }
  1709. usleep(50000);
  1710. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;//_LCM_NONE; //Jerry add
  1711. }
  1712. void _SelfTestTimeout()
  1713. {
  1714. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE) {
  1715. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  1716. setChargerMode(gun_index, MODE_ALARM);
  1717. }
  1718. }
  1719. ShmPsuData->Work_Step = _NO_WORKING;
  1720. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1721. PRINTF_FUNC("Self test timeout. \n");
  1722. }
  1723. void _AuthorizedTimeout()
  1724. {
  1725. if (IsAuthorizingMode()) {
  1726. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  1727. isCardScan = false;
  1728. //gAudiCustInfo->PricesInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected].Balance = 0.0; //Jerry add
  1729. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1730. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1731. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1732. ClearAuthorizedFlag();
  1733. }
  1734. }
  1735. void _DetectPlugInTimeout()
  1736. {
  1737. PRINTF_FUNC("*********** _DetectPlugInTimeout *********** \n");
  1738. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1739. ClearDetectPluginFlag();
  1740. //usleep(50000);
  1741. sleep(1); //Jerry add
  1742. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;//_LCM_NONE; //Jerry add
  1743. }
  1744. void _DetectEvChargingEnableTimeout(byte gunIndex)
  1745. {
  1746. if (chargingInfo[gunIndex]->Type == _Type_Chademo) {
  1747. if (!isEvGunLocked_chademo(gunIndex)) {
  1748. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  1749. }
  1750. } else if (chargingInfo[gunIndex]->Type == _Type_GB) {
  1751. if (!isEvGunLocked_ccs(gunIndex)) {
  1752. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  1753. }
  1754. } else if (chargingInfo[gunIndex]->Type == _Type_CCS_2) {
  1755. if (!isEvGunLocked_ccs(gunIndex)) {
  1756. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  1757. }
  1758. }
  1759. ChargingTerminalProcess(gunIndex);
  1760. _AutoReturnTimeout();
  1761. }
  1762. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  1763. {
  1764. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  1765. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  1766. {
  1767. setChargerMode(gunIndex, MODE_IDLE);
  1768. _AutoReturnTimeout();
  1769. }
  1770. }
  1771. void _PrepareTimeout(byte gunIndex)
  1772. {
  1773. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  1774. setChargerMode(gunIndex, MODE_IDLE);
  1775. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  1776. _AutoReturnTimeout();
  1777. }
  1778. void _CcsPrechargeTimeout(byte gunIndex)
  1779. {
  1780. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  1781. setChargerMode(gunIndex, MODE_IDLE);
  1782. }
  1783. //===============================================
  1784. // 取得卡號與卡號驗證
  1785. //===============================================
  1786. bool canStartCharging()
  1787. {
  1788. char buf2[16] = "";
  1789. memset(buf2, 0, ARRAY_SIZE(buf2));
  1790. for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++) {
  1791. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  1792. }
  1793. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  1794. // 因為無法得知實際的長度,所以只能用搜尋的方式
  1795. if (strcmp(buf2, "Accepted") == EQUAL) {
  1796. return true;
  1797. } else {
  1798. }
  1799. return false;
  1800. }
  1801. void AuthorizingStart()
  1802. {
  1803. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1804. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  1805. }
  1806. void ClearAuthorizedFlag()
  1807. {
  1808. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1809. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  1810. }
  1811. bool isAuthorizedComplete()
  1812. {
  1813. if (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == YES) {
  1814. return false;
  1815. }
  1816. return true;
  1817. }
  1818. bool IsAuthorizingMode()
  1819. {
  1820. if (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO) {
  1821. return false;
  1822. }
  1823. return true;
  1824. }
  1825. //===============================================
  1826. // 紀錄 Alarm Code
  1827. //===============================================
  1828. void RecordAlarmCode(byte gunIndex, char *code)
  1829. {
  1830. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  1831. if (strcmp(code, "012234") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES; }
  1832. if (strcmp(code, "012235") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES; }
  1833. if (strcmp(code, "012236") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES; }
  1834. if (strcmp(code, "012288") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES; }
  1835. if (strcmp(code, "012289") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES; }
  1836. if (strcmp(code, "012290") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES; }
  1837. }
  1838. void RecordWarningCode(byte gunIndex, char *code)
  1839. {
  1840. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, code, 6);
  1841. if (strcmp(code, "012296") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES; }
  1842. if (strcmp(code, "012297") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES; }
  1843. if (strcmp(code, "012298") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES; }
  1844. }
  1845. void ReleaseAlarmCode(byte gunIndex)
  1846. {
  1847. bool isCleanCheck = false;
  1848. char code[7];
  1849. if (chargingInfo[gunIndex]->Type == _Type_Chademo) {
  1850. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL &&
  1851. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES) {
  1852. memcpy(code, "012234", 6);
  1853. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1854. isCleanCheck = true;
  1855. } else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL &&
  1856. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail == YES) {
  1857. memcpy(code, "012289", 6);
  1858. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1859. isCleanCheck = true;
  1860. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES) {
  1861. ReleaseWarningCodeByString(gunIndex, "012296");
  1862. }
  1863. } else if (chargingInfo[gunIndex]->Type == _Type_GB) {
  1864. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL &&
  1865. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES) {
  1866. memcpy(code, "012236", 6);
  1867. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1868. isCleanCheck = true;
  1869. } else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL &&
  1870. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail == YES) {
  1871. memcpy(code, "012290", 6);
  1872. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1873. isCleanCheck = true;
  1874. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES) {
  1875. ReleaseWarningCodeByString(gunIndex, "012298");
  1876. }
  1877. } else if (chargingInfo[gunIndex]->Type == _Type_CCS_2) {
  1878. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL &&
  1879. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES) {
  1880. memcpy(code, "012235", 6);
  1881. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1882. isCleanCheck = true;
  1883. } else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL &&
  1884. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail == YES) {
  1885. memcpy(code, "012288", 6);
  1886. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1887. isCleanCheck = true;
  1888. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES) {
  1889. ReleaseWarningCodeByString(gunIndex, "012297");
  1890. }
  1891. }
  1892. if (isCleanCheck) {
  1893. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  1894. if (index != gunIndex || ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  1895. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, code, 6) != EQUAL) {
  1896. if (strncmp(code, "012234", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO; }
  1897. if (strncmp(code, "012289", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO; }
  1898. if (strncmp(code, "012236", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO; }
  1899. if (strncmp(code, "012290", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO; }
  1900. if (strncmp(code, "012235", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO; }
  1901. if (strncmp(code, "012288", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO; }
  1902. }
  1903. }
  1904. }
  1905. }
  1906. }
  1907. void ReleaseWarningCodeByString(byte gunIndex, char *code)
  1908. {
  1909. bool isCleanCheck = false;
  1910. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1911. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES) {
  1912. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1913. isCleanCheck = true;
  1914. } else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1915. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES) {
  1916. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1917. isCleanCheck = true;
  1918. } else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1919. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES) {
  1920. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1921. isCleanCheck = true;
  1922. }
  1923. if (isCleanCheck) {
  1924. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  1925. if (index != gunIndex || ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  1926. if (strncmp((char *)chargingInfo[index]->ConnectorWarningCode, code, 6) != EQUAL) {
  1927. if (strncmp(code, "012296", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO; }
  1928. if (strncmp(code, "012297", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO; }
  1929. if (strncmp(code, "012298", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO; }
  1930. }
  1931. }
  1932. }
  1933. }
  1934. }
  1935. //===============================================
  1936. // EmergencyStop and Charging Stop
  1937. //===============================================
  1938. void ChargingTerminalProcess(byte gunIndex)
  1939. {
  1940. setChargerMode(gunIndex, MODE_TERMINATING);
  1941. }
  1942. void ChargingAlarmProcess(byte gunIndex)
  1943. {
  1944. setChargerMode(gunIndex, MODE_ALARM);
  1945. }
  1946. void AcChargingTerminalProcess()
  1947. {
  1948. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  1949. }
  1950. void StopChargingProcessByString(byte level)
  1951. {
  1952. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level) {
  1953. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  1954. }
  1955. }
  1956. void ReleaseChargingProcessByString(byte level)
  1957. {
  1958. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level) {
  1959. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  1960. }
  1961. }
  1962. // 一般錯誤停止充電處理函式
  1963. void BoardErrOccurByString(byte index, char *code)
  1964. {
  1965. byte level = 1;
  1966. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  1967. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  1968. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO) {
  1969. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  1970. }
  1971. ChargingTerminalProcess(index);
  1972. }
  1973. StopChargingProcessByString(level);
  1974. }
  1975. // 急停狀況的停止充電處理函式
  1976. void EmcOccureByString(char *code)
  1977. {
  1978. byte level = 2;
  1979. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  1980. // 其錯誤等級為 2
  1981. if (strncmp(code, "012251", 6) == EQUAL ||
  1982. strncmp(code, "012252", 6) == EQUAL ||
  1983. strncmp(code, "012238", 6) == EQUAL ||
  1984. strncmp(code, "042251", 6) == EQUAL ||
  1985. strncmp(code, "042252", 6) == EQUAL ||
  1986. strncmp(code, "012304", 6) == EQUAL ||
  1987. strncmp(code, "042200", 6) == EQUAL ||
  1988. strncmp(code, "042201", 6) == EQUAL ||
  1989. strncmp(code, "042202", 6) == EQUAL ||
  1990. strncmp(code, "042267", 6) == EQUAL) {
  1991. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++) {
  1992. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  1993. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  1994. //ChargingTerminalProcess(gun);
  1995. ChargingAlarmProcess(gun);
  1996. }
  1997. }
  1998. StopChargingProcessByString(level);
  1999. InformOcppErrOccur(4);
  2000. }
  2001. }
  2002. void ReleaseBoardErrOccurByString(byte index, char *code)
  2003. {
  2004. bool isTrigger = false;
  2005. byte level = 1;
  2006. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES) {
  2007. isTrigger = true;
  2008. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2009. }
  2010. if (isTrigger) {
  2011. ReleaseChargingProcessByString(level);
  2012. }
  2013. }
  2014. void ReleaseEmsOccureByString(byte index, char *code)
  2015. {
  2016. bool isTrigger = false;
  2017. byte level = 2;
  2018. if (strncmp(code, "042251", 6) == 0 ) {
  2019. isTrigger = true;
  2020. } else if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES) {
  2021. isTrigger = true;
  2022. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2023. } else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES) {
  2024. isTrigger = true;
  2025. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2026. } else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES) {
  2027. isTrigger = true;
  2028. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2029. } else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  2030. isTrigger = true;
  2031. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2032. }
  2033. if (isTrigger) {
  2034. ReleaseChargingProcessByString(level);
  2035. InformOcppErrOccur(6);
  2036. }
  2037. }
  2038. //===============================================
  2039. // 確認硬體 (按鈕) 狀態
  2040. //===============================================
  2041. bool leftBtnPush = false;
  2042. bool rightBtnPush = false;
  2043. void ChkPrimaryStatus()
  2044. {
  2045. unsigned char Rtn;
  2046. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0) {
  2047. Rtn = 0;
  2048. for (unsigned char i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  2049. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042251", 6) == 0) {
  2050. EmcOccureByString("042251");
  2051. Rtn = 1;
  2052. } else if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042252", 6) == 0) {
  2053. EmcOccureByString("042252");
  2054. Rtn = 1;
  2055. } else if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042200", 6) == 0) {
  2056. EmcOccureByString("042200");
  2057. Rtn = 1;
  2058. } else if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042201", 6) == 0) {
  2059. EmcOccureByString("042201");
  2060. Rtn = 1;
  2061. } else if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042202", 6) == 0) {
  2062. EmcOccureByString("042202");
  2063. Rtn = 1;
  2064. } else if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "012304", 6) == 0) {
  2065. EmcOccureByString("012304");
  2066. Rtn = 1;
  2067. }
  2068. }
  2069. if (Rtn == 0) {
  2070. ReleaseEmsOccureByString(0, "042251");
  2071. ReleaseEmsOccureByString(0, "042252");
  2072. ReleaseEmsOccureByString(0, "042200");
  2073. ReleaseEmsOccureByString(0, "042201");
  2074. ReleaseEmsOccureByString(0, "042202");
  2075. ReleaseEmsOccureByString(0, "012304");
  2076. }
  2077. } else {
  2078. ReleaseEmsOccureByString(0, "042251");
  2079. ReleaseEmsOccureByString(0, "042252");
  2080. ReleaseEmsOccureByString(0, "042200");
  2081. ReleaseEmsOccureByString(0, "042201");
  2082. ReleaseEmsOccureByString(0, "042202");
  2083. ReleaseEmsOccureByString(0, "012304");
  2084. }
  2085. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL) {
  2086. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2087. EmcOccureByString("012251");
  2088. } else {
  2089. ReleaseEmsOccureByString(0, "012251");
  2090. }
  2091. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL) {
  2092. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2093. EmcOccureByString("012238");
  2094. } else {
  2095. ReleaseEmsOccureByString(0, "012238");
  2096. }
  2097. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL) {
  2098. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2099. } else {
  2100. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2101. }
  2102. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL) {
  2103. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2104. EmcOccureByString("012252");
  2105. } else {
  2106. ReleaseEmsOccureByString(0, "012252");
  2107. }
  2108. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS &&
  2109. !leftBtnPush &&
  2110. getCurLcmPage() != _LCM_AUTHORIZING &&
  2111. getCurLcmPage() != _LCM_AUTHORIZ_COMP &&
  2112. getCurLcmPage() != _LCM_AUTHORIZ_FAIL //&&
  2113. //getCurLcmPage() != _LCM_PRE_CHARGE
  2114. //getCurLcmPage() != _LCM_WAIT_FOR_PLUG
  2115. ) {
  2116. if (!leftBtnPush) {
  2117. leftBtnPush = true;
  2118. PRINTF_FUNC("left btn down...............................%x\n",
  2119. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus);
  2120. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE) {
  2121. switch (ac_chargingInfo[0]->SystemStatus) {
  2122. case S_IDLE: {
  2123. if (isDetectPlugin()) {
  2124. _DetectPlugInTimeout();
  2125. StopSystemTimeoutDet();
  2126. }
  2127. }
  2128. break;
  2129. case S_REASSIGN_CHECK:
  2130. case S_REASSIGN:
  2131. case S_PREPARNING:
  2132. case S_PREPARING_FOR_EV:
  2133. case S_PREPARING_FOR_EVSE:
  2134. case S_CCS_PRECHARGE_ST0:
  2135. case S_CCS_PRECHARGE_ST1: {
  2136. // 取消充電
  2137. AcChargingTerminalProcess();
  2138. }
  2139. break;
  2140. case S_CHARGING: {
  2141. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) {
  2142. // 停止充電
  2143. AcChargingTerminalProcess();
  2144. }
  2145. }
  2146. break;
  2147. case S_COMPLETE:
  2148. {}
  2149. break;
  2150. }
  2151. }
  2152. switch (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus) {
  2153. case S_IDLE: {
  2154. if (isDetectPlugin()) {
  2155. //printf("isDetectPlugin\r\n");
  2156. _DetectPlugInTimeout();
  2157. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2158. StopSystemTimeoutDet();
  2159. } else {
  2160. if (getConfirmSelectedGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected) == PASS) {
  2161. //printf("destroy gun = %d\r\n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2162. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2163. } else {
  2164. confirmSelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2165. PRINTF_FUNC("confirm select gun ............................... %d \n",
  2166. ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2167. }
  2168. }
  2169. }
  2170. break;
  2171. case S_REASSIGN_CHECK:
  2172. case S_REASSIGN:
  2173. case S_PREPARNING:
  2174. case S_PREPARING_FOR_EV:
  2175. case S_PREPARING_FOR_EVSE:
  2176. case S_CCS_PRECHARGE_ST0:
  2177. case S_CCS_PRECHARGE_ST1: {
  2178. // 取消充電
  2179. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE) {
  2180. AcChargingTerminalProcess();
  2181. } else {
  2182. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2183. }
  2184. }
  2185. break;
  2186. case S_CHARGING: {
  2187. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) {
  2188. // 停止充電
  2189. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2190. }
  2191. }
  2192. break;
  2193. case S_COMPLETE: {
  2194. // 回 IDLE
  2195. //PRINTF_FUNC("right btn down.................S_COMPLETE \n");
  2196. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_IDLE;
  2197. }
  2198. break;
  2199. }
  2200. }
  2201. } else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE) {
  2202. if (leftBtnPush) {
  2203. leftBtnPush = false;
  2204. //PRINTF_FUNC("left btn up............................... \n");
  2205. }
  2206. }
  2207. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS &&
  2208. !rightBtnPush &&
  2209. getCurLcmPage() != _LCM_IDLE &&
  2210. getCurLcmPage() != _LCM_AUTHORIZING &&
  2211. getCurLcmPage() != _LCM_AUTHORIZ_COMP &&
  2212. getCurLcmPage() != _LCM_AUTHORIZ_FAIL &&
  2213. getCurLcmPage() != _LCM_WAIT_FOR_PLUG
  2214. ) {
  2215. if (!rightBtnPush) {
  2216. rightBtnPush = true;
  2217. //PRINTF_FUNC("right btn down............................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2218. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2219. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO) {
  2220. //printf("CurGunSelected++\r\n");
  2221. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2222. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2223. } else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2224. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE) {
  2225. //printf("DEFAULT_AC_INDEX\r\n");
  2226. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2227. } else if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES) {
  2228. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  2229. if (chargingInfo[_index]->SystemStatus != S_BOOTING &&
  2230. chargingInfo[_index]->SystemStatus != S_IDLE &&
  2231. chargingInfo[_index]->SystemStatus != S_RESERVATION) {
  2232. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  2233. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2234. return;
  2235. }
  2236. }
  2237. //printf("1 CurGunSelected = 0\r\n");
  2238. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2239. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2240. } else {
  2241. //printf("2 CurGunSelected = 0\r\n");
  2242. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2243. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2244. }
  2245. }
  2246. PRINTF_FUNC("current select gun ............................... %d \n",
  2247. ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2248. } else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE) {
  2249. if (rightBtnPush) {
  2250. rightBtnPush = false;
  2251. //PRINTF_FUNC("right btn up............................... \n");
  2252. }
  2253. }
  2254. }
  2255. //===============================================
  2256. // 確認各小板偵測的錯誤狀況
  2257. //===============================================
  2258. void CheckErrorOccurStatus(byte index)
  2259. {
  2260. // 小板
  2261. if (chargingInfo[index]->Type == _Type_Chademo) {
  2262. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES) {
  2263. BoardErrOccurByString(index, "011012");
  2264. }
  2265. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  2266. // BoardErrOccurByString(index, "012234");
  2267. } else if (chargingInfo[index]->Type == _Type_GB) {
  2268. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault == YES) {
  2269. BoardErrOccurByString(index, "011016");
  2270. }
  2271. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  2272. // BoardErrOccurByString(index, "012236");
  2273. } else if (chargingInfo[index]->Type == _Type_CCS_2) {
  2274. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES) {
  2275. BoardErrOccurByString(index, "011014");
  2276. }
  2277. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  2278. // BoardErrOccurByString(index, "012235");
  2279. }
  2280. // RB
  2281. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES) {
  2282. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2283. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2284. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES) {
  2285. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  2286. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2287. StopChargingProcessByString(2);
  2288. InformOcppErrOccur(13);
  2289. }
  2290. } else {
  2291. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  2292. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2293. ReleaseChargingProcessByString(2);
  2294. InformOcppErrOccur(6);
  2295. }
  2296. }
  2297. } else {
  2298. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  2299. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2300. ReleaseChargingProcessByString(2);
  2301. InformOcppErrOccur(6);
  2302. }
  2303. }
  2304. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2305. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2306. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES) {
  2307. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  2308. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2309. StopChargingProcessByString(2);
  2310. InformOcppErrOccur(14);
  2311. }
  2312. } else {
  2313. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP) {
  2314. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2315. ReleaseChargingProcessByString(2);
  2316. InformOcppErrOccur(6);
  2317. }
  2318. }
  2319. }
  2320. //===============================================
  2321. // 確認 GPIO 狀態
  2322. //===============================================
  2323. void gpio_set_value(unsigned int gpio, unsigned int value)
  2324. {
  2325. int fd;
  2326. char buf[MAX_BUF];
  2327. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2328. fd = open(buf, O_WRONLY);
  2329. if (fd < 0) {
  2330. perror("gpio/set-value");
  2331. return;
  2332. }
  2333. if (value) {
  2334. write(fd, "1", 2);
  2335. } else {
  2336. write(fd, "0", 2);
  2337. }
  2338. close(fd);
  2339. }
  2340. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2341. {
  2342. int fd;
  2343. char buf[MAX_BUF];
  2344. char ch;
  2345. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2346. fd = open(buf, O_RDONLY);
  2347. if (fd < 0) {
  2348. perror("gpio/get-value");
  2349. return fd;
  2350. }
  2351. read(fd, &ch, 1);
  2352. if (ch != '0') {
  2353. *value = 1;
  2354. } else {
  2355. *value = 0;
  2356. }
  2357. close(fd);
  2358. return 0;
  2359. }
  2360. void CheckGunTypeFromHw()
  2361. {
  2362. int pinIn[4] = { 22, 23, 44, 45 };
  2363. unsigned int gpioValue = 0;
  2364. unsigned char tmp[2];
  2365. DEBUG_INFO_MSG("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  2366. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  2367. gpio_get_value(pinIn[i], &gpioValue);
  2368. switch (pinIn[i]) {
  2369. case 22:
  2370. bd1_1_status = gpioValue;
  2371. break;
  2372. case 23:
  2373. bd1_2_status = gpioValue;
  2374. break;
  2375. case 44:
  2376. bd0_1_status = gpioValue;
  2377. break;
  2378. case 45:
  2379. bd0_2_status = gpioValue;
  2380. break;
  2381. }
  2382. }
  2383. //BD1(Left-CCS-CND1-SMR2-左槍), BD2(Right-CHADEMO-CND2-SMR1-右槍), CCS: 10 , CHAdeMO: 01 , GBT: 11
  2384. //CcsChargingData [0至1] 分別為 Right至Left
  2385. //model name 槍順序左至右分別為Right至Left
  2386. memset(tmp, 0, sizeof(tmp));
  2387. tmp[1] = (bd0_1_status << 4 | bd0_2_status);
  2388. tmp[0] = (bd1_1_status << 4 | bd1_2_status);
  2389. for (int i = 0; i < 2; i++) {
  2390. switch (tmp[i]) {
  2391. case 0x01:
  2392. DEBUG_INFO_MSG("BD%d(%s) = %s ", i + 1, i == 1 ? "Right" : "Left", "CHAdeMO");
  2393. break;
  2394. case 0x10:
  2395. DEBUG_INFO_MSG("BD%d(%s) = %s ", i + 1, i == 1 ? "Right" : "Left", "CCS");
  2396. break;
  2397. case 0x11:
  2398. DEBUG_INFO_MSG("BD%d(%s) = %s ", i + 1, i == 1 ? "Right" : "Left", "GBT");
  2399. break;
  2400. case 0x00:
  2401. DEBUG_INFO_MSG("BD%d(%s) = %s ", i + 1, i == 1 ? "Right" : "Left", "None");
  2402. break;
  2403. }
  2404. }
  2405. }
  2406. void CheckGpioInStatus()
  2407. {
  2408. int pinIn[2] = { 27, 47 };//{IO BD1_2, IO BD2_2}
  2409. unsigned int gpioValue = 0;
  2410. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  2411. gpio_get_value(pinIn[i], &gpioValue);
  2412. if (gpioValue == 0x01) {
  2413. switch (pinIn[i]) {
  2414. // 小板緊急停止
  2415. case 47: {
  2416. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  2417. if (chargingInfo[i]->slotsIndex == 1) {
  2418. if (chargingInfo[i]->Type == _Type_Chademo) {
  2419. BoardErrOccurByString(i, "023730");
  2420. } else if (chargingInfo[i]->Type == _Type_CCS_2) {
  2421. BoardErrOccurByString(i, "013627");
  2422. }
  2423. break;
  2424. }
  2425. }
  2426. }
  2427. break;
  2428. case 27: {
  2429. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  2430. if (chargingInfo[i]->slotsIndex == 3) {
  2431. if (chargingInfo[i]->Type == _Type_Chademo) {
  2432. BoardErrOccurByString(i, "023730");
  2433. } else if (chargingInfo[i]->Type == _Type_CCS_2) {
  2434. BoardErrOccurByString(i, "013627");
  2435. }
  2436. break;
  2437. }
  2438. }
  2439. }
  2440. break;
  2441. }
  2442. } else {
  2443. switch (pinIn[i]) {
  2444. // 小板解除緊急停止
  2445. case 47: {
  2446. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  2447. if (chargingInfo[i]->slotsIndex == 1) {
  2448. if (chargingInfo[i]->Type == _Type_Chademo) {
  2449. ReleaseBoardErrOccurByString(i, "023730");
  2450. } else if (chargingInfo[i]->Type == _Type_CCS_2) {
  2451. ReleaseBoardErrOccurByString(i, "013627");
  2452. }
  2453. break;
  2454. }
  2455. }
  2456. }
  2457. break;
  2458. case 27: {
  2459. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  2460. if (chargingInfo[i]->slotsIndex == 3) {
  2461. if (chargingInfo[i]->Type == _Type_Chademo) {
  2462. ReleaseBoardErrOccurByString(i, "023730");
  2463. } else if (chargingInfo[i]->Type == _Type_CCS_2) {
  2464. ReleaseBoardErrOccurByString(i, "013627");
  2465. }
  2466. break;
  2467. }
  2468. }
  2469. }
  2470. break;
  2471. }
  2472. }
  2473. }
  2474. }
  2475. //===============================================
  2476. // Main process
  2477. //===============================================
  2478. // 檢查 Byte 中某個 Bit 的值
  2479. // _byte : 欲改變的 byte
  2480. // _bit : 該 byte 的第幾個 bit
  2481. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  2482. {
  2483. return ( _byte & mask_table[_bit] ) != 0x00;
  2484. }
  2485. // 設定 Byte 中某個 Bit的值
  2486. // _byte : 欲改變的 byte
  2487. // _bit : 該 byte 的第幾個 bit
  2488. // value : 修改的值為 0 or 1
  2489. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  2490. {
  2491. if (value == 1) {
  2492. *_byte |= (1 << _bit);
  2493. } else if (value == 0) {
  2494. *_byte ^= (1 << _bit);
  2495. }
  2496. }
  2497. void UserScanFunction()
  2498. {
  2499. bool idleReq = false;
  2500. unsigned char stopReq = 255;
  2501. // 當前非驗證的狀態
  2502. if (!IsAuthorizingMode()) {
  2503. //當前沒有選槍
  2504. if (getConfirmSelectedGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected) == FAIL) { //Jerry add
  2505. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2506. return;
  2507. }
  2508. // 先判斷現在是否可以提供刷卡
  2509. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  2510. // 2. 停止充電
  2511. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) {
  2512. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2513. return;
  2514. }
  2515. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  2516. if (chargingInfo[i]->SystemStatus == S_CHARGING) {
  2517. stopReq = i;
  2518. }
  2519. if ((chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable) == YES ||
  2520. (_acgunIndex > 0 && ac_chargingInfo[0]->SystemStatus == S_IDLE && ac_chargingInfo[0]->IsAvailable)) {
  2521. idleReq = true;
  2522. }
  2523. }
  2524. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2525. ac_chargingInfo[0]->SystemStatus == S_CHARGING) {
  2526. stopReq = DEFAULT_AC_INDEX;
  2527. }
  2528. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0) {
  2529. if (_acgunIndex > 0 && stopReq == DEFAULT_AC_INDEX && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  2530. char value[32];
  2531. PRINTF_FUNC("ac stop charging \n");
  2532. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  2533. ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2534. memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId,
  2535. ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  2536. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL) {
  2537. AcChargingTerminalProcess();
  2538. }
  2539. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2540. } else if (stopReq < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2541. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING &&
  2542. (_acgunIndex <= 0 || (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE))) {
  2543. char value[32];
  2544. PRINTF_FUNC("stop charging \n");
  2545. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  2546. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2547. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  2548. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  2549. // 同一張卡直接停掉
  2550. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL) {
  2551. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2552. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2553. } else {
  2554. // 進驗證
  2555. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  2556. _authorizeIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc;
  2557. } else {
  2558. _authorizeIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  2559. }
  2560. #if !defined DD360 && !defined DD360Audi
  2561. StartSystemTimeoutDet(Timeout_AuthorizingForStop);
  2562. AuthorizingStart();
  2563. #else
  2564. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2565. #endif //!defined DD360 && !defined DD360Audi
  2566. }
  2567. } else if (idleReq) {
  2568. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  2569. stopReq != 255 &&
  2570. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES) {
  2571. idleReq = false;
  2572. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2573. } else if ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  2574. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_IDLE) {
  2575. PRINTF_FUNC("// LCM => Authorizing \n");
  2576. setSelGunWaitToAuthor(ShmSysConfigAndInfo->SysInfo.CurGunSelected); //Jerry add
  2577. // LCM => Authorizing
  2578. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  2579. // 進入確認卡號狀態
  2580. AuthorizingStart();
  2581. } else {
  2582. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2583. }
  2584. } else {
  2585. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2586. }
  2587. }
  2588. } else {
  2589. // 透過後臺停止充電的判斷
  2590. if (isAuthorizedComplete() /*||
  2591. (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  2592. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)*/) {
  2593. // 判斷後台回覆狀態
  2594. if (canStartCharging() /*||
  2595. (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  2596. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)*/) {
  2597. if (_authorizeIndex != NO_DEFINE) {
  2598. // 先找 AC
  2599. if (_authorizeIndex == DEFAULT_AC_INDEX) {
  2600. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  2601. strcmp((char *)chargingInfo[_authorizeIndex]->StartUserId, "") != EQUAL) {
  2602. AcChargingTerminalProcess();
  2603. }
  2604. } else {
  2605. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  2606. strcmp((char *)chargingInfo[_authorizeIndex]->StartUserId, "") != EQUAL) {
  2607. ChargingTerminalProcess(_authorizeIndex);
  2608. }
  2609. }
  2610. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2611. _authorizeIndex = NO_DEFINE;
  2612. }
  2613. } else {
  2614. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2615. }
  2616. ClearAuthorizedFlag();
  2617. } else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST) {
  2618. // 白名單驗證
  2619. for (int i = 0; i < 10; i++) {
  2620. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL) {
  2621. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL) {
  2622. ChargingTerminalProcess(_authorizeIndex);
  2623. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2624. ClearAuthorizedFlag();
  2625. break;
  2626. }
  2627. }
  2628. }
  2629. }
  2630. }
  2631. }
  2632. unsigned char isModeChange(unsigned char gun_index)
  2633. {
  2634. unsigned char result = NO;
  2635. if (chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus) {
  2636. result = YES;
  2637. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  2638. }
  2639. return result;
  2640. }
  2641. void ScannerCardProcess()
  2642. {
  2643. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != 2 &&
  2644. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) {
  2645. isCardScan = true;
  2646. // 處理刷卡及驗證卡號的動作
  2647. UserScanFunction();
  2648. }
  2649. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING) {
  2650. StartSystemTimeoutDet(Timeout_Authorizing);
  2651. // 確認驗證卡號完成沒
  2652. if (isAuthorizedComplete() /*|| ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING*/) {
  2653. StopSystemTimeoutDet();
  2654. // 判斷後台回覆狀態
  2655. if (canStartCharging() /*|| ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING*/) {
  2656. // LCM => Authorize complete
  2657. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2658. } else {
  2659. // LCM => Authorize fail
  2660. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2661. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2662. }
  2663. ClearAuthorizedFlag();
  2664. } else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST) {
  2665. // 白名單驗證
  2666. for (int i = 0; i < 10; i++) {
  2667. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL) {
  2668. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL) {
  2669. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2670. ClearAuthorizedFlag();
  2671. break;
  2672. }
  2673. }
  2674. }
  2675. }
  2676. } else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL) {
  2677. StartSystemTimeoutDet(Timeout_VerifyFail);
  2678. isCardScan = false;
  2679. } else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP) {
  2680. StartSystemTimeoutDet(Timeout_VerifyComp);
  2681. } else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG) {
  2682. StartSystemTimeoutDet(Timeout_WaitPlug);
  2683. } else {
  2684. isCardScan = false;
  2685. }
  2686. }
  2687. bool AddGunInfoByConnector(byte typeValue, byte slots)
  2688. {
  2689. bool result = true;
  2690. switch (typeValue) {
  2691. case '0': // none
  2692. break;
  2693. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  2694. break;
  2695. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  2696. break;
  2697. case '3': // IEC 62196-2 Type 2 Plug
  2698. case '4': // IEC 62196-2 Type 2 Socket
  2699. if (AC_QUANTITY > _ac_Index) {
  2700. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  2701. // AC 固定 index
  2702. ac_chargingInfo[_acgunIndex]->Index = 0;
  2703. ac_chargingInfo[_acgunIndex]->ReservationId = -1;
  2704. ac_chargingInfo[_acgunIndex]->SystemStatus = S_IDLE;
  2705. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  2706. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  2707. _ac_Index++;
  2708. _acgunIndex++;
  2709. } else {
  2710. result = false;
  2711. }
  2712. break;
  2713. case '5': // GB/T AC Plug
  2714. break;
  2715. case '6': // GB/T AC Socket
  2716. break;
  2717. case 'J': { // CHAdeMO
  2718. if (CHAdeMO_QUANTITY > _chademoIndex) {
  2719. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  2720. chargingInfo[_gunIndex]->Index = _gunIndex;
  2721. chargingInfo[_gunIndex]->ReservationId = -1;
  2722. chargingInfo[_gunIndex]->slotsIndex = slots;
  2723. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2724. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  2725. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  2726. chargingInfo[_gunIndex]->IsAvailable = YES;
  2727. _chademoIndex++;
  2728. _gunIndex++;
  2729. } else {
  2730. result = false;
  2731. }
  2732. }
  2733. break;
  2734. case 'U': // CCS1 combo
  2735. case 'E': // CCS2 combo
  2736. case 'V': // Liquid CCS1 combo
  2737. case 'F': { // Liquid CCS2 combo
  2738. if (CCS_QUANTITY > _ccsIndex) {
  2739. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  2740. chargingInfo[_gunIndex]->Index = _gunIndex;
  2741. chargingInfo[_gunIndex]->ReservationId = -1;
  2742. chargingInfo[_gunIndex]->slotsIndex = slots;
  2743. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2744. chargingInfo[_gunIndex]->Type = _Type_CCS_2;
  2745. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  2746. chargingInfo[_gunIndex]->IsAvailable = YES;
  2747. // 現階段預設為走 DIN70121
  2748. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  2749. _ccsIndex++;
  2750. _gunIndex++;
  2751. } else {
  2752. result = false;
  2753. }
  2754. }
  2755. break;
  2756. case 'G': { // GBT DC
  2757. if (GB_QUANTITY > _gb_Index) {
  2758. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  2759. chargingInfo[_gunIndex]->Index = _gunIndex;
  2760. chargingInfo[_gunIndex]->ReservationId = -1;
  2761. chargingInfo[_gunIndex]->slotsIndex = slots;
  2762. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2763. chargingInfo[_gunIndex]->Type = _Type_GB;
  2764. chargingInfo[_gunIndex]->type_index = _gb_Index;
  2765. chargingInfo[_gunIndex]->IsAvailable = YES;
  2766. _gb_Index++;
  2767. _gunIndex++;
  2768. } else {
  2769. result = false;
  2770. }
  2771. }
  2772. break;
  2773. case 'D': // GBT DC x 2
  2774. break;
  2775. }
  2776. return result;
  2777. }
  2778. bool CheckConnectorTypeStatus()
  2779. {
  2780. bool result = true;
  2781. /*
  2782. PRINTF_FUNC("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  2783. bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
  2784. */
  2785. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9) {
  2786. printf("1 CheckConnectorTypeStatus\r\n");
  2787. byte slots = 1;
  2788. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++) {
  2789. if (!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots)) {
  2790. return false;
  2791. }
  2792. slots++;
  2793. }
  2794. // AC index 接在 DC 後面
  2795. if (AC_QUANTITY > 0) {
  2796. ac_chargingInfo[0]->Index += _gunIndex;
  2797. }
  2798. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  2799. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  2800. DEBUG_INFO_MSG("DC connector Quality = %d, AC connector Quality = %d",
  2801. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  2802. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  2803. DEBUG_INFO_MSG("Type 0~3 = CHAdeMO, CCS, GB, AC");
  2804. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  2805. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0) {
  2806. result = false;
  2807. }
  2808. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  2809. chargingInfo[0]->Evboard_id = 0x01;
  2810. DEBUG_INFO_MSG("index = %d, Type = %d, Evboard_id = %d", 0, chargingInfo[0]->Type, chargingInfo[0]->Evboard_id);
  2811. } else {
  2812. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  2813. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++) {
  2814. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1) {
  2815. // 與硬體相同 type : Chademo
  2816. if (chargingInfo[gunIndex]->Type == _Type_Chademo) {
  2817. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2818. }
  2819. } else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0) {
  2820. // 與硬體相同 type : CCS
  2821. if (chargingInfo[gunIndex]->Type == _Type_CCS_2) {
  2822. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2823. }
  2824. } else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1) {
  2825. // 與硬體相同 type : GB
  2826. if (chargingInfo[gunIndex]->Type == _Type_GB) {
  2827. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2828. }
  2829. }
  2830. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1) {
  2831. // 與硬體相同 type : Chademo
  2832. if (chargingInfo[gunIndex]->Type == _Type_Chademo) {
  2833. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2834. }
  2835. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  2836. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2837. }
  2838. } else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0) {
  2839. // 與硬體相同 type : CCS
  2840. if (chargingInfo[gunIndex]->Type == _Type_CCS_2) {
  2841. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2842. }
  2843. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  2844. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2845. }
  2846. } else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1) {
  2847. // 與硬體相同 type : GB
  2848. if (chargingInfo[gunIndex]->Type == _Type_GB) {
  2849. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2850. }
  2851. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  2852. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2853. }
  2854. }
  2855. DEBUG_INFO_MSG("index = %d, Type = %d, Evboard_id = %d", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
  2856. if (chargingInfo[gunIndex]->Evboard_id == 0x00) {
  2857. result = false;
  2858. }
  2859. }
  2860. }
  2861. } else {
  2862. // Module Name 不正確 - 告警
  2863. result = false;
  2864. }
  2865. return result;
  2866. }
  2867. void KillTask()
  2868. {
  2869. ChangeLcmByIndex(_LCM_FIX);
  2870. system("killall Module_EventLogging");
  2871. system("killall Module_PrimaryComm");
  2872. system("killall Module_EvComm");
  2873. system("killall Module_LcmControl");
  2874. system("killall Module_InternalComm");
  2875. //system("killall DoComm");
  2876. //system("killall Module_PsuComm");
  2877. //system("killall OcppBackend &");
  2878. //system("killall Module_4g &");
  2879. //system("killall Module_Wifi &");
  2880. }
  2881. void KillAllTask()
  2882. {
  2883. ChangeLcmByIndex(_LCM_FIX);
  2884. system("killall Module_EventLogging");
  2885. system("killall Module_PrimaryComm");
  2886. system("killall Module_EvComm");
  2887. system("killall Module_LcmControl");
  2888. system("killall Module_InternalComm");
  2889. system("killall DoComm");
  2890. //system("killall Module_PsuComm");
  2891. //system("killall OcppBackend &");
  2892. //system("killall Module_4g &");
  2893. //system("killall Module_Wifi &");
  2894. }
  2895. int CheckUpdateProcess(void)
  2896. {
  2897. //bool isPass = true;
  2898. uint8_t retSucc = 0;
  2899. uint8_t retFail = 0;
  2900. byte index = 0;
  2901. byte target = 0;
  2902. char *new_str = NULL;
  2903. unsigned char *ptr = NULL;
  2904. int fd = 0;
  2905. int CanFd = 0;
  2906. int uartFd = 0;
  2907. unsigned int Type = 0;
  2908. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  2909. DIR *d;
  2910. struct dirent *dir;
  2911. d = opendir("/mnt/");
  2912. if (d) {
  2913. while ((dir = readdir(d)) != NULL) {
  2914. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  2915. continue;
  2916. }
  2917. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  2918. //new_str[0] = '\0';
  2919. strcat(new_str, "/mnt/");
  2920. strcat(new_str, dir->d_name);
  2921. DEBUG_INFO_MSG("%s%s\r\n", "/mnt/", dir->d_name);
  2922. fd = open(new_str, O_RDONLY);
  2923. if (fd < 0) {
  2924. return FAIL;
  2925. }
  2926. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  2927. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2928. //get the image length
  2929. ImageLen = read(fd, ptr, MaxLen);
  2930. for (byte i = 0; i < 16; i++) {
  2931. if (ShmSysConfigAndInfo->SysConfig.ModelName[i] != ptr[i]) {
  2932. return FAIL;
  2933. }
  2934. }
  2935. PRINTF_FUNC("model name check pass. \n");
  2936. if (ImageLen > 20) {
  2937. Type = (((unsigned int)ptr[16]) << 24 |
  2938. ((unsigned int)ptr[17]) << 16 |
  2939. ((unsigned int)ptr[18]) << 8 |
  2940. ((unsigned int)ptr[19]));
  2941. PRINTF_FUNC("Typed...%x \r\n", Type);
  2942. switch (Type) {
  2943. case 0x10000001:
  2944. case 0x10000002:
  2945. case 0x10000003:
  2946. case 0x10000004:
  2947. case 0x10000005:
  2948. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS) {
  2949. //return PASS;
  2950. retSucc++;
  2951. } else {
  2952. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  2953. //return FAIL;
  2954. retFail++;
  2955. }
  2956. break;
  2957. case 0x10000007:
  2958. case 0x10000008:
  2959. case 0x10000009:
  2960. case 0x1000000A:
  2961. //isPass = true;
  2962. CanFd = InitCanBus();
  2963. if (CanFd > 0) {
  2964. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  2965. //if (!isPass) {
  2966. // break;
  2967. //}
  2968. if (chargingInfo[index]->Type == _Type_CCS_2) {
  2969. if (Upgrade_CCS(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL) {
  2970. //isPass = false;
  2971. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  2972. retFail++;
  2973. }
  2974. }
  2975. }
  2976. } else {
  2977. DEBUG_ERROR_MSG("Upgrade CCS open CAN FD fail.\n");
  2978. //isPass = false;
  2979. return FAIL;
  2980. }
  2981. if (retFail != 0) {
  2982. break;
  2983. } else {
  2984. retSucc++;
  2985. }
  2986. //return isPass;
  2987. break;
  2988. case 0x10000006:
  2989. case 0x1000000D:
  2990. case 0x1000000E:
  2991. case 0x20000002:
  2992. case 0x10000014:
  2993. // CSU_PRIMARY_CONTROLLER : 0x10000006
  2994. target = 0x00;
  2995. if (Type == 0x10000006) {
  2996. target = UPGRADE_PRI;
  2997. } else if (Type == 0x1000000D) {
  2998. target = UPGRADE_RB;
  2999. } else if (Type == 0x1000000E) {
  3000. target = UPGRADE_FAN;
  3001. } else if (Type == 0x20000002) {
  3002. target = UPGRADE_AC;
  3003. } else if (Type == 0x10000014) {
  3004. target = UPGRADE_LED;
  3005. }
  3006. uartFd = InitComPort(target);
  3007. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS) {
  3008. //return PASS;
  3009. retSucc++;
  3010. } else {
  3011. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  3012. //return FAIL;
  3013. return FAIL;
  3014. }
  3015. if (uartFd > 0) {
  3016. close(uartFd);
  3017. }
  3018. break;
  3019. case 0x1000000B:
  3020. case 0x1000000C:
  3021. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  3022. //bool isPass = true;
  3023. CanFd = InitCanBus();
  3024. if (CanFd > 0) {
  3025. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  3026. //if (!isPass) {
  3027. // break;
  3028. //}
  3029. if ((Type == 0x1000000B && chargingInfo[index]->Type == _Type_Chademo) ||
  3030. (Type == 0x1000000C && chargingInfo[index]->Type == _Type_GB)) {
  3031. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS) {
  3032. //isPass = PASS;
  3033. retSucc++;
  3034. } else {
  3035. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  3036. //isPass = FAIL;
  3037. retFail++;
  3038. }
  3039. }
  3040. }
  3041. } else {
  3042. PRINTF_FUNC("Upgrad FD fail. \n");
  3043. //isPass = false;
  3044. return FAIL;
  3045. }
  3046. //return isPass;
  3047. break;
  3048. }
  3049. }
  3050. free(new_str);
  3051. free(ptr);
  3052. }
  3053. }
  3054. free(dir);
  3055. closedir(d);
  3056. if (retFail != 0) {
  3057. return FAIL;
  3058. }
  3059. return PASS;
  3060. }
  3061. void CreateRfidFork()
  3062. {
  3063. pid_t rfidRecPid;
  3064. rfidRecPid = fork();
  3065. DEBUG_INFO_MSG("CreateRfidFork = %d\r\n", rfidRecPid);
  3066. if (rfidRecPid == 0) {
  3067. while (true) {
  3068. // 刷卡判斷
  3069. RFID rfid;
  3070. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  3071. !ShmSysConfigAndInfo->SysConfig.isRFID)
  3072. {}
  3073. else if (getRequestCardSN(rfidFd, 0, &rfid)) {
  3074. //DEBUG_INFO_MSG("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3075. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0) {
  3076. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE) {
  3077. switch (rfid.snType) {
  3078. case RFID_SN_TYPE_6BYTE:
  3079. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3080. "%02X%02X%02X%02X%02X%02X",
  3081. rfid.currentCard[0], rfid.currentCard[1],
  3082. rfid.currentCard[2], rfid.currentCard[3],
  3083. rfid.currentCard[4], rfid.currentCard[5]);
  3084. break;
  3085. case RFID_SN_TYPE_7BYTE:
  3086. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3087. "%02X%02X%02X%02X%02X%02X%02X",
  3088. rfid.currentCard[0], rfid.currentCard[1],
  3089. rfid.currentCard[2], rfid.currentCard[3],
  3090. rfid.currentCard[4], rfid.currentCard[5],
  3091. rfid.currentCard[6]);
  3092. break;
  3093. case RFID_SN_TYPE_10BYTE:
  3094. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3095. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3096. rfid.currentCard[0], rfid.currentCard[1],
  3097. rfid.currentCard[2], rfid.currentCard[3],
  3098. rfid.currentCard[4], rfid.currentCard[5],
  3099. rfid.currentCard[6], rfid.currentCard[7],
  3100. rfid.currentCard[8], rfid.currentCard[9]);
  3101. break;
  3102. case RFID_SN_TYPE_4BYTE:
  3103. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3104. "%02X%02X%02X%02X",
  3105. rfid.currentCard[0], rfid.currentCard[1],
  3106. rfid.currentCard[2], rfid.currentCard[3]);
  3107. break;
  3108. }
  3109. } else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG) {
  3110. switch (rfid.snType) {
  3111. case RFID_SN_TYPE_6BYTE:
  3112. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3113. "%02X%02X%02X%02X%02X%02X",
  3114. rfid.currentCard[5], rfid.currentCard[4],
  3115. rfid.currentCard[3], rfid.currentCard[2],
  3116. rfid.currentCard[1], rfid.currentCard[0]);
  3117. break;
  3118. case RFID_SN_TYPE_7BYTE:
  3119. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3120. "%02X%02X%02X%02X%02X%02X%02X",
  3121. rfid.currentCard[6], rfid.currentCard[5],
  3122. rfid.currentCard[4], rfid.currentCard[3],
  3123. rfid.currentCard[2], rfid.currentCard[1],
  3124. rfid.currentCard[0]);
  3125. break;
  3126. case RFID_SN_TYPE_10BYTE:
  3127. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3128. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3129. rfid.currentCard[9], rfid.currentCard[8],
  3130. rfid.currentCard[7], rfid.currentCard[6],
  3131. rfid.currentCard[5], rfid.currentCard[4],
  3132. rfid.currentCard[3], rfid.currentCard[2],
  3133. rfid.currentCard[1], rfid.currentCard[0]);
  3134. break;
  3135. case RFID_SN_TYPE_4BYTE:
  3136. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3137. "%02X%02X%02X%02X",
  3138. rfid.currentCard[3], rfid.currentCard[2],
  3139. rfid.currentCard[1], rfid.currentCard[0]);
  3140. break;
  3141. }
  3142. }
  3143. DEBUG_INFO_MSG("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3144. }
  3145. }
  3146. usleep(500000);
  3147. }
  3148. }
  3149. }
  3150. void StartSystemTimeoutDet(unsigned char flag)
  3151. {
  3152. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag) {
  3153. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3154. }
  3155. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  3156. }
  3157. void StopSystemTimeoutDet()
  3158. {
  3159. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3160. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  3161. }
  3162. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  3163. {
  3164. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount) {
  3165. if (chargingInfo[gunIndex]->TimeoutFlag != flag) {
  3166. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  3167. }
  3168. chargingInfo[gunIndex]->TimeoutFlag = flag;
  3169. }
  3170. }
  3171. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  3172. {
  3173. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount) {
  3174. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  3175. }
  3176. }
  3177. void CheckConnectionTimeout()
  3178. {
  3179. if (gAudiCustInfo->RemoteSetup.ConnectionTimeout != 0) { //Jerry add
  3180. _connectionTimeout = gAudiCustInfo->RemoteSetup.ConnectionTimeout;
  3181. } else {
  3182. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3183. }
  3184. #if 0
  3185. if (system("pidof -s OcppBackend > /dev/null") != 0) {
  3186. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3187. } else {
  3188. if (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0) {
  3189. _connectionTimeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3190. if (_connectionTimeout <= 0) {
  3191. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3192. }
  3193. } else {
  3194. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3195. }
  3196. }
  3197. #endif //0
  3198. }
  3199. void CreateTimeoutFork()
  3200. {
  3201. pid_t timeoutPid;
  3202. timeoutPid = fork();
  3203. DEBUG_INFO_MSG("CreateTimeoutFork = %d\r\n", timeoutPid);
  3204. if (timeoutPid == 0) {
  3205. gettimeofday(&_cmdSubPriority_time, NULL);
  3206. CheckConnectionTimeout();
  3207. while (1) {
  3208. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000) {
  3209. CheckConnectionTimeout();
  3210. gettimeofday(&_cmdSubPriority_time, NULL);
  3211. }
  3212. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  3213. // 系統
  3214. switch (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag) {
  3215. case Timeout_SelftestChk:
  3216. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT) {
  3217. _SelfTestTimeout();
  3218. StopSystemTimeoutDet();
  3219. destroySelGun(DESTROY_ALL_SEL); //jerry add
  3220. }
  3221. break;
  3222. case Timeout_Authorizing:
  3223. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT) {
  3224. _AuthorizedTimeout();
  3225. StopSystemTimeoutDet();
  3226. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3227. }
  3228. break;
  3229. case Timeout_VerifyFail:
  3230. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT) {
  3231. _AutoReturnTimeout();
  3232. StopSystemTimeoutDet();
  3233. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3234. }
  3235. break;
  3236. case Timeout_VerifyComp:
  3237. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_COMP_TIMEOUT) {
  3238. _AutoReturnTimeout();
  3239. StopSystemTimeoutDet();
  3240. }
  3241. break;
  3242. case Timeout_WaitPlug:
  3243. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout) {
  3244. _DetectPlugInTimeout();
  3245. StopSystemTimeoutDet();
  3246. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3247. }
  3248. break;
  3249. case Timeout_ReturnToChargingGunDet: {
  3250. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE) {
  3251. if (getCurLcmPage() != _LCM_PRE_CHARGE &&
  3252. getCurLcmPage() != _LCM_CHARGING &&
  3253. getCurLcmPage() != _LCM_COMPLETE) {
  3254. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected); //jerry add
  3255. }
  3256. DisplayChargingInfo();
  3257. StopSystemTimeoutDet();
  3258. }
  3259. }
  3260. break;
  3261. case Timeout_AuthorizingForStop: {
  3262. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT) {
  3263. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3264. ClearAuthorizedFlag();
  3265. StopSystemTimeoutDet();
  3266. }
  3267. }
  3268. break;
  3269. }
  3270. // 各槍
  3271. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  3272. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  3273. switch (chargingInfo[gun_index]->TimeoutFlag) {
  3274. case Timeout_Preparing: {
  3275. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PREPARE_TIMEOUT) {
  3276. _PrepareTimeout(gun_index);
  3277. StopGunInfoTimeoutDet(gun_index);
  3278. destroySelGun(gun_index); //jerry add
  3279. }
  3280. }
  3281. break;
  3282. case Timeout_EvChargingDet: {
  3283. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT) {
  3284. _DetectEvChargingEnableTimeout(gun_index);
  3285. StopGunInfoTimeoutDet(gun_index);
  3286. destroySelGun(gun_index); //jerry add
  3287. }
  3288. }
  3289. break;
  3290. case Timeout_EvseChargingDet: {
  3291. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT) {
  3292. _DetectEvseChargingEnableTimeout(gun_index);
  3293. StopGunInfoTimeoutDet(gun_index);
  3294. destroySelGun(gun_index); //jerry add
  3295. }
  3296. }
  3297. break;
  3298. case Timeout_EvseCompleteDet: {
  3299. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT) {
  3300. StopGunInfoTimeoutDet(gun_index);
  3301. destroySelGun(gun_index); //jerry add
  3302. }
  3303. }
  3304. break;
  3305. case Timeout_ForCcsPrechargeDet: {
  3306. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT) {
  3307. _CcsPrechargeTimeout(gun_index);
  3308. StopGunInfoTimeoutDet(gun_index);
  3309. destroySelGun(gun_index); //jerry add
  3310. }
  3311. }
  3312. break;
  3313. case Timeout_SelectGun: //Jerry add
  3314. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= SEL_GUN_TIMEOUT) {
  3315. DEBUG_INFO_MSG("Timeout_SelectGun\r\n");
  3316. StopGunInfoTimeoutDet(gun_index);
  3317. destroySelGun(gun_index); //jerry add
  3318. }
  3319. break;
  3320. }
  3321. }
  3322. sleep(1);
  3323. }
  3324. }
  3325. }
  3326. void GetSystemTime()
  3327. {
  3328. struct timeb csuTime;
  3329. struct tm *tmCSU;
  3330. ftime(&csuTime);
  3331. tmCSU = localtime(&csuTime.time);
  3332. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  3333. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3334. tmCSU->tm_sec);
  3335. // byte date[14];
  3336. //
  3337. //
  3338. // //sprintf(&date, "%d", );
  3339. //
  3340. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  3341. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  3342. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  3343. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  3344. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  3345. //
  3346. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  3347. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  3348. //
  3349. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  3350. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  3351. //
  3352. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  3353. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  3354. //
  3355. // date[10] = (tmCSU->tm_min) / 10 % 10;
  3356. // date[11] = (tmCSU->tm_min) / 1 % 10;
  3357. //
  3358. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  3359. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  3360. // PRINTF_FUNC("%x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x \n", date[0], date[1], date[2], date[3],
  3361. // date[4], date[5], date[6], date[7],
  3362. // date[8], date[9], date[10], date[11],
  3363. // date[12], date[13]);
  3364. }
  3365. void CheckFactoryConfigFunction()
  3366. {
  3367. if (ShmSysConfigAndInfo->SysInfo.FactoryConfiguration) {
  3368. system("cd /root;./FactoryConfig -m");
  3369. system("sync");
  3370. sleep(5);
  3371. system("reboot -f");
  3372. sleep(5);
  3373. system("reboot -f");
  3374. }
  3375. }
  3376. void CheckFwUpdateFunction()
  3377. {
  3378. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  3379. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  3380. DEBUG_INFO_MSG("ftp : update start. \n");
  3381. KillTask();
  3382. if (CheckUpdateProcess() == PASS) {
  3383. DEBUG_INFO_MSG("ftp : update complete. \n");
  3384. } else {
  3385. DEBUG_INFO_MSG("ftp : update fail. \n");
  3386. }
  3387. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  3388. sleep(5);
  3389. system("reboot -f");
  3390. } else if (ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES) {
  3391. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  3392. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL) {
  3393. DEBUG_INFO_MSG("Backend : update start. \n");
  3394. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3395. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3396. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3397. KillTask();
  3398. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  3399. setChargerMode(_index, MODE_UPDATE);
  3400. }
  3401. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++) {
  3402. ac_chargingInfo[_index]->SystemStatus = MODE_UPDATE;
  3403. }
  3404. if (CheckUpdateProcess() == PASS) {
  3405. DEBUG_INFO_MSG("Backend : update complete. \n");
  3406. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3407. } else {
  3408. DEBUG_INFO_MSG("Backend : update fail. \n");
  3409. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3410. }
  3411. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3412. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3413. sleep(5);
  3414. system("reboot -f");
  3415. }
  3416. }
  3417. }
  3418. //===============================================
  3419. // Check reservation date is expired
  3420. //===============================================
  3421. int isReservationExpired(unsigned char gun_index)
  3422. {
  3423. int result = NO;
  3424. struct tm expiredDate;
  3425. struct timeb expiredTime;
  3426. if (sscanf((char *) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
  3427. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  3428. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  3429. &expiredDate.tm_min, &expiredDate.tm_sec) == 6) {
  3430. expiredDate.tm_year -= 1900;
  3431. expiredDate.tm_mon -= 1;
  3432. expiredTime.time = mktime(&expiredDate);
  3433. if (!CheckTimeOut(expiredTime)) {
  3434. result = YES;
  3435. }
  3436. }
  3437. return result;
  3438. }
  3439. //===============================================
  3440. // OCPP
  3441. //===============================================
  3442. void CheckOcppStatus()
  3443. {
  3444. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES) {
  3445. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  3446. }
  3447. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES) {
  3448. bool canReset = true;
  3449. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2) {
  3450. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  3451. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  3452. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  3453. chargingInfo[_index]->SystemStatus != S_MAINTAIN) {
  3454. canReset = false;
  3455. if (chargingInfo[_index]->SystemStatus >= S_REASSIGN &&
  3456. chargingInfo[_index]->SystemStatus < S_TERMINATING) {
  3457. ChargingTerminalProcess(_index);
  3458. restartFlag = 1;
  3459. }
  3460. }
  3461. }
  3462. }
  3463. if (canReset) {
  3464. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  3465. sprintf((char *)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3466. if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL) {
  3467. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  3468. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  3469. sleep(3);
  3470. system("reboot -f");
  3471. } else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL) {
  3472. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  3473. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  3474. sleep(3);
  3475. system("killall OcppBackend &");
  3476. KillAllTask();
  3477. system("/usr/bin/run_evse_restart.sh");
  3478. }
  3479. }
  3480. }
  3481. }
  3482. void OcppStartTransation(byte gunIndex)
  3483. {
  3484. byte _OcppGunIndex = gunIndex;
  3485. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  3486. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1) {
  3487. _OcppGunIndex = 2;
  3488. }
  3489. if (strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL) {
  3490. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  3491. } else {
  3492. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3493. }
  3494. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  3495. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  3496. }
  3497. void OcppStopTransation(byte gunIndex)
  3498. {
  3499. byte _OcppGunIndex = gunIndex;
  3500. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  3501. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1) {
  3502. _OcppGunIndex = 2;
  3503. }
  3504. if (strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL) {
  3505. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  3506. } else {
  3507. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3508. }
  3509. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  3510. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  3511. }
  3512. bool OcppRemoteStop(byte gunIndex)
  3513. {
  3514. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  3515. // 有 AC 槍的話
  3516. if (acDirIndex > 0 && gunIndex > 0) {
  3517. gunIndex += acDirIndex;
  3518. }
  3519. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  3520. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES) {
  3521. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  3522. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  3523. }
  3524. return result;
  3525. }
  3526. bool WifiScheduleStop(byte gunIndex)
  3527. {
  3528. bool result = false;
  3529. result = chargingInfo[gunIndex]->schedule.isTriggerStop;
  3530. chargingInfo[gunIndex]->schedule.isTriggerStop = NO;
  3531. return result;
  3532. }
  3533. void OcppRemoteStartChk()
  3534. {
  3535. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3536. {}
  3537. else if (!isDetectPlugin()) {
  3538. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  3539. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  3540. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++) {
  3541. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES) {
  3542. if (ac_chargingInfo[ac_index]->SystemStatus == S_IDLE ||
  3543. ac_chargingInfo[ac_index]->SystemStatus == S_RESERVATION) {
  3544. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  3545. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  3546. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3547. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  3548. ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  3549. DetectPluginStart();
  3550. return;
  3551. }
  3552. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  3553. }
  3554. }
  3555. byte threeGunIndex = 0;
  3556. byte dcIndex = 0;
  3557. bool isGunUsingStatus = false;
  3558. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  3559. // 如果有 AC 槍,且 DC 槍也有兩把
  3560. if (acDirIndex == 1 && _index == 1) {
  3561. threeGunIndex = 1;
  3562. }
  3563. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES) {
  3564. dcIndex = _index;
  3565. }
  3566. if (chargingInfo[_index]->SystemStatus != S_IDLE) {
  3567. isGunUsingStatus = true;
  3568. }
  3569. }
  3570. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  3571. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf) {
  3572. if (dcIndex == 0) {
  3573. threeGunIndex = 0;
  3574. }
  3575. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  3576. return;
  3577. }
  3578. if (dcIndex == 0) {
  3579. threeGunIndex = 0;
  3580. }
  3581. if (ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq == YES) {
  3582. if (chargingInfo[dcIndex]->SystemStatus == S_IDLE ||
  3583. chargingInfo[dcIndex]->SystemStatus == S_RESERVATION) {
  3584. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  3585. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3586. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  3587. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  3588. DetectPluginStart();
  3589. setSelGunWaitToAuthor(dcIndex); //Jerry add
  3590. }
  3591. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  3592. }
  3593. }
  3594. }
  3595. void ChkOcppStatus(byte gunIndex)
  3596. {
  3597. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  3598. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES) {
  3599. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  3600. if (isReservationExpired(gunIndex)) {
  3601. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  3602. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  3603. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  3604. chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
  3605. }
  3606. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  3607. }
  3608. if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
  3609. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES) {
  3610. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  3611. if (isReservationExpired(gunIndex)) {
  3612. PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  3613. DEBUG_ERROR_MSG("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  3614. chargingInfo[gunIndex]->ReservationId = 0;
  3615. chargingInfo[gunIndex]->SystemStatus = S_IDLE;
  3616. }
  3617. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  3618. }
  3619. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES) {
  3620. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  3621. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  3622. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  3623. if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL) {
  3624. if (isDb_ready) {
  3625. DB_Update_Operactive(localDb, gunIndex, true);
  3626. }
  3627. chargingInfo[gunIndex]->IsAvailable = YES;
  3628. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  3629. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  3630. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN) {
  3631. setChargerMode(gunIndex, MODE_IDLE);
  3632. }
  3633. } else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL) {
  3634. if (isDb_ready) {
  3635. DB_Update_Operactive(localDb, gunIndex, false);
  3636. }
  3637. chargingInfo[gunIndex]->IsAvailable = NO;
  3638. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  3639. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  3640. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN) {
  3641. setChargerMode(gunIndex, MODE_MAINTAIN);
  3642. }
  3643. }
  3644. }
  3645. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES) {
  3646. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  3647. if (chargingInfo[gunIndex]->SystemStatus >= S_REASSIGN_CHECK ||
  3648. chargingInfo[gunIndex]->SystemStatus <= S_CHARGING) {
  3649. // 充電中,需停止充電
  3650. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  3651. ChargingTerminalProcess(gunIndex);
  3652. }
  3653. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  3654. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  3655. }
  3656. }
  3657. bool CheckBackendChargingTimeout(byte gunIndex)
  3658. {
  3659. bool result = false;
  3660. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) {
  3661. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) {
  3662. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60)) {
  3663. result = true;
  3664. }
  3665. }
  3666. } else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) {
  3667. // 隨插即充電的要看 offline
  3668. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) {
  3669. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60)) {
  3670. result = true;
  3671. }
  3672. }
  3673. }
  3674. return result;
  3675. }
  3676. bool CheckBackendChargingEnergy(byte gunIndex)
  3677. {
  3678. bool result = false;
  3679. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) {
  3680. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) {
  3681. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy) {
  3682. result = true;
  3683. }
  3684. }
  3685. } else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) {
  3686. // 隨插即充電的要看 offline
  3687. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) {
  3688. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) {
  3689. result = true;
  3690. }
  3691. }
  3692. }
  3693. return result;
  3694. }
  3695. void InformOcppErrOccur(byte codeType)
  3696. {
  3697. char _error[25];
  3698. switch (codeType) {
  3699. case 4: strcpy(_error, "InternalError"); break;
  3700. case 6: strcpy(_error, "NoError"); break;
  3701. case 7: strcpy(_error, "OtherError"); break;
  3702. case 13: strcpy(_error, "UnderVoltage"); break;
  3703. case 14: strcpy(_error, "OverVoltage"); break;
  3704. }
  3705. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  3706. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, _error);
  3707. }
  3708. }
  3709. //===============================================
  3710. // SQLite3 related routine
  3711. //===============================================
  3712. int DB_Open(sqlite3 *db)
  3713. {
  3714. int result = PASS;
  3715. char *errMsg = NULL;
  3716. char *createRecordSql = "CREATE TABLE IF NOT EXISTS charging_record("
  3717. "idx integer primary key AUTOINCREMENT, "
  3718. "reservationId text, "
  3719. "transactionId text, "
  3720. "startMethod text, "
  3721. "userId text, "
  3722. "dateTimeStart text, "
  3723. "dateTimeStop text,"
  3724. "socStart text, "
  3725. "socStop text, "
  3726. "chargeEnergy text, "
  3727. "stopReason text"
  3728. ");";
  3729. char *createCfgSql = "CREATE TABLE IF NOT EXISTS `config` ( "
  3730. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  3731. "`IsAvailable` TEXT NOT NULL, "
  3732. "`connector` INTEGER NOT NULL, "
  3733. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  3734. if (sqlite3_open(DB_FILE, &db)) {
  3735. result = FAIL;
  3736. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  3737. sqlite3_close(db);
  3738. } else {
  3739. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  3740. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK) {
  3741. result = FAIL;
  3742. PRINTF_FUNC( "Create local charging record table error message: %s\n", errMsg);
  3743. } else {
  3744. PRINTF_FUNC( "Opened local charging record table successfully\n");
  3745. }
  3746. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK) {
  3747. result = FAIL;
  3748. PRINTF_FUNC( "Create local config table error message: %s\n", errMsg);
  3749. } else {
  3750. PRINTF_FUNC( "Opened local config table successfully\n");
  3751. }
  3752. sqlite3_close(db);
  3753. }
  3754. return result;
  3755. }
  3756. int DB_Insert_Record(sqlite3 *db, int gun_index)
  3757. {
  3758. int result = PASS;
  3759. char *errMsg = NULL;
  3760. char insertSql[1024];
  3761. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  3762. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  3763. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  3764. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  3765. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  3766. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  3767. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  3768. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  3769. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  3770. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  3771. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  3772. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3773. if (sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db)) {
  3774. result = FAIL;
  3775. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  3776. sqlite3_close(db);
  3777. } else {
  3778. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  3779. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK) {
  3780. result = FAIL;
  3781. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  3782. } else {
  3783. PRINTF_FUNC( "Insert local charging record successfully\n");
  3784. }
  3785. sqlite3_close(db);
  3786. }
  3787. return result;
  3788. }
  3789. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  3790. {
  3791. uint8_t result = false;
  3792. char *errMsg = NULL;
  3793. char sqlStr[1024];
  3794. srand(time(NULL));
  3795. if (sqlite3_open(DB_FILE, &db)) {
  3796. result = FAIL;
  3797. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  3798. sqlite3_close(db);
  3799. } else {
  3800. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  3801. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  3802. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  3803. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK) {
  3804. result = FAIL;
  3805. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  3806. } else {
  3807. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  3808. }
  3809. sqlite3_close(db);
  3810. }
  3811. return result;
  3812. }
  3813. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  3814. {
  3815. uint8_t result = true;
  3816. char *errMsg = NULL;
  3817. char sqlStr[1024];
  3818. char **rs;
  3819. int rows, cols;
  3820. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  3821. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  3822. if (sqlite3_open(DB_FILE, &db)) {
  3823. result = FAIL;
  3824. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  3825. sqlite3_close(db);
  3826. } else {
  3827. PRINTF_FUNC( "Local config query database open successfully.\r\n");
  3828. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  3829. if (rows > 0) {
  3830. for (int idxRow = 1; idxRow <= rows; idxRow++) {
  3831. if (strcmp(rs[(idxRow * cols) + 3], "0") == 0) {
  3832. result = false;
  3833. }
  3834. PRINTF_FUNC("Query connector-%d isOperactive: %s\r\n", gun_index, rs[(idxRow * cols) + 3]);
  3835. }
  3836. } else {
  3837. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.\r\n", gun_index);
  3838. }
  3839. sqlite3_free_table(rs);
  3840. sqlite3_close(db);
  3841. }
  3842. return result;
  3843. }
  3844. //===============================================
  3845. // Config process
  3846. //===============================================
  3847. void AddPlugInTimes(byte gunIndex)
  3848. {
  3849. if (chargingInfo[gunIndex]->Type == _Type_Chademo) {
  3850. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  3851. } else if (chargingInfo[gunIndex]->Type == _Type_CCS_2) {
  3852. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  3853. } else if (chargingInfo[gunIndex]->Type == _Type_GB) {
  3854. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  3855. }
  3856. }
  3857. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  3858. {
  3859. char cmdBuf[32];
  3860. struct timeb csuTime;
  3861. struct tm *tmCSU;
  3862. ftime(&csuTime);
  3863. tmCSU = localtime(&csuTime.time);
  3864. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  3865. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3866. tmCSU->tm_sec);
  3867. if (isStart) {
  3868. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  3869. } else {
  3870. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  3871. }
  3872. }
  3873. void zipLogFiles()
  3874. {
  3875. const char *logPath = "/Storage/SystemLog";
  3876. // 獲取目錄
  3877. DIR *pDir = opendir(logPath);
  3878. if (pDir != NULL) {
  3879. struct timeb csuTime;
  3880. struct tm *tmCSU;
  3881. ftime(&csuTime);
  3882. tmCSU = localtime(&csuTime.time);
  3883. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  3884. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3885. // tmCSU->tm_sec);
  3886. // Read items inside the folder
  3887. struct dirent *pEntry = NULL;
  3888. while ((pEntry = readdir(pDir)) != NULL) {
  3889. if (strcmp(pEntry->d_name, ".") != 0 &&
  3890. strcmp(pEntry->d_name, "..") != 0 &&
  3891. strncmp(pEntry->d_name, "[", 1) == 0 &&
  3892. strstr(pEntry->d_name, "tar") < 0) {
  3893. char yearC[5];
  3894. unsigned short year = 0;
  3895. char monthC[3];
  3896. unsigned short month = 0;
  3897. yearC[4] = '\0';
  3898. strncpy(yearC, pEntry->d_name + 1, 4);
  3899. monthC[2] = '\0';
  3900. strncpy(monthC, pEntry->d_name + 6, 2);
  3901. year = atoi(yearC);
  3902. month = atoi(monthC);
  3903. if (year != 0) {
  3904. if (year < tmCSU->tm_year + 1900 ||
  3905. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1)) {
  3906. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  3907. char file[256];
  3908. memset(file, 0x00, sizeof(file));
  3909. strcat(file, "tar zcvf ");
  3910. strcat(file, logPath);
  3911. strncat(file, "/", 1);
  3912. strcat(file, pEntry->d_name);
  3913. strcat(file, ".tar");
  3914. strncat(file, " ", 1);
  3915. strcat(file, logPath);
  3916. strncat(file, "/", 1);
  3917. strcat(file, pEntry->d_name);
  3918. PRINTF_FUNC("zip = %s \n", file);
  3919. system(file);
  3920. }
  3921. }
  3922. }
  3923. }
  3924. }
  3925. // Close folder
  3926. closedir(pDir);
  3927. }
  3928. void ChangeGunSelectByIndex(byte sel)
  3929. {
  3930. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  3931. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  3932. }
  3933. void CheckIsAlternatvieByModelName()
  3934. {
  3935. // 黑白機 ?
  3936. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWWU301J0UT1PH") == EQUAL ||
  3937. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYE301J0ET1PH") == EQUAL ||
  3938. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DSYE301J3EW2PH") == EQUAL ||
  3939. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UW1PH") == EQUAL) {
  3940. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  3941. } else {
  3942. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  3943. }
  3944. }
  3945. void StopProcessingLoop()
  3946. {
  3947. for (;;) {
  3948. CheckFactoryConfigFunction();
  3949. CheckFwUpdateFunction();
  3950. sleep(1);
  3951. }
  3952. }
  3953. void CreateWatchdog()
  3954. {
  3955. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO) {
  3956. wtdFd = InitWatchDog();
  3957. if (wtdFd < 0) {
  3958. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  3959. }
  3960. }
  3961. }
  3962. bool IsConnectorWholeIdle()
  3963. {
  3964. bool result = true;
  3965. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++) {
  3966. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  3967. chargingInfo[count]->SystemStatus != S_RESERVATION) {
  3968. result = false;
  3969. break;
  3970. }
  3971. }
  3972. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++) {
  3973. if (ac_chargingInfo[count]->SystemStatus != S_IDLE &&
  3974. ac_chargingInfo[count]->IsErrorOccur == NO) {
  3975. result = false;
  3976. break;
  3977. }
  3978. }
  3979. return result;
  3980. }
  3981. void ClearAlarmCodeWhenAcOff()
  3982. {
  3983. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus) {
  3984. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  3985. }
  3986. }
  3987. //==========================================
  3988. // Check task processing
  3989. //==========================================
  3990. void CheckTask()
  3991. {
  3992. /*+++ 20200908, vern, disable it for DD360 +++*/
  3993. #if 0
  3994. if (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') {
  3995. if (system("pidof -s Module_4g > /dev/null") != 0) {
  3996. DEBUG_ERROR_MSG("Module_4g not running, restart it.\r\n");
  3997. system("/root/Module_4g &");
  3998. }
  3999. } else if (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') {
  4000. if (system("pidof -s Module_Wifi > /dev/null") != 0) {
  4001. DEBUG_ERROR_MSG("Module_Wifi not running, restart it.\r\n");
  4002. system("/root/Module_Wifi &");
  4003. }
  4004. }
  4005. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  4006. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL) {
  4007. if (system("pidof -s OcppBackend > /dev/null") != 0) {
  4008. DEBUG_ERROR_MSG("OcppBackend not running, restart it.\r\n");
  4009. system("/root/OcppBackend &");
  4010. }
  4011. }
  4012. #endif
  4013. /*--- 20200908, vern, disable it for DD360 ---*/
  4014. if (system("pidof -s Module_ProduceUtils > /dev/null") != 0) {
  4015. DEBUG_ERROR_MSG("Module_ProduceUtils not running, restart it.\r\n");
  4016. system ("/root/Module_ProduceUtils &");
  4017. }
  4018. }
  4019. //==========================================
  4020. // Check Smart Charging Profile
  4021. //==========================================
  4022. int GetStartScheduleTime(unsigned char *time)
  4023. {
  4024. int result = -1;
  4025. struct tm tmScheduleStart;
  4026. struct timeb tbScheduleStart;
  4027. if ((sscanf((char *)time, "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday, &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6)) {
  4028. tmScheduleStart.tm_year -= 1900;
  4029. tmScheduleStart.tm_mon -= 1;
  4030. tbScheduleStart.time = mktime(&tmScheduleStart);
  4031. tbScheduleStart.millitm = 0;
  4032. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  4033. }
  4034. return result;
  4035. }
  4036. void CheckSmartChargeProfile(byte _index)
  4037. {
  4038. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO) {
  4039. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO) {
  4040. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  4041. }
  4042. } else {
  4043. // Get Charging Profile
  4044. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  4045. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL) {
  4046. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  4047. byte _startCount = NO_DEFINE;
  4048. byte _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  4049. for (byte _count = 0; _count < _maxCount; _count++) {
  4050. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4051. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod &&
  4052. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR) {
  4053. _startCount = _count;
  4054. }
  4055. }
  4056. PRINTF_FUNC("_startCount = %d \n", _startCount);
  4057. if (_startCount < _maxCount) {
  4058. PRINTF_FUNC("*********Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  4059. chargingInfo[_index]->ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * 10;
  4060. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * chargingInfo[_index]->EvBatterytargetVoltage / 100;
  4061. //chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  4062. // if ((chargingInfo[_index]->EvBatterytargetVoltage * 10) > 0)
  4063. // {
  4064. // chargingInfo[_index]->ChargingProfileCurrent = chargingInfo[_index]->ChargingProfilePower / (chargingInfo[_index]->EvBatterytargetVoltage * 10);
  4065. // }
  4066. } else {
  4067. chargingInfo[_index]->ChargingProfilePower = -1;
  4068. chargingInfo[_index]->ChargingProfileCurrent = -1;
  4069. }
  4070. PRINTF_FUNC("ChargingProfilePower = %f \n", chargingInfo[_index]->ChargingProfilePower);
  4071. PRINTF_FUNC("ChargingProfileCurrent = %f \n", chargingInfo[_index]->ChargingProfileCurrent);
  4072. }
  4073. //
  4074. // printf("-------------Schedule------------\n");
  4075. // printf("index = %d \n", _index);
  4076. // printf("StartSchedule = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  4077. // printf("ChargingRateUnit = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingRateUnit);
  4078. // printf("----------SchedulePeriod---------\n");
  4079. // for (int v = 0; v < 10; v++)
  4080. // {
  4081. // printf("StartPeriod = %d \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].StartPeriod);
  4082. // printf("Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].Limit);
  4083. // }
  4084. // printf("---------------------------------\n");
  4085. }
  4086. }
  4087. void CheckReturnToChargingConn()
  4088. {
  4089. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  4090. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  4091. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  4092. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  4093. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG) {
  4094. bool isReturnTimeout = false;
  4095. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++) {
  4096. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  4097. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected) {
  4098. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  4099. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  4100. isReturnTimeout = false;
  4101. break;
  4102. }
  4103. } else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected) {
  4104. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  4105. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  4106. isReturnTimeout = true;
  4107. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  4108. }
  4109. }
  4110. }
  4111. // AC 槍
  4112. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0) {
  4113. // 沒有選中 AC,且 AC 在充電中
  4114. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  4115. (ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE)) {
  4116. // 當前 DC 充電槍在 idle 狀態
  4117. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_IDLE ||
  4118. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_RESERVATION) {
  4119. isReturnTimeout = true;
  4120. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  4121. }
  4122. } else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  4123. ((chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= S_COMPLETE) ||
  4124. (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= S_CCS_PRECHARGE_ST1))) {
  4125. // 當前 DC 充電槍在 idle 狀態
  4126. if (ac_chargingInfo[0]->SystemStatus == S_IDLE ||
  4127. ac_chargingInfo[0]->SystemStatus == S_RESERVATION) {
  4128. isReturnTimeout = true;
  4129. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  4130. }
  4131. }
  4132. }
  4133. if (!isReturnTimeout) {
  4134. StopSystemTimeoutDet();
  4135. }
  4136. }
  4137. }
  4138. bool GetStartChargingByAlterMode(byte _gun)
  4139. {
  4140. bool result = true;
  4141. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2 &&
  4142. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES) {
  4143. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select++) {
  4144. if (_select != _gun) {
  4145. if (chargingInfo[_select]->SystemStatus != S_IDLE &&
  4146. chargingInfo[_select]->SystemStatus != S_RESERVATION) {
  4147. result = false;
  4148. break;
  4149. }
  4150. }
  4151. }
  4152. }
  4153. return result;
  4154. }
  4155. void TheEndCharging(byte gun_index)
  4156. {
  4157. ftime(&endChargingTime[gun_index]);
  4158. if (chargingInfo[gun_index]->PresentChargedDuration != 0) {
  4159. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  4160. }
  4161. chargingInfo[gun_index]->isRemoteStart = NO;
  4162. StopGunInfoTimeoutDet(gun_index);
  4163. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  4164. ChangeStartOrStopDateTime(NO, gun_index);
  4165. DB_Insert_Record(localDb, gun_index);
  4166. }
  4167. void ResetDetAlarmStatus(byte gun)
  4168. {
  4169. if (chargingInfo[gun]->Type == _Type_Chademo) {
  4170. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP == YES) {
  4171. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = NO;
  4172. }
  4173. } else if (chargingInfo[gun]->Type == _Type_GB) {
  4174. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP == YES) {
  4175. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = NO;
  4176. }
  4177. } else if (chargingInfo[gun]->Type == _Type_CCS_2) {
  4178. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP == YES) {
  4179. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = NO;
  4180. }
  4181. }
  4182. }
  4183. static void powerCabinetPsuAlarmStatus(void)
  4184. {
  4185. uint8_t i = 0;
  4186. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  4187. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042267", 6) == 0) {
  4188. EmcOccureByString("042267");
  4189. }
  4190. }
  4191. }
  4192. int main(void)
  4193. {
  4194. if (CreateShareMemory() == 0) {
  4195. #ifdef SystemLogMessage
  4196. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  4197. #endif
  4198. if (ShmStatusCodeData != NULL) {
  4199. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  4200. }
  4201. sleep(5);
  4202. system("reboot -f");
  4203. sleep(5);
  4204. system("reboot -f");
  4205. }
  4206. DEBUG_INFO_MSG(" **************** FileSystem Boot up ***************\n");
  4207. if (!InitialSystemDefaultConfig()) {
  4208. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  4209. StopProcessingLoop();
  4210. }
  4211. CheckGunTypeFromHw();
  4212. CheckIsAlternatvieByModelName();
  4213. InitialShareMemoryInfo();
  4214. ChangeLcmByIndex(_LCM_INIT);
  4215. if (!CheckConnectorTypeStatus()) {
  4216. isModelNameMatch = false;
  4217. }
  4218. Initialization();
  4219. SpawnTask();
  4220. DEBUG_INFO_MSG("Spawned all Task");
  4221. if (!isModelNameMatch) {
  4222. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  4223. ChangeLcmByIndex(_LCM_FIX);
  4224. // Module Name 與硬體對應不正確
  4225. DEBUG_ERROR_MSG("Module Name & HW info none match. \n");
  4226. sleep(3);
  4227. KillAllTask();
  4228. StopProcessingLoop();
  4229. }
  4230. CreateTimeoutFork();
  4231. DEBUG_INFO_MSG("Start self test... \n");
  4232. SelfTestRun();
  4233. StopSystemTimeoutDet();
  4234. DEBUG_INFO_MSG("Self test finished : SelfTestSeq = %d, Work_Step = %d ", ShmSysConfigAndInfo->SysInfo.SelfTestSeq, ShmPsuData->Work_Step);
  4235. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  4236. ShmPsuData->Work_Step == _NO_WORKING) {
  4237. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2) {
  4238. DisplaySelfTestFailReason();
  4239. }
  4240. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  4241. setChargerMode(gun_index, MODE_ALARM);
  4242. }
  4243. ChangeLcmByIndex(_LCM_FIX);
  4244. sleep(3);
  4245. KillAllTask();
  4246. StopProcessingLoop();
  4247. } else {
  4248. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  4249. setChargerMode(gun_index, MODE_IDLE);
  4250. }
  4251. }
  4252. // Local DB
  4253. if (DB_Open(localDb) != PASS) {
  4254. PRINTF_FUNC("DB_Open fail. \n");
  4255. isDb_ready = false;
  4256. } else {
  4257. isDb_ready = true;
  4258. for (int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  4259. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  4260. }
  4261. }
  4262. ChangeLcmByIndex(_LCM_SELECT_GUN); //_LCM_IDLE //Jerry add
  4263. sleep(1);
  4264. //***** 須新增的偵測 *****//
  4265. // 1. Thernal - 控制風扇轉速
  4266. // 2. ouput fuse - 控制風扇轉速
  4267. CreateRfidFork();
  4268. // Create Watchdog
  4269. //CreateWatchdog();
  4270. // Main loop
  4271. PRINTF_FUNC("****************************Main Loop********************************** \n");
  4272. gettimeofday(&_cmdMainPriority_time, NULL);
  4273. for (;;) {
  4274. CheckOcppStatus();
  4275. ChkPrimaryStatus();
  4276. if ((IsConnectorWholeIdle() || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) &&
  4277. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet) {
  4278. CheckFactoryConfigFunction();
  4279. CheckFwUpdateFunction();
  4280. }
  4281. // OCPP 邏輯
  4282. OcppRemoteStartChk();
  4283. // 讀卡邏輯
  4284. ScannerCardProcess();
  4285. // 當 AC 沒有搭上時,清除一些錯誤碼
  4286. ClearAlarmCodeWhenAcOff();
  4287. // 確認是否要回到充電中的槍畫面邏輯判斷
  4288. CheckReturnToChargingConn();
  4289. //確認Power cabinet PSU Status
  4290. powerCabinetPsuAlarmStatus();
  4291. if (_acgunIndex > 0 && isDetectPlugin() && !isCardScan) {
  4292. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  4293. }
  4294. if ((GetTimeoutValue(_cmdMainPriority_time) / 1000) > 5000) {
  4295. CheckTask();
  4296. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  4297. if (chargingInfo[_index]->SystemStatus == S_CHARGING) {
  4298. CheckSmartChargeProfile(_index);
  4299. }
  4300. }
  4301. gettimeofday(&_cmdMainPriority_time, NULL);
  4302. }
  4303. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  4304. CheckGpioInStatus();
  4305. CheckErrorOccurStatus(gun_index);
  4306. ChkOcppStatus(gun_index);
  4307. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  4308. switch (chargingInfo[gun_index]->SystemStatus) {
  4309. case S_IDLE:
  4310. case S_RESERVATION:
  4311. case S_MAINTAIN:
  4312. case S_FAULT: {
  4313. if (chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  4314. isModeChange(gun_index)) {
  4315. DEBUG_INFO_MSG("S_IDLE================================== %x \n", gun_index);
  4316. chargingInfo[gun_index]->PresentChargedDuration = 0;
  4317. chargingInfo[gun_index]->RemainChargingDuration = 0;
  4318. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  4319. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  4320. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  4321. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  4322. //Jerry add
  4323. memset(&gAudiCustInfo->PricesInfo[gun_index], 0, sizeof(PricesInfo));
  4324. gAudiCustInfo->PricesInfo[gun_index].Balance = FAIL_BALANCE_PRICES;
  4325. ResetDetAlarmStatus(gun_index); //recovery OVP status code
  4326. } else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION &&
  4327. isModeChange(gun_index)) {
  4328. DEBUG_INFO_MSG("S_RESERVATION....................%x \n", gun_index);
  4329. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
  4330. }
  4331. if (chargingInfo[gun_index]->IsAvailable == NO) {
  4332. setChargerMode(gun_index, MODE_MAINTAIN);
  4333. }
  4334. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2) {
  4335. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected) {
  4336. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  4337. } else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != S_IDLE &&
  4338. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != S_RESERVATION &&
  4339. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != S_MAINTAIN &&
  4340. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != S_FAULT) {
  4341. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING) {
  4342. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_COMPLETE;
  4343. } else {
  4344. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;//_LCM_NONE;
  4345. }
  4346. }
  4347. ClearDetectPluginFlag();
  4348. //UpdateErrorCodeToOcpp(gun_index);
  4349. setChargerMode(gun_index, MODE_FAULT);
  4350. } else {
  4351. if (chargingInfo[gun_index]->SystemStatus == S_FAULT) {
  4352. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  4353. setChargerMode(gun_index, MODE_IDLE);
  4354. }
  4355. //if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_FIX) {
  4356. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;//_LCM_NONE; //Jerry add
  4357. // //for (byte g_index = 0; g_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; g_index++) {
  4358. // setChargerMode(gun_index, MODE_IDLE);
  4359. // //}
  4360. //}
  4361. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER) {
  4362. #if !defined DD360 && !defined DD360Audi
  4363. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  4364. #else
  4365. if (1)
  4366. #endif //!defined DD360 && !defined DD360Audi
  4367. {
  4368. // 均充 -> 最大充
  4369. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO) {
  4370. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE) {
  4371. DEBUG_INFO_MSG("=============Smart Charging============= Step 11 \n");
  4372. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  4373. }
  4374. } else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  4375. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING) {
  4376. DEBUG_INFO_MSG("=============Smart Charging============= Step 14 \n");
  4377. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  4378. } else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP) {
  4379. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  4380. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  4381. }
  4382. } else {
  4383. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  4384. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  4385. }
  4386. } else {
  4387. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  4388. }
  4389. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING) {
  4390. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4391. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_SELECT_GUN;//_LCM_IDLE;
  4392. }
  4393. } else {
  4394. // Idle 正常程序起點
  4395. // 判斷是否有啟用檢查插槍
  4396. if (isDetectPlugin()) {
  4397. //PRINTF_FUNC("----------------ShmSysConfigAndInfo->SysInfo.PageIndex--------------- %d \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  4398. //PRINTF_FUNC("----------------ShmSysConfigAndInfo->SysInfo.SystemPage--------------- %d \n", ShmSysConfigAndInfo->SysInfo.SystemPage);
  4399. //PRINTF_FUNC("----------------ShmSysConfigAndInfo->SysInfo.ConnectorPage--------------- %d \n", ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  4400. //PRINTF_FUNC("----------------isCardScan--------------- %d \n", isCardScan);
  4401. // 卡號驗證成功後,等待充電槍插入充電車
  4402. if (chargingInfo[gun_index]->RemoteStartFlag == YES) {
  4403. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  4404. chargingInfo[gun_index]->IsAvailable) {
  4405. PRINTF_FUNC("-----------------1----------------- %d \n", gun_index);
  4406. chargingInfo[gun_index]->RemoteStartFlag = NO;
  4407. ChangeGunSelectByIndex(gun_index);
  4408. AddPlugInTimes(gun_index);
  4409. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  4410. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  4411. ClearDetectPluginFlag();
  4412. continue;
  4413. }
  4414. } else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE) {
  4415. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  4416. chargingInfo[gun_index]->IsAvailable &&
  4417. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  4418. (waitRightGunPlugIt(gun_index) == PASS || //Jerry add
  4419. waitLeftGunPlugIt(gun_index) == PASS)
  4420. /*&&((ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)||
  4421. (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG))*/) {
  4422. PRINTF_FUNC("-----------------2----------------- \n");
  4423. ChangeGunSelectByIndex(gun_index);
  4424. AddPlugInTimes(gun_index);
  4425. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  4426. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  4427. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  4428. // 當前操作的槍號,進入 Preparing
  4429. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  4430. ClearDetectPluginFlag();
  4431. continue;
  4432. }
  4433. }
  4434. if (!isCardScan) {
  4435. // LCM => Waiting for plugging
  4436. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  4437. }
  4438. } else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION) {
  4439. if (!isReservationExpired(gun_index)) {
  4440. chargingInfo[gun_index]->SystemStatus = S_IDLE;
  4441. }
  4442. } else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  4443. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable)) {
  4444. PRINTF_FUNC("-----------------3----------------- \n");
  4445. bool isCanStartChargingFlag = GetStartChargingByAlterMode(gun_index);
  4446. if (isCanStartChargingFlag) {
  4447. ChangeGunSelectByIndex(gun_index);
  4448. AddPlugInTimes(gun_index);
  4449. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  4450. ClearDetectPluginFlag();
  4451. continue;
  4452. }
  4453. } else {
  4454. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4455. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_SELECT_GUN; //_LCM_IDLE; //Jerry add
  4456. }
  4457. }
  4458. } // Idle 正常程序終點
  4459. ReleaseAlarmCode(gun_index);
  4460. }
  4461. }
  4462. break;
  4463. case S_REASSIGN_CHECK: {
  4464. if (isModeChange(gun_index)) {
  4465. DEBUG_INFO_MSG("S_REASSIGN_CHECK================================== %x \n", gun_index);
  4466. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;//_LCM_NONE; //Jerry add
  4467. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE) {
  4468. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  4469. }
  4470. StopSystemTimeoutDet();
  4471. }
  4472. #if 0
  4473. bool isRessign = false;
  4474. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO) {
  4475. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX) {
  4476. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  4477. // 有其他槍已經分配好 psu 模塊
  4478. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  4479. chargingInfo[index]->SystemStatus >= S_PREPARNING &&
  4480. chargingInfo[index]->SystemStatus != S_MAINTAIN) {
  4481. DEBUG_INFO_MSG("=============Smart Charging============= Step 1 \n");
  4482. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  4483. isRessign = true;
  4484. break;
  4485. }
  4486. }
  4487. } else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  4488. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE) {
  4489. // 如果在切換最大充的過程中,需等待最大充切換完成後,在走均充流程
  4490. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES) {
  4491. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  4492. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING) {
  4493. DEBUG_INFO_MSG("=============Smart Charging============= Step 14 \n");
  4494. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  4495. } else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP) {
  4496. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  4497. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  4498. continue;
  4499. }
  4500. }
  4501. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4502. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  4503. }
  4504. continue;
  4505. }
  4506. }
  4507. #endif // 0
  4508. if (0) { // if (isRessign)
  4509. #if defined DD360 || defined DD360Audi
  4510. setChargerMode(gun_index, MODE_PRECHARGE);
  4511. #else
  4512. setChargerMode(gun_index, MODE_REASSIGN);
  4513. #endif //defined DD360 || defined DD360Audi
  4514. } else {
  4515. setChargerMode(gun_index, MODE_PRECHARGE);
  4516. }
  4517. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4518. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  4519. }
  4520. }
  4521. break;
  4522. case S_REASSIGN: {
  4523. if (isModeChange(gun_index)) {
  4524. PRINTF_FUNC("S_REASSIGN================================== %x \n", gun_index);
  4525. gettimeofday(&_toAverage_time, NULL);
  4526. }
  4527. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  4528. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  4529. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  4530. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER) {
  4531. if (ShmSysConfigAndInfo->SysInfo.CanAverageCharging) {
  4532. setChargerMode(gun_index, MODE_PRECHARGE);
  4533. } else {
  4534. setChargerMode(gun_index, MODE_IDLE);
  4535. }
  4536. } else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A &&
  4537. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO) {
  4538. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
  4539. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  4540. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  4541. }
  4542. //PRINTF_FUNC("CurGunSelected = %d, gun_index = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected, gun_index);
  4543. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4544. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  4545. }
  4546. }
  4547. break;
  4548. case S_PREPARNING: {
  4549. if (isModeChange(gun_index)) {
  4550. PRINTF_FUNC("S_PREPARNING================================== %x \n", gun_index);
  4551. StopGunInfoTimeoutDet(gun_index);
  4552. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  4553. }
  4554. if (ShmPsuData->SystemPresentPsuQuantity > 0 && ShmPsuData->SystemAvailablePower > 10 &&
  4555. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 5000000) {
  4556. if ((chargingInfo[gun_index]->MaximumChargingVoltage > 0) &&
  4557. (chargingInfo[gun_index]->AvailableChargingCurrent > 0) &&
  4558. (chargingInfo[gun_index]->AvailableChargingPower > 0)) {
  4559. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  4560. }
  4561. }
  4562. if (isEvBoardStopChargeFlag(gun_index) == YES) {
  4563. // 板端要求停止 (錯誤)
  4564. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  4565. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4566. }
  4567. ChargingAlarmProcess(gun_index);
  4568. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  4569. // 板端要求停止 (正常)
  4570. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  4571. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4572. }
  4573. ChargingTerminalProcess(gun_index);
  4574. } else if (OcppRemoteStop(gun_index) == YES ||
  4575. WifiScheduleStop(gun_index)) {
  4576. // 後臺要求停止
  4577. ChargingTerminalProcess(gun_index);
  4578. }
  4579. //if (isEvBoardStopChargeFlag(gun_index) == YES || OcppRemoteStop(gun_index) == YES) {
  4580. // // 板端或後臺要求停止
  4581. // ChargingTerminalProcess(gun_index);
  4582. //}
  4583. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4584. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  4585. }
  4586. }
  4587. break;
  4588. case S_PREPARING_FOR_EV: { // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  4589. if (isModeChange(gun_index)) {
  4590. DEBUG_INFO_MSG("S_PREPARING_FOR_EV================================== %x \n", gun_index);
  4591. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  4592. StopGunInfoTimeoutDet(gun_index);
  4593. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  4594. }
  4595. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  4596. // 檢查車端的槍鎖是否為鎖上
  4597. if (isEvGunLocked_chademo(gun_index) == YES) {
  4598. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  4599. }
  4600. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  4601. // 檢查車端的 charging enable 是否為 1
  4602. if (isEvGunLocked_gb(gun_index) == YES) {
  4603. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  4604. }
  4605. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  4606. // 檢查車端的 charging enable 是否為 1
  4607. if (isEvGunLocked_ccs(gun_index) == YES) {
  4608. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  4609. }
  4610. }
  4611. if (isEvBoardStopChargeFlag(gun_index) == YES) {
  4612. // 板端要求停止 (錯誤)
  4613. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  4614. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4615. }
  4616. ChargingAlarmProcess(gun_index);
  4617. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  4618. // 板端要求停止 (正常)
  4619. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  4620. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4621. }
  4622. ChargingTerminalProcess(gun_index);
  4623. } else if (OcppRemoteStop(gun_index) == YES ||
  4624. WifiScheduleStop(gun_index)) {
  4625. // 後臺要求停止
  4626. ChargingTerminalProcess(gun_index);
  4627. }
  4628. //if (isEvBoardStopChargeFlag(gun_index) == YES ||
  4629. // OcppRemoteStop(gun_index) == YES) {
  4630. // // 板端或後臺要求停止
  4631. // ChargingTerminalProcess(gun_index);
  4632. //}
  4633. // LCM => Pre-charging
  4634. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4635. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  4636. }
  4637. }
  4638. break;
  4639. case S_PREPARING_FOR_EVSE: { // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  4640. if (isModeChange(gun_index)) {
  4641. DEBUG_INFO_MSG("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
  4642. StopGunInfoTimeoutDet(gun_index);
  4643. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  4644. }
  4645. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  4646. // 檢查樁端的 GFD 結果
  4647. if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8) {
  4648. // 當前操作的槍號,進入 Charging
  4649. setChargerMode(gun_index, MODE_CHARGING);
  4650. }
  4651. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  4652. // GFD 錯誤停止
  4653. RecordAlarmCode(gun_index, "012234");
  4654. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  4655. // GFD 警告
  4656. RecordWarningCode(gun_index, "012296");
  4657. }
  4658. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  4659. // 檢查樁端的 GFD 結果
  4660. if (isPrechargeStatus_gb(gun_index) > 5 && isPrechargeStatus_gb(gun_index) < 9) {
  4661. setChargerMode(gun_index, MODE_CHARGING);
  4662. }
  4663. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  4664. // GFD 錯誤停止
  4665. RecordAlarmCode(gun_index, "012236");
  4666. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  4667. // GFD 警告
  4668. RecordWarningCode(gun_index, "012298");
  4669. }
  4670. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  4671. // 檢查樁端的 GFD 結果
  4672. if ((chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  4673. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)) {
  4674. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
  4675. }
  4676. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  4677. // GFD 錯誤停止
  4678. RecordAlarmCode(gun_index, "012235");
  4679. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  4680. // GFD 警告
  4681. RecordWarningCode(gun_index, "012297");
  4682. }
  4683. }
  4684. if (isEvBoardStopChargeFlag(gun_index) == YES) {
  4685. // 板端要求停止 (錯誤)
  4686. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  4687. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4688. }
  4689. ChargingAlarmProcess(gun_index);
  4690. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  4691. // 板端要求停止 (正常)
  4692. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  4693. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4694. }
  4695. ChargingTerminalProcess(gun_index);
  4696. } else if (OcppRemoteStop(gun_index) == YES ||
  4697. WifiScheduleStop(gun_index)) {
  4698. // 後臺要求停止
  4699. ChargingTerminalProcess(gun_index);
  4700. }
  4701. //if (isEvBoardStopChargeFlag(gun_index) == YES ||
  4702. // OcppRemoteStop(gun_index) == YES) {
  4703. // // 板端或後臺要求停止
  4704. // ChargingTerminalProcess(gun_index);
  4705. //}
  4706. // LCM => Pre-charging
  4707. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4708. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  4709. }
  4710. }
  4711. break;
  4712. case S_CHARGING: { // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  4713. if (isModeChange(gun_index)) {
  4714. DEBUG_INFO_MSG("S_CHARGING================================== %x \n", gun_index);
  4715. StopGunInfoTimeoutDet(gun_index);
  4716. ftime(&startChargingTime[gun_index]);
  4717. ChangeStartOrStopDateTime(YES, gun_index);
  4718. /*OcppStartTransation(gun_index);*//*+++ 20200908, vern, disable it for DD360 ---*/
  4719. }
  4720. if (ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf) {
  4721. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = NO;
  4722. }
  4723. ftime(&endChargingTime[gun_index]);
  4724. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  4725. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  4726. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  4727. // GFD 錯誤停止
  4728. RecordAlarmCode(gun_index, "012234");
  4729. }
  4730. //else if (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  4731. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL)) {
  4732. // // UVP
  4733. // RecordAlarmCode(gun_index, "012289");
  4734. // ChargingTerminalProcess(gun_index);
  4735. //}
  4736. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  4737. // GFD 警告
  4738. RecordWarningCode(gun_index, "012296");
  4739. }
  4740. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  4741. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  4742. // GFD 錯誤停止
  4743. RecordAlarmCode(gun_index, "012236");
  4744. }
  4745. //else if (isPrechargeStatus_gb(gun_index) == 10 &&
  4746. // (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  4747. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))) {
  4748. // // UVP
  4749. // RecordAlarmCode(gun_index, "012290");
  4750. // ChargingTerminalProcess(gun_index);
  4751. //}
  4752. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  4753. // GFD 警告
  4754. RecordWarningCode(gun_index, "012298");
  4755. }
  4756. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  4757. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  4758. // GFD 錯誤停止
  4759. RecordAlarmCode(gun_index, "012235");
  4760. }
  4761. //else if (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  4762. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL)) {
  4763. // // UVP
  4764. // RecordAlarmCode(gun_index, "012288");
  4765. // ChargingTerminalProcess(gun_index);
  4766. //}
  4767. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  4768. // GFD 警告
  4769. RecordWarningCode(gun_index, "012297");
  4770. }
  4771. }
  4772. if (isEvBoardStopChargeFlag(gun_index) == YES) {
  4773. // 板端要求停止 (錯誤)
  4774. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  4775. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4776. }
  4777. ChargingAlarmProcess(gun_index);
  4778. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  4779. // 板端要求停止 (正常)
  4780. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  4781. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4782. }
  4783. ChargingTerminalProcess(gun_index);
  4784. } else if (OcppRemoteStop(gun_index) == YES ||
  4785. WifiScheduleStop(gun_index) ||
  4786. CheckBackendChargingTimeout(gun_index) ||
  4787. CheckBackendChargingEnergy(gun_index) ||
  4788. strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid") == EQUAL) {
  4789. // 後臺要求停止
  4790. ChargingTerminalProcess(gun_index);
  4791. }
  4792. //if (isEvBoardStopChargeFlag(gun_index) ||
  4793. // OcppRemoteStop(gun_index) ||
  4794. // CheckBackendChargingTimeout(gun_index) ||
  4795. // CheckBackendChargingEnergy(gun_index) ||
  4796. // strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid") == EQUAL) {
  4797. // DEBUG_INFO_MSG("S_CHARGING=======Stop=========================== %f \n", (chargingInfo[gun_index]->EvBatterytargetVoltage * 10));
  4798. // // 板端或後臺要求停止
  4799. // ChargingTerminalProcess(gun_index);
  4800. //}
  4801. // LCM => Charging
  4802. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4803. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  4804. }
  4805. }
  4806. break;
  4807. case S_ALARM:
  4808. case S_TERMINATING: {
  4809. if (isModeChange(gun_index)) {
  4810. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  4811. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  4812. }
  4813. if (chargingInfo[gun_index]->SystemStatus == S_ALARM) {
  4814. PRINTF_FUNC("================== S_ALARM (%x) ================ \n", gun_index);
  4815. //UpdateErrorCodeToOcpp(gun_index);
  4816. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL) {
  4817. OcppStopTransation(gun_index);
  4818. }
  4819. TheEndCharging(gun_index);
  4820. } else {
  4821. PRINTF_FUNC("================== S_TERMINATING (%x) ================ \n", gun_index);
  4822. }
  4823. //DEBUG_INFO_MSG ("terminating......................... %x \n", gun_index);
  4824. StopGunInfoTimeoutDet(gun_index);
  4825. }
  4826. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  4827. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  4828. //if (isEvStopCharging_chademo(gun_index) == YES) {
  4829. // /*+++ 20200908, vern, disable it for DD360 +++*/
  4830. // /*if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  4831. // strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");*/
  4832. // /*--- 20200908, vern, disable it for DD360 ---*/
  4833. //}
  4834. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  4835. // GFD 錯誤停止
  4836. RecordAlarmCode(gun_index, "012234");
  4837. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  4838. // GFD 警告
  4839. RecordWarningCode(gun_index, "012296");
  4840. }
  4841. //if (isEvStopCharging_chademo(gun_index) == YES ||
  4842. // isPrechargeStatus_chademo(gun_index) <= 0) {
  4843. // setChargerMode(gun_index, MODE_COMPLETE);
  4844. //}
  4845. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  4846. DEBUG_INFO_MSG("************ GB lock Status = %d, status = %d \n", isEvStopCharging_gb(gun_index), isPrechargeStatus_gb(gun_index));
  4847. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  4848. //if (isEvStopCharging_chademo(gun_index) == YES) {
  4849. // /*+++ 20200908, vern, disable it for DD360 +++*/
  4850. // /*if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  4851. // strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");*/
  4852. // /*--- 20200908, vern, disable it for DD360 ---*/
  4853. //}
  4854. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  4855. // GFD 錯誤停止
  4856. RecordAlarmCode(gun_index, "012236");
  4857. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  4858. // GFD 警告
  4859. RecordWarningCode(gun_index, "012298");
  4860. }
  4861. //if (isEvStopCharging_gb(gun_index) == YES ||
  4862. // isPrechargeStatus_gb(gun_index) <= 0) {
  4863. // setChargerMode(gun_index, MODE_COMPLETE);
  4864. //}
  4865. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  4866. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  4867. //if (isEvStopCharging_chademo(gun_index) == YES) {
  4868. // /*+++ 20200908, vern, disable it for DD360 +++*/
  4869. // /*if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  4870. // strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");*/
  4871. // /*--- 20200908, vern, disable it for DD360 ---*/
  4872. //}
  4873. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  4874. // GFD 錯誤停止
  4875. RecordAlarmCode(gun_index, "012235");
  4876. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  4877. // GFD 警告
  4878. RecordWarningCode(gun_index, "012297");
  4879. }
  4880. //if (isEvStopCharging_ccs(gun_index) == YES &&
  4881. // (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  4882. // isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14)) {
  4883. // setChargerMode(gun_index, MODE_COMPLETE);
  4884. //}
  4885. }
  4886. if (chargingInfo[gun_index]->SystemStatus == S_ALARM) {
  4887. if (chargingInfo[gun_index]->ConnectorPlugIn == NO &&
  4888. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000) {
  4889. setChargerMode(gun_index, MODE_IDLE);
  4890. }
  4891. } else {
  4892. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  4893. if (isEvStopCharging_chademo(gun_index) == YES ||
  4894. isPrechargeStatus_chademo(gun_index) <= 0) {
  4895. setChargerMode(gun_index, MODE_COMPLETE);
  4896. }
  4897. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  4898. if (isEvStopCharging_gb(gun_index) == YES ||
  4899. isPrechargeStatus_gb(gun_index) <= 0) {
  4900. setChargerMode(gun_index, MODE_COMPLETE);
  4901. }
  4902. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  4903. if (isEvStopCharging_ccs(gun_index) == YES &&
  4904. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  4905. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14)) {
  4906. setChargerMode(gun_index, MODE_COMPLETE);
  4907. }
  4908. }
  4909. }
  4910. // 車端的停止
  4911. //if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  4912. // strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4913. //}
  4914. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4915. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  4916. }
  4917. }
  4918. break;
  4919. case S_COMPLETE: {
  4920. if (isModeChange(gun_index)) {
  4921. PRINTF_FUNC ("complete......................... %x \n", gun_index);
  4922. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL) {
  4923. OcppStopTransation(gun_index);
  4924. }
  4925. TheEndCharging(gun_index);
  4926. //ftime(&endChargingTime[gun_index]);
  4927. //if (chargingInfo[gun_index]->PresentChargedDuration != 0) {
  4928. // chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  4929. //}
  4930. //StopGunInfoTimeoutDet(gun_index);
  4931. //StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  4932. //ChangeStartOrStopDateTime(NO, gun_index);
  4933. }
  4934. //if (((chargingInfo[gun_index]->ConnectorPlugIn == NO) ||
  4935. // (restartFlag == 1)) &&
  4936. // (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)) {
  4937. // setChargerMode(gun_index, MODE_IDLE);
  4938. // destroySelGun(gun_index); //Jerry add
  4939. //}
  4940. if (chargingInfo[gun_index]->ConnectorPlugIn == NO &&
  4941. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000) {
  4942. setChargerMode(gun_index, MODE_IDLE);
  4943. destroySelGun(gun_index); //Jerry add
  4944. }
  4945. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4946. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  4947. }
  4948. }
  4949. break;
  4950. case S_CCS_PRECHARGE_ST0: {
  4951. if (isModeChange(gun_index)) {
  4952. PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
  4953. StopGunInfoTimeoutDet(gun_index);
  4954. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  4955. }
  4956. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  4957. // GFD 錯誤停止
  4958. RecordAlarmCode(gun_index, "012235");
  4959. }
  4960. if (isEvBoardStopChargeFlag(gun_index) == YES) {
  4961. // 板端要求停止 (錯誤)
  4962. ChargingAlarmProcess(gun_index);
  4963. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  4964. // 板端要求停止 (正常)
  4965. ChargingTerminalProcess(gun_index);
  4966. } else if (OcppRemoteStop(gun_index) == YES ||
  4967. WifiScheduleStop(gun_index)) {
  4968. // 後臺要求停止
  4969. ChargingTerminalProcess(gun_index);
  4970. }
  4971. //if (isEvBoardStopChargeFlag(gun_index) == YES) {
  4972. // // 板端要求停止
  4973. // ChargingTerminalProcess(gun_index);
  4974. //}
  4975. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  4976. // 切換 D+ Relay to Precharge Relay
  4977. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40) {
  4978. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  4979. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  4980. {
  4981. //PRINTF_FUNC("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
  4982. if (isPrechargeStatus_ccs(gun_index) == 39) {
  4983. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PreChargeResponse (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  4984. } else if (isPrechargeStatus_ccs(gun_index) == 40) {
  4985. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PowerDeliveryRequest start (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  4986. }
  4987. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  4988. }
  4989. } else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46) {
  4990. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
  4991. }
  4992. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  4993. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  4994. }
  4995. break;
  4996. }
  4997. case S_CCS_PRECHARGE_ST1: {
  4998. if (isModeChange(gun_index)) {
  4999. DEBUG_INFO_MSG("CCS Precharge Processing 2....................%x \n", gun_index);
  5000. }
  5001. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5002. // GFD 錯誤停止
  5003. RecordAlarmCode(gun_index, "012235");
  5004. }
  5005. if (isEvBoardStopChargeFlag(gun_index) == YES) {
  5006. // 板端要求停止 (錯誤)
  5007. ChargingAlarmProcess(gun_index);
  5008. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  5009. // 板端要求停止 (正常)
  5010. ChargingTerminalProcess(gun_index);
  5011. } else if (OcppRemoteStop(gun_index) == YES ||
  5012. WifiScheduleStop(gun_index)) {
  5013. // 後臺要求停止
  5014. ChargingTerminalProcess(gun_index);
  5015. }
  5016. //if (isEvBoardStopChargeFlag(gun_index) == YES) {
  5017. // // 板端要求停止
  5018. // ChargingTerminalProcess(gun_index);
  5019. //}
  5020. // 等待小板通知進入充電
  5021. // 切換 D+ Relay to Precharge Relay
  5022. if (chargingInfo[gun_index]->RelayK1K2Status == YES) {
  5023. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  5024. setChargerMode(gun_index, MODE_CHARGING);
  5025. }
  5026. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5027. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5028. }
  5029. break;
  5030. }
  5031. }
  5032. }
  5033. //if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE) {
  5034. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_SELECT_GUN) { //Jerry add
  5035. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  5036. } else {
  5037. bool dcPageRun = false;
  5038. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE) {
  5039. if (ac_chargingInfo[0]->SystemStatus == S_IDLE) {
  5040. ChangeLcmByIndex(_LCM_IDLE);
  5041. } else if (ac_chargingInfo[0]->SystemStatus == S_PREPARNING) {
  5042. ChangeLcmByIndex(_LCM_PRE_CHARGE);
  5043. } else if (ac_chargingInfo[0]->SystemStatus == S_CHARGING) {
  5044. ChangeLcmByIndex(_LCM_CHARGING);
  5045. } else if (ac_chargingInfo[0]->SystemStatus == S_TERMINATING ||
  5046. ac_chargingInfo[0]->SystemStatus == S_COMPLETE) {
  5047. ChangeLcmByIndex(_LCM_COMPLETE);
  5048. } else {
  5049. dcPageRun = true;
  5050. }
  5051. } else {
  5052. dcPageRun = true;
  5053. }
  5054. if (dcPageRun) {
  5055. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  5056. }
  5057. }
  5058. write(wtdFd, "a", 1);
  5059. usleep(whileLoopTime);
  5060. }
  5061. return FAIL;
  5062. }