Module_Wifi.c 168 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866
  1. /*
  2. * Module_Wifi.c
  3. *
  4. * Created on: 2020-01-14
  5. * Update on: 2020-11-19
  6. * Author: Jerry Wang, Eason Yang, Folus Wen
  7. * Version: V0.14
  8. *
  9. * Issue tracking:
  10. * 1. Set AP mode should be waiting for 30 seconds.
  11. * 2. If use web site change Station mode to Access point mode,there are probability TCP 54089 will disppear.
  12. *
  13. *
  14. */
  15. #include <sys/time.h>
  16. #include <sys/timeb.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/socket.h>
  22. #include <sys/ipc.h>
  23. #include <sys/shm.h>
  24. #include <sys/shm.h>
  25. #include <sys/mman.h>
  26. #include <linux/wireless.h>
  27. #include <arpa/inet.h>
  28. #include <netinet/in.h>
  29. #include <unistd.h>
  30. #include <stdarg.h>
  31. #include <stdio.h> /*標準輸入輸出定義*/
  32. #include <stdlib.h> /*標準函數庫定義*/
  33. #include <unistd.h> /*Unix 標準函數定義*/
  34. #include <fcntl.h> /*檔控制定義*/
  35. #include <termios.h> /*PPSIX 終端控制定義*/
  36. #include <errno.h> /*錯誤號定義*/
  37. #include <errno.h>
  38. #include <string.h>
  39. #include <time.h>
  40. #include <ctype.h>
  41. #include <ifaddrs.h>
  42. #include <sqlite3.h>
  43. #include <json-c/json.h>
  44. #include <signal.h>
  45. #include "define.h"
  46. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  47. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  48. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  49. #define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
  50. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  51. #define PASS 1
  52. #define FAIL -1
  53. #define ON 1
  54. #define OFF 0
  55. #define YES 1
  56. #define NO 0
  57. #define true 1
  58. #define false 0
  59. #define IP_TYPE_DHCP 0
  60. #define IP_TYPE_STATIC 1
  61. #define AUTH_TYPE_NONE 0
  62. #define AUTH_TYPE_WEP 1
  63. #define AUTH_TYPE_WPAPSK 2
  64. #define AUTH_TYPE_WPA2PSK 3
  65. #define WIFI_MODE_DISABLE 0
  66. #define WIFI_MODE_STA 1
  67. #define WIFI_MODE_AP 2
  68. #define WIFI_MODE_ADHOC 3
  69. #define STA_STATE_INIT 0
  70. #define STA_STATE_DEVICE_DETECT 1
  71. #define STA_STATE_AP_CONNECTED 2
  72. #define STA_STATE_INTERNET_CHECKING 3
  73. #define AP_STATE_INIT 0
  74. #define AP_STATE_DEVICE_DETECT 1
  75. #define AP_STATE_AP_START 2
  76. #define AP_STATE_SERVER_LISTEN 3
  77. #define LISTEN_PORT_UDP 54088
  78. #define LISTEN_PORT_TCP 54089
  79. #define CONNECTION_LIMIT 3
  80. #define MtdBlockSize 0x600000
  81. #define REGISTER 0x01
  82. #define CLEAR_REGISTER 0x02
  83. #define UBLOX
  84. //#define MT7601U
  85. #define DEBUG
  86. #define DB_FILE_PUK "/Storage/ChargeLog/puk_pin_list.db"
  87. #define DB_FILE_CHARGING "/Storage/ChargeLog/localCgargingRecord.db"
  88. enum WIFI_COMMAND
  89. {
  90. WIFI_CMD_LOGIN_SIGNIN = 0x01,
  91. WIFI_CMD_LOGIN_AUTH_PUK = 0x02,
  92. WIFI_CMD_LOGIN_GET_USER_ID = 0x03,
  93. WIFI_CMD_PROTOCOL_VERSION = 0x04,
  94. WIFI_CMD_GET_MODEL_NAME = 0x0d,
  95. WIFI_CMD_GET_SERIAL_NUMBER = 0x0e,
  96. WIFI_CMD_GET_SYSTEM_TIME = 0x0f,
  97. WIFI_CMD_GET_WHITE_CARD = 0x12,
  98. WIFI_CMD_GET_SYS_ALARM_CODE = 0x15,
  99. WIFI_CMD_GET_SYS_ID = 0x16,
  100. WIFI_CMD_GET_RATING_CURRENT = 0x17,
  101. WIFI_CMD_GET_VER_RFS = 0x1e,
  102. WIFI_CMD_GET_VER_PRIMARY_CSU = 0x1f,
  103. WIFI_CMD_GET_VER_LCM = 0x20,
  104. WIFI_CMD_GET_PSU_COUNT = 0x21,
  105. WIFI_CMD_GET_VER_PSU = 0x22,
  106. WIFI_CMD_GET_VER_AUX_POWER = 0x23,
  107. WIFI_CMD_GET_VER_FAN = 0x24,
  108. WIFI_CMD_GET_VER_RELAY = 0x25,
  109. WIFI_CMD_GET_VER_TELECOMM = 0x26,
  110. WIFI_CMD_GET_EVSE_CONFIG = 0x30,
  111. WIFI_CMD_GET_GUN_STS = 0x32,
  112. WIFI_CMD_GET_GUN_USER = 0x33,
  113. WIFI_CMD_GET_SCHEDULE = 0x36,
  114. WIFI_CMD_GET_NETWORK_STS = 0x41,
  115. WIFI_CMD_GET_ETH_DHCP = 0x42,
  116. WIFI_CMD_GET_ETH_MAC = 0x44,
  117. WIFI_CMD_GET_ETH_IP = 0x45,
  118. WIFI_CMD_GET_ETH_SUBMASK = 0x47,
  119. WIFI_CMD_GET_ETH_GATEWAY = 0x49,
  120. WIFI_CMD_GET_WIFI_MODE = 0x54,
  121. WIFI_CMD_GET_WIFI_SSID = 0x56,
  122. WIFI_CMD_GET_WIFI_PASSWD = 0x58,
  123. WIFI_CMD_GET_WIFI_RSSI = 0x5a,
  124. WIFI_CMD_GET_WIFI_DHCP = 0x5b,
  125. WIFI_CMD_GET_WIFI_MAC = 0x5d,
  126. WIFI_CMD_GET_WIFI_IP = 0x5e,
  127. WIFI_CMD_GET_WIFI_NETMASK = 0x60,
  128. WIFI_CMD_GET_WIFI_GATEWAY = 0x62,
  129. WIFI_CMD_GET_TELE_APN = 0x6b,
  130. WIFI_CMD_GET_TELE_RSSI = 0x6d,
  131. WIFI_CMD_GET_TELE_CHAP_ID = 0x6e,
  132. WIFI_CMD_GET_TELE_CHAP_PASSWD = 0x70,
  133. WIFI_CMD_GET_TELE_IMEI = 0x72,
  134. WIFI_CMD_GET_TELE_IMSI = 0x73,
  135. WIFI_CMD_GET_TELE_SIM_STS = 0x74,
  136. WIFI_CMD_GET_TELE_MODE = 0x75,
  137. WIFI_CMD_GET_TELE_IP = 0x76,
  138. WIFI_CMD_SET_SYSTEM_TIME = 0x10,
  139. WIFI_CMD_SET_WHITE_CARD = 0x13,
  140. WIFI_CMD_SET_FACTORY_RESET = 0x14,
  141. WIFI_CMD_SET_EVSE_CONFIG = 0x31,
  142. WIFI_CMD_SET_CHARGING_START = 0x34,
  143. WIFI_CMD_SET_CHARGING_STOP = 0x35,
  144. WIFI_CMD_SET_SCHEDULE = 0x37,
  145. WIFI_CMD_SET_ETH_CONFIG = 0x43,
  146. WIFI_CMD_SET_ETH_IP = 0x46,
  147. WIFI_CMD_SET_ETH_SUBMASK = 0x48,
  148. WIFI_CMD_SET_ETH_GATEWAY = 0x4a,
  149. WIFI_CMD_SET_WIFI_MODE = 0x55,
  150. WIFI_CMD_SET_WIFI_SSID = 0x57,
  151. WIFI_CMD_SET_WIFI_PASSWD = 0x59,
  152. WIFI_CMD_SET_WIFI_DHCP = 0x5c,
  153. WIFI_CMD_SET_WIFI_IP = 0x5f,
  154. WIFI_CMD_SET_WIFI_NETMASK = 0x61,
  155. WIFI_CMD_SET_WIFI_GATEWAY = 0x63,
  156. WIFI_CMD_SET_TELE_APN = 0x6c,
  157. WIFI_CMD_SET_TELE_CHAP_ID = 0x6f,
  158. WIFI_CMD_SET_TELE_CHAP_PASSWD = 0x71,
  159. WIFI_CMD_SET_TELE_IP = 0x77,
  160. WIFI_CMD_SET_CONFIRM = 0x81,
  161. WIFI_CMD_SET_DISCARD = 0x82,
  162. WIFI_CMD_UNKNOW = 0xfe
  163. };
  164. enum LOGIN_ROLE
  165. {
  166. ROLE_UNKNOWN = 0x00,
  167. ROLE_MANAGER = 0x01,
  168. ROLE_USER = 0x02
  169. };
  170. struct SYS_FLAG
  171. {
  172. uint8_t isPkill;
  173. }Sys_Flag;
  174. struct interface_info
  175. {
  176. int ipType;
  177. int authType;
  178. int rssi;
  179. int cnt_InternetFail;
  180. char *currentInterface;
  181. char currentSSID[256];
  182. char currentPasswd[256];
  183. char currentMAC[24];
  184. char currentIPAddr[24];
  185. char currentNetmask[24];
  186. char currentGateway[24];
  187. char staticIPAddr[24];
  188. }Wifi;
  189. struct Display_Request
  190. {
  191. unsigned char isShowed_IF_info:1;
  192. unsigned char isShowed_IP_info:1;
  193. }dispReq;
  194. struct auth_info
  195. {
  196. int auth_type;
  197. unsigned char ssid[256];
  198. unsigned char passwd[256];
  199. }Wifi_A;
  200. struct WIFI_LOGIN_INFO
  201. {
  202. uint8_t puk[8];
  203. uint8_t loginRole;
  204. uint8_t loginId[32];
  205. }wifi_login_info;
  206. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  207. struct StatusCodeData *ShmStatusCodeData;
  208. struct OCPP16Data *ShmOCPP16Data;
  209. struct PsuData *ShmPsuData;
  210. struct SysConfigData SysConfigTemporarily;
  211. int isFindInterface();
  212. int getInterfaceInfo();
  213. int isReachableInternet();
  214. int getLinkQuality();
  215. int setWPAconf();
  216. int restartWPA();
  217. void refreshUSB();
  218. void getParameters();
  219. int Wifi_module_sts = STA_STATE_INIT;
  220. int isSetWPA_OK = false;
  221. int cnt_pingDNS_Fail;
  222. int cnt_getAP_Fail;
  223. int cnt_getIP_Fail;
  224. sqlite3 *localDb;
  225. void trim(char *s);
  226. int mystrcmp(char *p1,char *p2);
  227. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  228. void split(char **arr, char *str, const char *del);
  229. int isValidCheckSum(uint8_t *message);
  230. char *Support_InterfaceSTA[2] = {"mlan0", "wlan0"};
  231. char *Support_InterfaceAP[1] = {"uap0"};
  232. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  233. char *Version_And_Date[2] = {"V0.17","2021-03-15"};
  234. int protocol_Version [] = {0,7,0};
  235. int StoreLogMsg(const char *fmt, ...)
  236. {
  237. char Buf[4096+256];
  238. char buffer[4096];
  239. time_t CurrentTime;
  240. struct tm *tm;
  241. va_list args;
  242. va_start(args, fmt);
  243. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  244. va_end(args);
  245. memset(Buf,0,sizeof(Buf));
  246. CurrentTime = time(NULL);
  247. tm=localtime(&CurrentTime);
  248. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]Wifi_SystemLog",
  249. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  250. buffer,
  251. tm->tm_year+1900,tm->tm_mon+1);
  252. #ifdef SystemLogMessage
  253. system(Buf);
  254. #endif
  255. #ifdef ConsloePrintLog
  256. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
  257. #endif
  258. return rc;
  259. }
  260. int DiffTimeb(struct timeb ST, struct timeb ET)
  261. {
  262. //return milli-second
  263. unsigned int StartTime,StopTime;
  264. StartTime=(unsigned int)ST.time;
  265. StopTime=(unsigned int)ET.time;
  266. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  267. }
  268. void displayMessage(uint8_t *data, uint16_t len, uint8_t isRX)
  269. {
  270. #ifdef DEBUG
  271. uint8_t output[8192];
  272. if(isRX)
  273. {
  274. DEBUG_INFO("- RX --------------------------------------------\n");
  275. }
  276. else
  277. {
  278. DEBUG_INFO("- TX --------------------------------------------\n");
  279. }
  280. memset(output, 0x00, ARRAY_SIZE(output));
  281. for(uint16_t idx=0;idx<16;idx++)
  282. sprintf((char*)output, "%s %02X", output, idx);
  283. DEBUG_INFO("%s\n", output);
  284. DEBUG_INFO("-------------------------------------------------\n");
  285. for(uint16_t idx = 0;idx<len;idx++)
  286. {
  287. if((idx%16)>0)
  288. {
  289. sprintf((char*)output, "%s %02X", output, data[idx]);
  290. }
  291. else
  292. {
  293. if(idx != 0)
  294. DEBUG_INFO("%s\n", output);
  295. memset(output, 0x00, ARRAY_SIZE(output));
  296. sprintf((char*)output, "%s %02X", output, data[idx]);
  297. }
  298. }
  299. DEBUG_INFO("%s\n", output);
  300. DEBUG_INFO("-------------------------------------------------\n");
  301. #endif
  302. }
  303. //==========================================
  304. // Init all share memory
  305. //==========================================
  306. int InitShareMemory()
  307. {
  308. int result = PASS;
  309. int MeterSMId;
  310. //creat ShmSysConfigAndInfo
  311. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  312. {
  313. #ifdef SystemLogMessage
  314. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  315. #endif
  316. result = FAIL;
  317. }
  318. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  319. {
  320. #ifdef SystemLogMessage
  321. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  322. #endif
  323. result = FAIL;
  324. }
  325. else
  326. {}
  327. //creat ShmStatusCodeData
  328. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  329. {
  330. #ifdef SystemLogMessage
  331. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  332. #endif
  333. result = FAIL;
  334. }
  335. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  336. {
  337. #ifdef SystemLogMessage
  338. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  339. #endif
  340. result = FAIL;
  341. }
  342. else
  343. {}
  344. //creat ShmOCPP16Data
  345. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  346. {
  347. #ifdef SystemLogMessage
  348. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  349. #endif
  350. result = FAIL;
  351. }
  352. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  353. {
  354. #ifdef SystemLogMessage
  355. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  356. #endif
  357. result = FAIL;
  358. }
  359. else
  360. {}
  361. //creat ShmPsuData
  362. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  363. {
  364. #ifdef SystemLogMessage
  365. DEBUG_ERROR("shmget ShmPsuData NG \n");
  366. #endif
  367. result = FAIL;
  368. }
  369. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  370. {
  371. #ifdef SystemLogMessage
  372. DEBUG_ERROR("shmat ShmPsuData NG \n");
  373. #endif
  374. result = FAIL;
  375. }
  376. return result;
  377. }
  378. //==========================================
  379. // Get parameters from shared memory
  380. //==========================================
  381. void getParameters(void)
  382. {
  383. DEBUG_INFO("============= Configuration ===============\n");
  384. strcpy((char*)Wifi_A.ssid, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  385. DEBUG_INFO("Wifi SSID: %s\n", Wifi_A.ssid);
  386. strcpy((char*)Wifi_A.passwd, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  387. DEBUG_INFO("Wifi Password: %s\n", Wifi_A.passwd);
  388. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress != NULL)
  389. strcpy((char*)Wifi.staticIPAddr, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  390. DEBUG_INFO("Wifi Static IP: %s\n", Wifi.staticIPAddr);
  391. Wifi.ipType = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient;
  392. DEBUG_INFO("DHCP client flag: %d\n", Wifi.ipType);
  393. DEBUG_INFO("===========================================\n");
  394. }
  395. //==========================================
  396. // Refresh USB interface
  397. //==========================================
  398. void refreshUSB(void)
  399. {
  400. FILE *fp;
  401. char cmd[256];
  402. // Get IP address & net mask
  403. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/unbind");
  404. fp = popen(cmd, "r");
  405. sleep(2);
  406. pclose(fp);
  407. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/bind");
  408. fp = popen(cmd, "r");
  409. sleep(5);
  410. pclose(fp);
  411. }
  412. //==========================================
  413. // Check wifi interface status
  414. //==========================================
  415. int isFindInterface()
  416. {
  417. int result = FAIL;
  418. struct ifaddrs *ifaddr, *ifa;
  419. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == WIFI_MODE_AP)
  420. {
  421. if (getifaddrs(&ifaddr) != FAIL)
  422. {
  423. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  424. {
  425. if (ifa->ifa_addr == NULL ||
  426. ifa->ifa_addr->sa_family != AF_PACKET) continue;
  427. for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceAP);idx++)
  428. {
  429. if(mystrcmp(ifa->ifa_name, Support_InterfaceAP[idx]) == PASS)
  430. {
  431. Wifi.currentInterface = ifa->ifa_name;
  432. result = PASS;
  433. }
  434. }
  435. }
  436. freeifaddrs(ifaddr);
  437. freeifaddrs(ifa);
  438. }
  439. }
  440. else
  441. {
  442. if (getifaddrs(&ifaddr) != FAIL)
  443. {
  444. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  445. {
  446. if (ifa->ifa_addr == NULL ||
  447. ifa->ifa_addr->sa_family != AF_PACKET) continue;
  448. for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceSTA);idx++)
  449. {
  450. if(mystrcmp(ifa->ifa_name, Support_InterfaceSTA[idx]) == PASS)
  451. {
  452. Wifi.currentInterface = ifa->ifa_name;
  453. result = PASS;
  454. }
  455. }
  456. }
  457. freeifaddrs(ifaddr);
  458. freeifaddrs(ifa);
  459. }
  460. }
  461. return result;
  462. }
  463. //==========================================
  464. // Get wifi interface info
  465. //==========================================
  466. int getInterfaceInfo()
  467. {
  468. int result = PASS;
  469. FILE *fp;
  470. char cmd[256];
  471. char buf[512];
  472. char tmp[512];
  473. // Get IP address & net mask
  474. strcpy(cmd, "ifconfig ");
  475. strcat(cmd, Wifi.currentInterface);
  476. fp = popen(cmd, "r");
  477. if(fp == NULL)
  478. result = FAIL;
  479. else
  480. {
  481. while(fgets(buf, sizeof(buf), fp) != NULL)
  482. {
  483. if(strstr(buf, "HWaddr") > 0)
  484. {
  485. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  486. strcpy(Wifi.currentMAC, tmp);
  487. }
  488. if(strstr(buf, "inet addr:") > 0)
  489. {
  490. sscanf(buf, "%*s%s", tmp);
  491. substr(Wifi.currentIPAddr, tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  492. sscanf(buf, "%*s%*s%*s%s", tmp);
  493. substr(Wifi.currentNetmask, tmp, strspn(tmp, "Mask:"), strlen(tmp)-strspn(tmp, "Mask:"));
  494. }
  495. }
  496. }
  497. pclose(fp);
  498. // Get gateway
  499. fp = popen("ip route", "r");
  500. if(fp == NULL)
  501. result = FAIL;
  502. else
  503. {
  504. while(fgets(buf, sizeof(buf), fp) != NULL)
  505. {
  506. #ifdef UBLOX
  507. if((strstr(buf, "default") != NULL) && (strstr(buf, "mlan0") != NULL))
  508. #endif
  509. #ifdef MT7601U
  510. if((strstr(buf, "default") != NULL) && (strstr(buf, "wlan0") != NULL))
  511. #endif
  512. break;
  513. }
  514. if(strstr(buf, "default") != NULL)
  515. {
  516. sscanf(buf, "%*s%*s%s", tmp);
  517. substr(Wifi.currentGateway, tmp, 0, strlen(tmp));
  518. }
  519. }
  520. pclose(fp);
  521. if(!dispReq.isShowed_IF_info)
  522. {
  523. DEBUG_INFO("MAC: %s\n", Wifi.currentMAC);
  524. dispReq.isShowed_IF_info = true;
  525. }
  526. if(!dispReq.isShowed_IP_info)
  527. {
  528. DEBUG_INFO("IP address: %s\n", Wifi.currentIPAddr);
  529. DEBUG_INFO("Net mask: %s\n", Wifi.currentNetmask);
  530. DEBUG_INFO("Default gateway: %s\n", Wifi.currentGateway);
  531. dispReq.isShowed_IP_info = true;
  532. }
  533. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, (const char*)Wifi.currentIPAddr);
  534. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, (const char*)Wifi.currentNetmask);
  535. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, (const char*)Wifi.currentGateway);
  536. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, (const char*)Wifi.currentMAC);
  537. return result;
  538. }
  539. //==========================================
  540. // Check if module connect to wifi AP
  541. //==========================================
  542. int isConnectAP()
  543. {
  544. int result = PASS;
  545. FILE *fp;
  546. char cmd[256];
  547. char buf[512];
  548. // Get IP address & net mask
  549. strcpy(cmd, "iwconfig ");
  550. strcat(cmd, Wifi.currentInterface);
  551. fp = popen(cmd, "r");
  552. if(fp == NULL)
  553. result = FAIL;
  554. else
  555. {
  556. while(fgets(buf, sizeof(buf), fp) != NULL)
  557. {
  558. if(strstr(buf, "Not-Associated") > 0)
  559. {
  560. result = FAIL;
  561. DEBUG_INFO("AP check: Not-Associated.\n");
  562. }
  563. }
  564. }
  565. pclose(fp);
  566. return result;
  567. }
  568. //==========================================
  569. // Check internet access status
  570. //==========================================
  571. int isReachableInternet()
  572. {
  573. int result = FAIL;
  574. FILE *fp;
  575. char cmd[256];
  576. char buf[512];
  577. //char tmp[512];
  578. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  579. {
  580. sprintf(cmd, "ping -c 1 -w 3 -I %s %s", Wifi.currentInterface, valid_Internet[idx]);
  581. fp = popen(cmd, "r");
  582. if(fp != NULL)
  583. {
  584. while(fgets(buf, sizeof(buf), fp) != NULL)
  585. {
  586. if(strstr(buf, "transmitted") > 0)
  587. {
  588. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  589. if(strstr(buf,"100%") != NULL)
  590. {
  591. }
  592. else
  593. {
  594. result = PASS;
  595. }
  596. //DEBUG_INFO("%s",buf);
  597. //DEBUG_INFO("%s\n",tmp);
  598. }
  599. }
  600. }
  601. pclose(fp);
  602. }
  603. return result;
  604. }
  605. //==========================================
  606. // Check module soft AP is start up
  607. //==========================================
  608. int isStartUpAP()
  609. {
  610. int result = FAIL;
  611. FILE *fp;
  612. char cmd[256];
  613. char buf[512];
  614. // Get IP address & net mask
  615. strcpy(cmd, "/root/uaputl sys_cfg_bss_status");
  616. fp = popen(cmd, "r");
  617. if(fp != NULL)
  618. {
  619. while(fgets(buf, sizeof(buf), fp) != NULL)
  620. {
  621. if(strstr(buf, "BSS status = started") > 0)
  622. {
  623. result = PASS;
  624. DEBUG_INFO("AP already start up.\n");
  625. }
  626. }
  627. }
  628. pclose(fp);
  629. return result;
  630. }
  631. //==========================================
  632. // Get wifi connection quality
  633. //==========================================
  634. int getLinkQuality()
  635. {
  636. int result = FAIL;
  637. FILE *fp;
  638. char cmd[256];
  639. char buf[512];
  640. char tmp[512];
  641. strcpy(cmd, "iwconfig ");
  642. strcat(cmd, Wifi.currentInterface);
  643. fp = popen(cmd, "r");
  644. if(fp == NULL)
  645. result = FAIL;
  646. else
  647. {
  648. while(fgets(buf, sizeof(buf), fp) != NULL)
  649. {
  650. if(strstr(buf, "Signal level") > 0)
  651. {
  652. sscanf(buf, "%*s%*s%*s%s", tmp);
  653. substr(tmp, tmp, strspn(tmp, "Signal level="), strlen(buf)-strspn(tmp, "Signal level="));
  654. Wifi.rssi = atoi(tmp);
  655. result = PASS;
  656. }
  657. }
  658. }
  659. pclose(fp);
  660. return result;
  661. }
  662. //==========================================
  663. // WPA parameter set
  664. //==========================================
  665. int setWPAconf()
  666. {
  667. int result = FAIL;
  668. char cmdBuf[512];
  669. FILE *pFile;
  670. char buffer[500]="ctrl_interface=/var/run/wpa_supplicant\nupdate_config=1\n";
  671. // Stop dhcp client or server
  672. sprintf(cmdBuf, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  673. system(cmdBuf);
  674. sprintf(cmdBuf, "pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  675. system(cmdBuf);
  676. // Clear wifi configuration file
  677. system("cat /dev/null > /etc/wpa.conf");
  678. system("cat /dev/null > /etc/hostapd.conf");
  679. switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
  680. {
  681. case WIFI_MODE_STA:
  682. // Fill out configuration value depend on share memory setting
  683. if(strlen((const char*)Wifi_A.passwd)>0)
  684. {
  685. if(strlen((const char*)Wifi_A.passwd)>=8)
  686. {
  687. strcat(buffer, "network={\n ssid=\"");
  688. strcat(buffer, (char*)Wifi_A.ssid);
  689. strcat(buffer, "\"\n key_mgmt=WPA-EAP WPA-PSK \n");
  690. strcat(buffer, " bgscan=\"simple:10:-60:60\" \n");
  691. strcat(buffer, " bgscan=\"learn:10:-60:60:/etc/wpa.bgscan\"\n");
  692. strcat(buffer, " psk=\"");
  693. strcat(buffer, (char*)Wifi_A.passwd);
  694. strcat(buffer, "\"\n}\n\n");
  695. }
  696. if(strlen((const char*)Wifi_A.passwd)==5 || strlen((const char*)Wifi_A.passwd)==13)
  697. {
  698. strcat(buffer, "network={\n ssid=\"");
  699. strcat(buffer, (char*)Wifi_A.ssid);
  700. strcat(buffer, "\"\n key_mgmt=NONE\n");
  701. strcat(buffer, " wep_key0=\"");
  702. strcat(buffer, (char*)Wifi_A.passwd);
  703. strcat(buffer, "\"\n auth_alg=OPEN SHARED\n");
  704. strcat(buffer, " bgscan=\"simple:10:-60:60\"\n");
  705. strcat(buffer, " bgscan=\"learn:10:-60:60:/etc/wpa.bgscan\"");
  706. strcat(buffer, "\n}\n\n");
  707. }
  708. strcat(buffer, "network={\n ssid=\"");
  709. strcat(buffer, (char*)Wifi_A.ssid);
  710. strcat(buffer, "\"\n key_mgmt=NONE");
  711. strcat(buffer, "\n bgscan=\"simple:10:-60:60\"");
  712. strcat(buffer, "\n bgscan=\"learn:10:-60:60:/etc/wpa.bgscan\"");
  713. strcat(buffer, "\n}");
  714. }
  715. else
  716. {
  717. strcat(buffer, "network={\n ssid=\"");
  718. strcat(buffer, (char*)Wifi_A.ssid);
  719. strcat(buffer, "\"\n key_mgmt=NONE");
  720. strcat(buffer, "\n bgscan=\"simple:10:-60:60\"");
  721. strcat(buffer, "\n bgscan=\"learn:10:-60:60:/etc/wpa.bgscan\"");
  722. strcat(buffer, "\n}");
  723. }
  724. // Configuration value write to /etc/wpa.conf
  725. pFile = fopen("/etc/wpa.conf","w");
  726. fwrite(buffer,strlen(buffer), 1, pFile);
  727. fclose(pFile);
  728. // Restart wpa_supplicant
  729. sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
  730. system(cmdBuf);
  731. #ifdef UBLOX
  732. sprintf(cmdBuf, "mlanutl mlan0 hostcmd /root/u-blox_tools/ed_mac_ctrl_V1_8801.conf ed_mac_ctrl");
  733. system(cmdBuf);
  734. sprintf(cmdBuf, "mlanutl mlan0 regrdwr 2 0x70");
  735. system(cmdBuf);
  736. sprintf(cmdBuf, "mlanutl mlan0 deepsleep 0");
  737. system(cmdBuf);
  738. sprintf(cmdBuf, "mlanutl mlan0 psmode 0");
  739. system(cmdBuf);
  740. #endif
  741. sprintf(cmdBuf, "/root/wpa_supplicant -i %s -c /etc/wpa.conf -B", Wifi.currentInterface);
  742. system(cmdBuf);
  743. // Stop wifi AP
  744. sprintf(cmdBuf, "pgrep -f \"hostapd\" | xargs kill");
  745. system(cmdBuf);
  746. sleep(5);
  747. // Wifi IP set by DHCP client or static
  748. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
  749. {
  750. sprintf(cmdBuf, "/sbin/udhcpc -i %s -x hostname:CSU3_%s -s /root/dhcp_script/wifi.script > /dev/null &", Wifi.currentInterface, ShmSysConfigAndInfo->SysConfig.SystemId);
  751. system(cmdBuf);
  752. }
  753. else
  754. {
  755. memset(cmdBuf, 0, ARRAY_SIZE(cmdBuf));
  756. sprintf(cmdBuf,"/sbin/ifconfig %s %s netmask %s",
  757. Wifi.currentInterface,
  758. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress,
  759. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  760. system(cmdBuf);
  761. memset(cmdBuf, 0, ARRAY_SIZE(cmdBuf));
  762. sprintf(cmdBuf,"route add default gw %s %s ",
  763. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress,
  764. Wifi.currentInterface);
  765. system(cmdBuf);
  766. DEBUG_INFO("Setting %s static IP to %s submask to %s gateway to %s...\n",
  767. Wifi.currentInterface,
  768. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress,
  769. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress,
  770. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  771. }
  772. break;
  773. case WIFI_MODE_AP:
  774. // Stop dhcp client or server
  775. sprintf(cmdBuf, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  776. system(cmdBuf);
  777. sprintf(cmdBuf, "pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  778. system(cmdBuf);
  779. // Restart wpa_supplicant
  780. sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
  781. system(cmdBuf);
  782. sprintf(cmdBuf, "pgrep -f \"hostapd\" | xargs kill");
  783. system(cmdBuf);
  784. sprintf(cmdBuf, "echo interface=uap0 >> /etc/hostapd.conf");
  785. system(cmdBuf);
  786. sprintf(cmdBuf, "echo driver=nl80211 >> /etc/hostapd.conf");
  787. system(cmdBuf);
  788. sprintf(cmdBuf, "echo ssid=%s%s >> /etc/hostapd.conf", ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber);
  789. system(cmdBuf);
  790. sprintf(cmdBuf, "echo channel=6 >> /etc/hostapd.conf");
  791. system(cmdBuf);
  792. sprintf(cmdBuf, "echo wpa=2 >> /etc/hostapd.conf");
  793. system(cmdBuf);
  794. sprintf(cmdBuf, "echo wpa_passphrase=%s%s >> /etc/hostapd.conf", ShmSysConfigAndInfo->SysConfig.SerialNumber, ShmSysConfigAndInfo->SysConfig.ModelName);
  795. system(cmdBuf);
  796. sprintf(cmdBuf, "echo wpa_key_mgmt=WPA-PSK >> /etc/hostapd.conf");
  797. system(cmdBuf);
  798. sprintf(cmdBuf, "echo wpa_pairwise=CCMP >> /etc/hostapd.conf");
  799. system(cmdBuf);
  800. sprintf(cmdBuf, "hostapd /etc/hostapd.conf -B");
  801. system(cmdBuf);
  802. sleep(5);
  803. // Set wifi AP ip address
  804. sprintf(cmdBuf, "ifconfig %s 192.168.10.10", Wifi.currentInterface);
  805. system(cmdBuf);
  806. DEBUG_INFO("Setting %s static IP to 192.168.10.10...\n", Wifi.currentInterface);
  807. // Start DHCP server
  808. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpServer==0)
  809. {
  810. sprintf(cmdBuf, "/usr/sbin/udhcpd /etc/udhcpd.conf > /dev/null &");
  811. system(cmdBuf);
  812. }
  813. break;
  814. case WIFI_MODE_ADHOC:
  815. break;
  816. case WIFI_MODE_DISABLE:
  817. default:
  818. break;
  819. }
  820. result = PASS;
  821. return result;
  822. }
  823. //==========================================
  824. // UDP socket server routine
  825. //==========================================
  826. int split2int(char *dest, char *src, const char *flag, int base)
  827. {
  828. uint8_t result = 0;
  829. char *s = strtok(src, flag);
  830. while(s != NULL)
  831. {
  832. *dest++ = (base==10?atoi(s):strtol(s, NULL, 16));
  833. s = strtok(NULL, flag);
  834. result++;
  835. }
  836. return result;
  837. }
  838. uint8_t parseIP(uint8_t *ip)
  839. {
  840. uint8_t result = false;
  841. const char *flag_dot = ".";
  842. int8_t buf[16];
  843. memcpy(buf, ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, ARRAY_SIZE(buf));
  844. if(split2int((char*)ip , (char*)buf, flag_dot, 10)==4)
  845. result = PASS;
  846. return result;
  847. }
  848. uint8_t parseMAC(uint8_t *mac)
  849. {
  850. uint8_t result = false;
  851. const char *flag_colon = ":";
  852. int8_t buf[18];
  853. memcpy(buf, ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, ARRAY_SIZE(buf));
  854. if(split2int((char*)mac , (char*)buf, flag_colon, 16)==6)
  855. result = PASS;
  856. return result;
  857. }
  858. int udpSocketServerStart(void)
  859. {
  860. int sockFd;
  861. struct sockaddr_in servaddr;
  862. struct sockaddr_in peeraddr;
  863. socklen_t peerlen = sizeof(peeraddr);
  864. uint8_t inputBuffer[2048] = {};
  865. uint8_t outBuffer[2048] = {};
  866. int16_t read_size;
  867. int16_t tx_size;
  868. uint8_t broadcastCmd[6] = {0xff, 0xff, 0x00, 0x02, 0x00, 0x02};
  869. uint8_t validResult;
  870. uint8_t chksum;
  871. uint8_t ip[4];
  872. uint8_t mac[6];
  873. memset(&servaddr, 0, sizeof(servaddr));
  874. servaddr.sin_family = AF_INET;
  875. servaddr.sin_port = htons(LISTEN_PORT_UDP);
  876. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  877. if ((sockFd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  878. {
  879. DEBUG_ERROR("UDP server socket create fail.\n");
  880. return FAIL;
  881. }
  882. if (bind(sockFd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
  883. {
  884. DEBUG_ERROR("UDP server socket bind fail.\n");
  885. return FAIL;
  886. }
  887. else
  888. DEBUG_INFO("UDP server initial.\n");
  889. for(;;)
  890. {
  891. if((read_size = recvfrom(sockFd, inputBuffer, sizeof(inputBuffer), 0, (struct sockaddr *)&peeraddr, &peerlen)) > 0)
  892. {
  893. DEBUG_INFO("Revieve from: %s:%d\n", inet_ntoa(peeraddr.sin_addr), htons(peeraddr.sin_port));
  894. DEBUG_INFO("read_size: %d\n",read_size);
  895. displayMessage(inputBuffer, read_size, YES);
  896. if(read_size>=6)
  897. {
  898. validResult = PASS;
  899. for(uint8_t idx=0;idx<6;idx++)
  900. {
  901. if(inputBuffer[idx] != broadcastCmd[idx])
  902. {
  903. validResult = FAIL;
  904. break;
  905. }
  906. }
  907. if(validResult)
  908. {
  909. DEBUG_INFO("Receive UDP broadcast command.\n");
  910. DEBUG_INFO("GET IP WifiIpAddress : %s.\n",ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  911. memset(outBuffer, 0x00, ARRAY_SIZE(outBuffer));
  912. tx_size = 41;
  913. outBuffer[0] = 0xff;
  914. outBuffer[1] = 0xff;
  915. outBuffer[2] = (0x25 << 0x08) & 0xff;
  916. outBuffer[3] = 0x25 & 0xff;
  917. outBuffer[4] = 0x00;
  918. if(parseIP(ip)==PASS)
  919. {
  920. outBuffer[5] = ip[0];
  921. outBuffer[6] = ip[1];
  922. outBuffer[7] = ip[2];
  923. outBuffer[8] = ip[3];
  924. }
  925. else
  926. {
  927. outBuffer[5] = 0x00;
  928. outBuffer[6] = 0x00;
  929. outBuffer[7] = 0x00;
  930. outBuffer[8] = 0x00;
  931. }
  932. if(parseMAC(mac)==PASS)
  933. {
  934. outBuffer[9] = mac[0];
  935. outBuffer[10] = mac[1];
  936. outBuffer[11] = mac[2];
  937. outBuffer[12] = mac[3];
  938. outBuffer[13] = mac[4];
  939. outBuffer[14] = mac[5];
  940. }
  941. else
  942. {
  943. outBuffer[9] = 0x00;
  944. outBuffer[10] = 0x00;
  945. outBuffer[11] = 0x00;
  946. outBuffer[12] = 0x00;
  947. outBuffer[13] = 0x00;
  948. outBuffer[14] = 0x00;
  949. }
  950. memcpy(&outBuffer[15], ShmSysConfigAndInfo->SysConfig.ModelName, 0x0e);
  951. memcpy(&outBuffer[29], ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x0b);
  952. chksum = 0x00;
  953. for(uint8_t idx=0;idx<36;idx++)
  954. {
  955. chksum ^= outBuffer[idx];
  956. }
  957. outBuffer[40] = chksum;
  958. displayMessage(outBuffer, tx_size, NO);
  959. sendto(sockFd, outBuffer, tx_size, 0, (struct sockaddr *)&peeraddr, peerlen);
  960. }
  961. }
  962. }
  963. }
  964. return FAIL;
  965. }
  966. //===============================================
  967. // SQLite3 related routine
  968. //===============================================
  969. int DB_Open(sqlite3 *db)
  970. {
  971. int result = PASS;
  972. char* errMsg = NULL;
  973. char* createSql="CREATE TABLE IF NOT EXISTS list("
  974. "idx integer primary key AUTOINCREMENT, "
  975. "list_type text, "
  976. "code text, "
  977. "user_id text, "
  978. "isReg text"
  979. ");";
  980. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  981. if(sqlite3_open(DB_FILE_PUK, &db))
  982. {
  983. result = FAIL;
  984. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  985. sqlite3_close(db);
  986. }
  987. else
  988. {
  989. DEBUG_INFO( "Local puk/pin list database open successfully.\n");
  990. if (sqlite3_exec(db, createSql, 0, 0, &errMsg) != SQLITE_OK)
  991. {
  992. result = FAIL;
  993. DEBUG_INFO( "Create puk/pin list table error message: %s\n", errMsg);
  994. }
  995. else
  996. {
  997. DEBUG_INFO( "Opened puk/pin list table successfully\n");
  998. }
  999. sqlite3_close(db);
  1000. }
  1001. return result;
  1002. }
  1003. uint8_t isPukReg(sqlite3 *db)
  1004. {
  1005. uint8_t result = false;
  1006. char* errMsg = NULL;
  1007. char sqlStr[1024];
  1008. char **rs;
  1009. int rows, cols;
  1010. sprintf(sqlStr, "select * from list where list_type='puk';");
  1011. DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1012. if(sqlite3_open(DB_FILE_PUK, &db))
  1013. {
  1014. result = FAIL;
  1015. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1016. sqlite3_close(db);
  1017. }
  1018. else
  1019. {
  1020. DEBUG_INFO( "Local puk/pin list database open successfully.\n");
  1021. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1022. if(rows>0)
  1023. result = true;
  1024. sqlite3_free_table(rs);
  1025. sqlite3_close(db);
  1026. }
  1027. return result;
  1028. }
  1029. uint8_t PukReg(sqlite3 *db)
  1030. {
  1031. uint8_t result = false;
  1032. char* errMsg = NULL;
  1033. char sqlStr[1024];
  1034. sprintf(sqlStr, "insert into list(list_type, code, user_id, isReg) "
  1035. "values('puk', '%s', '%s', '1');",
  1036. wifi_login_info.puk,
  1037. wifi_login_info.loginId);
  1038. DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1039. if(sqlite3_open(DB_FILE_PUK, &db))
  1040. {
  1041. result = FAIL;
  1042. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1043. sqlite3_close(db);
  1044. }
  1045. else
  1046. {
  1047. DEBUG_INFO( "Local puk/pin list database open successfully.\n");
  1048. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1049. {
  1050. result = FAIL;
  1051. DEBUG_INFO( "Insert puk/pin list error message: %s\n", errMsg);
  1052. }
  1053. else
  1054. {
  1055. DEBUG_INFO( "Insert puk/pin list successfully\n");
  1056. }
  1057. sqlite3_close(db);
  1058. }
  1059. return result;
  1060. }
  1061. uint8_t isPinGen(sqlite3 *db)
  1062. {
  1063. uint8_t result = false;
  1064. char* errMsg = NULL;
  1065. char sqlStr[1024];
  1066. char **rs;
  1067. int rows, cols;
  1068. sprintf(sqlStr, "select * from list where list_type='pin';");
  1069. DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1070. if(sqlite3_open(DB_FILE_PUK, &db))
  1071. {
  1072. result = FAIL;
  1073. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1074. sqlite3_close(db);
  1075. }
  1076. else
  1077. {
  1078. DEBUG_INFO( "Local puk/pin list database open successfully.\n");
  1079. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1080. if(rows>0)
  1081. result = true;
  1082. sqlite3_free_table(rs);
  1083. sqlite3_close(db);
  1084. }
  1085. return result;
  1086. }
  1087. uint8_t PinGen(sqlite3 *db)
  1088. {
  1089. uint8_t result = false;
  1090. char* errMsg = NULL;
  1091. char sqlStr[1024];
  1092. srand(time(NULL));
  1093. if(sqlite3_open(DB_FILE_PUK, &db))
  1094. {
  1095. result = FAIL;
  1096. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1097. sqlite3_close(db);
  1098. }
  1099. else
  1100. {
  1101. DEBUG_INFO( "Local puk/pin list database open successfully.\n");
  1102. sprintf(sqlStr, "delete from list where list_type='pin';");
  1103. DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1104. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1105. {
  1106. result = FAIL;
  1107. DEBUG_INFO( "Delete puk/pin list error message: %s\n", errMsg);
  1108. }
  1109. else
  1110. {
  1111. for(int idxList=0;idxList<20;idxList++)
  1112. {
  1113. sprintf(sqlStr, "insert into list(list_type, code, user_id, isReg) "
  1114. "values('pin', '%06X', '', '0');", (rand() % 0xffffff));
  1115. DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1116. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1117. {
  1118. result = FAIL;
  1119. DEBUG_INFO( "Insert puk/pin list error message: %s\n", errMsg);
  1120. }
  1121. else
  1122. {
  1123. DEBUG_INFO( "Insert puk/pin list successfully\n");
  1124. }
  1125. }
  1126. }
  1127. sqlite3_close(db);
  1128. }
  1129. return result;
  1130. }
  1131. uint8_t isValidPin(sqlite3 *db, uint8_t *pinCode)
  1132. {
  1133. uint8_t result = false;
  1134. char* errMsg = NULL;
  1135. char sqlStr[1024];
  1136. char **rs;
  1137. int rows, cols;
  1138. sprintf(sqlStr, "select * from list where list_type='pin' and code='%s';", pinCode);
  1139. DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1140. if(sqlite3_open(DB_FILE_PUK, &db))
  1141. {
  1142. result = FAIL;
  1143. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1144. sqlite3_close(db);
  1145. }
  1146. else
  1147. {
  1148. DEBUG_INFO( "Local puk/pin list database open successfully.\n");
  1149. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1150. if(rows>0)
  1151. result = true;
  1152. sqlite3_free_table(rs);
  1153. sqlite3_close(db);
  1154. }
  1155. return result;
  1156. }
  1157. uint8_t loginValid(sqlite3 *db)
  1158. {
  1159. uint8_t result = ROLE_UNKNOWN;
  1160. char* errMsg = NULL;
  1161. char sqlStr[1024];
  1162. char **rs;
  1163. int rows, cols;
  1164. sprintf(sqlStr, "select * from list where user_id='%s';", wifi_login_info.loginId);
  1165. DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1166. DEBUG_INFO("Login id: %s\n", wifi_login_info.loginId);
  1167. if(sqlite3_open(DB_FILE_PUK, &db))
  1168. {
  1169. result = FAIL;
  1170. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1171. sqlite3_close(db);
  1172. }
  1173. else
  1174. {
  1175. DEBUG_INFO( "Local puk/pin list database open successfully.\n");
  1176. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1177. for(int idxRow=0;idxRow<rows;idxRow++)
  1178. {
  1179. if(strcmp(rs[(idxRow*cols)+1], "puk") == 0)
  1180. {
  1181. result = ROLE_MANAGER;
  1182. DEBUG_INFO("Login role: Manager\n");
  1183. }
  1184. else if(strcmp(rs[(idxRow*cols)+1], "pin") == 0)
  1185. {
  1186. result = ROLE_USER;
  1187. DEBUG_INFO("Login role: User\n");
  1188. }
  1189. }
  1190. sqlite3_free_table(rs);
  1191. sqlite3_close(db);
  1192. }
  1193. return result;
  1194. }
  1195. int cbScheduleSave(void *para, int columnCount, char **columnValue, char **columnName)
  1196. {
  1197. DEBUG_INFO("Config schedule callback.\n");
  1198. for(int i=0; i<columnCount; i++)
  1199. {
  1200. DEBUG_INFO("%s = %s\n", columnName[i], columnValue[i] ? columnValue[i] : "NULL");
  1201. }
  1202. return 0;
  1203. }
  1204. int scheduleSaveToDb(sqlite3 *db, uint8_t gun_index, struct Schedule *schedule)
  1205. {
  1206. int result = PASS;
  1207. char* errMsg = NULL;
  1208. char sqlStr[512];
  1209. json_object *scheduleObj = json_object_new_object();
  1210. // Save config to local db file
  1211. json_object_object_add(scheduleObj, "isEnable", json_object_new_int(schedule->isEnable));
  1212. json_object_object_add(scheduleObj, "scheduleType", json_object_new_int(schedule->scheduleType));
  1213. json_object_object_add(scheduleObj, "scheduleMethod", json_object_new_int(schedule->scheduleMethod));
  1214. json_object_object_add(scheduleObj, "startTimeHour", json_object_new_int(schedule->startTimeHour));
  1215. json_object_object_add(scheduleObj, "startTimeMinute", json_object_new_int(schedule->startTimeMinute));
  1216. json_object_object_add(scheduleObj, "stopTimeHour", json_object_new_int(schedule->stopTimeHour));
  1217. json_object_object_add(scheduleObj, "stopTimeMinute", json_object_new_int(schedule->stopTimeMinute));
  1218. sprintf((char*)sqlStr, "insert or replace into config (item, connector, val) VALUES('schedule', %d, '%s'); SELECT * from config;", gun_index, json_object_to_json_string(scheduleObj));
  1219. //* Execute SQL statement */
  1220. if(sqlite3_open(DB_FILE_CHARGING, &db))
  1221. {
  1222. result = FAIL;
  1223. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1224. sqlite3_close(db);
  1225. }
  1226. else
  1227. {
  1228. if(sqlite3_exec(db, (char*)sqlStr, cbScheduleSave, 0, &errMsg) != SQLITE_OK)
  1229. {
  1230. DEBUG_INFO("SQL error: %s\n", errMsg);
  1231. result = FAIL;
  1232. }
  1233. sqlite3_close(db);
  1234. }
  1235. return result;
  1236. }
  1237. int cbScheduleLoad(void *para, int columnCount, char **columnValue, char **columnName)
  1238. {
  1239. uint8_t gun_index;
  1240. uint8_t idxAC = 0;
  1241. uint8_t idxCCS = 0;
  1242. uint8_t idxCHAdeMO = 0;
  1243. uint8_t idxGBT = 0;
  1244. uint8_t AC_GUN_IDX = 0;
  1245. json_object *scheduleObj = json_tokener_parse(columnValue[3]);
  1246. DEBUG_INFO("Query schedule callback.\n");
  1247. for(int i=0; i<columnCount; i++)
  1248. {
  1249. DEBUG_INFO("%s = %s\n", columnName[i], columnValue[i] ? columnValue[i] : "NULL");
  1250. }
  1251. if((columnCount>0) && !is_error(scheduleObj))
  1252. {
  1253. gun_index = atoi(columnValue[2]);
  1254. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1255. {
  1256. if(gun_index == 2)
  1257. {
  1258. AC_GUN_IDX = 0;
  1259. }
  1260. else if(gun_index == 1)
  1261. {
  1262. AC_GUN_IDX = 1;
  1263. }
  1264. else if(gun_index == 0)
  1265. {
  1266. AC_GUN_IDX = 2;
  1267. }
  1268. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1269. {
  1270. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1271. {
  1272. case '1' ... '6':
  1273. idxAC++;
  1274. break;
  1275. }
  1276. }
  1277. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1278. {
  1279. case '1' ... '6':
  1280. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable = json_object_get_int(json_object_object_get(scheduleObj, "isEnable"));
  1281. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleType = json_object_get_int(json_object_object_get(scheduleObj, "scheduleType"));
  1282. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleMethod = json_object_get_int(json_object_object_get(scheduleObj, "scheduleMethod"));
  1283. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour = json_object_get_int(json_object_object_get(scheduleObj, "startTimeHour"));
  1284. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute = json_object_get_int(json_object_object_get(scheduleObj, "startTimeMinute"));
  1285. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour = json_object_get_int(json_object_object_get(scheduleObj, "stopTimeHour"));
  1286. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute = json_object_get_int(json_object_object_get(scheduleObj, "stopTimeMinute"));
  1287. break;
  1288. }
  1289. }
  1290. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1291. {
  1292. for(uint8_t idx=0;idx<gun_index;idx++)
  1293. {
  1294. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1295. {
  1296. case '1' ... '6':
  1297. idxAC++;
  1298. break;
  1299. case 'J':
  1300. idxCHAdeMO++;
  1301. break;
  1302. case 'U':
  1303. case 'E':
  1304. idxCCS++;
  1305. break;
  1306. case 'G':
  1307. idxGBT++;
  1308. break;
  1309. }
  1310. }
  1311. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1312. {
  1313. case '1' ... '6':
  1314. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable = json_object_get_int(json_object_object_get(scheduleObj, "isEnable"));
  1315. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleType = json_object_get_int(json_object_object_get(scheduleObj, "scheduleType"));
  1316. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleMethod = json_object_get_int(json_object_object_get(scheduleObj, "scheduleMethod"));
  1317. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour = json_object_get_int(json_object_object_get(scheduleObj, "startTimeHour"));
  1318. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute = json_object_get_int(json_object_object_get(scheduleObj, "startTimeMinute"));
  1319. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour = json_object_get_int(json_object_object_get(scheduleObj, "stopTimeHour"));
  1320. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute = json_object_get_int(json_object_object_get(scheduleObj, "stopTimeMinute"));
  1321. break;
  1322. case 'J':
  1323. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isEnable = json_object_get_int(json_object_object_get(scheduleObj, "isEnable"));
  1324. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.scheduleType = json_object_get_int(json_object_object_get(scheduleObj, "scheduleType"));
  1325. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.scheduleMethod = json_object_get_int(json_object_object_get(scheduleObj, "scheduleMethod"));
  1326. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeHour = json_object_get_int(json_object_object_get(scheduleObj, "startTimeHour"));
  1327. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeMinute = json_object_get_int(json_object_object_get(scheduleObj, "startTimeMinute"));
  1328. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeHour = json_object_get_int(json_object_object_get(scheduleObj, "stopTimeHour"));
  1329. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeMinute = json_object_get_int(json_object_object_get(scheduleObj, "stopTimeMinute"));
  1330. break;
  1331. case 'U':
  1332. case 'E':
  1333. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isEnable = json_object_get_int(json_object_object_get(scheduleObj, "isEnable"));
  1334. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.scheduleType = json_object_get_int(json_object_object_get(scheduleObj, "scheduleType"));
  1335. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.scheduleMethod = json_object_get_int(json_object_object_get(scheduleObj, "scheduleMethod"));
  1336. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeHour = json_object_get_int(json_object_object_get(scheduleObj, "startTimeHour"));
  1337. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeMinute = json_object_get_int(json_object_object_get(scheduleObj, "startTimeMinute"));
  1338. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeHour = json_object_get_int(json_object_object_get(scheduleObj, "stopTimeHour"));
  1339. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeMinute = json_object_get_int(json_object_object_get(scheduleObj, "stopTimeMinute"));
  1340. break;
  1341. case 'G':
  1342. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isEnable = json_object_get_int(json_object_object_get(scheduleObj, "isEnable"));
  1343. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.scheduleType = json_object_get_int(json_object_object_get(scheduleObj, "scheduleType"));
  1344. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.scheduleMethod = json_object_get_int(json_object_object_get(scheduleObj, "scheduleMethod"));
  1345. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeHour = json_object_get_int(json_object_object_get(scheduleObj, "startTimeHour"));
  1346. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeMinute = json_object_get_int(json_object_object_get(scheduleObj, "startTimeMinute"));
  1347. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeHour = json_object_get_int(json_object_object_get(scheduleObj, "stopTimeHour"));
  1348. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeMinute = json_object_get_int(json_object_object_get(scheduleObj, "stopTimeMinute"));
  1349. break;
  1350. }
  1351. }
  1352. }
  1353. json_object_put(scheduleObj);
  1354. return 0;
  1355. }
  1356. int scheduleLoadFromDb(sqlite3 *db, uint8_t gun_index)
  1357. {
  1358. int result = PASS;
  1359. char* errMsg = NULL;
  1360. char sqlStr[512];
  1361. sprintf(sqlStr, "select * from config where item='schedule' and connector=%d", gun_index);
  1362. //* Execute SQL statement */
  1363. if(sqlite3_open(DB_FILE_CHARGING, &db))
  1364. {
  1365. result = FAIL;
  1366. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1367. sqlite3_close(db);
  1368. }
  1369. else
  1370. {
  1371. if(sqlite3_exec(db, sqlStr, cbScheduleLoad, 0, &errMsg) != SQLITE_OK)
  1372. {
  1373. DEBUG_INFO("SQL error: %s\n", errMsg);
  1374. result = FAIL;
  1375. }
  1376. sqlite3_close(db);
  1377. }
  1378. return result;
  1379. }
  1380. //==========================================
  1381. // TCP socket server routine
  1382. //==========================================
  1383. uint32_t stm32crc(uint32_t *ptr, uint32_t len)
  1384. {
  1385. uint32_t xbit, data;
  1386. uint32_t crc32 = 0xFFFFFFFF;
  1387. const uint32_t polynomial = 0x04c11db7;
  1388. for(uint8_t i = 0; i < len; i ++)
  1389. {
  1390. xbit = 1 << 31;
  1391. data = ptr[i];
  1392. for (uint32_t bits = 0; bits < 32; bits++)
  1393. {
  1394. if (crc32 & 0x80000000)
  1395. {
  1396. crc32 <<= 1;
  1397. crc32 ^= polynomial;
  1398. }
  1399. else
  1400. crc32 <<= 1;
  1401. if (data & xbit)
  1402. crc32 ^= polynomial;
  1403. xbit >>= 1;
  1404. }
  1405. }
  1406. return crc32;
  1407. }
  1408. int isValidCheckSum(uint8_t *message)
  1409. {
  1410. uint8_t chksum=0;
  1411. for(int idx=0;idx<(((message[2]<<8) | message[3])>1024?1024:(((message[2]<<8) | message[3])-1));idx++)
  1412. {
  1413. chksum ^= message[4+idx];
  1414. }
  1415. return ((chksum == message[3+(((message[2]<<8) | message[3])>1024?1024:((message[2]<<8) | message[3]))]) ? PASS : FAIL);
  1416. }
  1417. int chksumCal(uint8_t *message)
  1418. {
  1419. uint8_t chksum=0;
  1420. for(int idx = 0;idx<(((message[2]<<8) | message[3])>1024?1024:(((message[2]<<8) | message[3])-1));idx++)
  1421. {
  1422. chksum ^= message[4+idx];
  1423. }
  1424. return chksum & 0xff;
  1425. }
  1426. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1427. {
  1428. int result = PASS;
  1429. int fd,wrd;
  1430. unsigned int i,Chk;
  1431. unsigned char *ptr, *BufTmp;
  1432. Chk=0;
  1433. ptr=(unsigned char *)UsrData;
  1434. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  1435. {
  1436. memset(BufTmp,0,MtdBlockSize);
  1437. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  1438. for(i=0;i<MtdBlockSize-4;i++)
  1439. Chk+=*(BufTmp+i);
  1440. memcpy( BufTmp+MtdBlockSize-4,&Chk,4);
  1441. fd = open("/dev/mtdblock10", O_RDWR);
  1442. if (fd>0)
  1443. {
  1444. wrd=write(fd, BufTmp, MtdBlockSize);
  1445. close(fd);
  1446. if(wrd>=MtdBlockSize)
  1447. {
  1448. fd = open("/dev/mtdblock11", O_RDWR);
  1449. if (fd>0)
  1450. {
  1451. wrd=write(fd, BufTmp, MtdBlockSize);
  1452. close(fd);
  1453. if(wrd<MtdBlockSize)
  1454. {
  1455. DEBUG_ERROR("write /dev/mtdblock11(backup) NG\n");
  1456. result = FAIL;
  1457. }
  1458. }
  1459. else
  1460. {
  1461. DEBUG_ERROR("open /dev/mtdblock11(backup) NG\n");
  1462. result = FAIL;
  1463. }
  1464. }
  1465. else
  1466. {
  1467. DEBUG_ERROR("write /dev/mtdblock10 NG\n");
  1468. result = FAIL;
  1469. }
  1470. }
  1471. else
  1472. {
  1473. DEBUG_ERROR("open /dev/mtdblock10 NG\n");
  1474. result = FAIL;
  1475. }
  1476. }
  1477. else
  1478. {
  1479. DEBUG_ERROR("alloc BlockSize NG\n");
  1480. result = FAIL;
  1481. }
  1482. if(BufTmp!=NULL)
  1483. free(BufTmp);
  1484. return result;
  1485. }
  1486. void getConnectorInfo(uint8_t gun_index, uint8_t *outBuffer)
  1487. {
  1488. uint8_t idxAC = 0;
  1489. uint8_t idxCCS = 0;
  1490. uint8_t idxCHAdeMO = 0;
  1491. uint8_t idxGBT = 0;
  1492. uint8_t AC_GUN_IDX = 0;
  1493. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1494. {
  1495. if(gun_index == 2)
  1496. {
  1497. AC_GUN_IDX = 0;
  1498. }
  1499. else if(gun_index == 1)
  1500. {
  1501. AC_GUN_IDX = 1;
  1502. }
  1503. else if(gun_index == 0)
  1504. {
  1505. AC_GUN_IDX = 2;
  1506. }
  1507. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1508. {
  1509. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1510. {
  1511. case '1' ... '6':
  1512. idxAC++;
  1513. break;
  1514. }
  1515. }
  1516. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1517. {
  1518. case '1' ... '6':
  1519. // AC
  1520. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus;
  1521. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>8)&0xff;
  1522. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>0)&0xff;
  1523. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>8)&0xff;
  1524. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>0)&0xff;
  1525. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>8)&0xff;
  1526. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>0)&0xff;
  1527. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>8)&0xff;
  1528. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>0)&0xff;
  1529. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>8)&0xff;
  1530. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>0)&0xff;
  1531. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>8)&0xff;
  1532. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>0)&0xff;
  1533. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].EvBatterySoc)>>0)&0xff;
  1534. break;
  1535. default:
  1536. break;
  1537. }
  1538. }
  1539. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1540. {
  1541. for(uint8_t idx=0;idx<gun_index;idx++)
  1542. {
  1543. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1544. {
  1545. case '1' ... '6':
  1546. idxAC++;
  1547. break;
  1548. case 'J':
  1549. idxCHAdeMO++;
  1550. break;
  1551. case 'U':
  1552. case 'E':
  1553. idxCCS++;
  1554. break;
  1555. case 'G':
  1556. idxGBT++;
  1557. break;
  1558. }
  1559. }
  1560. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1561. {
  1562. case '1' ... '6':
  1563. // AC
  1564. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus;
  1565. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>8)&0xff;
  1566. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>0)&0xff;
  1567. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>8)&0xff;
  1568. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>0)&0xff;
  1569. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>8)&0xff;
  1570. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>0)&0xff;
  1571. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>8)&0xff;
  1572. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>0)&0xff;
  1573. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>8)&0xff;
  1574. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>0)&0xff;
  1575. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>8)&0xff;
  1576. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>0)&0xff;
  1577. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].EvBatterySoc)>>0)&0xff;
  1578. break;
  1579. case 'J':
  1580. // CHAdeMO
  1581. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus;
  1582. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingVoltage*10)>>8)&0xff;
  1583. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingVoltage*10)>>0)&0xff;
  1584. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingCurrent*10)>>8)&0xff;
  1585. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingCurrent*10)>>0)&0xff;
  1586. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingPower*10)>>8)&0xff;
  1587. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingPower*10)>>0)&0xff;
  1588. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedEnergy*10)>>8)&0xff;
  1589. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedEnergy*10)>>0)&0xff;
  1590. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedDuration)>>8)&0xff;
  1591. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedDuration)>>0)&0xff;
  1592. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].RemainChargingDuration)>>8)&0xff;
  1593. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].RemainChargingDuration)>>0)&0xff;
  1594. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].EvBatterySoc)>>0)&0xff;
  1595. break;
  1596. case 'U':
  1597. case 'E':
  1598. // CCS
  1599. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus;
  1600. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingVoltage*10)>>8)&0xff;
  1601. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingVoltage*10)>>0)&0xff;
  1602. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingCurrent*10)>>8)&0xff;
  1603. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingCurrent*10)>>0)&0xff;
  1604. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingPower*10)>>8)&0xff;
  1605. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingPower*10)>>0)&0xff;
  1606. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedEnergy*10)>>8)&0xff;
  1607. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedEnergy*10)>>0)&0xff;
  1608. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedDuration)>>8)&0xff;
  1609. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedDuration)>>0)&0xff;
  1610. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].RemainChargingDuration)>>8)&0xff;
  1611. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].RemainChargingDuration)>>0)&0xff;
  1612. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].EvBatterySoc)>>0)&0xff;
  1613. break;
  1614. case 'G':
  1615. // GBT
  1616. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus;
  1617. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingVoltage*10)>>8)&0xff;
  1618. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingVoltage*10)>>0)&0xff;
  1619. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingCurrent*10)>>8)&0xff;
  1620. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingCurrent*10)>>0)&0xff;
  1621. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingPower*10)>>8)&0xff;
  1622. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingPower*10)>>0)&0xff;
  1623. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedEnergy*10)>>8)&0xff;
  1624. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedEnergy*10)>>0)&0xff;
  1625. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedDuration)>>8)&0xff;
  1626. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedDuration)>>0)&0xff;
  1627. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].RemainChargingDuration)>>8)&0xff;
  1628. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].RemainChargingDuration)>>0)&0xff;
  1629. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].EvBatterySoc)>>0)&0xff;
  1630. break;
  1631. default:
  1632. break;
  1633. }
  1634. }
  1635. }
  1636. void getConnectorSchedule(uint8_t gun_index, uint8_t *outBuffer)
  1637. {
  1638. uint8_t idxAC = 0;
  1639. uint8_t idxCCS = 0;
  1640. uint8_t idxCHAdeMO = 0;
  1641. uint8_t idxGBT = 0;
  1642. uint8_t AC_GUN_IDX = 0;
  1643. scheduleLoadFromDb(localDb, gun_index);
  1644. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1645. {
  1646. if(gun_index == 2)
  1647. {
  1648. AC_GUN_IDX = 0;
  1649. }
  1650. else if(gun_index == 1)
  1651. {
  1652. AC_GUN_IDX = 1;
  1653. }
  1654. else if(gun_index == 0)
  1655. {
  1656. AC_GUN_IDX = 2;
  1657. }
  1658. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1659. {
  1660. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1661. {
  1662. case '1' ... '6':
  1663. idxAC++;
  1664. break;
  1665. }
  1666. }
  1667. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1668. {
  1669. case '1' ... '6':
  1670. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable;
  1671. outBuffer[7] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleType;
  1672. outBuffer[8] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleMethod;
  1673. outBuffer[9] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour;
  1674. outBuffer[10] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute;
  1675. outBuffer[11] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour;
  1676. outBuffer[12] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute;
  1677. break;
  1678. }
  1679. }
  1680. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1681. {
  1682. for(uint8_t idx=0;idx<gun_index;idx++)
  1683. {
  1684. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1685. {
  1686. case '1' ... '6':
  1687. idxAC++;
  1688. break;
  1689. case 'J':
  1690. idxCHAdeMO++;
  1691. break;
  1692. case 'U':
  1693. case 'E':
  1694. idxCCS++;
  1695. break;
  1696. case 'G':
  1697. idxGBT++;
  1698. break;
  1699. }
  1700. }
  1701. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1702. {
  1703. case '1' ... '6':
  1704. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable;
  1705. outBuffer[7] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleType;
  1706. outBuffer[8] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleMethod;
  1707. outBuffer[9] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour;
  1708. outBuffer[10] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute;
  1709. outBuffer[11] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour;
  1710. outBuffer[12] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute;
  1711. break;
  1712. case 'J':
  1713. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isEnable;
  1714. outBuffer[7] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.scheduleType;
  1715. outBuffer[8] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.scheduleMethod;
  1716. outBuffer[9] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeHour;
  1717. outBuffer[10] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeMinute;
  1718. outBuffer[11] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeHour;
  1719. outBuffer[12] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeMinute;
  1720. break;
  1721. case 'U':
  1722. case 'E':
  1723. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isEnable;
  1724. outBuffer[7] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.scheduleType;
  1725. outBuffer[8] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.scheduleMethod;
  1726. outBuffer[9] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeHour;
  1727. outBuffer[10] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeMinute;
  1728. outBuffer[11] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeHour;
  1729. outBuffer[12] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeMinute;
  1730. break;
  1731. case 'G':
  1732. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isEnable;
  1733. outBuffer[7] = ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.scheduleType;
  1734. outBuffer[8] = ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.scheduleMethod;
  1735. outBuffer[9] = ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeHour;
  1736. outBuffer[10] = ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeMinute;
  1737. outBuffer[11] = ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeHour;
  1738. outBuffer[12] = ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeMinute;
  1739. break;
  1740. }
  1741. }
  1742. }
  1743. uint8_t startConnectorCharging(uint8_t gun_index)
  1744. {
  1745. uint8_t idxAC = 0;
  1746. uint8_t idxCCS = 0;
  1747. uint8_t idxCHAdeMO = 0;
  1748. uint8_t idxGBT = 0;
  1749. uint8_t result = OFF;
  1750. uint8_t AC_GUN_IDX = 0;
  1751. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1752. {
  1753. if(gun_index == 2)
  1754. {
  1755. AC_GUN_IDX = 0;
  1756. }
  1757. else if(gun_index == 1)
  1758. {
  1759. AC_GUN_IDX = 1;
  1760. }
  1761. else if(gun_index == 0)
  1762. {
  1763. AC_GUN_IDX = 2;
  1764. }
  1765. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1766. {
  1767. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1768. {
  1769. case '1' ... '6':
  1770. idxAC++;
  1771. break;
  1772. }
  1773. }
  1774. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1775. {
  1776. case '1' ... '6':
  1777. // AC
  1778. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)
  1779. {
  1780. //ShmOCPP16Data->CsMsg.bits[idxAC].RemoteStartTransactionReq = ON;
  1781. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStart = ON;
  1782. DEBUG_INFO("AC isTriggerStar : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStart);
  1783. DEBUG_INFO("AC gun_index : [%d].\n", gun_index);
  1784. DEBUG_INFO("AC idxAC : [%d].\n", idxAC);
  1785. result = ON;
  1786. }
  1787. break;
  1788. default:
  1789. break;
  1790. }
  1791. return result;
  1792. }
  1793. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1794. {
  1795. for(uint8_t idx=0;idx<gun_index;idx++)
  1796. {
  1797. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1798. {
  1799. case '1' ... '6':
  1800. idxAC++;
  1801. break;
  1802. case 'J':
  1803. idxCHAdeMO++;
  1804. break;
  1805. case 'U':
  1806. case 'E':
  1807. idxCCS++;
  1808. break;
  1809. case 'G':
  1810. idxGBT++;
  1811. break;
  1812. }
  1813. }
  1814. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1815. {
  1816. case '1' ... '6':
  1817. // AC
  1818. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)
  1819. {
  1820. //ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1821. //DEBUG_INFO("AC : %d...\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1822. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStart = ON;
  1823. DEBUG_INFO("AC isTriggerStar : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStart);
  1824. DEBUG_INFO("AC gun_index : [%d].\n", gun_index);
  1825. DEBUG_INFO("AC idxAC : [%d].\n", idxAC);
  1826. result = ON;
  1827. }
  1828. break;
  1829. case 'J':
  1830. // CHAdeMO
  1831. if(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus == SYS_MODE_IDLE)
  1832. {
  1833. //ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1834. //DEBUG_INFO("CHAdeMO : %d...\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1835. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isTriggerStart = ON;
  1836. DEBUG_INFO("CHAdeMO isTriggerStar : [%d].\n", ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isTriggerStart);
  1837. DEBUG_INFO("CHAdeMO gun_index : [%d].\n", gun_index);
  1838. DEBUG_INFO("CHAdeMO idxCHAdeMO : [%d].\n", idxCHAdeMO);
  1839. result = ON;
  1840. }
  1841. break;
  1842. case 'U':
  1843. case 'E':
  1844. // CCS
  1845. if(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus == SYS_MODE_IDLE)
  1846. {
  1847. //ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1848. //DEBUG_INFO("CCS : %d...\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1849. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isTriggerStart = ON;
  1850. DEBUG_INFO("CCS isTriggerStar : [%d].\n", ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isTriggerStart);
  1851. DEBUG_INFO("CCS gun_index : [%d].\n", gun_index);
  1852. DEBUG_INFO("CCS idxCCS : [%d].\n", idxCCS);
  1853. result = ON;
  1854. }
  1855. break;
  1856. case 'G':
  1857. // GBT
  1858. if(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus == SYS_MODE_IDLE)
  1859. {
  1860. //ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1861. //DEBUG_INFO("GBT : %d...\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1862. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isTriggerStart = ON;
  1863. DEBUG_INFO("GBT isTriggerStar : [%d].\n", ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isTriggerStart);
  1864. DEBUG_INFO("GBT gun_index : [%d].\n", gun_index);
  1865. DEBUG_INFO("GBT idxGBT : [%d].\n", idxGBT);
  1866. result = ON;
  1867. }
  1868. break;
  1869. default:
  1870. break;
  1871. }
  1872. }
  1873. return result;
  1874. }
  1875. uint8_t stopConnectorCharging(uint8_t gun_index)
  1876. {
  1877. uint8_t idxAC = 0;
  1878. uint8_t idxCCS = 0;
  1879. uint8_t idxCHAdeMO = 0;
  1880. uint8_t idxGBT = 0;
  1881. uint8_t result = OFF;
  1882. uint8_t AC_GUN_IDX = 0;
  1883. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1884. {
  1885. if(gun_index == 2)
  1886. {
  1887. AC_GUN_IDX = 0;
  1888. }
  1889. else if(gun_index == 1)
  1890. {
  1891. AC_GUN_IDX = 1;
  1892. }
  1893. else if(gun_index == 0)
  1894. {
  1895. AC_GUN_IDX = 2;
  1896. }
  1897. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1898. {
  1899. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1900. {
  1901. case '1' ... '6':
  1902. idxAC++;
  1903. break;
  1904. }
  1905. }
  1906. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1907. {
  1908. case '1' ... '6':
  1909. // AC
  1910. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus != SYS_MODE_IDLE)
  1911. {
  1912. //ShmOCPP16Data->CsMsg.bits[idxAC].RemoteStopTransactionReq = ON;
  1913. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStop = ON;
  1914. DEBUG_INFO("AC isTriggerStop : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStop);
  1915. DEBUG_INFO("AC gun_index : [%d].\n", gun_index);
  1916. DEBUG_INFO("AC idxAC : [%d].\n", idxAC);
  1917. result = ON;
  1918. }
  1919. break;
  1920. default:
  1921. break;
  1922. }
  1923. return result;
  1924. }
  1925. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1926. {
  1927. for(uint8_t idx=0;idx<gun_index;idx++)
  1928. {
  1929. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1930. {
  1931. case '1' ... '6':
  1932. idxAC++;
  1933. break;
  1934. case 'J':
  1935. idxCHAdeMO++;
  1936. break;
  1937. case 'U':
  1938. case 'E':
  1939. idxCCS++;
  1940. break;
  1941. case 'G':
  1942. idxGBT++;
  1943. break;
  1944. }
  1945. }
  1946. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1947. {
  1948. case '1' ... '6':
  1949. // AC
  1950. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus != SYS_MODE_IDLE)
  1951. {
  1952. //ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1953. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStop = ON;
  1954. DEBUG_INFO("AC isTriggerStop : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStop);
  1955. DEBUG_INFO("AC gun_index : [%d].\n", gun_index);
  1956. DEBUG_INFO("AC idxAC : [%d].\n", idxAC);
  1957. result = ON;
  1958. }
  1959. break;
  1960. case 'J':
  1961. // CHAdeMO
  1962. if(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus != SYS_MODE_IDLE)
  1963. {
  1964. //ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1965. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isTriggerStop = ON;
  1966. DEBUG_INFO("CHAdeMO isTriggerStop : [%d].\n", ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isTriggerStop);
  1967. DEBUG_INFO("CHAdeMO gun_index : [%d].\n", gun_index);
  1968. DEBUG_INFO("CHAdeMO idxCHAdeMO : [%d].\n", idxCHAdeMO);
  1969. result = ON;
  1970. }
  1971. break;
  1972. case 'U':
  1973. case 'E':
  1974. // CCS
  1975. if(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus != SYS_MODE_IDLE)
  1976. {
  1977. //ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1978. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isTriggerStop = ON;
  1979. DEBUG_INFO("CCS isTriggerStop : [%d].\n", ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isTriggerStop);
  1980. DEBUG_INFO("CCS gun_index : [%d].\n", gun_index);
  1981. DEBUG_INFO("CCS idxCCS : [%d].\n", idxCCS);
  1982. result = ON;
  1983. }
  1984. break;
  1985. case 'G':
  1986. // GBT
  1987. if(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus != SYS_MODE_IDLE)
  1988. {
  1989. //ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1990. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isTriggerStop = ON;
  1991. DEBUG_INFO("GBT isTriggerStop : [%d].\n", ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isTriggerStop);
  1992. DEBUG_INFO("GBT gun_index : [%d].\n", gun_index);
  1993. DEBUG_INFO("GBT idxCCS : [%d].\n", idxGBT);
  1994. result = ON;
  1995. }
  1996. break;
  1997. default:
  1998. break;
  1999. }
  2000. }
  2001. return result;
  2002. }
  2003. uint8_t setConnectorSchedule(uint8_t gun_index, uint8_t *inputBuffer)
  2004. {
  2005. uint8_t idxAC = 0;
  2006. uint8_t idxCCS = 0;
  2007. uint8_t idxCHAdeMO = 0;
  2008. uint8_t idxGBT = 0;
  2009. uint8_t result = OFF;
  2010. uint8_t AC_GUN_IDX = 0;
  2011. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  2012. {
  2013. if(gun_index == 2)
  2014. {
  2015. AC_GUN_IDX = 0;
  2016. }
  2017. else if(gun_index == 1)
  2018. {
  2019. AC_GUN_IDX = 1;
  2020. }
  2021. else if(gun_index == 0)
  2022. {
  2023. AC_GUN_IDX = 2;
  2024. }
  2025. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  2026. {
  2027. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  2028. {
  2029. case '1' ... '6':
  2030. idxAC++;
  2031. break;
  2032. }
  2033. }
  2034. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  2035. {
  2036. case '1' ... '6':
  2037. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable = inputBuffer[10];
  2038. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleType = inputBuffer[11];
  2039. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleMethod = inputBuffer[12];
  2040. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour = inputBuffer[13];
  2041. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute = inputBuffer[14];
  2042. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour = inputBuffer[15];
  2043. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute = inputBuffer[16];
  2044. if(scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule) == PASS)
  2045. result = ON;
  2046. else
  2047. result = OFF;
  2048. break;
  2049. }
  2050. return result;
  2051. }
  2052. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  2053. {
  2054. for(uint8_t idx=0;idx<gun_index;idx++)
  2055. {
  2056. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  2057. {
  2058. case '1' ... '6':
  2059. idxAC++;
  2060. break;
  2061. case 'J':
  2062. idxCHAdeMO++;
  2063. break;
  2064. case 'U':
  2065. case 'E':
  2066. idxCCS++;
  2067. break;
  2068. case 'G':
  2069. idxGBT++;
  2070. break;
  2071. }
  2072. }
  2073. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  2074. {
  2075. case '1' ... '6':
  2076. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable = inputBuffer[10];
  2077. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleType = inputBuffer[11];
  2078. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleMethod = inputBuffer[12];
  2079. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour = inputBuffer[13];
  2080. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute = inputBuffer[14];
  2081. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour = inputBuffer[15];
  2082. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute = inputBuffer[16];
  2083. if(scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule) == PASS)
  2084. result = ON;
  2085. else
  2086. result = OFF;
  2087. break;
  2088. case 'J':
  2089. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isEnable = inputBuffer[10];
  2090. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.scheduleType = inputBuffer[11];
  2091. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.scheduleMethod = inputBuffer[12];
  2092. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeHour = inputBuffer[13];
  2093. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeMinute = inputBuffer[14];
  2094. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeHour = inputBuffer[15];
  2095. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeMinute = inputBuffer[16];
  2096. if(scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule) == PASS)
  2097. result = ON;
  2098. else
  2099. result = OFF;
  2100. break;
  2101. case 'U':
  2102. case 'E':
  2103. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isEnable = inputBuffer[10];
  2104. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.scheduleType = inputBuffer[11];
  2105. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.scheduleMethod = inputBuffer[12];
  2106. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeHour = inputBuffer[13];
  2107. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeMinute = inputBuffer[14];
  2108. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeHour = inputBuffer[15];
  2109. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeMinute = inputBuffer[16];
  2110. if(scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule) == PASS)
  2111. result = ON;
  2112. else
  2113. result = OFF;
  2114. break;
  2115. case 'G':
  2116. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isEnable = inputBuffer[10];
  2117. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.scheduleType = inputBuffer[11];
  2118. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.scheduleMethod = inputBuffer[12];
  2119. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeHour = inputBuffer[13];
  2120. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeMinute = inputBuffer[14];
  2121. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeHour = inputBuffer[15];
  2122. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeMinute = inputBuffer[16];
  2123. if(scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule) == PASS)
  2124. result = ON;
  2125. else
  2126. result = OFF;
  2127. break;
  2128. }
  2129. }
  2130. return result;
  2131. }
  2132. void getConnectorUser(uint8_t gun_index, uint8_t *outBuffer)
  2133. {
  2134. uint8_t idxAC = 0;
  2135. uint8_t idxCCS = 0;
  2136. uint8_t idxCHAdeMO = 0;
  2137. uint8_t idxGBT = 0;
  2138. uint8_t AC_GUN_IDX = 0;
  2139. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  2140. {
  2141. if(gun_index == 2)
  2142. {
  2143. AC_GUN_IDX = 0;
  2144. }
  2145. else if(gun_index == 1)
  2146. {
  2147. AC_GUN_IDX = 1;
  2148. }
  2149. else if(gun_index == 0)
  2150. {
  2151. AC_GUN_IDX = 2;
  2152. }
  2153. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  2154. {
  2155. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  2156. {
  2157. case '1' ... '6':
  2158. idxAC++;
  2159. break;
  2160. }
  2161. }
  2162. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  2163. {
  2164. case '1' ... '6':
  2165. // AC
  2166. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  2167. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId));
  2168. break;
  2169. default:
  2170. break;
  2171. }
  2172. }
  2173. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  2174. {
  2175. for(uint8_t idx=0;idx<gun_index;idx++)
  2176. {
  2177. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  2178. {
  2179. case '1' ... '6':
  2180. idxAC++;
  2181. break;
  2182. case 'J':
  2183. idxCHAdeMO++;
  2184. break;
  2185. case 'U':
  2186. case 'E':
  2187. idxCCS++;
  2188. break;
  2189. case 'G':
  2190. idxGBT++;
  2191. break;
  2192. }
  2193. }
  2194. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  2195. {
  2196. case '1' ... '6':
  2197. // AC
  2198. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  2199. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId));
  2200. break;
  2201. case 'J':
  2202. // CHAdeMO
  2203. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  2204. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].StartUserId));
  2205. break;
  2206. case 'U':
  2207. case 'E':
  2208. // CCS
  2209. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  2210. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].StartUserId));
  2211. break;
  2212. case 'G':
  2213. // GBT
  2214. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  2215. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].StartUserId));
  2216. break;
  2217. default:
  2218. break;
  2219. }
  2220. }
  2221. }
  2222. void checkSchedule()
  2223. {
  2224. uint8_t idxAC = 0;
  2225. uint8_t idxCCS = 0;
  2226. uint8_t idxCHAdeMO = 0;
  2227. uint8_t idxGBT = 0;
  2228. uint8_t AC_GUN_IDX = 0;
  2229. time_t CurrentTime;
  2230. struct tm *tm;
  2231. CurrentTime = time(NULL);
  2232. tm=localtime(&CurrentTime);
  2233. if((time((time_t*)NULL)%60) == 0)
  2234. {
  2235. for(int gun_index=0;gun_index<(AC_QUANTITY+CHAdeMO_QUANTITY+CCS_QUANTITY+GB_QUANTITY);gun_index++)
  2236. {
  2237. idxAC = 0;
  2238. idxCCS = 0;
  2239. idxCHAdeMO = 0;
  2240. idxGBT = 0;
  2241. AC_GUN_IDX = 0;
  2242. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  2243. {
  2244. //DEBUG_INFO("Schedule gun_index : [%d].\n", gun_index);
  2245. if(gun_index == 2)
  2246. {
  2247. AC_GUN_IDX = 0;
  2248. }
  2249. else if(gun_index == 1)
  2250. {
  2251. AC_GUN_IDX = 1;
  2252. }
  2253. else if(gun_index == 0)
  2254. {
  2255. AC_GUN_IDX = 2;
  2256. }
  2257. //DEBUG_INFO("Schedule AC_GUN_IDX : [%d].\n", AC_GUN_IDX);
  2258. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  2259. {
  2260. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  2261. {
  2262. case '1' ... '6':
  2263. idxAC++;
  2264. //DEBUG_INFO("Schedule idxAC : [%d].\n", idxAC);
  2265. break;
  2266. }
  2267. }
  2268. /*
  2269. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  2270. DEBUG_INFO("idxAC : [%d].\n", idxAC);
  2271. DEBUG_INFO("AC Schedule isEnable : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable);
  2272. DEBUG_INFO("AC Schedule scheduleMethod : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleMethod);
  2273. DEBUG_INFO("AC Schedule startTimeHour : [%d] >> tm->tm_hour : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour, tm->tm_hour);
  2274. DEBUG_INFO("AC Schedule startTimeMinute : [%d] >> tm->tm_min : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute, tm->tm_min);
  2275. DEBUG_INFO("AC Schedule stopTimeHour : [%d] >> tm->tm_hourr : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour, tm->tm_hour);
  2276. DEBUG_INFO("AC Schedule stopTimeMinute : [%d] >> tm->tm_min : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute, tm->tm_min);
  2277. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  2278. */
  2279. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  2280. {
  2281. case '1' ... '6':
  2282. // Check schedule start
  2283. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable) &&
  2284. (ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour == tm->tm_hour) &&
  2285. (ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute == tm->tm_min))
  2286. {
  2287. DEBUG_INFO("Gun-%02d Schedule trigger start...%02d:%02d\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour, ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute);
  2288. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStart = ON;
  2289. // Disable schedule time if type is once(0)
  2290. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleType == 0)
  2291. {
  2292. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour = 0xff;
  2293. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute = 0xff;
  2294. scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule);
  2295. }
  2296. }
  2297. // Check schedule stop
  2298. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable) &&
  2299. (ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleMethod) &&
  2300. (ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour == tm->tm_hour) &&
  2301. (ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute == tm->tm_min))
  2302. {
  2303. DEBUG_INFO("Gun-%02d Schedule trigger stop...%02d:%02d\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour, ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute);
  2304. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStop = ON;
  2305. // Disable schedule time if type is once(0)
  2306. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleType == 0)
  2307. {
  2308. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour = 0xff;
  2309. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute = 0xff;
  2310. scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule);
  2311. }
  2312. }
  2313. break;
  2314. }
  2315. }
  2316. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  2317. {
  2318. for(uint8_t idx=0;idx<gun_index;idx++)
  2319. {
  2320. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  2321. {
  2322. case '1' ... '6':
  2323. idxAC++;
  2324. break;
  2325. case 'J':
  2326. idxCHAdeMO++;
  2327. break;
  2328. case 'U':
  2329. case 'E':
  2330. idxCCS++;
  2331. break;
  2332. case 'G':
  2333. idxGBT++;
  2334. break;
  2335. }
  2336. }
  2337. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  2338. {
  2339. case '1' ... '6':
  2340. /*
  2341. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  2342. DEBUG_INFO("idxAC : [%d].\n", idxAC);
  2343. DEBUG_INFO("AC Schedule isEnable : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable);
  2344. DEBUG_INFO("AC Schedule scheduleMethod : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleMethod);
  2345. DEBUG_INFO("AC Schedule startTimeHour : [%d] >> tm->tm_hour : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour, tm->tm_hour);
  2346. DEBUG_INFO("AC Schedule startTimeMinute : [%d] >> tm->tm_min : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute, tm->tm_min);
  2347. DEBUG_INFO("AC Schedule stopTimeHour : [%d] >> tm->tm_hourr : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour, tm->tm_hour);
  2348. DEBUG_INFO("AC Schedule stopTimeMinute : [%d] >> tm->tm_min : [%d].\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute, tm->tm_min);
  2349. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  2350. */
  2351. // AC
  2352. // Check schedule start
  2353. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable) &&
  2354. (ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour == tm->tm_hour) &&
  2355. (ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute == tm->tm_min))
  2356. {
  2357. DEBUG_INFO("Gun-%02d Schedule trigger start...%02d:%02d\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour, ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute);
  2358. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStart = ON;
  2359. // Disable schedule time if type is once(0)
  2360. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleType == 0)
  2361. {
  2362. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeHour = 0xff;
  2363. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.startTimeMinute = 0xff;
  2364. scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule);
  2365. }
  2366. }
  2367. // Check schedule stop
  2368. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isEnable) &&
  2369. (ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleMethod) &&
  2370. (ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour == tm->tm_hour) &&
  2371. (ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute == tm->tm_min))
  2372. {
  2373. DEBUG_INFO("Gun-%02d Schedule trigger stop...%02d:%02d\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour, ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute);
  2374. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.isTriggerStop = ON;
  2375. // Disable schedule time if type is once(0)
  2376. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.scheduleType == 0)
  2377. {
  2378. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeHour = 0xff;
  2379. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule.stopTimeMinute = 0xff;
  2380. scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].schedule);
  2381. }
  2382. }
  2383. break;
  2384. case 'J':
  2385. /*
  2386. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  2387. DEBUG_INFO("idxCHAdeMO : [%d].\n", idxCHAdeMO);
  2388. DEBUG_INFO("CHAdeMO Schedule isEnable : [%d].\n", ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isEnable);
  2389. DEBUG_INFO("CHAdeMO Schedule scheduleMethod : [%d].\n", ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.scheduleMethod);
  2390. DEBUG_INFO("CHAdeMO Schedule startTimeHour : [%d] >> tm->tm_hour : [%d].\n", ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeHour, tm->tm_hour);
  2391. DEBUG_INFO("CHAdeMO Schedule startTimeMinute : [%d] >> tm->tm_min : [%d].\n", ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeMinute, tm->tm_min);
  2392. DEBUG_INFO("CHAdeMO Schedule stopTimeHour : [%d] >> tm->tm_hourr : [%d].\n", ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeHour, tm->tm_hour);
  2393. DEBUG_INFO("CHAdeMO Schedule stopTimeMinute : [%d] >> tm->tm_min : [%d].\n", ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeMinute, tm->tm_min);
  2394. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  2395. */
  2396. // CHAdeMO
  2397. // Check schedule start
  2398. if((ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isEnable) &&
  2399. (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeHour == tm->tm_hour) &&
  2400. (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeMinute == tm->tm_min))
  2401. {
  2402. DEBUG_INFO("Gun-%02d Schedule trigger start...%02d:%02d\n", gun_index, ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeHour, ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeMinute);
  2403. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isTriggerStart = ON;
  2404. // Disable schedule time if type is once(0)
  2405. if(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.scheduleType == 0)
  2406. {
  2407. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeHour = 0xff;
  2408. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.startTimeMinute = 0xff;
  2409. scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule);
  2410. }
  2411. }
  2412. // Check schedule stop
  2413. if((ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isEnable) &&
  2414. (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.scheduleMethod) &&
  2415. (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeHour == tm->tm_hour) &&
  2416. (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeMinute == tm->tm_min))
  2417. {
  2418. DEBUG_INFO("Gun-%02d Schedule trigger stop...%02d:%02d\n", gun_index, ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeHour, ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeMinute);
  2419. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.isTriggerStop = ON;
  2420. // Disable schedule time if type is once(0)
  2421. if(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.scheduleType == 0)
  2422. {
  2423. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeHour = 0xff;
  2424. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule.stopTimeMinute = 0xff;
  2425. scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].schedule);
  2426. }
  2427. }
  2428. break;
  2429. case 'U':
  2430. case 'E':
  2431. /*
  2432. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  2433. DEBUG_INFO("idxCCS : [%d].\n", idxCCS);
  2434. DEBUG_INFO("CCS Schedule isEnable : [%d].\n", ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isEnable);
  2435. DEBUG_INFO("CCS Schedule scheduleMethod : [%d].\n", ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.scheduleMethod);
  2436. DEBUG_INFO("CCS Schedule startTimeHour : [%d] >> tm->tm_hour : [%d].\n", ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeHour, tm->tm_hour);
  2437. DEBUG_INFO("CCS Schedule startTimeMinute : [%d] >> tm->tm_min : [%d].\n", ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeMinute, tm->tm_min);
  2438. DEBUG_INFO("CCS Schedule stopTimeHour : [%d] >> tm->tm_hourr : [%d].\n", ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeHour, tm->tm_hour);
  2439. DEBUG_INFO("CCS Schedule stopTimeMinute : [%d] >> tm->tm_min : [%d].\n", ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeMinute, tm->tm_min);
  2440. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  2441. */
  2442. // CCS
  2443. // Check schedule start
  2444. if((ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isEnable) &&
  2445. (ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeHour == tm->tm_hour) &&
  2446. (ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeMinute == tm->tm_min))
  2447. {
  2448. DEBUG_INFO("Gun-%02d Schedule trigger start...%02d:%02d\n", gun_index, ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeHour, ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeMinute);
  2449. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isTriggerStart = ON;
  2450. // Disable schedule time if type is once(0)
  2451. if(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.scheduleType == 0)
  2452. {
  2453. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeHour = 0xff;
  2454. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.startTimeMinute = 0xff;
  2455. scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule);
  2456. }
  2457. }
  2458. // Check schedule stop
  2459. if((ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isEnable) &&
  2460. (ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.scheduleMethod) &&
  2461. (ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeHour == tm->tm_hour) &&
  2462. (ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeMinute == tm->tm_min))
  2463. {
  2464. DEBUG_INFO("Gun-%02d Schedule trigger stop...%02d:%02d\n", gun_index, ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeHour, ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeMinute);
  2465. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.isTriggerStop = ON;
  2466. // Disable schedule time if type is once(0)
  2467. if(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.scheduleType == 0)
  2468. {
  2469. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeHour = 0xff;
  2470. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule.stopTimeMinute = 0xff;
  2471. scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].schedule);
  2472. }
  2473. }
  2474. break;
  2475. case 'G':
  2476. /*
  2477. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  2478. DEBUG_INFO("idxGBT : [%d].\n", idxGBT);
  2479. DEBUG_INFO("GBT Schedule isEnable : [%d].\n", ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isEnable);
  2480. DEBUG_INFO("GBT Schedule scheduleMethod : [%d].\n", ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.scheduleMethod);
  2481. DEBUG_INFO("GBT Schedule startTimeHour : [%d] >> tm->tm_hour : [%d].\n", ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeHour, tm->tm_hour);
  2482. DEBUG_INFO("GBT Schedule startTimeMinute : [%d] >> tm->tm_min : [%d].\n", ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeMinute, tm->tm_min);
  2483. DEBUG_INFO("GBT Schedule stopTimeHour : [%d] >> tm->tm_hourr : [%d].\n", ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeHour, tm->tm_hour);
  2484. DEBUG_INFO("GBT Schedule stopTimeMinute : [%d] >> tm->tm_min : [%d].\n", ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeMinute, tm->tm_min);
  2485. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  2486. */
  2487. // GBT
  2488. // Check schedule start
  2489. if((ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isEnable) &&
  2490. (ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeHour == tm->tm_hour) &&
  2491. (ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeMinute == tm->tm_min))
  2492. {
  2493. DEBUG_INFO("Gun-%02d Schedule trigger start...%02d:%02d\n", gun_index, ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeHour, ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeMinute);
  2494. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isTriggerStart = ON;
  2495. // Disable schedule time if type is once(0)
  2496. if(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.scheduleType == 0)
  2497. {
  2498. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeHour = 0xff;
  2499. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.startTimeMinute = 0xff;
  2500. scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.GbChargingData[idxCHAdeMO].schedule);
  2501. }
  2502. }
  2503. // Check schedule stop
  2504. if((ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isEnable) &&
  2505. (ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.scheduleMethod) &&
  2506. (ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeHour == tm->tm_hour) &&
  2507. (ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeMinute == tm->tm_min))
  2508. {
  2509. DEBUG_INFO("Gun-%02d Schedule trigger stop...%02d:%02d\n", gun_index, ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeHour, ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeMinute);
  2510. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.isTriggerStop = ON;
  2511. // Disable schedule time if type is once(0)
  2512. if(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.scheduleType == 0)
  2513. {
  2514. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeHour = 0xff;
  2515. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule.stopTimeMinute = 0xff;
  2516. scheduleSaveToDb(localDb, gun_index, &ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].schedule);
  2517. }
  2518. }
  2519. break;
  2520. default:
  2521. break;
  2522. }
  2523. }
  2524. }
  2525. }
  2526. }
  2527. int tcpSocketServerStart(void)
  2528. {
  2529. int sockFd = 0;
  2530. int clientSockFd = 0;
  2531. uint8_t inputBuffer[2048] = {};
  2532. uint8_t outBuffer[2048] = {};
  2533. int16_t read_size;
  2534. int16_t tx_size;
  2535. uint32_t randomChk;
  2536. struct sockaddr_in serverInfo, clientInfo;
  2537. socklen_t addrlen = sizeof(clientInfo);
  2538. uint8_t cmdBuf[512];
  2539. time_t rawtime;
  2540. struct tm ts;
  2541. sockFd = socket(AF_INET , SOCK_STREAM , 0);
  2542. if(sockFd == -1)
  2543. {
  2544. DEBUG_ERROR("TCP service socket create fail.\n");
  2545. sleep(5);
  2546. return FAIL;
  2547. }
  2548. bzero(&serverInfo,sizeof(serverInfo));
  2549. serverInfo.sin_family = PF_INET;
  2550. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  2551. serverInfo.sin_port = htons(LISTEN_PORT_TCP);
  2552. if(bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo)) < 0)
  2553. DEBUG_ERROR("TCP server socket bind fail.\n");
  2554. if(listen(sockFd, CONNECTION_LIMIT) < 0)
  2555. DEBUG_ERROR("TCP server socket listen fail.\n");
  2556. else
  2557. DEBUG_INFO("TCP server initial.\n");
  2558. // Main loop
  2559. for(;;)
  2560. {
  2561. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  2562. DEBUG_INFO("Client connect in.\n");
  2563. DEBUG_INFO("clientSockFd : %d...\n", clientSockFd);
  2564. // Fork a child process to handle the new connection
  2565. if(fork()==0)
  2566. {
  2567. // Copy ShmSysConfigAndInfo to SysConfigTemporarily
  2568. memcpy(&SysConfigTemporarily, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2569. while((read_size = recv(clientSockFd, inputBuffer, sizeof(inputBuffer), 0)) > 0)
  2570. {
  2571. displayMessage(inputBuffer, read_size, YES);
  2572. if(isValidCheckSum(inputBuffer) && (randomChk != ((inputBuffer[4]<<24) | (inputBuffer[5]<<16) | (inputBuffer[6]<<8) | (inputBuffer[7]<<0))))
  2573. {
  2574. randomChk = ((inputBuffer[4]<<24) | (inputBuffer[5]<<16) | (inputBuffer[6]<<8) | (inputBuffer[7]<<0));
  2575. memset(outBuffer, 0x00, sizeof(outBuffer));
  2576. switch(inputBuffer[8])
  2577. {
  2578. case WIFI_CMD_LOGIN_SIGNIN:
  2579. memset(&wifi_login_info.loginId[0], 0x00, ARRAY_SIZE(wifi_login_info.loginId));
  2580. memcpy((char*)&wifi_login_info.loginId[0], (char*)&inputBuffer[9], (((inputBuffer[2] << 8)| inputBuffer[3])-6));
  2581. wifi_login_info.loginRole = loginValid(localDb);
  2582. DEBUG_INFO("Receive login command.\n");
  2583. DEBUG_INFO("Login user id: %s\n", wifi_login_info.loginId);
  2584. DEBUG_INFO("User id: %d\n", wifi_login_info.loginRole);
  2585. tx_size = 7;
  2586. outBuffer[0] = 0xff;
  2587. outBuffer[1] = 0xff;
  2588. outBuffer[2] = 0x00;
  2589. outBuffer[3] = 0x03;
  2590. outBuffer[4] = WIFI_CMD_LOGIN_SIGNIN;
  2591. switch(wifi_login_info.loginRole)
  2592. {
  2593. case ROLE_MANAGER:
  2594. case ROLE_USER:
  2595. // Login success
  2596. outBuffer[5] = 0x02;
  2597. break;
  2598. case ROLE_UNKNOWN:
  2599. default:
  2600. if(!isPukReg(localDb))
  2601. {
  2602. // Login fail, PUK never be registered
  2603. outBuffer[5] = 0x01;
  2604. }
  2605. else
  2606. {
  2607. // Login fail
  2608. outBuffer[5] = 0x00;
  2609. }
  2610. break;
  2611. }
  2612. outBuffer[6] = chksumCal(outBuffer);
  2613. break;
  2614. case WIFI_CMD_LOGIN_AUTH_PUK:
  2615. DEBUG_INFO("Receive authorize PUK command.\n");
  2616. if(!isPukReg(localDb))
  2617. {
  2618. // PUK never be registered
  2619. if((wifi_login_info.puk[0] == inputBuffer[9]) &&
  2620. (wifi_login_info.puk[1] == inputBuffer[10]) &&
  2621. (wifi_login_info.puk[2] == inputBuffer[11]) &&
  2622. (wifi_login_info.puk[3] == inputBuffer[12]) &&
  2623. (wifi_login_info.puk[4] == inputBuffer[13]) &&
  2624. (wifi_login_info.puk[5] == inputBuffer[14]) &&
  2625. (wifi_login_info.puk[6] == inputBuffer[15]) &&
  2626. (wifi_login_info.puk[7] == inputBuffer[16]))
  2627. {
  2628. // PUK valid success
  2629. if(PukReg(localDb))
  2630. {
  2631. // PUK registered success
  2632. tx_size = 7 + strlen((char*)wifi_login_info.loginId);
  2633. outBuffer[0] = 0xff;
  2634. outBuffer[1] = 0xff;
  2635. outBuffer[2] = ((0x03 + strlen((char*)wifi_login_info.loginId))>>8)&0xff;
  2636. outBuffer[3] = (0x03 + strlen((char*)wifi_login_info.loginId)) & 0xff;
  2637. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  2638. outBuffer[5] = 0x01;
  2639. memcpy(&outBuffer[6], wifi_login_info.loginId, strlen((char*)wifi_login_info.loginId));
  2640. outBuffer[6+strlen((char*)wifi_login_info.loginId)] = chksumCal(outBuffer);
  2641. }
  2642. else
  2643. {
  2644. // PUK registered fail
  2645. tx_size = 7;
  2646. outBuffer[0] = 0xff;
  2647. outBuffer[1] = 0xff;
  2648. outBuffer[2] = 0x00;
  2649. outBuffer[3] = 0x03;
  2650. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  2651. outBuffer[5] = 0x00;
  2652. outBuffer[6] = chksumCal(outBuffer);
  2653. }
  2654. }
  2655. else
  2656. {
  2657. // PUK valid fail
  2658. tx_size = 7;
  2659. outBuffer[0] = 0xff;
  2660. outBuffer[1] = 0xff;
  2661. outBuffer[2] = 0x00;
  2662. outBuffer[3] = 0x03;
  2663. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  2664. outBuffer[5] = 0x00;
  2665. outBuffer[6] = chksumCal(outBuffer);
  2666. }
  2667. }
  2668. else
  2669. {
  2670. // PUK already be registered
  2671. tx_size = 7;
  2672. outBuffer[0] = 0xff;
  2673. outBuffer[1] = 0xff;
  2674. outBuffer[2] = 0x00;
  2675. outBuffer[3] = 0x03;
  2676. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  2677. outBuffer[5] = 0x02;
  2678. outBuffer[6] = chksumCal(outBuffer);
  2679. }
  2680. break;
  2681. case WIFI_CMD_LOGIN_GET_USER_ID:
  2682. DEBUG_INFO("Receive login get user ID command.\n");
  2683. if(wifi_login_info.loginRole != ROLE_UNKNOWN)
  2684. {
  2685. // Login role is Manager or User
  2686. tx_size = 7 + strlen((char*)wifi_login_info.loginId);
  2687. outBuffer[0] = 0xff;
  2688. outBuffer[1] = 0xff;
  2689. outBuffer[2] = ((0x03 + strlen((char*)wifi_login_info.loginId))>>8)&0xff;
  2690. outBuffer[3] = (0x03 + strlen((char*)wifi_login_info.loginId)) & 0xff;
  2691. outBuffer[4] = WIFI_CMD_LOGIN_GET_USER_ID;
  2692. outBuffer[5] = 0x01;
  2693. memcpy(&outBuffer[6], wifi_login_info.loginId, strlen((char*)wifi_login_info.loginId));
  2694. outBuffer[6+strlen((char*)wifi_login_info.loginId)] = chksumCal(outBuffer);
  2695. }
  2696. else
  2697. {
  2698. // Login role is Unknown
  2699. tx_size = 7;
  2700. outBuffer[0] = 0xff;
  2701. outBuffer[1] = 0xff;
  2702. outBuffer[2] = 0x00;
  2703. outBuffer[3] = 0x03;
  2704. outBuffer[4] = WIFI_CMD_LOGIN_GET_USER_ID;
  2705. outBuffer[5] = 0x00;
  2706. outBuffer[6] = chksumCal(outBuffer);
  2707. }
  2708. break;
  2709. case WIFI_CMD_PROTOCOL_VERSION:
  2710. DEBUG_INFO("Receive get protocol version command.\n");
  2711. DEBUG_INFO("Protocol version: %d.%d.%d.\n", protocol_Version[0], protocol_Version[1], protocol_Version[2]);
  2712. tx_size = 12;
  2713. outBuffer[0] = 0xff;
  2714. outBuffer[1] = 0xff;
  2715. outBuffer[2] = 0x00;
  2716. outBuffer[3] = 0x08;
  2717. outBuffer[4] = WIFI_CMD_PROTOCOL_VERSION;
  2718. outBuffer[5] = (protocol_Version[0] >> 0x08) & 0xff;
  2719. outBuffer[6] = (protocol_Version[0] >> 0x00) & 0xff;
  2720. outBuffer[7] = (protocol_Version[1] >> 0x08) & 0xff;
  2721. outBuffer[8] = (protocol_Version[1] >> 0x00) & 0xff;
  2722. outBuffer[9] = (protocol_Version[2] >> 0x08) & 0xff;
  2723. outBuffer[10] = (protocol_Version[2] >> 0x00) & 0xff;
  2724. outBuffer[11] = chksumCal(outBuffer);
  2725. break;
  2726. case WIFI_CMD_GET_MODEL_NAME:
  2727. DEBUG_INFO("Receive get model name command.\n");
  2728. DEBUG_INFO("Model name: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2729. tx_size = 70;
  2730. outBuffer[0] = 0xff;
  2731. outBuffer[1] = 0xff;
  2732. outBuffer[2] = 0x00;
  2733. outBuffer[3] = 0x42;
  2734. outBuffer[4] = WIFI_CMD_GET_MODEL_NAME;
  2735. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  2736. outBuffer[69] = chksumCal(outBuffer);
  2737. break;
  2738. case WIFI_CMD_GET_SERIAL_NUMBER:
  2739. DEBUG_INFO("Receive get serial number command.\n");
  2740. DEBUG_INFO("Serial number: %s\n", ShmSysConfigAndInfo->SysConfig.SerialNumber);
  2741. tx_size = 70;
  2742. outBuffer[0] = 0xff;
  2743. outBuffer[1] = 0xff;
  2744. outBuffer[2] = 0x00;
  2745. outBuffer[3] = 0x42;
  2746. outBuffer[4] = WIFI_CMD_GET_SERIAL_NUMBER;
  2747. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  2748. outBuffer[69] = chksumCal(outBuffer);
  2749. break;
  2750. case WIFI_CMD_GET_SYSTEM_TIME:
  2751. DEBUG_INFO("Receive get system time command.\n");
  2752. tx_size = 10;
  2753. outBuffer[0] = 0xff;
  2754. outBuffer[1] = 0xff;
  2755. outBuffer[2] = 0x00;
  2756. outBuffer[3] = 0x06;
  2757. outBuffer[4] = WIFI_CMD_GET_SYSTEM_TIME;
  2758. outBuffer[5] = (time(NULL)>>24)&0xff;
  2759. outBuffer[6] = (time(NULL)>>16)&0xff;
  2760. outBuffer[7] = (time(NULL)>>8)&0xff;
  2761. outBuffer[8] = (time(NULL)>>0)&0xff;
  2762. outBuffer[9] = chksumCal(outBuffer);
  2763. break;
  2764. case WIFI_CMD_GET_WHITE_CARD:
  2765. DEBUG_INFO("Receive get white list-%02d command.\n", inputBuffer[9]);
  2766. DEBUG_INFO("White list: %s.\n", ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[inputBuffer[9]]);
  2767. tx_size = 40;
  2768. outBuffer[0] = 0xff;
  2769. outBuffer[1] = 0xff;
  2770. outBuffer[2] = 0x00;
  2771. outBuffer[3] = 0x24;
  2772. outBuffer[4] = WIFI_CMD_GET_WHITE_CARD;
  2773. outBuffer[5] = inputBuffer[9];
  2774. outBuffer[6] = ((strlen((char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[inputBuffer[9]])>0)?0x00:0x01);
  2775. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[inputBuffer[9]], ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[inputBuffer[9]]));
  2776. outBuffer[39] = chksumCal(outBuffer);
  2777. break;
  2778. case WIFI_CMD_GET_SYS_ALARM_CODE:
  2779. DEBUG_INFO("Receive get alarm code command.\n");
  2780. tx_size = 70;
  2781. outBuffer[0] = 0xff;
  2782. outBuffer[1] = 0xff;
  2783. outBuffer[2] = 0x00;
  2784. outBuffer[3] = 0x42;
  2785. outBuffer[4] = WIFI_CMD_GET_SYS_ALARM_CODE;
  2786. memcpy(&outBuffer[5], ShmStatusCodeData->FaultCode.FaultEvents.FaultVal, ARRAY_SIZE(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal));
  2787. memcpy(&outBuffer[13], ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal, ARRAY_SIZE(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal));
  2788. memcpy(&outBuffer[29], ShmStatusCodeData->InfoCode.InfoEvents.InfoVal, ARRAY_SIZE(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal));
  2789. outBuffer[69] = chksumCal(outBuffer);
  2790. break;
  2791. case WIFI_CMD_GET_SYS_ID:
  2792. DEBUG_INFO("Receive get system ID command.\n");
  2793. DEBUG_INFO("System ID: %s.\n", ShmSysConfigAndInfo->SysConfig.SystemId);
  2794. tx_size = 70;
  2795. outBuffer[0] = 0xff;
  2796. outBuffer[1] = 0xff;
  2797. outBuffer[2] = 0x00;
  2798. outBuffer[3] = 0x42;
  2799. outBuffer[4] = WIFI_CMD_GET_SYS_ID;
  2800. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.SystemId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  2801. outBuffer[69] = chksumCal(outBuffer);
  2802. break;
  2803. case WIFI_CMD_GET_RATING_CURRENT:
  2804. DEBUG_INFO("Receive get rating current command.\n");
  2805. DEBUG_INFO("Rating current: %d.\n", ShmSysConfigAndInfo->SysConfig.RatingCurrent);
  2806. DEBUG_INFO("AcRating current: %d.\n", ShmSysConfigAndInfo->SysConfig.AcRatingCurrent);
  2807. tx_size = 10;
  2808. outBuffer[0] = 0xff;
  2809. outBuffer[1] = 0xff;
  2810. outBuffer[2] = 0x00;
  2811. outBuffer[3] = 0x06;
  2812. outBuffer[4] = WIFI_CMD_GET_RATING_CURRENT;
  2813. outBuffer[5] = (ShmSysConfigAndInfo->SysConfig.RatingCurrent>>0x08) & 0xff;
  2814. outBuffer[6] = (ShmSysConfigAndInfo->SysConfig.RatingCurrent>>0x00) & 0xff;
  2815. outBuffer[7] = (ShmSysConfigAndInfo->SysConfig.AcRatingCurrent>>0x08) & 0xff;
  2816. outBuffer[8] = (ShmSysConfigAndInfo->SysConfig.AcRatingCurrent>>0x00) & 0xff;
  2817. outBuffer[9] = chksumCal(outBuffer);
  2818. break;
  2819. case WIFI_CMD_GET_VER_RFS:
  2820. DEBUG_INFO("Receive get RFS version command.\n");
  2821. DEBUG_INFO("Version: %s.\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2822. tx_size = 38;
  2823. outBuffer[0] = 0xff;
  2824. outBuffer[1] = 0xff;
  2825. outBuffer[2] = 0x00;
  2826. outBuffer[3] = 0x22;
  2827. outBuffer[4] = WIFI_CMD_GET_VER_RFS;
  2828. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev));
  2829. outBuffer[37] = chksumCal(outBuffer);
  2830. break;
  2831. case WIFI_CMD_GET_VER_PRIMARY_CSU:
  2832. DEBUG_INFO("Receive get primary MCU version command.\n");
  2833. DEBUG_INFO("Version: %s.\n", ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev);
  2834. tx_size = 38;
  2835. outBuffer[0] = 0xff;
  2836. outBuffer[1] = 0xff;
  2837. outBuffer[2] = 0x00;
  2838. outBuffer[3] = 0x22;
  2839. outBuffer[4] = WIFI_CMD_GET_VER_PRIMARY_CSU;
  2840. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev));
  2841. outBuffer[37] = chksumCal(outBuffer);
  2842. break;
  2843. case WIFI_CMD_GET_VER_LCM:
  2844. DEBUG_INFO("Receive get LCM version command.\n");
  2845. DEBUG_INFO("Version: %s.\n", ShmSysConfigAndInfo->SysInfo.LcmHwRev);
  2846. tx_size = 38;
  2847. outBuffer[0] = 0xff;
  2848. outBuffer[1] = 0xff;
  2849. outBuffer[2] = 0x00;
  2850. outBuffer[3] = 0x22;
  2851. outBuffer[4] = WIFI_CMD_GET_VER_LCM;
  2852. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.LcmHwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.LcmHwRev));
  2853. outBuffer[37] = chksumCal(outBuffer);
  2854. break;
  2855. case WIFI_CMD_GET_PSU_COUNT:
  2856. DEBUG_INFO("Receive get PSU quantity command.\n");
  2857. DEBUG_INFO("Quantity: %d.\n", ShmPsuData->SystemPresentPsuQuantity);
  2858. tx_size = 7;
  2859. outBuffer[0] = 0xff;
  2860. outBuffer[1] = 0xff;
  2861. outBuffer[2] = 0x00;
  2862. outBuffer[3] = 0x03;
  2863. outBuffer[4] = WIFI_CMD_GET_PSU_COUNT;
  2864. outBuffer[5] = ShmPsuData->SystemPresentPsuQuantity;
  2865. outBuffer[6] = chksumCal(outBuffer);
  2866. break;
  2867. case WIFI_CMD_GET_VER_PSU:
  2868. DEBUG_INFO("Receive get PSU-%d version command.\n", inputBuffer[9]);
  2869. DEBUG_INFO("Primary version: %s.\n", ShmPsuData->PsuVersion[inputBuffer[9]].FwPrimaryVersion);
  2870. DEBUG_INFO("Second version: %s.\n", ShmPsuData->PsuVersion[inputBuffer[9]].FwSecondVersion);
  2871. tx_size = 39;
  2872. outBuffer[0] = 0xff;
  2873. outBuffer[1] = 0xff;
  2874. outBuffer[2] = 0x00;
  2875. outBuffer[3] = 0x23;
  2876. outBuffer[4] = WIFI_CMD_GET_VER_PSU;
  2877. outBuffer[5] = inputBuffer[9];
  2878. memcpy(&outBuffer[6], ShmPsuData->PsuVersion[inputBuffer[9]].FwPrimaryVersion, ARRAY_SIZE(ShmPsuData->PsuVersion[inputBuffer[9]].FwPrimaryVersion));
  2879. memcpy(&outBuffer[22], ShmPsuData->PsuVersion[inputBuffer[9]].FwSecondVersion, ARRAY_SIZE(ShmPsuData->PsuVersion[inputBuffer[9]].FwSecondVersion));
  2880. outBuffer[38] = chksumCal(outBuffer);
  2881. break;
  2882. case WIFI_CMD_GET_VER_AUX_POWER:
  2883. DEBUG_INFO("Receive get AUX power version command.\n");
  2884. DEBUG_INFO("Version: %s.\n", ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev);
  2885. tx_size = 38;
  2886. outBuffer[0] = 0xff;
  2887. outBuffer[1] = 0xff;
  2888. outBuffer[2] = 0x00;
  2889. outBuffer[3] = 0x22;
  2890. outBuffer[4] = WIFI_CMD_GET_VER_AUX_POWER;
  2891. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev));
  2892. outBuffer[37] = chksumCal(outBuffer);
  2893. break;
  2894. case WIFI_CMD_GET_VER_FAN:
  2895. DEBUG_INFO("Receive get fan board version command.\n");
  2896. DEBUG_INFO("Version: %s.\n", ShmSysConfigAndInfo->SysInfo.FanModuleFwRev);
  2897. tx_size = 38;
  2898. outBuffer[0] = 0xff;
  2899. outBuffer[1] = 0xff;
  2900. outBuffer[2] = 0x00;
  2901. outBuffer[3] = 0x22;
  2902. outBuffer[4] = WIFI_CMD_GET_VER_FAN;
  2903. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  2904. outBuffer[37] = chksumCal(outBuffer);
  2905. break;
  2906. case WIFI_CMD_GET_VER_RELAY:
  2907. DEBUG_INFO("Receive get relay board version command.\n");
  2908. DEBUG_INFO("Version: %s.\n", ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev);
  2909. tx_size = 38;
  2910. outBuffer[0] = 0xff;
  2911. outBuffer[1] = 0xff;
  2912. outBuffer[2] = 0x00;
  2913. outBuffer[3] = 0x22;
  2914. outBuffer[4] = WIFI_CMD_GET_VER_RELAY;
  2915. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  2916. outBuffer[37] = chksumCal(outBuffer);
  2917. break;
  2918. case WIFI_CMD_GET_VER_TELECOMM:
  2919. DEBUG_INFO("Receive get telecomm version command.\n");
  2920. DEBUG_INFO("Vesion: %s.\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  2921. tx_size = 38;
  2922. outBuffer[0] = 0xff;
  2923. outBuffer[1] = 0xff;
  2924. outBuffer[2] = 0x00;
  2925. outBuffer[3] = 0x22;
  2926. outBuffer[4] = WIFI_CMD_GET_VER_TELECOMM;
  2927. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer));
  2928. outBuffer[37] = chksumCal(outBuffer);
  2929. break;
  2930. case WIFI_CMD_GET_EVSE_CONFIG:
  2931. DEBUG_INFO("Receive get EVSE config command.\n");
  2932. DEBUG_INFO("MaxChargingEnergy: %d.\n", SysConfigTemporarily.MaxChargingEnergy);
  2933. DEBUG_INFO("MaxChargingPower: %d.\n", SysConfigTemporarily.MaxChargingPower);
  2934. DEBUG_INFO("MaxChargingCurrent: %d.\n", SysConfigTemporarily.MaxChargingCurrent);
  2935. DEBUG_INFO("MaxChargingDuration: %d.\n", SysConfigTemporarily.MaxChargingDuration);
  2936. DEBUG_INFO("AcMaxChargingCurrent: %d.\n", SysConfigTemporarily.AcMaxChargingCurrent);
  2937. tx_size = 16;
  2938. outBuffer[0] = 0xff;
  2939. outBuffer[1] = 0xff;
  2940. outBuffer[2] = 0x00;
  2941. outBuffer[3] = 0x0C;
  2942. outBuffer[4] = WIFI_CMD_GET_EVSE_CONFIG;
  2943. outBuffer[5] = (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy>>0x08) & 0xff;
  2944. outBuffer[6] = (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy>>0x00) & 0xff;
  2945. outBuffer[7] = (ShmSysConfigAndInfo->SysConfig.MaxChargingPower>>0x08) & 0xff;
  2946. outBuffer[8] = (ShmSysConfigAndInfo->SysConfig.MaxChargingPower>>0x00) & 0xff;
  2947. outBuffer[9] = (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent>>0x08) & 0xff;
  2948. outBuffer[10] = (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent>>0x00) & 0xff;
  2949. outBuffer[11] = (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration>>0x08) & 0xff;
  2950. outBuffer[12] = (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration>>0x00) & 0xff;
  2951. outBuffer[13] = (ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent>>0x08) & 0xff;
  2952. outBuffer[14] = (ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent>>0x00) & 0xff;
  2953. outBuffer[15] = chksumCal(outBuffer);
  2954. break;
  2955. case WIFI_CMD_GET_GUN_STS:
  2956. DEBUG_INFO("Receive get connector-%02d status command.\n", inputBuffer[9]);
  2957. tx_size = 21;
  2958. outBuffer[0] = 0xff;
  2959. outBuffer[1] = 0xff;
  2960. outBuffer[2] = 0x00;
  2961. outBuffer[3] = 0x11;
  2962. outBuffer[4] = WIFI_CMD_GET_GUN_STS;
  2963. outBuffer[5] = inputBuffer[9];
  2964. getConnectorInfo(inputBuffer[9], outBuffer);
  2965. outBuffer[20] = chksumCal(outBuffer);
  2966. DEBUG_INFO("Status: %d.\n", outBuffer[6]);
  2967. break;
  2968. case WIFI_CMD_GET_GUN_USER:
  2969. DEBUG_INFO("Receive get connector user command.\n");
  2970. tx_size = 40;
  2971. outBuffer[0] = 0xff;
  2972. outBuffer[1] = 0xff;
  2973. outBuffer[2] = 0x00;
  2974. outBuffer[3] = 0x24;
  2975. outBuffer[4] = WIFI_CMD_GET_GUN_USER;
  2976. outBuffer[5] = inputBuffer[9];
  2977. getConnectorUser(inputBuffer[9], outBuffer);
  2978. outBuffer[39] = chksumCal(outBuffer);
  2979. DEBUG_INFO("User ID: %s.\n", &outBuffer[7]);
  2980. break;
  2981. case WIFI_CMD_GET_SCHEDULE:
  2982. DEBUG_INFO("Receive get connector-%02d schedule command.\n", inputBuffer[9]);
  2983. tx_size = 14;
  2984. outBuffer[0] = 0xff;
  2985. outBuffer[1] = 0xff;
  2986. outBuffer[2] = 0x00;
  2987. outBuffer[3] = 0x0a;
  2988. outBuffer[4] = WIFI_CMD_GET_SCHEDULE;
  2989. outBuffer[5] = inputBuffer[9];
  2990. getConnectorSchedule(inputBuffer[9], outBuffer);
  2991. outBuffer[13] = chksumCal(outBuffer);
  2992. break;
  2993. case WIFI_CMD_GET_NETWORK_STS:
  2994. DEBUG_INFO("Receive get network status command.\n");
  2995. if(ShmSysConfigAndInfo->SysInfo.InternetConn)
  2996. DEBUG_INFO("Internet enable.\n");
  2997. else
  2998. DEBUG_INFO("Internet disable.\n");
  2999. tx_size = 7;
  3000. outBuffer[0] = 0xff;
  3001. outBuffer[1] = 0xff;
  3002. outBuffer[2] = 0x00;
  3003. outBuffer[3] = 0x03;
  3004. outBuffer[4] = WIFI_CMD_GET_NETWORK_STS;
  3005. outBuffer[5] = ShmSysConfigAndInfo->SysInfo.InternetConn;
  3006. outBuffer[6] = chksumCal(outBuffer);
  3007. break;
  3008. case WIFI_CMD_GET_ETH_DHCP:
  3009. DEBUG_INFO("Receive get eth dhcp command.\n");
  3010. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient^1)
  3011. DEBUG_INFO("DHCP enable.\n");
  3012. else
  3013. DEBUG_INFO("DHCP disable.\n");
  3014. tx_size = 7;
  3015. outBuffer[0] = 0xff;
  3016. outBuffer[1] = 0xff;
  3017. outBuffer[2] = 0x00;
  3018. outBuffer[3] = 0x03;
  3019. outBuffer[4] = WIFI_CMD_GET_ETH_DHCP;
  3020. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient^1;
  3021. outBuffer[6] = chksumCal(outBuffer);
  3022. break;
  3023. case WIFI_CMD_GET_ETH_MAC:
  3024. DEBUG_INFO("Receive get eth MAC address command.\n");
  3025. DEBUG_INFO("MAC address: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress);
  3026. tx_size = 24;
  3027. outBuffer[0] = 0xff;
  3028. outBuffer[1] = 0xff;
  3029. outBuffer[2] = 0x00;
  3030. outBuffer[3] = 0x14;
  3031. outBuffer[4] = WIFI_CMD_GET_ETH_MAC;
  3032. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress));
  3033. outBuffer[23] = chksumCal(outBuffer);
  3034. break;
  3035. case WIFI_CMD_GET_ETH_IP:
  3036. DEBUG_INFO("Receive get eth ip address command.\n");
  3037. DEBUG_INFO("IP address: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress);
  3038. tx_size = 22;
  3039. outBuffer[0] = 0xff;
  3040. outBuffer[1] = 0xff;
  3041. outBuffer[2] = 0x00;
  3042. outBuffer[3] = 0x12;
  3043. outBuffer[4] = WIFI_CMD_GET_ETH_IP;
  3044. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress));
  3045. outBuffer[21] = chksumCal(outBuffer);
  3046. break;
  3047. case WIFI_CMD_GET_ETH_SUBMASK:
  3048. DEBUG_INFO("Receive get eth submask command.\n");
  3049. DEBUG_INFO("Submask address: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  3050. tx_size = 22;
  3051. outBuffer[0] = 0xff;
  3052. outBuffer[1] = 0xff;
  3053. outBuffer[2] = 0x00;
  3054. outBuffer[3] = 0x12;
  3055. outBuffer[4] = WIFI_CMD_GET_ETH_SUBMASK;
  3056. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress));
  3057. outBuffer[21] = chksumCal(outBuffer);
  3058. break;
  3059. case WIFI_CMD_GET_ETH_GATEWAY:
  3060. DEBUG_INFO("Receive get eth gateway command.\n");
  3061. DEBUG_INFO("Gateway address: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  3062. tx_size = 22;
  3063. outBuffer[0] = 0xff;
  3064. outBuffer[1] = 0xff;
  3065. outBuffer[2] = 0x00;
  3066. outBuffer[3] = 0x12;
  3067. outBuffer[4] = WIFI_CMD_GET_ETH_GATEWAY;
  3068. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress));
  3069. outBuffer[21] = chksumCal(outBuffer);
  3070. break;
  3071. case WIFI_CMD_GET_WIFI_MODE:
  3072. DEBUG_INFO("Receive get wifi mode command.\n");
  3073. switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
  3074. {
  3075. case 0:
  3076. DEBUG_INFO("Disable.\n");
  3077. break;
  3078. case 1:
  3079. DEBUG_INFO("Station mode.\n");
  3080. break;
  3081. case 2:
  3082. DEBUG_INFO("Ap mode.\n");
  3083. break;
  3084. }
  3085. tx_size = 7;
  3086. outBuffer[0] = 0xff;
  3087. outBuffer[1] = 0xff;
  3088. outBuffer[2] = 0x00;
  3089. outBuffer[3] = 0x03;
  3090. outBuffer[4] = WIFI_CMD_GET_WIFI_MODE;
  3091. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode;
  3092. outBuffer[6] = chksumCal(outBuffer);
  3093. break;
  3094. case WIFI_CMD_GET_WIFI_SSID:
  3095. DEBUG_INFO("Receive get wifi ssid command.\n");
  3096. DEBUG_INFO("SSID: %s.\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  3097. tx_size = 261;
  3098. outBuffer[0] = 0xff;
  3099. outBuffer[1] = 0xff;
  3100. outBuffer[2] = 0x01;
  3101. outBuffer[3] = 0x01;
  3102. outBuffer[4] = WIFI_CMD_GET_WIFI_SSID;
  3103. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid));
  3104. outBuffer[260] = chksumCal(outBuffer);
  3105. break;
  3106. case WIFI_CMD_GET_WIFI_PASSWD:
  3107. DEBUG_INFO("Receive get wifi password command.\n");
  3108. DEBUG_INFO("Password: %s.\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  3109. tx_size = 261;
  3110. outBuffer[0] = 0xff;
  3111. outBuffer[1] = 0xff;
  3112. outBuffer[2] = 0x01;
  3113. outBuffer[3] = 0x01;
  3114. outBuffer[4] = WIFI_CMD_GET_WIFI_PASSWD;
  3115. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword));
  3116. outBuffer[260] = chksumCal(outBuffer);
  3117. break;
  3118. case WIFI_CMD_GET_WIFI_RSSI:
  3119. DEBUG_INFO("Receive get wifi rssi command.\n");
  3120. DEBUG_INFO("RSSI: %d.\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi);
  3121. tx_size = 7;
  3122. outBuffer[0] = 0xff;
  3123. outBuffer[1] = 0xff;
  3124. outBuffer[2] = 0x00;
  3125. outBuffer[3] = 0x03;
  3126. outBuffer[4] = WIFI_CMD_GET_WIFI_RSSI;
  3127. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi+100;
  3128. outBuffer[6] = chksumCal(outBuffer);
  3129. break;
  3130. case WIFI_CMD_GET_WIFI_DHCP:
  3131. DEBUG_INFO("Receive get wifi dhcp command.\n");
  3132. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient^1)
  3133. DEBUG_INFO("DHCP enable.\n");
  3134. else
  3135. DEBUG_INFO("DHCP disable.\n");
  3136. tx_size = 7;
  3137. outBuffer[0] = 0xff;
  3138. outBuffer[1] = 0xff;
  3139. outBuffer[2] = 0x00;
  3140. outBuffer[3] = 0x03;
  3141. outBuffer[4] = WIFI_CMD_GET_WIFI_DHCP;
  3142. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient^1;
  3143. outBuffer[6] = chksumCal(outBuffer);
  3144. break;
  3145. case WIFI_CMD_GET_WIFI_MAC:
  3146. DEBUG_INFO("Receive get wifi MAC command.\n");
  3147. DEBUG_INFO("MAC address: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  3148. tx_size = 24;
  3149. outBuffer[0] = 0xff;
  3150. outBuffer[1] = 0xff;
  3151. outBuffer[2] = 0x00;
  3152. outBuffer[3] = 0x14;
  3153. outBuffer[4] = WIFI_CMD_GET_WIFI_MAC;
  3154. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress));
  3155. outBuffer[23] = chksumCal(outBuffer);
  3156. break;
  3157. case WIFI_CMD_GET_WIFI_IP:
  3158. DEBUG_INFO("Receive get wifi ip address command.\n");
  3159. DEBUG_INFO("IP address: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  3160. tx_size = 22;
  3161. outBuffer[0] = 0xff;
  3162. outBuffer[1] = 0xff;
  3163. outBuffer[2] = 0x00;
  3164. outBuffer[3] = 0x12;
  3165. outBuffer[4] = WIFI_CMD_GET_WIFI_IP;
  3166. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress));
  3167. outBuffer[21] = chksumCal(outBuffer);
  3168. break;
  3169. case WIFI_CMD_GET_WIFI_NETMASK:
  3170. DEBUG_INFO("Receive get wifi submask command.\n");
  3171. DEBUG_INFO("Submask address: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  3172. tx_size = 22;
  3173. outBuffer[0] = 0xff;
  3174. outBuffer[1] = 0xff;
  3175. outBuffer[2] = 0x00;
  3176. outBuffer[3] = 0x12;
  3177. outBuffer[4] = WIFI_CMD_GET_WIFI_NETMASK;
  3178. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress));
  3179. outBuffer[21] = chksumCal(outBuffer);
  3180. break;
  3181. case WIFI_CMD_GET_WIFI_GATEWAY:
  3182. DEBUG_INFO("Receive get wifi gateway command.\n");
  3183. DEBUG_INFO("Gateway address: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  3184. tx_size = 22;
  3185. outBuffer[0] = 0xff;
  3186. outBuffer[1] = 0xff;
  3187. outBuffer[2] = 0x00;
  3188. outBuffer[3] = 0x12;
  3189. outBuffer[4] = WIFI_CMD_GET_WIFI_GATEWAY;
  3190. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress));
  3191. outBuffer[21] = chksumCal(outBuffer);
  3192. break;
  3193. case WIFI_CMD_GET_TELE_APN:
  3194. DEBUG_INFO("Receive get telecomm APN command.\n");
  3195. DEBUG_INFO("APN: %s.\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn);
  3196. tx_size = 262;
  3197. outBuffer[0] = 0xff;
  3198. outBuffer[1] = 0xff;
  3199. outBuffer[2] = 0x01;
  3200. outBuffer[3] = 0x02;
  3201. outBuffer[4] = WIFI_CMD_GET_TELE_APN;
  3202. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn));
  3203. outBuffer[261] = chksumCal(outBuffer);
  3204. break;
  3205. case WIFI_CMD_GET_TELE_RSSI:
  3206. DEBUG_INFO("Receive get telecomm rssi command.\n");
  3207. DEBUG_INFO("RSSI: %d.\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
  3208. tx_size = 7;
  3209. outBuffer[0] = 0xff;
  3210. outBuffer[1] = 0xff;
  3211. outBuffer[2] = 0x00;
  3212. outBuffer[3] = 0x03;
  3213. outBuffer[4] = WIFI_CMD_GET_TELE_RSSI;
  3214. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi+100;
  3215. outBuffer[6] = chksumCal(outBuffer);
  3216. break;
  3217. case WIFI_CMD_GET_TELE_CHAP_ID:
  3218. DEBUG_INFO("Receive get telecomm CHAP ID command.\n");
  3219. DEBUG_INFO("ID: %s.\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  3220. tx_size = 262;
  3221. outBuffer[0] = 0xff;
  3222. outBuffer[1] = 0xff;
  3223. outBuffer[2] = 0x01;
  3224. outBuffer[3] = 0x02;
  3225. outBuffer[4] = WIFI_CMD_GET_TELE_CHAP_ID;
  3226. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId));
  3227. outBuffer[261] = chksumCal(outBuffer);
  3228. break;
  3229. case WIFI_CMD_GET_TELE_CHAP_PASSWD:
  3230. DEBUG_INFO("Receive get telecomm CHAP password command.\n");
  3231. DEBUG_INFO("Password: %s.\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
  3232. tx_size = 262;
  3233. outBuffer[0] = 0xff;
  3234. outBuffer[1] = 0xff;
  3235. outBuffer[2] = 0x01;
  3236. outBuffer[3] = 0x02;
  3237. outBuffer[4] = WIFI_CMD_GET_TELE_CHAP_PASSWD;
  3238. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd));
  3239. outBuffer[261] = chksumCal(outBuffer);
  3240. break;
  3241. case WIFI_CMD_GET_TELE_IMEI:
  3242. DEBUG_INFO("Receive get telecomm IMEI command.\n");
  3243. DEBUG_INFO("Telecomm IMEI: %s.\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  3244. tx_size = 22;
  3245. outBuffer[0] = 0xff;
  3246. outBuffer[1] = 0xff;
  3247. outBuffer[2] = 0x00;
  3248. outBuffer[3] = 0x12;
  3249. outBuffer[4] = WIFI_CMD_GET_TELE_IMEI;
  3250. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei));
  3251. outBuffer[21] = chksumCal(outBuffer);
  3252. break;
  3253. case WIFI_CMD_GET_TELE_IMSI:
  3254. DEBUG_INFO("Receive get telecomm IMSI command.\n");
  3255. DEBUG_INFO("Telecomm IMSI: %s.\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  3256. tx_size = 22;
  3257. outBuffer[0] = 0xff;
  3258. outBuffer[1] = 0xff;
  3259. outBuffer[2] = 0x00;
  3260. outBuffer[3] = 0x12;
  3261. outBuffer[4] = WIFI_CMD_GET_TELE_IMSI;
  3262. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi));
  3263. outBuffer[21] = chksumCal(outBuffer);
  3264. break;
  3265. case WIFI_CMD_GET_TELE_SIM_STS:
  3266. DEBUG_INFO("Receive get telecomm SIM status command.\n");
  3267. DEBUG_INFO("Telecomm SIM status: %d.\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus);
  3268. tx_size = 7;
  3269. outBuffer[0] = 0xff;
  3270. outBuffer[1] = 0xff;
  3271. outBuffer[2] = 0x00;
  3272. outBuffer[3] = 0x03;
  3273. outBuffer[4] = WIFI_CMD_GET_TELE_SIM_STS;
  3274. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus;
  3275. outBuffer[6] = chksumCal(outBuffer);
  3276. break;
  3277. case WIFI_CMD_GET_TELE_MODE:
  3278. DEBUG_INFO("Receive get telecommand mode command.\n");
  3279. DEBUG_INFO("Telecomm mode: %d.\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
  3280. tx_size = 7;
  3281. outBuffer[0] = 0xff;
  3282. outBuffer[1] = 0xff;
  3283. outBuffer[2] = 0x00;
  3284. outBuffer[3] = 0x03;
  3285. outBuffer[4] = WIFI_CMD_GET_TELE_MODE;
  3286. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode;
  3287. outBuffer[6] = chksumCal(outBuffer);
  3288. break;
  3289. case WIFI_CMD_GET_TELE_IP:
  3290. DEBUG_INFO("Receive get telecomm ip address command.\n");
  3291. DEBUG_INFO("IP adress: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  3292. tx_size = 22;
  3293. outBuffer[0] = 0xff;
  3294. outBuffer[1] = 0xff;
  3295. outBuffer[2] = 0x00;
  3296. outBuffer[3] = 0x12;
  3297. outBuffer[4] = WIFI_CMD_GET_TELE_IP;
  3298. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress));
  3299. outBuffer[21] = chksumCal(outBuffer);
  3300. break;
  3301. case WIFI_CMD_SET_SYSTEM_TIME:
  3302. DEBUG_INFO("Receive set system time command.\n");
  3303. rawtime = (inputBuffer[9]<<0) | (inputBuffer[10]<<8) | (inputBuffer[11]<<16) |(inputBuffer[12]<<24);
  3304. ts = *localtime(&rawtime);
  3305. sprintf((char*)cmdBuf, "date -u -s \"%04d-%02d-%02d %02d:%02d:%02d\"", (ts.tm_year+1900),
  3306. (ts.tm_mon+1),
  3307. ts.tm_mday,
  3308. ts.tm_hour,
  3309. ts.tm_min,
  3310. ts.tm_sec);
  3311. system((char*)cmdBuf);
  3312. system("hwclock -w -u");
  3313. system("hwclock -s");
  3314. tx_size = 7;
  3315. outBuffer[0] = 0xff;
  3316. outBuffer[1] = 0xff;
  3317. outBuffer[2] = 0x00;
  3318. outBuffer[3] = 0x03;
  3319. outBuffer[4] = WIFI_CMD_SET_SYSTEM_TIME;
  3320. outBuffer[5] = 0x01;
  3321. outBuffer[6] = chksumCal(outBuffer);
  3322. break;
  3323. case WIFI_CMD_SET_WHITE_CARD:
  3324. DEBUG_INFO("Receive set white list command.\n");
  3325. if((0<=inputBuffer[9]) && (inputBuffer[9]<10))
  3326. {
  3327. if(inputBuffer[10] == REGISTER)
  3328. {
  3329. // Register white card
  3330. memcpy(SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]], &inputBuffer[11],(((inputBuffer[2] << 8) | inputBuffer[3])-8));
  3331. DEBUG_INFO("Register list-%02d: %s.\n", inputBuffer[9], SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]]);
  3332. }
  3333. else if (inputBuffer[10] == CLEAR_REGISTER)
  3334. {
  3335. // Clear register white card
  3336. memset(SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]], 0x00, ARRAY_SIZE(SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]]));
  3337. DEBUG_INFO("Unregister list-%02d: %s.\n", inputBuffer[9], SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]]);
  3338. }
  3339. outBuffer[6] = 0x01;
  3340. }
  3341. else
  3342. {
  3343. outBuffer[6] = 0x00;
  3344. }
  3345. tx_size = 8;
  3346. outBuffer[0] = 0xff;
  3347. outBuffer[1] = 0xff;
  3348. outBuffer[2] = 0x00;
  3349. outBuffer[3] = 0x04;
  3350. outBuffer[4] = WIFI_CMD_SET_WHITE_CARD;
  3351. outBuffer[5] = inputBuffer[9];
  3352. outBuffer[7] = chksumCal(outBuffer);
  3353. break;
  3354. case WIFI_CMD_SET_FACTORY_RESET:
  3355. DEBUG_INFO("Receive set factory default command.\n");
  3356. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = ON;
  3357. tx_size = 7;
  3358. outBuffer[0] = 0xff;
  3359. outBuffer[1] = 0xff;
  3360. outBuffer[2] = 0x00;
  3361. outBuffer[3] = 0x03;
  3362. outBuffer[4] = WIFI_CMD_SET_FACTORY_RESET;
  3363. outBuffer[5] = 0x01;
  3364. outBuffer[6] = chksumCal(outBuffer);
  3365. break;
  3366. case WIFI_CMD_SET_EVSE_CONFIG:
  3367. DEBUG_INFO("Receive set EVSE config command.\n");
  3368. if((inputBuffer[9]>>4)&0x01)
  3369. {
  3370. SysConfigTemporarily.AcMaxChargingCurrent = (inputBuffer[18]) | (inputBuffer[19]<<8);
  3371. DEBUG_INFO("AcMaxChargingCurrent: %d.\n", SysConfigTemporarily.AcMaxChargingCurrent);
  3372. }
  3373. if((inputBuffer[9]>>3)&0x01)
  3374. {
  3375. SysConfigTemporarily.MaxChargingEnergy = (inputBuffer[10]) | (inputBuffer[11]<<8);
  3376. DEBUG_INFO("MaxChargingEnergy: %d.\n", SysConfigTemporarily.MaxChargingEnergy);
  3377. }
  3378. if((inputBuffer[9]>>2)&0x01)
  3379. {
  3380. SysConfigTemporarily.MaxChargingPower = (inputBuffer[12]) | (inputBuffer[13]<<8);
  3381. DEBUG_INFO("MaxChargingPower: %d.\n", SysConfigTemporarily.MaxChargingPower);
  3382. }
  3383. if((inputBuffer[9]>>1)&0x01)
  3384. {
  3385. SysConfigTemporarily.MaxChargingCurrent = (inputBuffer[14]) | (inputBuffer[15]<<8);
  3386. DEBUG_INFO("MaxChargingCurrent: %d.\n", SysConfigTemporarily.MaxChargingCurrent);
  3387. }
  3388. if(inputBuffer[9]&0x01)
  3389. {
  3390. SysConfigTemporarily.MaxChargingDuration = (inputBuffer[16]) | (inputBuffer[17]<<8);
  3391. DEBUG_INFO("MaxChargingDuration: %d.\n", SysConfigTemporarily.MaxChargingDuration);
  3392. }
  3393. tx_size = 7;
  3394. outBuffer[0] = 0xff;
  3395. outBuffer[1] = 0xff;
  3396. outBuffer[2] = 0x00;
  3397. outBuffer[3] = 0x03;
  3398. outBuffer[4] = WIFI_CMD_SET_EVSE_CONFIG;
  3399. outBuffer[5] = 0x01;
  3400. outBuffer[6] = chksumCal(outBuffer);
  3401. break;
  3402. case WIFI_CMD_SET_CHARGING_START:
  3403. DEBUG_INFO("Receive set connector start charging command.\n");
  3404. DEBUG_INFO("Gun_index: %d.\n", inputBuffer[9]);
  3405. tx_size = 7;
  3406. outBuffer[0] = 0xff;
  3407. outBuffer[1] = 0xff;
  3408. outBuffer[2] = 0x00;
  3409. outBuffer[3] = 0x03;
  3410. outBuffer[4] = WIFI_CMD_SET_CHARGING_START;
  3411. outBuffer[5] = (startConnectorCharging(inputBuffer[9])?0x01:0x00);
  3412. outBuffer[6] = chksumCal(outBuffer);
  3413. break;
  3414. case WIFI_CMD_SET_CHARGING_STOP:
  3415. DEBUG_INFO("Receive set connector stop charging command.\n");
  3416. DEBUG_INFO("Gun_index: %d.\n", inputBuffer[9]);
  3417. tx_size = 7;
  3418. outBuffer[0] = 0xff;
  3419. outBuffer[1] = 0xff;
  3420. outBuffer[2] = 0x00;
  3421. outBuffer[3] = 0x03;
  3422. outBuffer[4] = WIFI_CMD_SET_CHARGING_STOP;
  3423. outBuffer[5] = (stopConnectorCharging(inputBuffer[9])?0x01:0x00);
  3424. outBuffer[6] = chksumCal(outBuffer);
  3425. break;
  3426. case WIFI_CMD_SET_SCHEDULE:
  3427. DEBUG_INFO("Receive set connector schedule command.\n");
  3428. DEBUG_INFO("Gun_index: %d.\n", inputBuffer[9]);
  3429. if(inputBuffer[10])
  3430. DEBUG_INFO("Schedule enable.\n");
  3431. else
  3432. DEBUG_INFO("Schedule disable.\n");
  3433. if(inputBuffer[11])
  3434. DEBUG_INFO("Schedule type: Daily.\n");
  3435. else
  3436. DEBUG_INFO("Schedule type: Once.\n");
  3437. if(inputBuffer[12])
  3438. DEBUG_INFO("Schedule end method: Specific stop time.\n");
  3439. else
  3440. DEBUG_INFO("Schedule end method: Continuous.\n");
  3441. DEBUG_INFO("Schedule start hour: %d.\n", inputBuffer[13]);
  3442. DEBUG_INFO("Schedule start minute: %d.\n", inputBuffer[14]);
  3443. DEBUG_INFO("Schedule stop hour: %d.\n", inputBuffer[15]);
  3444. DEBUG_INFO("Schedule stop minute: %d.\n", inputBuffer[16]);
  3445. //setConnectorSchedule(inputBuffer[9], inputBuffer);
  3446. tx_size = 7;
  3447. outBuffer[0] = 0xff;
  3448. outBuffer[1] = 0xff;
  3449. outBuffer[2] = 0x00;
  3450. outBuffer[3] = 0x03;
  3451. outBuffer[4] = WIFI_CMD_SET_SCHEDULE;
  3452. outBuffer[5] = (setConnectorSchedule(inputBuffer[9], inputBuffer)?0x01:0x00);
  3453. outBuffer[6] = chksumCal(outBuffer);
  3454. break;
  3455. case WIFI_CMD_SET_ETH_CONFIG:
  3456. DEBUG_INFO("Receive set eth dhcp command.\n");
  3457. if(inputBuffer[9])
  3458. DEBUG_INFO("DHCP enable.\n");
  3459. else
  3460. DEBUG_INFO("DHCP disable.\n");
  3461. SysConfigTemporarily.Eth0Interface.EthDhcpClient = inputBuffer[9]^1;
  3462. tx_size = 7;
  3463. outBuffer[0] = 0xff;
  3464. outBuffer[1] = 0xff;
  3465. outBuffer[2] = 0x00;
  3466. outBuffer[3] = 0x03;
  3467. outBuffer[4] = WIFI_CMD_SET_ETH_CONFIG;
  3468. outBuffer[5] = 0x01;
  3469. outBuffer[6] = chksumCal(outBuffer);
  3470. break;
  3471. case WIFI_CMD_SET_ETH_IP:
  3472. DEBUG_INFO("Receive set eth ip address command.\n");
  3473. memset(SysConfigTemporarily.Eth0Interface.EthIpAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.Eth0Interface.EthIpAddress));
  3474. memcpy(SysConfigTemporarily.Eth0Interface.EthIpAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3475. DEBUG_INFO("IP adress: %s\n", SysConfigTemporarily.Eth0Interface.EthIpAddress);
  3476. tx_size = 7;
  3477. outBuffer[0] = 0xff;
  3478. outBuffer[1] = 0xff;
  3479. outBuffer[2] = 0x00;
  3480. outBuffer[3] = 0x03;
  3481. outBuffer[4] = WIFI_CMD_SET_ETH_IP;
  3482. outBuffer[5] = 0x01;
  3483. outBuffer[6] = chksumCal(outBuffer);
  3484. break;
  3485. case WIFI_CMD_SET_ETH_SUBMASK:
  3486. DEBUG_INFO("Receive set eth submask command.\n");
  3487. memset(SysConfigTemporarily.Eth0Interface.EthSubmaskAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.Eth0Interface.EthSubmaskAddress));
  3488. memcpy(SysConfigTemporarily.Eth0Interface.EthSubmaskAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3489. DEBUG_INFO("Submask adress: %s\n", SysConfigTemporarily.Eth0Interface.EthSubmaskAddress);
  3490. tx_size = 7;
  3491. outBuffer[0] = 0xff;
  3492. outBuffer[1] = 0xff;
  3493. outBuffer[2] = 0x00;
  3494. outBuffer[3] = 0x03;
  3495. outBuffer[4] = WIFI_CMD_SET_ETH_SUBMASK;
  3496. outBuffer[5] = 0x01;
  3497. outBuffer[6] = chksumCal(outBuffer);
  3498. break;
  3499. case WIFI_CMD_SET_ETH_GATEWAY:
  3500. DEBUG_INFO("Receive set eth gateway command.\n");
  3501. memset(SysConfigTemporarily.Eth0Interface.EthGatewayAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.Eth0Interface.EthGatewayAddress));
  3502. memcpy(SysConfigTemporarily.Eth0Interface.EthGatewayAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3503. DEBUG_INFO("Gateway adress: %s\n", SysConfigTemporarily.Eth0Interface.EthGatewayAddress);
  3504. tx_size = 7;
  3505. outBuffer[0] = 0xff;
  3506. outBuffer[1] = 0xff;
  3507. outBuffer[2] = 0x00;
  3508. outBuffer[3] = 0x03;
  3509. outBuffer[4] = WIFI_CMD_SET_ETH_GATEWAY;
  3510. outBuffer[5] = 0x01;
  3511. outBuffer[6] = chksumCal(outBuffer);
  3512. break;
  3513. case WIFI_CMD_SET_WIFI_MODE:
  3514. DEBUG_INFO("Receive set wifi mode command.\n");
  3515. switch(inputBuffer[9])
  3516. {
  3517. case 0:
  3518. DEBUG_INFO("WIFI disable.\n");
  3519. break;
  3520. case 1:
  3521. DEBUG_INFO("WIFI station mode.\n");
  3522. break;
  3523. case 2:
  3524. DEBUG_INFO("WIFI ap mode.\n");
  3525. break;
  3526. }
  3527. SysConfigTemporarily.AthInterface.WifiMode = inputBuffer[9];
  3528. tx_size = 7;
  3529. outBuffer[0] = 0xff;
  3530. outBuffer[1] = 0xff;
  3531. outBuffer[2] = 0x00;
  3532. outBuffer[3] = 0x03;
  3533. outBuffer[4] = WIFI_CMD_SET_WIFI_MODE;
  3534. outBuffer[5] = 0x01;
  3535. outBuffer[6] = chksumCal(outBuffer);
  3536. break;
  3537. case WIFI_CMD_SET_WIFI_SSID:
  3538. DEBUG_INFO("Receive set wifi SSID command.\n");
  3539. memset(SysConfigTemporarily.AthInterface.WifiSsid, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiSsid));
  3540. memcpy(SysConfigTemporarily.AthInterface.WifiSsid, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3541. DEBUG_INFO("SSID: %s\n", SysConfigTemporarily.AthInterface.WifiSsid);
  3542. tx_size = 7;
  3543. outBuffer[0] = 0xff;
  3544. outBuffer[1] = 0xff;
  3545. outBuffer[2] = 0x00;
  3546. outBuffer[3] = 0x03;
  3547. outBuffer[4] = WIFI_CMD_SET_WIFI_SSID;
  3548. outBuffer[5] = 0x01;
  3549. outBuffer[6] = chksumCal(outBuffer);
  3550. break;
  3551. case WIFI_CMD_SET_WIFI_PASSWD:
  3552. DEBUG_INFO("Receive set wifi password command.\n");
  3553. memset(SysConfigTemporarily.AthInterface.WifiPassword, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiPassword));
  3554. memcpy(SysConfigTemporarily.AthInterface.WifiPassword, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3555. DEBUG_INFO("Password: %s\n", SysConfigTemporarily.AthInterface.WifiPassword);
  3556. tx_size = 7;
  3557. outBuffer[0] = 0xff;
  3558. outBuffer[1] = 0xff;
  3559. outBuffer[2] = 0x00;
  3560. outBuffer[3] = 0x03;
  3561. outBuffer[4] = WIFI_CMD_SET_WIFI_PASSWD;
  3562. outBuffer[5] = 0x01;
  3563. outBuffer[6] = chksumCal(outBuffer);
  3564. break;
  3565. case WIFI_CMD_SET_WIFI_DHCP:
  3566. DEBUG_INFO("Receive set wifi dhcp command.\n");
  3567. if(inputBuffer[9])
  3568. DEBUG_INFO("DHCP enable.\n");
  3569. else
  3570. DEBUG_INFO("DHCP disable.\n");
  3571. SysConfigTemporarily.AthInterface.WifiDhcpClient = inputBuffer[9]^1;
  3572. tx_size = 7;
  3573. outBuffer[0] = 0xff;
  3574. outBuffer[1] = 0xff;
  3575. outBuffer[2] = 0x00;
  3576. outBuffer[3] = 0x03;
  3577. outBuffer[4] = WIFI_CMD_SET_WIFI_DHCP;
  3578. outBuffer[5] = 0x01;
  3579. outBuffer[6] = chksumCal(outBuffer);
  3580. break;
  3581. case WIFI_CMD_SET_WIFI_IP:
  3582. DEBUG_INFO("Receive set wifi ip address command.\n");
  3583. memset(SysConfigTemporarily.AthInterface.WifiIpAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiIpAddress));
  3584. memcpy(SysConfigTemporarily.AthInterface.WifiIpAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3585. DEBUG_INFO("IP adress: %s\n", SysConfigTemporarily.AthInterface.WifiIpAddress);
  3586. tx_size = 7;
  3587. outBuffer[0] = 0xff;
  3588. outBuffer[1] = 0xff;
  3589. outBuffer[2] = 0x00;
  3590. outBuffer[3] = 0x03;
  3591. outBuffer[4] = WIFI_CMD_SET_WIFI_IP;
  3592. outBuffer[5] = 0x01;
  3593. outBuffer[6] = chksumCal(outBuffer);
  3594. break;
  3595. case WIFI_CMD_SET_WIFI_NETMASK:
  3596. DEBUG_INFO("Receive set wifi submake command.\n");
  3597. DEBUG_INFO("Submask adress: %d.%d.%d.%d\n", inputBuffer[9], inputBuffer[10], inputBuffer[11], inputBuffer[12]);
  3598. memset(SysConfigTemporarily.AthInterface.WifiSubmaskAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiSubmaskAddress));
  3599. memcpy(SysConfigTemporarily.AthInterface.WifiSubmaskAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3600. DEBUG_INFO("Submask adress: %s\n", SysConfigTemporarily.AthInterface.WifiSubmaskAddress);
  3601. tx_size = 7;
  3602. outBuffer[0] = 0xff;
  3603. outBuffer[1] = 0xff;
  3604. outBuffer[2] = 0x00;
  3605. outBuffer[3] = 0x03;
  3606. outBuffer[4] = WIFI_CMD_SET_WIFI_NETMASK;
  3607. outBuffer[5] = 0x01;
  3608. outBuffer[6] = chksumCal(outBuffer);
  3609. break;
  3610. case WIFI_CMD_SET_WIFI_GATEWAY:
  3611. DEBUG_INFO("Receive set wifi gateway command.\n");
  3612. memset(SysConfigTemporarily.AthInterface.WifiGatewayAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiGatewayAddress));
  3613. memcpy(SysConfigTemporarily.AthInterface.WifiGatewayAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3614. DEBUG_INFO("Gateway adress: %s\n", SysConfigTemporarily.AthInterface.WifiGatewayAddress);
  3615. tx_size = 7;
  3616. outBuffer[0] = 0xff;
  3617. outBuffer[1] = 0xff;
  3618. outBuffer[2] = 0x00;
  3619. outBuffer[3] = 0x03;
  3620. outBuffer[4] = WIFI_CMD_SET_WIFI_GATEWAY;
  3621. outBuffer[5] = 0x01;
  3622. outBuffer[6] = chksumCal(outBuffer);
  3623. break;
  3624. case WIFI_CMD_SET_TELE_APN:
  3625. DEBUG_INFO("Receive set telecomm APN command.\n");
  3626. memset(SysConfigTemporarily.TelecomInterface.TelcomApn, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomApn));
  3627. memcpy(SysConfigTemporarily.TelecomInterface.TelcomApn, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3628. DEBUG_INFO("APN: %s\n", SysConfigTemporarily.TelecomInterface.TelcomApn);
  3629. tx_size = 7;
  3630. outBuffer[0] = 0xff;
  3631. outBuffer[1] = 0xff;
  3632. outBuffer[2] = 0x00;
  3633. outBuffer[3] = 0x03;
  3634. outBuffer[4] = WIFI_CMD_SET_TELE_APN;
  3635. outBuffer[5] = 0x01;
  3636. outBuffer[6] = chksumCal(outBuffer);
  3637. break;
  3638. case WIFI_CMD_SET_TELE_CHAP_ID:
  3639. DEBUG_INFO("Receive set telecomm CHAP ID command.\n");
  3640. memset(SysConfigTemporarily.TelecomInterface.TelcomChapPapId, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomChapPapId));
  3641. memcpy(SysConfigTemporarily.TelecomInterface.TelcomChapPapId, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3642. DEBUG_INFO("ID: %s\n", SysConfigTemporarily.TelecomInterface.TelcomChapPapId);
  3643. tx_size = 7;
  3644. outBuffer[0] = 0xff;
  3645. outBuffer[1] = 0xff;
  3646. outBuffer[2] = 0x00;
  3647. outBuffer[3] = 0x03;
  3648. outBuffer[4] = WIFI_CMD_SET_TELE_CHAP_ID;
  3649. outBuffer[5] = 0x01;
  3650. outBuffer[6] = chksumCal(outBuffer);
  3651. break;
  3652. case WIFI_CMD_SET_TELE_CHAP_PASSWD:
  3653. DEBUG_INFO("Receive set telecomm CHAP password command.\n");
  3654. memset(SysConfigTemporarily.TelecomInterface.TelcomChapPapPwd, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomChapPapPwd));
  3655. memcpy(SysConfigTemporarily.TelecomInterface.TelcomChapPapPwd, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3656. DEBUG_INFO("Password: %s\n", SysConfigTemporarily.TelecomInterface.TelcomChapPapPwd);
  3657. tx_size = 7;
  3658. outBuffer[0] = 0xff;
  3659. outBuffer[1] = 0xff;
  3660. outBuffer[2] = 0x00;
  3661. outBuffer[3] = 0x03;
  3662. outBuffer[4] = WIFI_CMD_SET_TELE_CHAP_PASSWD;
  3663. outBuffer[5] = 0x01;
  3664. outBuffer[6] = chksumCal(outBuffer);
  3665. break;
  3666. case WIFI_CMD_SET_TELE_IP:
  3667. DEBUG_INFO("Receive set telecomm ip address command.\n");
  3668. memset(SysConfigTemporarily.TelecomInterface.TelcomIpAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomIpAddress));
  3669. memcpy(SysConfigTemporarily.TelecomInterface.TelcomIpAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  3670. DEBUG_INFO("IP adress: %s\n", SysConfigTemporarily.TelecomInterface.TelcomIpAddress);
  3671. tx_size = 7;
  3672. outBuffer[0] = 0xff;
  3673. outBuffer[1] = 0xff;
  3674. outBuffer[2] = 0x00;
  3675. outBuffer[3] = 0x03;
  3676. outBuffer[4] = WIFI_CMD_SET_TELE_IP;
  3677. outBuffer[5] = 0x01;
  3678. outBuffer[6] = chksumCal(outBuffer);
  3679. break;
  3680. case WIFI_CMD_SET_CONFIRM:
  3681. DEBUG_INFO("Receive set confirm configuration command.\n");
  3682. // Copy SysConfigTemporarily to ShmSysConfigAndInfo->SysConfig
  3683. memcpy(&ShmSysConfigAndInfo->SysConfig,&SysConfigTemporarily, sizeof(struct SysConfigData));
  3684. tx_size = 7;
  3685. outBuffer[0] = 0xff;
  3686. outBuffer[1] = 0xff;
  3687. outBuffer[2] = 0x00;
  3688. outBuffer[3] = 0x03;
  3689. outBuffer[4] = WIFI_CMD_SET_CONFIRM;
  3690. outBuffer[5] = (StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig)!=0x01?0x00:0x01);
  3691. outBuffer[6] = chksumCal(outBuffer);
  3692. Sys_Flag.isPkill = ON;
  3693. break;
  3694. case WIFI_CMD_SET_DISCARD:
  3695. DEBUG_INFO("Receive set discard command.\n");
  3696. // Copy ShmSysConfigAndInfo to SysConfigTemporarily
  3697. memcpy(&SysConfigTemporarily, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  3698. tx_size = 7;
  3699. outBuffer[0] = 0xff;
  3700. outBuffer[1] = 0xff;
  3701. outBuffer[2] = 0x00;
  3702. outBuffer[3] = 0x03;
  3703. outBuffer[4] = WIFI_CMD_SET_DISCARD;
  3704. outBuffer[5] = 0x01;
  3705. outBuffer[6] = chksumCal(outBuffer);
  3706. break;
  3707. default:
  3708. DEBUG_WARN("Receive unknown command.\n");
  3709. tx_size = 6;
  3710. outBuffer[0] = 0xff;
  3711. outBuffer[1] = 0xff;
  3712. outBuffer[2] = 0x00;
  3713. outBuffer[3] = 0x02;
  3714. outBuffer[4] = WIFI_CMD_UNKNOW;
  3715. outBuffer[5] = chksumCal(outBuffer);
  3716. break;
  3717. }
  3718. }
  3719. else
  3720. {
  3721. DEBUG_WARN("Receive command check sum error.\n");
  3722. tx_size = 6;
  3723. outBuffer[0] = 0xff;
  3724. outBuffer[1] = 0xff;
  3725. outBuffer[2] = 0x00;
  3726. outBuffer[3] = 0x02;
  3727. outBuffer[4] = inputBuffer[8];
  3728. outBuffer[5] = chksumCal(outBuffer);
  3729. }
  3730. displayMessage(outBuffer, tx_size, NO);
  3731. send(clientSockFd, outBuffer, tx_size, 0);
  3732. }
  3733. if(read_size == 0)
  3734. {
  3735. DEBUG_INFO("Client disconnected.\n");
  3736. wifi_login_info.loginRole = ROLE_UNKNOWN;
  3737. memset(&wifi_login_info.loginId[0], 0x00, ARRAY_SIZE(wifi_login_info.loginId));
  3738. close(clientSockFd);
  3739. if(Sys_Flag.isPkill == ON)
  3740. {
  3741. Sys_Flag.isPkill = OFF;
  3742. shutdown(sockFd, SHUT_RDWR);
  3743. close(sockFd);
  3744. DEBUG_INFO("pKill Module_Wifi after set confirm...\n");
  3745. system("pkill Module_Wifi");
  3746. }
  3747. // Copy ShmSysConfigAndInfo to SysConfigTemporarily
  3748. memcpy(&SysConfigTemporarily, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  3749. fflush(stdout);
  3750. }
  3751. else if(read_size == -1)
  3752. {
  3753. DEBUG_ERROR("Socket recv failed.\n");
  3754. }
  3755. exit(0);
  3756. }
  3757. else
  3758. {
  3759. // if parent, close the socket and go back to listening new requests
  3760. close(clientSockFd);
  3761. }
  3762. sleep(1);
  3763. }
  3764. return FAIL;
  3765. }
  3766. //==========================================
  3767. // Common routine
  3768. //==========================================
  3769. void trim(char *s)
  3770. {
  3771. int i=0, j, k, l=0;
  3772. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  3773. i++;
  3774. j = strlen(s)-1;
  3775. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  3776. j--;
  3777. if(i==0 && j==strlen(s)-1) { }
  3778. else if(i==0) s[j+1] = '\0';
  3779. else {
  3780. for(k=i; k<=j; k++) s[l++] = s[k];
  3781. s[l] = '\0';
  3782. }
  3783. }
  3784. int mystrcmp(char *p1,char *p2)
  3785. {
  3786. while(*p1==*p2)
  3787. {
  3788. if(*p1=='\0' || *p2=='\0')
  3789. break;
  3790. p1++;
  3791. p2++;
  3792. }
  3793. if(*p1=='\0' && *p2=='\0')
  3794. return(PASS);
  3795. else
  3796. return(FAIL);
  3797. }
  3798. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  3799. {
  3800. strncpy(dest, src + start, cnt);
  3801. dest[cnt] = 0;
  3802. }
  3803. void split(char **arr, char *str, const char *del)
  3804. {
  3805. char *s = strtok(str, del);
  3806. while(s != NULL)
  3807. {
  3808. *arr++ = s;
  3809. s = strtok(NULL, del);
  3810. }
  3811. }
  3812. int checkIP(void)
  3813. {
  3814. // DHCP or static setting
  3815. int isGetIP = FAIL;
  3816. FILE *fp;
  3817. char buf[512];
  3818. char tmp[512];
  3819. char cmd[512];
  3820. if(Wifi.ipType == IP_TYPE_DHCP) {
  3821. // Get IP address & net mask
  3822. strcpy(cmd, "ifconfig ");
  3823. strcat(cmd, Wifi.currentInterface);
  3824. fp = popen(cmd, "r");
  3825. if(fp == NULL)
  3826. isGetIP = FAIL;
  3827. else
  3828. {
  3829. while(fgets(buf, sizeof(buf), fp) != NULL)
  3830. {
  3831. if(strstr(buf, "addr:") > 0)
  3832. {
  3833. if(strstr(buf, "192.168.1.10") > 0)
  3834. {
  3835. DEBUG_INFO("IP cannot be same as default value of Ethernet...\n");
  3836. }
  3837. else
  3838. {
  3839. isGetIP = PASS;
  3840. cnt_getIP_Fail = 0;
  3841. }
  3842. sscanf(buf, "%*s%s", tmp);
  3843. substr(Wifi.currentIPAddr, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  3844. sscanf(buf, "%*s%*s%*s%s", tmp);
  3845. substr(Wifi.currentNetmask, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  3846. }else {
  3847. }
  3848. }
  3849. }
  3850. if(isGetIP == FAIL && cnt_getIP_Fail<3) {
  3851. sprintf(cmd, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  3852. system(cmd);
  3853. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
  3854. {
  3855. sprintf(cmd, "/sbin/udhcpc -i %s -s /root/dhcp_script/wifi.script > /dev/null &", Wifi.currentInterface);
  3856. system(cmd);
  3857. }
  3858. DEBUG_INFO("Sending DHCP request...\n");
  3859. cnt_getIP_Fail++;
  3860. sleep(5);
  3861. }
  3862. pclose(fp);
  3863. }else {
  3864. strcpy(cmd, "ifconfig ");
  3865. strcat(cmd, Wifi.currentInterface);
  3866. strcat(cmd, " ");
  3867. strcat(cmd, Wifi.staticIPAddr);
  3868. fp = popen(cmd, "r");
  3869. DEBUG_INFO("Setting static IP to %s ...\n", Wifi.staticIPAddr);
  3870. pclose(fp);
  3871. isGetIP = PASS;
  3872. }
  3873. cnt_getIP_Fail = 0;
  3874. return isGetIP;
  3875. }
  3876. int restartWPA(void)
  3877. {
  3878. int result = FAIL;
  3879. FILE *fp;
  3880. char buf[512];
  3881. char cmd[50];
  3882. // Get IP address & net mask
  3883. strcpy(cmd, "wpa_cli -i ");
  3884. strcat(cmd, Wifi.currentInterface);
  3885. strcat(cmd, " reconfigure");
  3886. fp = popen(cmd, "r");
  3887. DEBUG_INFO("WPA reconfigure %s ...\n", Wifi.currentInterface);
  3888. if(fp == NULL)
  3889. result = FAIL;
  3890. else
  3891. {
  3892. while(fgets(buf, sizeof(buf), fp) != NULL)
  3893. {
  3894. if(strstr(buf, "OK") > 0)
  3895. {
  3896. DEBUG_INFO("reconfigure OK.\n");
  3897. result = PASS;
  3898. }else {
  3899. DEBUG_INFO("reconfigure failed.\n");
  3900. result = FAIL;
  3901. }
  3902. }
  3903. }
  3904. pclose(fp);
  3905. if(result == PASS) {
  3906. strcpy(cmd, "wpa_cli -i ");
  3907. strcat(cmd, Wifi.currentInterface);
  3908. strcat(cmd, " reconfigure");
  3909. fp = popen(cmd, "r");
  3910. DEBUG_INFO("WPA reconnect %s ...\n", Wifi.currentInterface);
  3911. if(fp == NULL)
  3912. result = FAIL;
  3913. else{
  3914. while(fgets(buf, sizeof(buf), fp) != NULL)
  3915. {
  3916. if(strstr(buf, "OK") > 0){
  3917. DEBUG_INFO("reconnect OK.\n");
  3918. result = PASS;
  3919. }else {
  3920. DEBUG_INFO("reconnect failed.\n");
  3921. result = FAIL;
  3922. }
  3923. }
  3924. }
  3925. pclose(fp);
  3926. }
  3927. return result;
  3928. }
  3929. int resetModule(void)
  3930. {
  3931. //refreshUSB();
  3932. #ifdef UBLOX
  3933. system("ifconfig mlan0 down");
  3934. sleep(1);
  3935. system("ifconfig mlan0 up");
  3936. #endif
  3937. #ifdef MT7601U
  3938. system("ifconfig wlan0 down");
  3939. sleep(1);
  3940. system("ifconfig wlan0 up");
  3941. #endif
  3942. DEBUG_WARN("Wifi interface restart.\n");
  3943. return PASS;
  3944. }
  3945. void proc_sta()
  3946. {
  3947. switch(Wifi_module_sts)
  3948. {
  3949. case STA_STATE_INIT:
  3950. dispReq.isShowed_IF_info = false;
  3951. // get info from shared memory
  3952. getParameters();
  3953. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3954. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  3955. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3956. isSetWPA_OK = false;
  3957. // check interface
  3958. if((strlen((const char*)Wifi_A.ssid)>0) && (isFindInterface() == PASS))
  3959. {
  3960. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  3961. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  3962. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  3963. DEBUG_INFO("=================[State 1]===================\n");
  3964. }
  3965. else
  3966. {
  3967. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
  3968. DEBUG_ERROR("Wifi support interface valid result: Fail\n");
  3969. sleep(30);
  3970. }
  3971. break;
  3972. case STA_STATE_DEVICE_DETECT:
  3973. dispReq.isShowed_IP_info = false;
  3974. if(!isSetWPA_OK && (setWPAconf() == PASS))
  3975. {
  3976. isSetWPA_OK = true;
  3977. sleep(5);
  3978. }
  3979. else if((isSetWPA_OK == true) && (isConnectAP() == PASS))
  3980. {
  3981. Wifi_module_sts = STA_STATE_AP_CONNECTED;
  3982. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  3983. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  3984. DEBUG_INFO("=================[State 2]===================\n");
  3985. }
  3986. else
  3987. {
  3988. cnt_getAP_Fail++;
  3989. DEBUG_INFO("Fail to connect to the AP %d times...\n",cnt_getAP_Fail);
  3990. if(cnt_getAP_Fail>=3)
  3991. {
  3992. resetModule();
  3993. Wifi_module_sts = STA_STATE_INIT;
  3994. cnt_getAP_Fail = 0;
  3995. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
  3996. DEBUG_INFO("=================[State 0]===================\n");
  3997. }
  3998. else
  3999. {
  4000. sleep(10);
  4001. }
  4002. }
  4003. break;
  4004. case STA_STATE_AP_CONNECTED:
  4005. getInterfaceInfo();
  4006. getLinkQuality();
  4007. DEBUG_INFO("Wifi quality: %d dBm\n", Wifi.rssi);
  4008. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = Wifi.rssi;
  4009. if(isReachableInternet() == PASS)
  4010. {
  4011. DEBUG_INFO("Wifi internet valid result: Pass\n");
  4012. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=0;
  4013. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=1;
  4014. cnt_pingDNS_Fail = 0;
  4015. sleep(30);
  4016. }
  4017. else
  4018. {
  4019. cnt_pingDNS_Fail++;
  4020. DEBUG_INFO("Wifi internet valid result: Fail %d time\n", cnt_pingDNS_Fail);
  4021. sleep(5);
  4022. }
  4023. if(cnt_pingDNS_Fail >= 3)
  4024. {
  4025. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  4026. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  4027. cnt_pingDNS_Fail = 0;
  4028. DEBUG_INFO("Ping DNS failed...\n");
  4029. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  4030. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  4031. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  4032. resetModule();
  4033. if(isFindInterface() == PASS)
  4034. {
  4035. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  4036. getParameters();
  4037. isSetWPA_OK = false;
  4038. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  4039. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  4040. DEBUG_INFO("=================[State 1]===================\n");
  4041. }
  4042. else
  4043. {
  4044. DEBUG_INFO("Wifi support interface valid result: Fail\n");
  4045. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  4046. Wifi_module_sts = STA_STATE_INIT;
  4047. DEBUG_INFO("=================[State 0]===================\n");
  4048. }
  4049. }
  4050. break;
  4051. default:
  4052. Wifi_module_sts = STA_STATE_INIT;
  4053. break;
  4054. }
  4055. }
  4056. void proc_ap()
  4057. {
  4058. switch(Wifi_module_sts)
  4059. {
  4060. case AP_STATE_INIT:
  4061. // get info from shared memory
  4062. getParameters();
  4063. isSetWPA_OK = false;
  4064. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  4065. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  4066. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  4067. // check interface
  4068. if(isFindInterface() == PASS)
  4069. {
  4070. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  4071. Wifi_module_sts = AP_STATE_DEVICE_DETECT;
  4072. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  4073. DEBUG_INFO("=================[State 1]===================\n");
  4074. }
  4075. else
  4076. {
  4077. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
  4078. DEBUG_ERROR("Wifi support interface valid result: Fail\n");
  4079. sleep(30);
  4080. }
  4081. break;
  4082. case AP_STATE_DEVICE_DETECT:
  4083. dispReq.isShowed_IP_info = false;
  4084. if(!isSetWPA_OK && (setWPAconf() == PASS))
  4085. {
  4086. isSetWPA_OK = true;
  4087. sleep(5);
  4088. }
  4089. else if(isSetWPA_OK && (isStartUpAP() == PASS))
  4090. {
  4091. Wifi_module_sts = AP_STATE_AP_START;
  4092. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  4093. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  4094. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  4095. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  4096. DEBUG_INFO("=================[State 2]===================\n");
  4097. }
  4098. else
  4099. {
  4100. cnt_getAP_Fail++;
  4101. DEBUG_INFO("Fail to start up AP %d times...\n",cnt_getAP_Fail);
  4102. if(cnt_getAP_Fail>=3)
  4103. {
  4104. Wifi_module_sts = AP_STATE_INIT;
  4105. cnt_getAP_Fail = 0;
  4106. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
  4107. DEBUG_INFO("=================[State 0]===================\n");
  4108. }
  4109. else
  4110. {
  4111. sleep(10);
  4112. }
  4113. }
  4114. break;
  4115. case AP_STATE_AP_START:
  4116. getInterfaceInfo();
  4117. if(isStartUpAP() == PASS)
  4118. {
  4119. DEBUG_INFO("Wifi AP start up valid result: Pass\n");
  4120. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  4121. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  4122. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  4123. cnt_pingDNS_Fail = 0;
  4124. sleep(30);
  4125. }
  4126. else
  4127. {
  4128. cnt_pingDNS_Fail++;
  4129. DEBUG_INFO("Wifi AP start up valid result: Fail %d time\n", cnt_pingDNS_Fail);
  4130. sleep(5);
  4131. }
  4132. if(cnt_pingDNS_Fail >= 3)
  4133. {
  4134. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  4135. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  4136. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  4137. cnt_pingDNS_Fail = 0;
  4138. DEBUG_INFO("Wifi AP start up status failed...\n");
  4139. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  4140. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  4141. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  4142. if(isFindInterface() == PASS)
  4143. {
  4144. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  4145. isSetWPA_OK = false;
  4146. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  4147. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  4148. DEBUG_INFO("=================[State 1]===================\n");
  4149. }
  4150. else
  4151. {
  4152. DEBUG_INFO("Wifi support interface valid result: Fail\n");
  4153. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  4154. Wifi_module_sts = STA_STATE_INIT;
  4155. DEBUG_INFO("=================[State 0]===================\n");
  4156. }
  4157. }
  4158. break;
  4159. default:
  4160. Wifi_module_sts = AP_STATE_INIT;
  4161. break;
  4162. }
  4163. }
  4164. void proc_killConnection()
  4165. {
  4166. char cmdBuf[512];
  4167. // Initialization flags when network is disable mode
  4168. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=OFF;
  4169. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=OFF;
  4170. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=OFF;
  4171. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=OFF;
  4172. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=OFF;
  4173. // Stop dhcp client or server
  4174. sprintf(cmdBuf, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  4175. system(cmdBuf);
  4176. sprintf(cmdBuf, "pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  4177. system(cmdBuf);
  4178. // Restart wpa_supplicant
  4179. sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
  4180. system(cmdBuf);
  4181. // Stop wifi AP
  4182. sprintf(cmdBuf, "pgrep -f \"hostapd\" | xargs kill");
  4183. system(cmdBuf);
  4184. #ifdef UBLOX
  4185. system("ifconfig mlan0 down");
  4186. system("ifconfig uap0 down");
  4187. #endif
  4188. #ifdef MT7601U
  4189. system("ifconfig wlan0 down");
  4190. #endif
  4191. // Clean share memory when network is disable mode
  4192. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  4193. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  4194. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  4195. }
  4196. unsigned int isKernelSupportNAT()
  4197. {
  4198. unsigned int result = NO;
  4199. unsigned int version = 0;
  4200. FILE *fp;
  4201. char cmd[256];
  4202. char buf[512];
  4203. // Get IP address & net mask
  4204. strcpy(cmd, "uname -v");
  4205. fp = popen(cmd, "r");
  4206. if(fp != NULL)
  4207. {
  4208. if(fgets(buf, sizeof(buf), fp) != NULL)
  4209. {
  4210. sscanf(buf, "#%d", &version);
  4211. DEBUG_INFO("Kernel version: %d\n", version);
  4212. if(version >= 30)
  4213. result = YES;
  4214. }
  4215. }
  4216. pclose(fp);
  4217. return result;
  4218. }
  4219. //==========================================
  4220. // Main loop
  4221. //==========================================
  4222. int main(void)
  4223. {
  4224. pid_t pid;
  4225. signal(SIGCHLD,SIG_IGN);
  4226. if(InitShareMemory() == FAIL)
  4227. {
  4228. DEBUG_ERROR("InitShareMemory NG\n");
  4229. if(ShmStatusCodeData!=NULL)
  4230. {
  4231. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
  4232. }
  4233. sleep(5);
  4234. return 0;
  4235. }
  4236. // Local database initial
  4237. if(DB_Open(localDb) != PASS)
  4238. {
  4239. DEBUG_ERROR("Puk pin list local db initial fail.\n");
  4240. }
  4241. // Load schedule from local database
  4242. for(int gun_index=0;gun_index<(AC_QUANTITY+CHAdeMO_QUANTITY+CCS_QUANTITY+GB_QUANTITY);gun_index++)
  4243. scheduleLoadFromDb(localDb, gun_index);
  4244. // Wifi login info initial & PUK generate
  4245. wifi_login_info.loginRole = ROLE_UNKNOWN;
  4246. memset(&wifi_login_info.loginId[0], 0x00, ARRAY_SIZE(wifi_login_info.loginId));
  4247. memset(&wifi_login_info.puk[0], 0x00, ARRAY_SIZE(wifi_login_info.puk));
  4248. sprintf((char*)wifi_login_info.puk, "%08X", ~stm32crc((uint32_t*)&ShmSysConfigAndInfo->SysConfig.SystemId[0], (ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)>>2)));
  4249. DEBUG_INFO("System ID: %s\n", ShmSysConfigAndInfo->SysConfig.SystemId);
  4250. DEBUG_INFO("EVSE PUK: %s\n", wifi_login_info.puk);
  4251. // UDP socket server start
  4252. pid = fork();
  4253. if(pid == 0)
  4254. {
  4255. if(udpSocketServerStart() == FAIL)
  4256. {
  4257. DEBUG_ERROR("UDP socket server down.\n");
  4258. return 0;
  4259. }
  4260. }
  4261. // TCP socket server start
  4262. pid = fork();
  4263. if(pid == 0)
  4264. {
  4265. if(tcpSocketServerStart() == FAIL)
  4266. {
  4267. DEBUG_ERROR("TCP socket server down.\n");
  4268. return 0;
  4269. }
  4270. }
  4271. // Schedule check thread start
  4272. pid = fork();
  4273. if(pid == 0)
  4274. {
  4275. for(;;)
  4276. {
  4277. // Check schedule start/stop event
  4278. checkSchedule();
  4279. sleep(1);
  4280. }
  4281. return 0;
  4282. }
  4283. //=============================================
  4284. // Install WIFI module driver
  4285. //=============================================
  4286. #ifdef UBLOX
  4287. if(isKernelSupportNAT() == YES)
  4288. {
  4289. system("insmod /lib/modules/mlan_nat.ko");
  4290. system("insmod /lib/modules/usb8801_nat.ko");
  4291. }
  4292. else
  4293. {
  4294. system("insmod /lib/modules/mlan.ko");
  4295. system("insmod /lib/modules/usb8801.ko");
  4296. }
  4297. system("echo 1 > /proc/sys/net/ipv4/ip_forward");
  4298. sleep(5);
  4299. system("ifconfig mlan0 up");
  4300. system("ifconfig uap0 up");
  4301. #endif
  4302. #ifdef MT7601U
  4303. system("insmod /lib/modules/mt7601u.ko");
  4304. sleep(5);
  4305. system("ifconfig wlan0 up");
  4306. #endif
  4307. DEBUG_INFO("========================================\n");
  4308. DEBUG_INFO("Latest Firmware Version : [ %s ].\n", Version_And_Date[0]);
  4309. DEBUG_INFO("Latest Upgrade Date : [ %s ].\n", Version_And_Date[1]);
  4310. DEBUG_INFO("========================================\n");
  4311. DEBUG_INFO("=================[State 0]===================\n");
  4312. for(;;)
  4313. {
  4314. switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
  4315. {
  4316. case WIFI_MODE_STA:
  4317. proc_sta();
  4318. break;
  4319. case WIFI_MODE_AP:
  4320. proc_ap();
  4321. break;
  4322. case WIFI_MODE_ADHOC:
  4323. break;
  4324. case WIFI_MODE_DISABLE:
  4325. default:
  4326. proc_killConnection();
  4327. sleep(30);
  4328. break;
  4329. }
  4330. }
  4331. return 0;
  4332. }