wifi.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. // Module for interfacing with WIFI
  2. // FIXME: sprintf->snprintf everywhere.
  3. #include "module.h"
  4. #include "lauxlib.h"
  5. #include "platform.h"
  6. #include "c_string.h"
  7. #include "c_stdlib.h"
  8. #include "ctype.h"
  9. #include "c_types.h"
  10. #include "user_interface.h"
  11. #include "wifi_common.h"
  12. #ifdef WIFI_SMART_ENABLE
  13. #include "smart.h"
  14. #include "smartconfig.h"
  15. static int wifi_smart_succeed = LUA_NOREF;
  16. #endif
  17. static uint8 getap_output_format=0;
  18. #define INVALID_MAC_STR "MAC:FF:FF:FF:FF:FF:FF"
  19. #ifdef WIFI_SMART_ENABLE
  20. static void wifi_smart_succeed_cb(sc_status status, void *pdata){
  21. NODE_DBG("wifi_smart_succeed_cb is called.\n");
  22. lua_State* L = lua_getstate();
  23. if (status == SC_STATUS_LINK_OVER)
  24. {
  25. smartconfig_stop();
  26. return;
  27. }
  28. #if defined( NODE_SMART_OLDSTYLE )
  29. if (status != SC_STATUS_LINK || !pdata)
  30. return;
  31. if(wifi_smart_succeed == LUA_NOREF)
  32. return;
  33. lua_State* L = (lua_State *)arg;
  34. lua_rawgeti(L, LUA_REGISTRYINDEX, wifi_smart_succeed);
  35. lua_call(L, 0, 0);
  36. #else
  37. if (status != SC_STATUS_LINK || !pdata)
  38. return;
  39. struct station_config *sta_conf = pdata;
  40. wifi_station_set_config(sta_conf);
  41. wifi_station_disconnect();
  42. wifi_station_connect();
  43. if(wifi_smart_succeed != LUA_NOREF)
  44. {
  45. lua_rawgeti(L, LUA_REGISTRYINDEX, wifi_smart_succeed);
  46. lua_pushstring(L, sta_conf->ssid);
  47. lua_pushstring(L, sta_conf->password);
  48. lua_call(L, 2, 0);
  49. unregister_lua_cb(L, &wifi_smart_succeed);
  50. }
  51. #endif // defined( NODE_SMART_OLDSTYLE )
  52. }
  53. #endif // WIFI_SMART_ENABLE
  54. static int wifi_scan_succeed = LUA_NOREF;
  55. // callback for wifi_station_listap
  56. static void wifi_scan_done(void *arg, STATUS status)
  57. {
  58. lua_State* L = lua_getstate();
  59. uint8 ssid[33];
  60. char temp[sizeof("11:22:33:44:55:66")];
  61. if(wifi_scan_succeed == LUA_NOREF)
  62. return;
  63. if(arg == NULL)
  64. return;
  65. lua_rawgeti(L, LUA_REGISTRYINDEX, wifi_scan_succeed);
  66. if (status == OK)
  67. {
  68. struct bss_info *bss_link = (struct bss_info *)arg;
  69. lua_newtable( L );
  70. while (bss_link != NULL)
  71. {
  72. c_memset(ssid, 0, 33);
  73. if (c_strlen(bss_link->ssid) <= 32)
  74. {
  75. c_memcpy(ssid, bss_link->ssid, c_strlen(bss_link->ssid));
  76. }
  77. else
  78. {
  79. c_memcpy(ssid, bss_link->ssid, 32);
  80. }
  81. if(getap_output_format==1) //use new format(BSSID : SSID, RSSI, Authmode, Channel)
  82. {
  83. c_sprintf(temp,MACSTR, MAC2STR(bss_link->bssid));
  84. wifi_add_sprintf_field(L, temp, "%s,%d,%d,%d",
  85. ssid, bss_link->rssi, bss_link->authmode, bss_link->channel);
  86. NODE_DBG(MACSTR" : %s\n",MAC2STR(bss_link->bssid) , temp);//00 00 00 00 00 00
  87. }
  88. else //use old format(SSID : Authmode, RSSI, BSSID, Channel)
  89. {
  90. wifi_add_sprintf_field(L, ssid, "%d,%d,"MACSTR",%d",
  91. bss_link->authmode, bss_link->rssi, MAC2STR(bss_link->bssid),bss_link->channel);
  92. NODE_DBG("%s : %s\n", ssid, temp);
  93. }
  94. bss_link = bss_link->next.stqe_next;
  95. }
  96. }
  97. else
  98. {
  99. lua_newtable( L );
  100. }
  101. lua_call(L, 1, 0);
  102. unregister_lua_cb(L, &wifi_scan_succeed);
  103. }
  104. #ifdef WIFI_SMART_ENABLE
  105. // Lua: smart(channel, function succeed_cb)
  106. // Lua: smart(type, function succeed_cb)
  107. static int wifi_start_smart( lua_State* L )
  108. {
  109. #if defined( NODE_SMART_OLDSTYLE )
  110. unsigned channel;
  111. int stack = 1;
  112. if ( lua_isnumber(L, stack) )
  113. {
  114. channel = lua_tointeger(L, stack);
  115. stack++;
  116. }
  117. else
  118. {
  119. channel = 6;
  120. }
  121. // luaL_checkanyfunction(L, stack);
  122. if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION)
  123. {
  124. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  125. if(wifi_smart_succeed != LUA_NOREF)
  126. luaL_unref(L, LUA_REGISTRYINDEX, wifi_smart_succeed);
  127. wifi_smart_succeed = luaL_ref(L, LUA_REGISTRYINDEX);
  128. }
  129. if ( channel > 14 || channel < 1 )
  130. return luaL_error( L, "wrong arg range" );
  131. if(wifi_smart_succeed == LUA_NOREF){
  132. smart_begin(channel, NULL, NULL);
  133. }
  134. else
  135. {
  136. smart_begin(channel, (smart_succeed )wifi_smart_succeed_cb, L);
  137. }
  138. #else
  139. if(wifi_get_opmode() != STATION_MODE)
  140. {
  141. return luaL_error( L, "Smart link only in STATION mode" );
  142. }
  143. uint8_t smart_type = 0;
  144. int stack = 1;
  145. if ( lua_isnumber(L, stack) )
  146. {
  147. smart_type = lua_tointeger(L, stack);
  148. stack++;
  149. }
  150. if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION)
  151. {
  152. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  153. register_lua_cb(L, &wifi_smart_succeed);
  154. }
  155. if ( smart_type > 1 )
  156. return luaL_error( L, "wrong arg range" );
  157. smartconfig_set_type(smart_type);
  158. smartconfig_start(wifi_smart_succeed_cb);
  159. #endif // defined( NODE_SMART_OLDSTYLE )
  160. return 0;
  161. }
  162. // Lua: exit_smart()
  163. static int wifi_exit_smart( lua_State* L )
  164. {
  165. #if defined( NODE_SMART_OLDSTYLE )
  166. smart_end();
  167. #else
  168. smartconfig_stop();
  169. #endif // defined( NODE_SMART_OLDSTYLE )
  170. unregister_lua_cb(L, &wifi_smart_succeed);
  171. return 0;
  172. }
  173. #endif // WIFI_SMART_ENABLE
  174. // Lua: wifi.setmode(mode, save_to_flash)
  175. static int wifi_setmode( lua_State* L )
  176. {
  177. unsigned mode;
  178. bool save_to_flash=true;
  179. mode = luaL_checkinteger( L, 1 );
  180. luaL_argcheck(L, mode == STATION_MODE || mode == SOFTAP_MODE || mode == STATIONAP_MODE || mode == NULL_MODE, 1, "Invalid mode");
  181. if(!lua_isnoneornil(L, 2))
  182. {
  183. if(!lua_isboolean(L, 2))
  184. {
  185. luaL_typerror(L, 2, lua_typename(L, LUA_TBOOLEAN));
  186. }
  187. save_to_flash=lua_toboolean(L, 2);
  188. }
  189. if(save_to_flash)
  190. {
  191. wifi_set_opmode( (uint8_t)mode);
  192. }
  193. else
  194. {
  195. wifi_set_opmode_current( (uint8_t)mode);
  196. }
  197. mode = (unsigned)wifi_get_opmode();
  198. lua_pushinteger( L, mode );
  199. return 1;
  200. }
  201. // Lua: wifi.getmode()
  202. static int wifi_getmode( lua_State* L )
  203. {
  204. unsigned mode;
  205. mode = (unsigned)wifi_get_opmode();
  206. lua_pushinteger( L, mode );
  207. return 1;
  208. }
  209. // Lua: wifi.getdefaultmode()
  210. static int wifi_getdefaultmode( lua_State* L )
  211. {
  212. unsigned mode;
  213. mode = (unsigned)wifi_get_opmode_default();
  214. lua_pushinteger( L, mode );
  215. return 1;
  216. }
  217. // Lua: wifi.getchannel()
  218. static int wifi_getchannel( lua_State* L )
  219. {
  220. unsigned channel;
  221. channel = (unsigned)wifi_get_channel();
  222. lua_pushinteger( L, channel );
  223. return 1;
  224. }
  225. // Lua: wifi.setphymode()
  226. static int wifi_setphymode( lua_State* L )
  227. {
  228. unsigned mode;
  229. mode = luaL_checkinteger( L, 1 );
  230. if ( mode != PHY_MODE_11B && mode != PHY_MODE_11G && mode != PHY_MODE_11N )
  231. return luaL_error( L, "wrong arg type" );
  232. wifi_set_phy_mode( (uint8_t)mode);
  233. mode = (unsigned)wifi_get_phy_mode();
  234. lua_pushinteger( L, mode );
  235. return 1;
  236. }
  237. // Lua: wifi.getphymode()
  238. static int wifi_getphymode( lua_State* L )
  239. {
  240. unsigned mode;
  241. mode = (unsigned)wifi_get_phy_mode();
  242. lua_pushinteger( L, mode );
  243. return 1;
  244. }
  245. // Lua: wifi.setmaxtxpower()
  246. static int wifi_setmaxtxpower( lua_State* L )
  247. {
  248. unsigned power;
  249. power = luaL_checkinteger( L, 1 );
  250. luaL_argcheck(L, (power > 0 && power < 83), 1, "tx power out of range (0->82)");
  251. system_phy_set_max_tpw( (uint8_t) power);
  252. return 1;
  253. }
  254. #ifdef PMSLEEP_ENABLE
  255. /* Begin WiFi suspend functions*/
  256. #include "pmSleep.h"
  257. static int wifi_resume_cb_ref = LUA_NOREF; // Holds resume callback reference
  258. static int wifi_suspend_cb_ref = LUA_NOREF; // Holds suspend callback reference
  259. void wifi_pmSleep_suspend_CB(void)
  260. {
  261. PMSLEEP_DBG("\n\tDBG: %s start\n", __func__);
  262. if (wifi_suspend_cb_ref != LUA_NOREF)
  263. {
  264. lua_State* L = lua_getstate(); // Get main Lua thread pointer
  265. lua_rawgeti(L, LUA_REGISTRYINDEX, wifi_suspend_cb_ref); // Push suspend callback onto stack
  266. lua_unref(L, wifi_suspend_cb_ref); // remove suspend callback from LUA_REGISTRY
  267. wifi_suspend_cb_ref = LUA_NOREF; // Update variable since reference is no longer valid
  268. lua_call(L, 0, 0); // Execute suspend callback
  269. }
  270. else
  271. {
  272. PMSLEEP_DBG("\n\tDBG: lua cb unavailable\n");
  273. }
  274. PMSLEEP_DBG("\n\tDBG: %s end\n", __func__);
  275. return;
  276. }
  277. void wifi_pmSleep_resume_CB(void)
  278. {
  279. PMSLEEP_DBG("\n\tDBG: %s start\n", __func__);
  280. // If resume callback was defined
  281. pmSleep_execute_lua_cb(&wifi_resume_cb_ref);
  282. PMSLEEP_DBG("\n\tDBG: %s end\n", __func__);
  283. return;
  284. }
  285. // Lua: wifi.suspend({duration, suspend_cb, resume_cb, preserve_mode})
  286. static int wifi_suspend(lua_State* L)
  287. {
  288. // If no parameters were provided
  289. if (lua_isnone(L, 1))
  290. {
  291. // Return current WiFi suspension state
  292. lua_pushnumber(L, pmSleep_get_state());
  293. return 1; // Return WiFi suspension state
  294. }
  295. pmSleep_INIT_CFG(cfg);
  296. cfg.sleep_mode = MODEM_SLEEP_T;
  297. if(lua_istable(L, 1))
  298. {
  299. pmSleep_parse_table_lua(L, 1, &cfg, &wifi_suspend_cb_ref, &wifi_resume_cb_ref);
  300. }
  301. else
  302. return luaL_argerror(L, 1, "must be table");
  303. cfg.resume_cb_ptr = &wifi_pmSleep_resume_CB;
  304. cfg.suspend_cb_ptr = &wifi_pmSleep_suspend_CB;
  305. pmSleep_suspend(&cfg);
  306. return 0;
  307. }
  308. // Lua: wifi.resume([Resume_CB])
  309. static int wifi_resume(lua_State* L)
  310. {
  311. PMSLEEP_DBG("\n\tDBG: %s start\n", __func__);
  312. uint8 fpm_state = pmSleep_get_state();
  313. // If forced sleep api is not enabled, return error
  314. if (fpm_state == 0)
  315. {
  316. return luaL_error(L, "WIFi not suspended");
  317. }
  318. // If a resume callback was provided
  319. if (lua_isfunction(L, 1))
  320. {
  321. // If there is already a resume callback reference
  322. lua_pushvalue(L, 1); //Push resume callback to the top of the stack
  323. register_lua_cb(L, &wifi_resume_cb_ref);
  324. PMSLEEP_DBG("\n\tDBG: Resume CB registered\n");
  325. }
  326. pmSleep_resume(NULL);
  327. PMSLEEP_DBG("\n\tDBG: %s end\n", __func__);
  328. return 0;
  329. }
  330. /* End WiFi suspend functions*/
  331. #endif
  332. // Lua: wifi.nullmodesleep()
  333. static int wifi_null_mode_auto_sleep(lua_State* L)
  334. {
  335. if (!lua_isnone(L, 1))
  336. {
  337. bool auto_sleep_setting=lua_toboolean(L, 1);
  338. if (auto_sleep_setting!=(bool) get_fpm_auto_sleep_flag())
  339. {
  340. wifi_fpm_auto_sleep_set_in_null_mode((uint8)auto_sleep_setting);
  341. //if esp is already in NULL_MODE, auto sleep setting won't take effect until next wifi_set_opmode(NULL_MODE) call.
  342. if(wifi_get_opmode()==NULL_MODE)
  343. {
  344. wifi_set_opmode_current(NULL_MODE);
  345. }
  346. }
  347. }
  348. lua_pushboolean(L, (bool) get_fpm_auto_sleep_flag());
  349. return 1;
  350. }
  351. // Lua: mac = wifi.xx.getmac()
  352. static int wifi_getmac( lua_State* L, uint8_t mode )
  353. {
  354. char temp[64];
  355. uint8_t mac[6];
  356. wifi_get_macaddr(mode, mac);
  357. c_sprintf(temp, MACSTR, MAC2STR(mac));
  358. lua_pushstring( L, temp );
  359. return 1;
  360. }
  361. // Lua: mac = wifi.xx.setmac()
  362. static int wifi_setmac( lua_State* L, uint8_t mode )
  363. {
  364. uint8_t mac[6];
  365. unsigned len = 0;
  366. const char *macaddr = luaL_checklstring( L, 1, &len );
  367. luaL_argcheck(L, len==17, 1, INVALID_MAC_STR);
  368. ets_str2macaddr(mac, macaddr);
  369. lua_pushboolean(L,wifi_set_macaddr(mode, (uint8 *)mac));
  370. return 1;
  371. }
  372. // Lua: ip = wifi.xx.getip()
  373. static int wifi_getip( lua_State* L, uint8_t mode )
  374. {
  375. struct ip_info pTempIp;
  376. char temp[64];
  377. wifi_get_ip_info(mode, &pTempIp);
  378. if(pTempIp.ip.addr==0){
  379. lua_pushnil(L);
  380. return 1;
  381. }
  382. else
  383. {
  384. c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.ip) );
  385. lua_pushstring( L, temp );
  386. c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.netmask) );
  387. lua_pushstring( L, temp );
  388. c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.gw) );
  389. lua_pushstring( L, temp );
  390. return 3;
  391. }
  392. }
  393. // Lua: broadcast = wifi.xx.getbroadcast()
  394. static int wifi_getbroadcast( lua_State* L, uint8_t mode )
  395. {
  396. struct ip_info pTempIp;
  397. char temp[64];
  398. wifi_get_ip_info(mode, &pTempIp);
  399. if(pTempIp.ip.addr==0){
  400. lua_pushnil(L);
  401. return 1;
  402. }
  403. else
  404. {
  405. struct ip_addr broadcast_address;
  406. uint32 subnet_mask32 = pTempIp.netmask.addr & pTempIp.ip.addr;
  407. uint32 broadcast_address32 = ~pTempIp.netmask.addr | subnet_mask32;
  408. broadcast_address.addr = broadcast_address32;
  409. c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&broadcast_address) );
  410. lua_pushstring( L, temp );
  411. return 1;
  412. }
  413. }
  414. // Used by wifi_setip
  415. static uint32_t parse_key(lua_State* L, const char * key){
  416. lua_getfield(L, 1, key);
  417. if( lua_isstring(L, -1) ) // deal with the ip/netmask/gw string
  418. {
  419. const char *ip = luaL_checkstring( L, -1 );
  420. return ipaddr_addr(ip);
  421. }
  422. lua_pop(L, 1);
  423. return 0;
  424. }
  425. // Lua: ip = wifi.xx.setip()
  426. static int wifi_setip( lua_State* L, uint8_t mode )
  427. {
  428. struct ip_info pTempIp;
  429. wifi_get_ip_info(mode, &pTempIp);
  430. if (!lua_istable(L, 1))
  431. return luaL_error( L, "wrong arg type" );
  432. uint32_t ip = parse_key(L, "ip");
  433. if(ip!=0)
  434. pTempIp.ip.addr = ip;
  435. ip = parse_key(L, "netmask");
  436. if(ip!=0)
  437. pTempIp.netmask.addr = ip;
  438. ip = parse_key(L, "gateway");
  439. if(mode==SOFTAP_IF || ip!=0)
  440. pTempIp.gw.addr = ip;
  441. if(STATION_IF == mode)
  442. {
  443. wifi_station_dhcpc_stop();
  444. lua_pushboolean(L,wifi_set_ip_info(mode, &pTempIp));
  445. }
  446. else
  447. {
  448. wifi_softap_dhcps_stop();
  449. lua_pushboolean(L,wifi_set_ip_info(mode, &pTempIp));
  450. wifi_softap_dhcps_start();
  451. }
  452. return 1;
  453. }
  454. // Lua: wifi.sta.getapinfo
  455. static int wifi_station_get_ap_info4lua( lua_State* L )
  456. {
  457. struct station_config config[5];
  458. char temp[sizeof(config[0].password)+1]; //max password length + '\0'
  459. uint8 number_of_aps = wifi_station_get_ap_info(config);
  460. #if defined(WIFI_DEBUG)
  461. char debug_temp[128];
  462. #endif
  463. lua_newtable(L);
  464. lua_pushnumber(L, number_of_aps);
  465. lua_setfield(L, -2, "qty");
  466. WIFI_DBG("\n\t# of APs stored in flash:%d\n", number_of_aps);
  467. WIFI_DBG(" %-6s %-32s %-64s %-17s\n", "index:", "ssid:", "password:", "bssid:");
  468. for(int i=0;i<number_of_aps;i++)
  469. {
  470. lua_newtable(L);
  471. memset(temp, 0, sizeof(temp));
  472. memcpy(temp, config[i].ssid, sizeof(config[i].ssid));
  473. lua_pushstring(L, temp);
  474. lua_setfield(L, -2, "ssid");
  475. #if defined(WIFI_DEBUG)
  476. c_sprintf(debug_temp, " %-6d %-32s ", i, temp);
  477. #endif
  478. memset(temp, 0, sizeof(temp));
  479. if(strlen(config[i].password) > 0) /* WPA = min 8, WEP = min 5 ASCII characters for a 40-bit key */
  480. {
  481. memcpy(temp, config[i].password, sizeof(config[i].password));
  482. lua_pushstring(L, temp);
  483. lua_setfield(L, -2, "pwd");
  484. }
  485. #if defined(WIFI_DEBUG)
  486. c_sprintf(debug_temp + strlen(debug_temp), "%-64s ", temp);
  487. #endif
  488. memset(temp, 0, sizeof(temp));
  489. if (config[i].bssid_set)
  490. {
  491. c_sprintf(temp, MACSTR, MAC2STR(config[i].bssid));
  492. lua_pushstring(L, temp);
  493. lua_setfield(L, -2, "bssid");
  494. }
  495. #if defined(WIFI_DEBUG)
  496. WIFI_DBG("%s%-17s \n", debug_temp, temp);
  497. #endif
  498. lua_pushnumber(L, i+1); //Add one, so that AP index follows Lua Conventions
  499. lua_insert(L, -2);
  500. lua_settable(L, -3);
  501. }
  502. return 1;
  503. }
  504. // Lua: wifi.setapnumber(number_of_aps_to_save)
  505. static int wifi_station_ap_number_set4lua( lua_State* L )
  506. {
  507. unsigned limit=luaL_checkinteger(L, 1);
  508. luaL_argcheck(L, (limit >= 1 && limit <= 5), 1, "Valid range: 1-5");
  509. lua_pushboolean(L, wifi_station_ap_number_set((uint8)limit));
  510. return 1;
  511. }
  512. // Lua: wifi.setapnumber(number_of_aps_to_save)
  513. static int wifi_station_change_ap( lua_State* L )
  514. {
  515. uint8 ap_index=luaL_checkinteger(L, 1);
  516. luaL_argcheck(L, (ap_index >= 1 && ap_index <= 5), 1, "Valid range: 1-5");
  517. lua_pushboolean(L, wifi_station_ap_change(ap_index-1));
  518. return 1;
  519. }
  520. // Lua: wifi.setapnumber(number_of_aps_to_save)
  521. static int wifi_station_get_ap_index( lua_State* L )
  522. {
  523. lua_pushnumber(L, wifi_station_get_current_ap_id()+1);
  524. return 1;
  525. }
  526. // Lua: wifi.sta.getmac()
  527. static int wifi_station_getmac( lua_State* L ){
  528. return wifi_getmac(L, STATION_IF);
  529. }
  530. // Lua: wifi.sta.setmac()
  531. static int wifi_station_setmac( lua_State* L ){
  532. return wifi_setmac(L, STATION_IF);
  533. }
  534. // Lua: wifi.sta.getip()
  535. static int wifi_station_getip( lua_State* L ){
  536. return wifi_getip(L, STATION_IF);
  537. }
  538. // Lua: wifi.sta.setip()
  539. static int wifi_station_setip( lua_State* L ){
  540. return wifi_setip(L, STATION_IF);
  541. }
  542. // Lua: wifi.sta.getbroadcast()
  543. static int wifi_station_getbroadcast( lua_State* L ){
  544. return wifi_getbroadcast(L, STATION_IF);
  545. }
  546. // Used by wifi_station_getconfig_xxx
  547. static int wifi_station_getconfig( lua_State* L, bool get_flash_cfg)
  548. {
  549. struct station_config sta_conf;
  550. char temp[sizeof(sta_conf.password)+1]; //max password length + '\0'
  551. if(get_flash_cfg)
  552. {
  553. wifi_station_get_config_default(&sta_conf);
  554. }
  555. else
  556. {
  557. wifi_station_get_config(&sta_conf);
  558. }
  559. if(sta_conf.ssid==0)
  560. {
  561. lua_pushnil(L);
  562. return 1;
  563. }
  564. else
  565. {
  566. if(lua_isboolean(L, 1) && lua_toboolean(L, 1)==true)
  567. {
  568. lua_newtable(L);
  569. memset(temp, 0, sizeof(temp));
  570. memcpy(temp, sta_conf.ssid, sizeof(sta_conf.ssid));
  571. lua_pushstring(L, temp);
  572. lua_setfield(L, -2, "ssid");
  573. if(strlen(sta_conf.password) > 0) /* WPA = min 8, WEP = min 5 ASCII characters for a 40-bit key */
  574. {
  575. memset(temp, 0, sizeof(temp));
  576. memcpy(temp, sta_conf.password, sizeof(sta_conf.password));
  577. lua_pushstring(L, temp);
  578. lua_setfield(L, -2, "pwd");
  579. }
  580. lua_pushboolean(L, sta_conf.bssid_set);
  581. lua_setfield(L, -2, "bssid_set");
  582. memset(temp, 0, sizeof(temp));
  583. c_sprintf(temp, MACSTR, MAC2STR(sta_conf.bssid));
  584. lua_pushstring( L, temp);
  585. lua_setfield(L, -2, "bssid");
  586. return 1;
  587. }
  588. else
  589. {
  590. memset(temp, 0, sizeof(temp));
  591. memcpy(temp, sta_conf.ssid, sizeof(sta_conf.ssid));
  592. lua_pushstring(L, temp);
  593. memset(temp, 0, sizeof(temp));
  594. memcpy(temp, sta_conf.password, sizeof(sta_conf.password));
  595. lua_pushstring(L, temp);
  596. lua_pushinteger( L, sta_conf.bssid_set);
  597. c_sprintf(temp, MACSTR, MAC2STR(sta_conf.bssid));
  598. lua_pushstring( L, temp);
  599. return 4;
  600. }
  601. }
  602. }
  603. // Lua: wifi.sta.getconfig()
  604. static int wifi_station_getconfig_current(lua_State *L)
  605. {
  606. return wifi_station_getconfig(L, false);
  607. }
  608. // Lua: wifi.sta.getdefaultconfig()
  609. static int wifi_station_getconfig_default(lua_State *L)
  610. {
  611. return wifi_station_getconfig(L, true);
  612. }
  613. // Lua: wifi.sta.clearconfig()
  614. static int wifi_station_clear_config ( lua_State* L )
  615. {
  616. struct station_config sta_conf;
  617. bool auto_connect=true;
  618. bool save_to_flash=true;
  619. memset(sta_conf.ssid, 0, sizeof(sta_conf.ssid));
  620. memset(sta_conf.password, 0, sizeof(sta_conf.password));
  621. memset(sta_conf.bssid, 0, sizeof(sta_conf.bssid));
  622. sta_conf.bssid_set=0;
  623. wifi_station_disconnect();
  624. bool config_success;
  625. if(save_to_flash)
  626. {
  627. config_success = wifi_station_set_config(&sta_conf);
  628. }
  629. else
  630. {
  631. config_success = wifi_station_set_config_current(&sta_conf);
  632. }
  633. wifi_station_set_auto_connect((uint8)0);
  634. lua_pushboolean(L, config_success);
  635. return 1;
  636. }
  637. // Lua: wifi.sta.config()
  638. static int wifi_station_config( lua_State* L )
  639. {
  640. struct station_config sta_conf;
  641. bool auto_connect=true;
  642. bool save_to_flash=true;
  643. size_t sl, pl, ml;
  644. memset(sta_conf.ssid, 0, sizeof(sta_conf.ssid));
  645. memset(sta_conf.password, 0, sizeof(sta_conf.password));
  646. memset(sta_conf.bssid, 0, sizeof(sta_conf.bssid));
  647. sta_conf.bssid_set=0;
  648. if(lua_istable(L, 1))
  649. {
  650. lua_getfield(L, 1, "ssid");
  651. if (!lua_isnil(L, -1))
  652. {
  653. if( lua_isstring(L, -1) )
  654. {
  655. const char *ssid = luaL_checklstring( L, -1, &sl );
  656. luaL_argcheck(L, ((sl>=0 && sl<=sizeof(sta_conf.ssid)) ), 1, "ssid: length:0-32"); /* Zero-length SSID is valid as a way to clear config */
  657. memcpy(sta_conf.ssid, ssid, sl);
  658. }
  659. else
  660. {
  661. return luaL_argerror( L, 1, "ssid:not string" );
  662. }
  663. }
  664. else
  665. {
  666. return luaL_argerror( L, 1, "ssid required" );
  667. }
  668. lua_pop(L, 1);
  669. lua_getfield(L, 1, "pwd");
  670. if (!lua_isnil(L, -1))
  671. {
  672. if( lua_isstring(L, -1) )
  673. {
  674. const char *pwd = luaL_checklstring( L, -1, &pl );
  675. luaL_argcheck(L, ((pl>=0 && pl<=sizeof(sta_conf.password)) ), 1, "pwd: length:0-64"); /* WPA = min 8, WEP = min 5 ASCII characters for a 40-bit key */
  676. memcpy(sta_conf.password, pwd, pl);
  677. }
  678. else
  679. {
  680. return luaL_argerror( L, 1, "pwd:not string" );
  681. }
  682. }
  683. lua_pop(L, 1);
  684. lua_getfield(L, 1, "bssid");
  685. if (!lua_isnil(L, -1))
  686. {
  687. if (lua_isstring(L, -1))
  688. {
  689. const char *macaddr = luaL_checklstring( L, -1, &ml );
  690. luaL_argcheck(L, ((ml==sizeof("AA:BB:CC:DD:EE:FF")-1) ), 1, "bssid: FF:FF:FF:FF:FF:FF");
  691. ets_str2macaddr(sta_conf.bssid, macaddr);
  692. sta_conf.bssid_set = 1;
  693. }
  694. else
  695. {
  696. return luaL_argerror(L, 1, "bssid:not string");
  697. }
  698. }
  699. lua_pop(L, 1);
  700. lua_getfield(L, 1, "auto");
  701. if (!lua_isnil(L, -1))
  702. {
  703. if (lua_isboolean(L, -1))
  704. {
  705. auto_connect=lua_toboolean(L, -1);
  706. }
  707. else
  708. {
  709. return luaL_argerror(L, 1, "auto:not boolean");
  710. }
  711. }
  712. lua_pop(L, 1);
  713. lua_getfield(L, 1, "save");
  714. if (!lua_isnil(L, -1))
  715. {
  716. if (lua_isboolean(L, -1))
  717. {
  718. save_to_flash=lua_toboolean(L, -1);
  719. }
  720. else
  721. {
  722. return luaL_argerror(L, 1, "save:not boolean");
  723. }
  724. }
  725. else
  726. {
  727. save_to_flash=true;
  728. }
  729. lua_pop(L, 1);
  730. #ifdef WIFI_SDK_EVENT_MONITOR_ENABLE
  731. lua_State* L_temp = NULL;
  732. lua_getfield(L, 1, "connect_cb");
  733. if (!lua_isnil(L, -1))
  734. {
  735. if (lua_isfunction(L, -1))
  736. {
  737. L_temp = lua_newthread(L);
  738. lua_pushnumber(L, EVENT_STAMODE_CONNECTED);
  739. lua_pushvalue(L, -3);
  740. lua_xmove(L, L_temp, 2);
  741. wifi_event_monitor_register(L_temp);
  742. }
  743. else
  744. {
  745. return luaL_argerror(L, 1, "connect_cb:not function");
  746. }
  747. }
  748. lua_pop(L, 1);
  749. lua_getfield(L, 1, "disconnect_cb");
  750. if (!lua_isnil(L, -1))
  751. {
  752. if (lua_isfunction(L, -1))
  753. {
  754. L_temp = lua_newthread(L);
  755. lua_pushnumber(L, EVENT_STAMODE_DISCONNECTED);
  756. lua_pushvalue(L, -3);
  757. lua_xmove(L, L_temp, 2);
  758. wifi_event_monitor_register(L_temp);
  759. }
  760. else
  761. {
  762. return luaL_argerror(L, 1, "disconnect_cb:not function");
  763. }
  764. }
  765. lua_pop(L, 1);
  766. lua_getfield(L, 1, "authmode_change_cb");
  767. if (!lua_isnil(L, -1))
  768. {
  769. if (lua_isfunction(L, -1))
  770. {
  771. L_temp = lua_newthread(L);
  772. lua_pushnumber(L, EVENT_STAMODE_AUTHMODE_CHANGE);
  773. lua_pushvalue(L, -3);
  774. lua_xmove(L, L_temp, 2);
  775. wifi_event_monitor_register(L_temp);
  776. }
  777. else
  778. {
  779. return luaL_argerror(L, 1, "authmode_change_cb:not function");
  780. }
  781. }
  782. lua_pop(L, 1);
  783. lua_getfield(L, 1, "got_ip_cb");
  784. if (!lua_isnil(L, -1))
  785. {
  786. if (lua_isfunction(L, -1))
  787. {
  788. L_temp = lua_newthread(L);
  789. lua_pushnumber(L, EVENT_STAMODE_GOT_IP);
  790. lua_pushvalue(L, -3);
  791. lua_xmove(L, L_temp, 2);
  792. wifi_event_monitor_register(L_temp);
  793. }
  794. else
  795. {
  796. return luaL_argerror(L, 1, "gotip_cb:not function");
  797. }
  798. }
  799. lua_pop(L, 1);
  800. lua_getfield(L, 1, "dhcp_timeout_cb");
  801. if (!lua_isnil(L, -1))
  802. {
  803. if (lua_isfunction(L, -1))
  804. {
  805. L_temp = lua_newthread(L);
  806. lua_pushnumber(L, EVENT_STAMODE_DHCP_TIMEOUT);
  807. lua_pushvalue(L, -3);
  808. lua_xmove(L, L_temp, 2);
  809. wifi_event_monitor_register(L_temp);
  810. }
  811. else
  812. {
  813. return luaL_argerror(L, 1, "dhcp_timeout_cb:not function");
  814. }
  815. }
  816. lua_pop(L, 1);
  817. #endif
  818. }
  819. else
  820. {
  821. return luaL_argerror(L, 1, "config table not found!");
  822. }
  823. #if defined(WIFI_DEBUG)
  824. char debug_temp[sizeof(sta_conf.password)+1]; //max password length + '\0'
  825. memset(debug_temp, 0, sizeof(debug_temp));
  826. memcpy(debug_temp, sta_conf.ssid, sizeof(sta_conf.ssid));
  827. WIFI_DBG("\n\tsta_conf.ssid=\"%s\" len=%d\n", debug_temp, sl);
  828. memset(debug_temp, 0, sizeof(debug_temp));
  829. memcpy(debug_temp, sta_conf.password, sizeof(sta_conf.password));
  830. WIFI_DBG("\tsta_conf.password=\"%s\" len=%d\n", debug_temp, pl);
  831. WIFI_DBG("\tsta_conf.bssid=\""MACSTR"\"\tbssid_set=%d\n", MAC2STR(sta_conf.bssid), sta_conf.bssid_set);
  832. WIFI_DBG("\tsave_to_flash=%s\n", save_to_flash ? "true":"false");
  833. #endif
  834. wifi_station_disconnect();
  835. bool config_success;
  836. if(save_to_flash)
  837. {
  838. config_success = wifi_station_set_config(&sta_conf);
  839. }
  840. else
  841. {
  842. config_success = wifi_station_set_config_current(&sta_conf);
  843. }
  844. wifi_station_set_auto_connect((uint8)auto_connect);
  845. if(auto_connect)
  846. {
  847. wifi_station_connect();
  848. }
  849. lua_pushboolean(L, config_success);
  850. return 1;
  851. }
  852. // Lua: wifi.sta.connect()
  853. static int wifi_station_connect4lua( lua_State* L )
  854. {
  855. #ifdef WIFI_SDK_EVENT_MONITOR_ENABLE
  856. if(lua_isfunction(L, 1)){
  857. lua_pushnumber(L, EVENT_STAMODE_CONNECTED);
  858. lua_pushvalue(L, 1);
  859. lua_remove(L, 1);
  860. wifi_event_monitor_register(L);
  861. }
  862. #endif
  863. wifi_station_connect();
  864. return 0;
  865. }
  866. // Lua: wifi.sta.disconnect()
  867. static int wifi_station_disconnect4lua( lua_State* L )
  868. {
  869. #ifdef WIFI_SDK_EVENT_MONITOR_ENABLE
  870. if(lua_isfunction(L, 1)){
  871. lua_pushnumber(L, EVENT_STAMODE_DISCONNECTED);
  872. lua_pushvalue(L, 1);
  873. lua_remove(L, 1);
  874. wifi_event_monitor_register(L);
  875. }
  876. #endif
  877. wifi_station_disconnect();
  878. return 0;
  879. }
  880. // Lua: wifi.sta.auto(true/false)
  881. static int wifi_station_setauto( lua_State* L )
  882. {
  883. unsigned a;
  884. a = luaL_checkinteger( L, 1 );
  885. luaL_argcheck(L, ( a == 0 || a == 1 ), 1, "0 or 1");
  886. wifi_station_set_auto_connect(a);
  887. return 0;
  888. }
  889. // Lua: wifi.sta.listap()
  890. static int wifi_station_listap( lua_State* L )
  891. {
  892. if(wifi_get_opmode() == SOFTAP_MODE)
  893. {
  894. return luaL_error( L, "Can't list ap in SOFTAP mode" );
  895. }
  896. struct scan_config scan_cfg;
  897. getap_output_format=0;
  898. if (lua_type(L, 1)==LUA_TTABLE)
  899. {
  900. char ssid[32];
  901. char bssid[6];
  902. uint8 channel=0;
  903. uint8 show_hidden=0;
  904. size_t len;
  905. lua_getfield(L, 1, "ssid");
  906. if (!lua_isnil(L, -1)) /* found? */
  907. {
  908. if( lua_isstring(L, -1) ) // deal with the ssid string
  909. {
  910. const char *ssidstr = luaL_checklstring( L, -1, &len );
  911. if(len>32)
  912. return luaL_error( L, "ssid:<32" );
  913. c_memset(ssid, 0, 32);
  914. c_memcpy(ssid, ssidstr, len);
  915. scan_cfg.ssid=ssid;
  916. NODE_DBG(scan_cfg.ssid);
  917. NODE_DBG("\n");
  918. }
  919. else
  920. {
  921. return luaL_error( L, "wrong arg type" );
  922. }
  923. }
  924. else
  925. {
  926. scan_cfg.ssid=NULL;
  927. }
  928. lua_getfield(L, 1, "bssid");
  929. if (!lua_isnil(L, -1)) /* found? */
  930. {
  931. if( lua_isstring(L, -1) ) // deal with the ssid string
  932. {
  933. const char *macaddr = luaL_checklstring( L, -1, &len );
  934. luaL_argcheck(L, len==17, 1, INVALID_MAC_STR);
  935. c_memset(bssid, 0, 6);
  936. ets_str2macaddr(bssid, macaddr);
  937. scan_cfg.bssid=bssid;
  938. NODE_DBG(MACSTR, MAC2STR(scan_cfg.bssid));
  939. NODE_DBG("\n");
  940. }
  941. else
  942. {
  943. return luaL_error( L, "wrong arg type" );
  944. }
  945. }
  946. else
  947. {
  948. scan_cfg.bssid=NULL;
  949. }
  950. lua_getfield(L, 1, "channel");
  951. if (!lua_isnil(L, -1)) /* found? */
  952. {
  953. if( lua_isnumber(L, -1) ) // deal with the ssid string
  954. {
  955. channel = luaL_checknumber( L, -1);
  956. if(!(channel>=0 && channel<=13))
  957. return luaL_error( L, "channel: 0 or 1-13" );
  958. scan_cfg.channel=channel;
  959. NODE_DBG("%d\n", scan_cfg.channel);
  960. }
  961. else
  962. {
  963. return luaL_error( L, "wrong arg type" );
  964. }
  965. }
  966. else
  967. {
  968. scan_cfg.channel=0;
  969. }
  970. lua_getfield(L, 1, "show_hidden");
  971. if (!lua_isnil(L, -1)) /* found? */
  972. {
  973. if( lua_isnumber(L, -1) ) // deal with the ssid string
  974. {
  975. show_hidden = luaL_checknumber( L, -1);
  976. if(show_hidden!=0 && show_hidden!=1)
  977. return luaL_error( L, "show_hidden: 0 or 1" );
  978. scan_cfg.show_hidden=show_hidden;
  979. NODE_DBG("%d\n", scan_cfg.show_hidden);
  980. }
  981. else
  982. {
  983. return luaL_error( L, "wrong arg type" );
  984. }
  985. }
  986. else
  987. {
  988. scan_cfg.show_hidden=0;
  989. }
  990. if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION)
  991. {
  992. lua_pushnil(L);
  993. lua_insert(L, 2);
  994. }
  995. lua_pop(L, -4);
  996. }
  997. else if (lua_type(L, 1) == LUA_TNUMBER)
  998. {
  999. lua_pushnil(L);
  1000. lua_insert(L, 1);
  1001. }
  1002. else if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION)
  1003. {
  1004. lua_pushnil(L);
  1005. lua_insert(L, 1);
  1006. lua_pushnil(L);
  1007. lua_insert(L, 1);
  1008. }
  1009. else if(lua_isnil(L, 1))
  1010. {
  1011. if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION)
  1012. {
  1013. lua_pushnil(L);
  1014. lua_insert(L, 2);
  1015. }
  1016. }
  1017. else
  1018. {
  1019. return luaL_error( L, "wrong arg type" );
  1020. }
  1021. if (lua_type(L, 2) == LUA_TNUMBER) //this section changes the output format
  1022. {
  1023. getap_output_format=luaL_checkinteger( L, 2 );
  1024. if (getap_output_format != 0 && getap_output_format != 1)
  1025. return luaL_error( L, "wrong arg type" );
  1026. }
  1027. NODE_DBG("Use alternate output format: %d\n", getap_output_format);
  1028. if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION)
  1029. {
  1030. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  1031. register_lua_cb(L, &wifi_scan_succeed);
  1032. if (lua_type(L, 1)==LUA_TTABLE)
  1033. {
  1034. wifi_station_scan(&scan_cfg,wifi_scan_done);
  1035. }
  1036. else
  1037. {
  1038. wifi_station_scan(NULL,wifi_scan_done);
  1039. }
  1040. }
  1041. else
  1042. {
  1043. unregister_lua_cb(L, &wifi_scan_succeed);
  1044. }
  1045. return 0;
  1046. }
  1047. // Lua: wifi.sta.gethostname()
  1048. static int wifi_sta_gethostname( lua_State* L )
  1049. {
  1050. char* hostname = wifi_station_get_hostname();
  1051. lua_pushstring(L, hostname);
  1052. return 1;
  1053. }
  1054. // Used by wifi_sta_sethostname_lua and wifi_change_default_hostname
  1055. // This function checks host name to ensure that it follows RFC 952 & RFC 1123 host name standards.
  1056. static bool wifi_sta_checkhostname(const char *hostname, size_t len)
  1057. {
  1058. //the hostname must be 32 chars or less and first and last char must be alphanumeric
  1059. if (len == 0 || len > 32 || !isalnum(hostname[0]) || !isalnum(hostname[len-1])){
  1060. return false;
  1061. }
  1062. //characters in the middle of the host name must be alphanumeric or a hyphen(-) only
  1063. for (int i=1; i<len; i++){
  1064. if (!(isalnum(hostname[i]) || hostname[i]=='-')){
  1065. return false;
  1066. }
  1067. }
  1068. return true;
  1069. }
  1070. // Lua: wifi.sta.sethostname()
  1071. static int wifi_sta_sethostname_lua( lua_State* L )
  1072. {
  1073. size_t len;
  1074. const char *hostname = luaL_checklstring(L, 1, &len);
  1075. luaL_argcheck(L, wifi_sta_checkhostname(hostname, len), 1, "Invalid hostname");
  1076. lua_pushboolean(L, wifi_station_set_hostname((char*)hostname));
  1077. return 1;
  1078. }
  1079. // Lua: wifi.sta.sleeptype(type)
  1080. static int wifi_station_sleeptype( lua_State* L )
  1081. {
  1082. unsigned type;
  1083. if ( lua_isnumber(L, 1) )
  1084. {
  1085. type = lua_tointeger(L, 1);
  1086. luaL_argcheck(L, (type == NONE_SLEEP_T || type == LIGHT_SLEEP_T || type == MODEM_SLEEP_T), 1, "range:0-2");
  1087. if(!wifi_set_sleep_type(type)){
  1088. lua_pushnil(L);
  1089. return 1;
  1090. }
  1091. }
  1092. type = wifi_get_sleep_type();
  1093. lua_pushinteger( L, type );
  1094. return 1;
  1095. }
  1096. // Lua: wifi.sta.status()
  1097. static int wifi_station_status( lua_State* L )
  1098. {
  1099. uint8_t status = wifi_station_get_connect_status();
  1100. lua_pushinteger( L, status );
  1101. return 1;
  1102. }
  1103. // Lua: wifi.sta.getrssi()
  1104. static int wifi_station_getrssi( lua_State* L ){
  1105. sint8 rssival=wifi_station_get_rssi();
  1106. NODE_DBG("\n\tRSSI is %d\n", rssival);
  1107. if (rssival<10)
  1108. {
  1109. lua_pushinteger(L, rssival);
  1110. }
  1111. else
  1112. {
  1113. lua_pushnil(L);
  1114. }
  1115. return 1;
  1116. }
  1117. //Lua: wifi.ap.deauth()
  1118. static int wifi_ap_deauth( lua_State* L )
  1119. {
  1120. uint8_t mac[6];
  1121. unsigned len = 0;
  1122. if(lua_isstring(L, 1))
  1123. {
  1124. const char *macaddr = luaL_checklstring( L, 1, &len );
  1125. luaL_argcheck(L, len==17, 1, INVALID_MAC_STR);
  1126. ets_str2macaddr(mac, macaddr);
  1127. }
  1128. else
  1129. {
  1130. c_memset(&mac, 0xFF, sizeof(mac));
  1131. }
  1132. lua_pushboolean(L,wifi_softap_deauth(mac));
  1133. return 1;
  1134. }
  1135. // Lua: wifi.ap.getmac()
  1136. static int wifi_ap_getmac( lua_State* L ){
  1137. return wifi_getmac(L, SOFTAP_IF);
  1138. }
  1139. // Lua: wifi.ap.setmac()
  1140. static int wifi_ap_setmac( lua_State* L ){
  1141. return wifi_setmac(L, SOFTAP_IF);
  1142. }
  1143. // Lua: wifi.ap.getip()
  1144. static int wifi_ap_getip( lua_State* L ){
  1145. return wifi_getip(L, SOFTAP_IF);
  1146. }
  1147. // Lua: wifi.ap.setip()
  1148. static int wifi_ap_setip( lua_State* L ){
  1149. return wifi_setip(L, SOFTAP_IF);
  1150. }
  1151. // Lua: wifi.ap.getbroadcast()
  1152. static int wifi_ap_getbroadcast( lua_State* L ){
  1153. return wifi_getbroadcast(L, SOFTAP_IF);
  1154. }
  1155. // Lua: wifi.ap.getconfig()
  1156. static int wifi_ap_getconfig( lua_State* L, bool get_flash_cfg)
  1157. {
  1158. struct softap_config config;
  1159. char temp[sizeof(config.password)+1]; //max password length + '\0'
  1160. if (get_flash_cfg)
  1161. {
  1162. wifi_softap_get_config_default(&config);
  1163. }
  1164. else
  1165. {
  1166. wifi_softap_get_config(&config);
  1167. }
  1168. if(lua_isboolean(L, 1) && lua_toboolean(L, 1)==true)
  1169. {
  1170. lua_newtable(L);
  1171. memset(temp, 0, sizeof(temp));
  1172. memcpy(temp, config.ssid, sizeof(config.ssid));
  1173. lua_pushstring(L, temp);
  1174. lua_setfield(L, -2, "ssid");
  1175. if(config.authmode!=AUTH_OPEN)
  1176. {
  1177. memset(temp, 0, sizeof(temp));
  1178. memcpy(temp, config.password, sizeof(config.password));
  1179. lua_pushstring(L, temp);
  1180. lua_setfield(L, -2, "pwd");
  1181. }
  1182. lua_pushnumber(L, config.authmode);
  1183. lua_setfield(L, -2, "auth");
  1184. lua_pushnumber(L, config.channel);
  1185. lua_setfield(L, -2, "channel");
  1186. lua_pushboolean(L, (bool)config.ssid_hidden);
  1187. lua_setfield(L, -2, "hidden");
  1188. lua_pushnumber(L, config.max_connection);
  1189. lua_setfield(L, -2, "max");
  1190. lua_pushnumber(L, config.beacon_interval);
  1191. lua_setfield(L, -2, "beacon");
  1192. return 1;
  1193. }
  1194. else
  1195. {
  1196. memset(temp, 0, sizeof(temp));
  1197. memcpy(temp, config.ssid, sizeof(config.ssid));
  1198. lua_pushstring(L, temp);
  1199. if(config.authmode == AUTH_OPEN)
  1200. {
  1201. lua_pushnil(L);
  1202. }
  1203. else
  1204. {
  1205. memset(temp, 0, sizeof(temp));
  1206. memcpy(temp, config.password, sizeof(config.password));
  1207. lua_pushstring(L, temp);
  1208. }
  1209. return 2;
  1210. }
  1211. }
  1212. // Lua: wifi.sta.getconfig()
  1213. static int wifi_ap_getconfig_current(lua_State *L)
  1214. {
  1215. return wifi_ap_getconfig(L, false);
  1216. }
  1217. // Lua: wifi.sta.getdefaultconfig()
  1218. static int wifi_ap_getconfig_default(lua_State *L)
  1219. {
  1220. return wifi_ap_getconfig(L, true);
  1221. }
  1222. // Lua: wifi.ap.config(table)
  1223. static int wifi_ap_config( lua_State* L )
  1224. {
  1225. if (!lua_istable(L, 1))
  1226. {
  1227. return luaL_typerror(L, 1, lua_typename(L, LUA_TTABLE));
  1228. }
  1229. struct softap_config config;
  1230. bool save_to_flash=true;
  1231. size_t sl = 0 , pl = 0;
  1232. lua_Integer lint=0;
  1233. int Ltype_tmp=LUA_TNONE;
  1234. memset(config.ssid, 0, sizeof(config.ssid));
  1235. memset(config.password, 0, sizeof(config.password));
  1236. lua_getfield(L, 1, "ssid");
  1237. if (!lua_isnil(L, -1)) /* found? */
  1238. {
  1239. if( lua_isstring(L, -1) ) // deal with the ssid string
  1240. {
  1241. const char *ssid = luaL_checklstring( L, -1, &sl );
  1242. luaL_argcheck(L, ((sl>=1 && sl<=sizeof(config.ssid)) ), 1, "ssid: length:1-32");
  1243. memcpy(config.ssid, ssid, sl);
  1244. config.ssid_len = sl;
  1245. config.ssid_hidden = 0;
  1246. }
  1247. else
  1248. {
  1249. return luaL_argerror( L, 1, "ssid: not string" );
  1250. }
  1251. }
  1252. else
  1253. {
  1254. return luaL_argerror( L, 1, "ssid: required" );
  1255. }
  1256. lua_pop(L, 1);
  1257. lua_getfield(L, 1, "pwd");
  1258. if (!lua_isnil(L, -1)) /* found? */
  1259. {
  1260. if( lua_isstring(L, -1) ) // deal with the password string
  1261. {
  1262. const char *pwd = luaL_checklstring( L, -1, &pl );
  1263. luaL_argcheck(L, (pl>=8 && pl<=sizeof(config.password)), 1, "pwd: length:0 or 8-64");
  1264. memcpy(config.password, pwd, pl);
  1265. config.authmode = AUTH_WPA_WPA2_PSK;
  1266. }
  1267. else
  1268. {
  1269. return luaL_argerror( L, 1, "pwd: not string" );
  1270. }
  1271. }
  1272. else
  1273. {
  1274. config.authmode = AUTH_OPEN;
  1275. }
  1276. lua_pop(L, 1);
  1277. lua_getfield(L, 1, "auth");
  1278. if (!lua_isnil(L, -1))
  1279. {
  1280. if(lua_isnumber(L, -1))
  1281. {
  1282. lint=luaL_checkinteger(L, -1);
  1283. luaL_argcheck(L, (lint >= 0 && lint < AUTH_MAX), 1, "auth: Range:0-4");
  1284. config.authmode = (uint8_t)luaL_checkinteger(L, -1);
  1285. }
  1286. else
  1287. {
  1288. return luaL_argerror(L, 1, "auth: not number");
  1289. }
  1290. }
  1291. lua_pop(L, 1);
  1292. lua_getfield(L, 1, "channel");
  1293. if (!lua_isnil(L, -1))
  1294. {
  1295. if(lua_isnumber(L, -1))
  1296. {
  1297. lint=luaL_checkinteger(L, -1);
  1298. luaL_argcheck(L, (lint >= 1 && lint <= 13), 1, "channel: Range:1-13");
  1299. config.channel = (uint8_t)lint;
  1300. }
  1301. else
  1302. {
  1303. luaL_argerror(L, 1, "channel: not number");
  1304. }
  1305. }
  1306. else
  1307. {
  1308. config.channel = 6;
  1309. }
  1310. lua_pop(L, 1);
  1311. lua_getfield(L, 1, "hidden");
  1312. if (!lua_isnil(L, -1))
  1313. {
  1314. Ltype_tmp=lua_type(L, -1);
  1315. if(Ltype_tmp==LUA_TNUMBER||Ltype_tmp==LUA_TBOOLEAN)
  1316. {
  1317. if(Ltype_tmp==LUA_TNUMBER)
  1318. {
  1319. lint=luaL_checkinteger(L, -1);
  1320. }
  1321. if(Ltype_tmp==LUA_TBOOLEAN)
  1322. {
  1323. lint=(lua_Number)lua_toboolean(L, -1);
  1324. }
  1325. luaL_argcheck(L, (lint == 0 || lint==1), 1, "hidden: 0 or 1");
  1326. config.ssid_hidden = (uint8_t)lint;
  1327. }
  1328. else
  1329. {
  1330. return luaL_argerror(L, 1, "hidden: not boolean");
  1331. }
  1332. }
  1333. else
  1334. {
  1335. config.ssid_hidden = 0;
  1336. }
  1337. lua_pop(L, 1);
  1338. lua_getfield(L, 1, "max");
  1339. if (!lua_isnil(L, -1))
  1340. {
  1341. if(lua_isnumber(L, -1))
  1342. {
  1343. lint=luaL_checkinteger(L, -1);
  1344. luaL_argcheck(L, (lint >= 1 && lint <= 4), 1, "max: 1-4");
  1345. config.max_connection = (uint8_t)lint;
  1346. }
  1347. else
  1348. {
  1349. return luaL_argerror(L, 1, "max: not number");
  1350. }
  1351. }
  1352. else
  1353. {
  1354. config.max_connection = 4;
  1355. }
  1356. lua_pop(L, 1);
  1357. lua_getfield(L, 1, "beacon");
  1358. if (!lua_isnil(L, -1))
  1359. {
  1360. if(lua_isnumber(L, -1))
  1361. {
  1362. lint=luaL_checkinteger(L, -1);
  1363. luaL_argcheck(L, (lint >= 100 && lint <= 60000), 1, "beacon: 100-60000");
  1364. config.beacon_interval = (uint16_t)lint;
  1365. }
  1366. else
  1367. {
  1368. return luaL_argerror(L, 1, "beacon: not number");
  1369. }
  1370. }
  1371. else
  1372. {
  1373. config.beacon_interval = 100;
  1374. }
  1375. lua_pop(L, 1);
  1376. lua_getfield(L, 1, "save");
  1377. if (!lua_isnil(L, -1))
  1378. {
  1379. if (lua_isboolean(L, -1))
  1380. {
  1381. save_to_flash=lua_toboolean(L, -1);
  1382. }
  1383. else
  1384. {
  1385. return luaL_argerror(L, 1, "save: not boolean");
  1386. }
  1387. }
  1388. lua_pop(L, 1);
  1389. #ifdef WIFI_SDK_EVENT_MONITOR_ENABLE
  1390. lua_State* L_temp = NULL;
  1391. lua_getfield(L, 1, "staconnected_cb");
  1392. if (!lua_isnil(L, -1))
  1393. {
  1394. if (lua_isfunction(L, -1))
  1395. {
  1396. L_temp = lua_newthread(L);
  1397. lua_pushnumber(L, EVENT_SOFTAPMODE_STACONNECTED);
  1398. lua_pushvalue(L, -3);
  1399. lua_xmove(L, L_temp, 2);
  1400. wifi_event_monitor_register(L_temp);
  1401. }
  1402. else
  1403. {
  1404. return luaL_argerror(L, 1, "staconnected_cb:not function");
  1405. }
  1406. }
  1407. lua_pop(L, 1);
  1408. lua_getfield(L, 1, "stadisconnected_cb");
  1409. if (!lua_isnil(L, -1))
  1410. {
  1411. if (lua_isfunction(L, -1))
  1412. {
  1413. L_temp = lua_newthread(L);
  1414. lua_pushnumber(L, EVENT_SOFTAPMODE_STADISCONNECTED);
  1415. lua_pushvalue(L, -3);
  1416. lua_xmove(L, L_temp, 2);
  1417. wifi_event_monitor_register(L_temp);
  1418. }
  1419. else
  1420. {
  1421. return luaL_argerror(L, 1, "stadisconnected_cb:not function");
  1422. }
  1423. }
  1424. lua_pop(L, 1);
  1425. lua_getfield(L, 1, "probereq_cb");
  1426. if (!lua_isnil(L, -1))
  1427. {
  1428. if (lua_isfunction(L, -1))
  1429. {
  1430. L_temp = lua_newthread(L);
  1431. lua_pushnumber(L, EVENT_SOFTAPMODE_PROBEREQRECVED);
  1432. lua_pushvalue(L, -3);
  1433. lua_xmove(L, L_temp, 2);
  1434. wifi_event_monitor_register(L_temp);
  1435. }
  1436. else
  1437. {
  1438. return luaL_argerror(L, 1, "probereq_cb:not function");
  1439. }
  1440. }
  1441. lua_pop(L, 1);
  1442. #endif
  1443. #if defined(WIFI_DEBUG)
  1444. char debug_temp[sizeof(config.password)+1];
  1445. memset(debug_temp, 0, sizeof(debug_temp));
  1446. memcpy(debug_temp, config.ssid, sizeof(config.ssid));
  1447. WIFI_DBG("\n\tconfig.ssid=\"%s\" len=%d\n", debug_temp, sl);
  1448. memset(debug_temp, 0, sizeof(debug_temp));
  1449. memcpy(debug_temp, config.password, sizeof(config.password));
  1450. WIFI_DBG("\tconfig.password=\"%s\" len=%d\n", debug_temp, pl);
  1451. WIFI_DBG("\tconfig.authmode=%d\n", config.authmode);
  1452. WIFI_DBG("\tconfig.channel=%d\n", config.channel);
  1453. WIFI_DBG("\tconfig.ssid_hidden=%d\n", config.ssid_hidden);
  1454. WIFI_DBG("\tconfig.max_connection=%d\n", config.max_connection);
  1455. WIFI_DBG("\tconfig.beacon_interval=%d\n", config.beacon_interval);
  1456. WIFI_DBG("\tsave_to_flash=%s\n", save_to_flash ? "true":"false");
  1457. #endif
  1458. bool config_success;
  1459. if(save_to_flash)
  1460. {
  1461. config_success = wifi_softap_set_config(&config);
  1462. }
  1463. else
  1464. {
  1465. config_success = wifi_softap_set_config_current(&config);
  1466. }
  1467. lua_pushboolean(L, config_success);
  1468. return 1;
  1469. }
  1470. // Lua: table = wifi.ap.getclient()
  1471. static int wifi_ap_listclient( lua_State* L )
  1472. {
  1473. if (wifi_get_opmode() == STATION_MODE)
  1474. {
  1475. return luaL_error( L, "Can't list client in STATION_MODE mode" );
  1476. }
  1477. char temp[64];
  1478. lua_newtable(L);
  1479. struct station_info * station = wifi_softap_get_station_info();
  1480. struct station_info * next_station;
  1481. while (station != NULL)
  1482. {
  1483. c_sprintf(temp, MACSTR, MAC2STR(station->bssid));
  1484. wifi_add_sprintf_field(L, temp, IPSTR, IP2STR(&station->ip));
  1485. next_station = STAILQ_NEXT(station, next);
  1486. c_free(station);
  1487. station = next_station;
  1488. }
  1489. return 1;
  1490. }
  1491. // Lua: ip = wifi.ap.dhcp.config()
  1492. static int wifi_ap_dhcp_config( lua_State* L )
  1493. {
  1494. if (!lua_istable(L, 1))
  1495. return luaL_error( L, "wrong arg type" );
  1496. struct dhcps_lease lease;
  1497. uint32_t ip;
  1498. ip = parse_key(L, "start");
  1499. if (ip == 0)
  1500. return luaL_error( L, "wrong arg type" );
  1501. lease.start_ip.addr = ip;
  1502. NODE_DBG(IPSTR, IP2STR(&lease.start_ip));
  1503. NODE_DBG("\n");
  1504. // use configured max_connection to determine end
  1505. struct softap_config config;
  1506. wifi_softap_get_config(&config);
  1507. lease.end_ip = lease.start_ip;
  1508. ip4_addr4(&lease.end_ip) += config.max_connection - 1;
  1509. char temp[64];
  1510. c_sprintf(temp, IPSTR, IP2STR(&lease.start_ip));
  1511. lua_pushstring(L, temp);
  1512. c_sprintf(temp, IPSTR, IP2STR(&lease.end_ip));
  1513. lua_pushstring(L, temp);
  1514. // note: DHCP max range = 101 from start_ip to end_ip
  1515. wifi_softap_dhcps_stop();
  1516. wifi_softap_set_dhcps_lease(&lease);
  1517. wifi_softap_dhcps_start();
  1518. return 2;
  1519. }
  1520. // Lua: wifi.ap.dhcp.start()
  1521. static int wifi_ap_dhcp_start( lua_State* L )
  1522. {
  1523. lua_pushboolean(L, wifi_softap_dhcps_start());
  1524. return 1;
  1525. }
  1526. // Lua: wifi.ap.dhcp.stop()
  1527. static int wifi_ap_dhcp_stop( lua_State* L )
  1528. {
  1529. lua_pushboolean(L, wifi_softap_dhcps_stop());
  1530. return 1;
  1531. }
  1532. // Module function map
  1533. static const LUA_REG_TYPE wifi_station_map[] = {
  1534. { LSTRKEY( "autoconnect" ), LFUNCVAL( wifi_station_setauto ) },
  1535. { LSTRKEY( "changeap" ), LFUNCVAL( wifi_station_change_ap ) },
  1536. { LSTRKEY( "clearconfig"), LFUNCVAL( wifi_station_clear_config ) },
  1537. { LSTRKEY( "config" ), LFUNCVAL( wifi_station_config ) },
  1538. { LSTRKEY( "connect" ), LFUNCVAL( wifi_station_connect4lua ) },
  1539. { LSTRKEY( "disconnect" ), LFUNCVAL( wifi_station_disconnect4lua ) },
  1540. { LSTRKEY( "getap" ), LFUNCVAL( wifi_station_listap ) },
  1541. { LSTRKEY( "getapindex" ), LFUNCVAL( wifi_station_get_ap_index ) },
  1542. { LSTRKEY( "getapinfo" ), LFUNCVAL( wifi_station_get_ap_info4lua ) },
  1543. { LSTRKEY( "getbroadcast" ), LFUNCVAL( wifi_station_getbroadcast) },
  1544. { LSTRKEY( "getconfig" ), LFUNCVAL( wifi_station_getconfig_current ) },
  1545. { LSTRKEY( "getdefaultconfig" ), LFUNCVAL( wifi_station_getconfig_default ) },
  1546. { LSTRKEY( "gethostname" ), LFUNCVAL( wifi_sta_gethostname ) },
  1547. { LSTRKEY( "getip" ), LFUNCVAL( wifi_station_getip ) },
  1548. { LSTRKEY( "getmac" ), LFUNCVAL( wifi_station_getmac ) },
  1549. { LSTRKEY( "getrssi" ), LFUNCVAL( wifi_station_getrssi ) },
  1550. { LSTRKEY( "setaplimit" ), LFUNCVAL( wifi_station_ap_number_set4lua ) },
  1551. { LSTRKEY( "sethostname" ), LFUNCVAL( wifi_sta_sethostname_lua ) },
  1552. { LSTRKEY( "setip" ), LFUNCVAL( wifi_station_setip ) },
  1553. { LSTRKEY( "setmac" ), LFUNCVAL( wifi_station_setmac ) },
  1554. { LSTRKEY( "sleeptype" ), LFUNCVAL( wifi_station_sleeptype ) },
  1555. { LSTRKEY( "status" ), LFUNCVAL( wifi_station_status ) },
  1556. { LNILKEY, LNILVAL }
  1557. };
  1558. static const LUA_REG_TYPE wifi_ap_dhcp_map[] = {
  1559. { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_dhcp_config ) },
  1560. { LSTRKEY( "start" ), LFUNCVAL( wifi_ap_dhcp_start ) },
  1561. { LSTRKEY( "stop" ), LFUNCVAL( wifi_ap_dhcp_stop ) },
  1562. { LNILKEY, LNILVAL }
  1563. };
  1564. static const LUA_REG_TYPE wifi_ap_map[] = {
  1565. { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_config ) },
  1566. { LSTRKEY( "deauth" ), LFUNCVAL( wifi_ap_deauth ) },
  1567. { LSTRKEY( "getip" ), LFUNCVAL( wifi_ap_getip ) },
  1568. { LSTRKEY( "setip" ), LFUNCVAL( wifi_ap_setip ) },
  1569. { LSTRKEY( "getbroadcast" ), LFUNCVAL( wifi_ap_getbroadcast) },
  1570. { LSTRKEY( "getmac" ), LFUNCVAL( wifi_ap_getmac ) },
  1571. { LSTRKEY( "setmac" ), LFUNCVAL( wifi_ap_setmac ) },
  1572. { LSTRKEY( "getclient" ), LFUNCVAL( wifi_ap_listclient ) },
  1573. { LSTRKEY( "getconfig" ), LFUNCVAL( wifi_ap_getconfig_current ) },
  1574. { LSTRKEY( "getdefaultconfig" ), LFUNCVAL( wifi_ap_getconfig_default ) },
  1575. { LSTRKEY( "dhcp" ), LROVAL( wifi_ap_dhcp_map ) },
  1576. //{ LSTRKEY( "__metatable" ), LROVAL( wifi_ap_map ) },
  1577. { LNILKEY, LNILVAL }
  1578. };
  1579. static const LUA_REG_TYPE wifi_map[] = {
  1580. { LSTRKEY( "setmode" ), LFUNCVAL( wifi_setmode ) },
  1581. { LSTRKEY( "getmode" ), LFUNCVAL( wifi_getmode ) },
  1582. { LSTRKEY( "getdefaultmode" ), LFUNCVAL( wifi_getdefaultmode ) },
  1583. { LSTRKEY( "getchannel" ), LFUNCVAL( wifi_getchannel ) },
  1584. { LSTRKEY( "setphymode" ), LFUNCVAL( wifi_setphymode ) },
  1585. { LSTRKEY( "getphymode" ), LFUNCVAL( wifi_getphymode ) },
  1586. { LSTRKEY( "setmaxtxpower" ), LFUNCVAL( wifi_setmaxtxpower ) },
  1587. #ifdef PMSLEEP_ENABLE
  1588. { LSTRKEY( "suspend" ), LFUNCVAL( wifi_suspend ) },
  1589. { LSTRKEY( "resume" ), LFUNCVAL( wifi_resume ) },
  1590. #endif
  1591. { LSTRKEY( "nullmodesleep" ), LFUNCVAL( wifi_null_mode_auto_sleep ) },
  1592. #ifdef WIFI_SMART_ENABLE
  1593. { LSTRKEY( "startsmart" ), LFUNCVAL( wifi_start_smart ) },
  1594. { LSTRKEY( "stopsmart" ), LFUNCVAL( wifi_exit_smart ) },
  1595. #endif
  1596. { LSTRKEY( "sleeptype" ), LFUNCVAL( wifi_station_sleeptype ) },
  1597. { LSTRKEY( "sta" ), LROVAL( wifi_station_map ) },
  1598. { LSTRKEY( "ap" ), LROVAL( wifi_ap_map ) },
  1599. #if defined(WIFI_SDK_EVENT_MONITOR_ENABLE)
  1600. { LSTRKEY( "eventmon" ), LROVAL( wifi_event_monitor_map ) }, //declared in wifi_eventmon.c
  1601. #endif
  1602. #if defined(LUA_USE_MODULES_WIFI_MONITOR)
  1603. { LSTRKEY( "monitor" ), LROVAL( wifi_monitor_map ) }, //declared in wifi_monitor.c
  1604. #endif
  1605. { LSTRKEY( "NULLMODE" ), LNUMVAL( NULL_MODE ) },
  1606. { LSTRKEY( "STATION" ), LNUMVAL( STATION_MODE ) },
  1607. { LSTRKEY( "SOFTAP" ), LNUMVAL( SOFTAP_MODE ) },
  1608. { LSTRKEY( "STATIONAP" ), LNUMVAL( STATIONAP_MODE ) },
  1609. { LSTRKEY( "PHYMODE_B" ), LNUMVAL( PHY_MODE_11B ) },
  1610. { LSTRKEY( "PHYMODE_G" ), LNUMVAL( PHY_MODE_11G ) },
  1611. { LSTRKEY( "PHYMODE_N" ), LNUMVAL( PHY_MODE_11N ) },
  1612. { LSTRKEY( "NONE_SLEEP" ), LNUMVAL( NONE_SLEEP_T ) },
  1613. { LSTRKEY( "LIGHT_SLEEP" ), LNUMVAL( LIGHT_SLEEP_T ) },
  1614. { LSTRKEY( "MODEM_SLEEP" ), LNUMVAL( MODEM_SLEEP_T ) },
  1615. { LSTRKEY( "OPEN" ), LNUMVAL( AUTH_OPEN ) },
  1616. //{ LSTRKEY( "WEP" ), LNUMVAL( AUTH_WEP ) },
  1617. { LSTRKEY( "WPA_PSK" ), LNUMVAL( AUTH_WPA_PSK ) },
  1618. { LSTRKEY( "WPA2_PSK" ), LNUMVAL( AUTH_WPA2_PSK ) },
  1619. { LSTRKEY( "WPA_WPA2_PSK" ), LNUMVAL( AUTH_WPA_WPA2_PSK ) },
  1620. { LSTRKEY( "STA_IDLE" ), LNUMVAL( STATION_IDLE ) },
  1621. { LSTRKEY( "STA_CONNECTING" ), LNUMVAL( STATION_CONNECTING ) },
  1622. { LSTRKEY( "STA_WRONGPWD" ), LNUMVAL( STATION_WRONG_PASSWORD ) },
  1623. { LSTRKEY( "STA_APNOTFOUND" ), LNUMVAL( STATION_NO_AP_FOUND ) },
  1624. { LSTRKEY( "STA_FAIL" ), LNUMVAL( STATION_CONNECT_FAIL ) },
  1625. { LSTRKEY( "STA_GOTIP" ), LNUMVAL( STATION_GOT_IP ) },
  1626. { LSTRKEY( "__metatable" ), LROVAL( wifi_map ) },
  1627. { LNILKEY, LNILVAL }
  1628. };
  1629. // Used by user_rf_pre_init(user_main.c)
  1630. void wifi_change_default_host_name(void)
  1631. {
  1632. uint8 opmode_temp=wifi_get_opmode();
  1633. wifi_set_opmode_current(STATION_MODE);
  1634. char temp[33] = {0};//32 chars + NULL
  1635. uint8_t mac[6];
  1636. wifi_get_macaddr(STATION_IF, mac);
  1637. #ifndef WIFI_STA_HOSTNAME
  1638. c_sprintf(temp, "NODE-%X%X%X", (mac)[3], (mac)[4], (mac)[5]);
  1639. #elif defined(WIFI_STA_HOSTNAME) && !defined(WIFI_STA_HOSTNAME_APPEND_MAC)
  1640. if(wifi_sta_checkhostname(WIFI_STA_HOSTNAME, strlen(WIFI_STA_HOSTNAME))){
  1641. c_sprintf(temp, "%s", WIFI_STA_HOSTNAME);
  1642. }
  1643. else{
  1644. c_sprintf(temp, "NODE-%X%X%X", (mac)[3], (mac)[4], (mac)[5]);
  1645. }
  1646. #elif defined(WIFI_STA_HOSTNAME) && defined(WIFI_STA_HOSTNAME_APPEND_MAC)
  1647. if(strlen(WIFI_STA_HOSTNAME) <= 26 && wifi_sta_checkhostname(WIFI_STA_HOSTNAME, strlen(WIFI_STA_HOSTNAME))){
  1648. c_sprintf(temp, "%s%X%X%X", WIFI_STA_HOSTNAME, (mac)[3], (mac)[4], (mac)[5]);
  1649. }
  1650. else{
  1651. c_sprintf(temp, "NODE-%X%X%X", (mac)[3], (mac)[4], (mac)[5]);
  1652. }
  1653. #endif
  1654. wifi_station_set_hostname((char*)temp);
  1655. if(opmode_temp != wifi_get_opmode()){
  1656. wifi_set_opmode_current(opmode_temp);
  1657. }
  1658. }
  1659. int luaopen_wifi( lua_State *L )
  1660. {
  1661. wifi_fpm_auto_sleep_set_in_null_mode(1);
  1662. //if esp is already in NULL_MODE, auto sleep setting won't take effect until next wifi_set_opmode(NULL_MODE) call.
  1663. if(wifi_get_opmode()==NULL_MODE)
  1664. {
  1665. wifi_set_opmode_current(NULL_MODE);
  1666. }
  1667. #if defined(WIFI_SDK_EVENT_MONITOR_ENABLE)
  1668. wifi_eventmon_init();
  1669. #endif
  1670. #if defined(LUA_USE_MODULES_WIFI_MONITOR)
  1671. wifi_monitor_init(L);
  1672. #endif
  1673. return 0;
  1674. }
  1675. NODEMCU_MODULE(WIFI, "wifi", wifi_map, luaopen_wifi);