main.c 192 KB

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