Module_Wifi.c 124 KB

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