Module_Wifi.c 167 KB

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