main.c 222 KB

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