main.c 192 KB

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