main.c 193 KB

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