main.c 160 KB

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