main.c 191 KB

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