main.c 203 KB

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