main.c 206 KB

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