main.c 193 KB

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