wifi.c 46 KB

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