main.c 198 KB

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