wifi.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. // Module for interfacing with WIFI
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. #include "c_string.h"
  6. #include "c_stdlib.h"
  7. #include "ctype.h"
  8. #include "c_types.h"
  9. #include "user_interface.h"
  10. #include "wifi_common.h"
  11. #ifdef WIFI_SMART_ENABLE
  12. #include "smart.h"
  13. #include "smartconfig.h"
  14. static int wifi_smart_succeed = LUA_NOREF;
  15. #endif
  16. static uint8 getap_output_format=0;
  17. #define INVALID_MAC_STR "MAC:FF:FF:FF:FF:FF:FF"
  18. //wifi.sleep variables
  19. #define FPM_SLEEP_MAX_TIME 0xFFFFFFF
  20. static bool FLAG_wifi_force_sleep_enabled=0;
  21. #ifdef WIFI_SMART_ENABLE
  22. static void wifi_smart_succeed_cb(sc_status status, void *pdata){
  23. NODE_DBG("wifi_smart_succeed_cb is called.\n");
  24. lua_State* L = lua_getstate();
  25. if (status == SC_STATUS_LINK_OVER)
  26. {
  27. smartconfig_stop();
  28. return;
  29. }
  30. #if defined( NODE_SMART_OLDSTYLE )
  31. if (status != SC_STATUS_LINK || !pdata)
  32. return;
  33. if(wifi_smart_succeed == LUA_NOREF)
  34. return;
  35. lua_State* L = (lua_State *)arg;
  36. lua_rawgeti(L, LUA_REGISTRYINDEX, wifi_smart_succeed);
  37. lua_call(L, 0, 0);
  38. #else
  39. if (status != SC_STATUS_LINK || !pdata)
  40. return;
  41. struct station_config *sta_conf = pdata;
  42. wifi_station_set_config(sta_conf);
  43. wifi_station_disconnect();
  44. wifi_station_connect();
  45. if(wifi_smart_succeed != LUA_NOREF)
  46. {
  47. lua_rawgeti(L, LUA_REGISTRYINDEX, wifi_smart_succeed);
  48. lua_pushstring(L, sta_conf->ssid);
  49. lua_pushstring(L, sta_conf->password);
  50. lua_call(L, 2, 0);
  51. unregister_lua_cb(L, &wifi_smart_succeed);
  52. }
  53. #endif // defined( NODE_SMART_OLDSTYLE )
  54. }
  55. #endif // WIFI_SMART_ENABLE
  56. static int wifi_scan_succeed = LUA_NOREF;
  57. /**
  58. * @brief Wifi ap scan over callback to display.
  59. * @param arg: contain the aps information
  60. * @param status: scan over status
  61. * @retval None
  62. */
  63. static void wifi_scan_done(void *arg, STATUS status)
  64. {
  65. lua_State* L = lua_getstate();
  66. uint8 ssid[33];
  67. char temp[sizeof("11:22:33:44:55:66")];
  68. if(wifi_scan_succeed == LUA_NOREF)
  69. return;
  70. if(arg == NULL)
  71. return;
  72. lua_rawgeti(L, LUA_REGISTRYINDEX, wifi_scan_succeed);
  73. if (status == OK)
  74. {
  75. struct bss_info *bss_link = (struct bss_info *)arg;
  76. lua_newtable( L );
  77. while (bss_link != NULL)
  78. {
  79. c_memset(ssid, 0, 33);
  80. if (c_strlen(bss_link->ssid) <= 32)
  81. {
  82. c_memcpy(ssid, bss_link->ssid, c_strlen(bss_link->ssid));
  83. }
  84. else
  85. {
  86. c_memcpy(ssid, bss_link->ssid, 32);
  87. }
  88. if(getap_output_format==1) //use new format(BSSID : SSID, RSSI, Authmode, Channel)
  89. {
  90. c_sprintf(temp,MACSTR, MAC2STR(bss_link->bssid));
  91. wifi_add_sprintf_field(L, temp, "%s,%d,%d,%d",
  92. ssid, bss_link->rssi, bss_link->authmode, bss_link->channel);
  93. NODE_DBG(MACSTR" : %s\n",MAC2STR(bss_link->bssid) , temp);//00 00 00 00 00 00
  94. }
  95. else//use old format(SSID : Authmode, RSSI, BSSID, Channel)
  96. {
  97. wifi_add_sprintf_field(L, ssid, "%d,%d,"MACSTR",%d",
  98. bss_link->authmode, bss_link->rssi, MAC2STR(bss_link->bssid),bss_link->channel);
  99. NODE_DBG("%s : %s\n", ssid, temp);
  100. }
  101. bss_link = bss_link->next.stqe_next;
  102. }
  103. }
  104. else
  105. {
  106. lua_newtable( L );
  107. }
  108. lua_call(L, 1, 0);
  109. unregister_lua_cb(L, &wifi_scan_succeed);
  110. }
  111. #ifdef WIFI_SMART_ENABLE
  112. // Lua: smart(channel, function succeed_cb)
  113. // Lua: smart(type, function succeed_cb)
  114. static int wifi_start_smart( lua_State* L )
  115. {
  116. #if defined( NODE_SMART_OLDSTYLE )
  117. unsigned channel;
  118. int stack = 1;
  119. if ( lua_isnumber(L, stack) ){
  120. channel = lua_tointeger(L, stack);
  121. stack++;
  122. } else {
  123. channel = 6;
  124. }
  125. // luaL_checkanyfunction(L, stack);
  126. if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
  127. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  128. if(wifi_smart_succeed != LUA_NOREF)
  129. luaL_unref(L, LUA_REGISTRYINDEX, wifi_smart_succeed);
  130. wifi_smart_succeed = luaL_ref(L, LUA_REGISTRYINDEX);
  131. }
  132. if ( channel > 14 || channel < 1 )
  133. return luaL_error( L, "wrong arg range" );
  134. if(wifi_smart_succeed == LUA_NOREF){
  135. smart_begin(channel, NULL, NULL);
  136. }else{
  137. smart_begin(channel, (smart_succeed )wifi_smart_succeed_cb, L);
  138. }
  139. #else
  140. if(wifi_get_opmode() != STATION_MODE)
  141. {
  142. return luaL_error( L, "Smart link only in STATION mode" );
  143. }
  144. uint8_t smart_type = 0;
  145. int stack = 1;
  146. if ( lua_isnumber(L, stack) )
  147. {
  148. smart_type = lua_tointeger(L, stack);
  149. stack++;
  150. }
  151. if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION)
  152. {
  153. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  154. register_lua_cb(L, &wifi_smart_succeed);
  155. }
  156. if ( smart_type > 1 )
  157. return luaL_error( L, "wrong arg range" );
  158. smartconfig_set_type(smart_type);
  159. smartconfig_start(wifi_smart_succeed_cb);
  160. #endif // defined( NODE_SMART_OLDSTYLE )
  161. return 0;
  162. }
  163. // Lua: exit_smart()
  164. static int wifi_exit_smart( lua_State* L )
  165. {
  166. #if defined( NODE_SMART_OLDSTYLE )
  167. smart_end();
  168. #else
  169. smartconfig_stop();
  170. #endif // defined( NODE_SMART_OLDSTYLE )
  171. unregister_lua_cb(L, &wifi_smart_succeed);
  172. return 0;
  173. }
  174. #endif // WIFI_SMART_ENABLE
  175. // Lua: realmode = setmode(mode)
  176. static int wifi_setmode( lua_State* L )
  177. {
  178. unsigned mode;
  179. mode = luaL_checkinteger( L, 1 );
  180. if ( mode != STATION_MODE && mode != SOFTAP_MODE && mode != STATIONAP_MODE && mode != NULL_MODE )
  181. return luaL_error( L, "wrong arg type" );
  182. wifi_set_opmode( (uint8_t)mode);
  183. mode = (unsigned)wifi_get_opmode();
  184. lua_pushinteger( L, mode );
  185. return 1;
  186. }
  187. // Lua: realmode = getmode()
  188. static int wifi_getmode( lua_State* L )
  189. {
  190. unsigned mode;
  191. mode = (unsigned)wifi_get_opmode();
  192. lua_pushinteger( L, mode );
  193. return 1;
  194. }
  195. /**
  196. * wifi.getchannel()
  197. * Description:
  198. * Get current wifi Channel
  199. *
  200. * Syntax:
  201. * wifi.getchannel()
  202. * Parameters:
  203. * nil
  204. *
  205. * Returns:
  206. * Current wifi channel
  207. */
  208. static int wifi_getchannel( lua_State* L )
  209. {
  210. unsigned channel;
  211. channel = (unsigned)wifi_get_channel();
  212. lua_pushinteger( L, channel );
  213. return 1;
  214. }
  215. /**
  216. * wifi.setphymode()
  217. * Description:
  218. * Set wifi physical mode(802.11 b/g/n)
  219. * Note: SoftAP only supports 802.11 b/g.
  220. * Syntax:
  221. * wifi.setphymode(mode)
  222. * Parameters:
  223. * mode:
  224. * wifi.PHYMODE_B
  225. * wifi.PHYMODE_G
  226. * wifi.PHYMODE_N
  227. * Returns:
  228. * Current physical mode after setup
  229. */
  230. static int wifi_setphymode( lua_State* L )
  231. {
  232. unsigned mode;
  233. mode = luaL_checkinteger( L, 1 );
  234. if ( mode != PHY_MODE_11B && mode != PHY_MODE_11G && mode != PHY_MODE_11N )
  235. return luaL_error( L, "wrong arg type" );
  236. wifi_set_phy_mode( (uint8_t)mode);
  237. mode = (unsigned)wifi_get_phy_mode();
  238. lua_pushinteger( L, mode );
  239. return 1;
  240. }
  241. /**
  242. * wifi.getphymode()
  243. * Description:
  244. * Get wifi physical mode(802.11 b/g/n)
  245. * Syntax:
  246. * wifi.getphymode()
  247. * Parameters:
  248. * nil
  249. * Returns:
  250. * Current physical mode.
  251. *
  252. */
  253. static int wifi_getphymode( lua_State* L )
  254. {
  255. unsigned mode;
  256. mode = (unsigned)wifi_get_phy_mode();
  257. lua_pushinteger( L, mode );
  258. return 1;
  259. }
  260. //wifi.sleep()
  261. static int wifi_sleep(lua_State* L)
  262. {
  263. uint8 desired_sleep_state = 2;
  264. sint8 wifi_fpm_do_sleep_return_value = 1;
  265. if(lua_isnumber(L, 1))
  266. {
  267. if(luaL_checknumber(L, 1) == 0)
  268. {
  269. desired_sleep_state = 0;
  270. }
  271. else if(luaL_checknumber(L, 1) == 1)
  272. {
  273. desired_sleep_state = 1;
  274. }
  275. }
  276. if (!FLAG_wifi_force_sleep_enabled && desired_sleep_state == 1 )
  277. {
  278. uint8 wifi_current_opmode = wifi_get_opmode();
  279. if (wifi_current_opmode == 1 || wifi_current_opmode == 3 )
  280. {
  281. wifi_station_disconnect();
  282. }
  283. // set WiFi mode to null mode
  284. wifi_set_opmode(NULL_MODE);
  285. // set force sleep type
  286. wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
  287. wifi_fpm_open();
  288. wifi_fpm_do_sleep_return_value = wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
  289. if (wifi_fpm_do_sleep_return_value == 0)
  290. {
  291. FLAG_wifi_force_sleep_enabled = TRUE;
  292. }
  293. else
  294. {
  295. wifi_fpm_close();
  296. FLAG_wifi_force_sleep_enabled = FALSE;
  297. }
  298. }
  299. else if(FLAG_wifi_force_sleep_enabled && desired_sleep_state == 0)
  300. {
  301. FLAG_wifi_force_sleep_enabled = FALSE;
  302. // wake up to use WiFi again
  303. wifi_fpm_do_wakeup();
  304. wifi_fpm_close();
  305. }
  306. if (desired_sleep_state == 1 && FLAG_wifi_force_sleep_enabled == FALSE)
  307. {
  308. lua_pushnil(L);
  309. lua_pushnumber(L, wifi_fpm_do_sleep_return_value);
  310. }
  311. else
  312. {
  313. lua_pushnumber(L, FLAG_wifi_force_sleep_enabled);
  314. lua_pushnil(L);
  315. }
  316. return 2;
  317. }
  318. //wifi.nullmodesleep()
  319. static int wifi_null_mode_auto_sleep(lua_State* L)
  320. {
  321. if (!lua_isnone(L, 1))
  322. {
  323. bool auto_sleep_setting=lua_toboolean(L, 1);
  324. if (auto_sleep_setting!=(bool) get_fpm_auto_sleep_flag())
  325. {
  326. wifi_fpm_auto_sleep_set_in_null_mode((uint8)auto_sleep_setting);
  327. //if esp is already in NULL_MODE, auto sleep setting won't take effect until next wifi_set_opmode(NULL_MODE) call.
  328. if(wifi_get_opmode()==NULL_MODE)
  329. {
  330. wifi_set_opmode_current(NULL_MODE);
  331. }
  332. }
  333. }
  334. lua_pushboolean(L, (bool) get_fpm_auto_sleep_flag());
  335. return 1;
  336. }
  337. // Lua: mac = wifi.xx.getmac()
  338. static int wifi_getmac( lua_State* L, uint8_t mode )
  339. {
  340. char temp[64];
  341. uint8_t mac[6];
  342. wifi_get_macaddr(mode, mac);
  343. c_sprintf(temp, MACSTR, MAC2STR(mac));
  344. lua_pushstring( L, temp );
  345. return 1;
  346. }
  347. // Lua: mac = wifi.xx.setmac()
  348. static int wifi_setmac( lua_State* L, uint8_t mode )
  349. {
  350. uint8_t mac[6];
  351. unsigned len = 0;
  352. const char *macaddr = luaL_checklstring( L, 1, &len );
  353. luaL_argcheck(L, len==17, 1, INVALID_MAC_STR);
  354. ets_str2macaddr(mac, macaddr);
  355. lua_pushboolean(L,wifi_set_macaddr(mode, (uint8 *)mac));
  356. return 1;
  357. }
  358. // Lua: ip = wifi.xx.getip()
  359. static int wifi_getip( lua_State* L, uint8_t mode )
  360. {
  361. struct ip_info pTempIp;
  362. char temp[64];
  363. wifi_get_ip_info(mode, &pTempIp);
  364. if(pTempIp.ip.addr==0){
  365. lua_pushnil(L);
  366. return 1;
  367. } else {
  368. c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.ip) );
  369. lua_pushstring( L, temp );
  370. c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.netmask) );
  371. lua_pushstring( L, temp );
  372. c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.gw) );
  373. lua_pushstring( L, temp );
  374. return 3;
  375. }
  376. }
  377. // Lua: broadcast = wifi.xx.getbroadcast()
  378. static int wifi_getbroadcast( lua_State* L, uint8_t mode )
  379. {
  380. struct ip_info pTempIp;
  381. char temp[64];
  382. wifi_get_ip_info(mode, &pTempIp);
  383. if(pTempIp.ip.addr==0){
  384. lua_pushnil(L);
  385. return 1;
  386. } else {
  387. struct ip_addr broadcast_address;
  388. uint32 subnet_mask32 = pTempIp.netmask.addr & pTempIp.ip.addr;
  389. uint32 broadcast_address32 = ~pTempIp.netmask.addr | subnet_mask32;
  390. broadcast_address.addr = broadcast_address32;
  391. c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&broadcast_address) );
  392. lua_pushstring( L, temp );
  393. return 1;
  394. }
  395. }
  396. static uint32_t parse_key(lua_State* L, const char * key){
  397. lua_getfield(L, 1, key);
  398. if( lua_isstring(L, -1) ) // deal with the ip/netmask/gw string
  399. {
  400. const char *ip = luaL_checkstring( L, -1 );
  401. return ipaddr_addr(ip);
  402. }
  403. lua_pop(L, 1);
  404. return 0;
  405. }
  406. // Lua: ip = wifi.xx.setip()
  407. static int wifi_setip( lua_State* L, uint8_t mode )
  408. {
  409. struct ip_info pTempIp;
  410. wifi_get_ip_info(mode, &pTempIp);
  411. if (!lua_istable(L, 1))
  412. return luaL_error( L, "wrong arg type" );
  413. uint32_t ip = parse_key(L, "ip");
  414. if(ip!=0)
  415. pTempIp.ip.addr = ip;
  416. ip = parse_key(L, "netmask");
  417. if(ip!=0)
  418. pTempIp.netmask.addr = ip;
  419. ip = parse_key(L, "gateway");
  420. if(mode==SOFTAP_IF || ip!=0)
  421. pTempIp.gw.addr = ip;
  422. if(STATION_IF == mode)
  423. {
  424. wifi_station_dhcpc_stop();
  425. lua_pushboolean(L,wifi_set_ip_info(mode, &pTempIp));
  426. }
  427. else
  428. {
  429. wifi_softap_dhcps_stop();
  430. lua_pushboolean(L,wifi_set_ip_info(mode, &pTempIp));
  431. wifi_softap_dhcps_start();
  432. }
  433. return 1;
  434. }
  435. // Lua: realtype = sleeptype(type)
  436. static int wifi_sleeptype( lua_State* L )
  437. {
  438. unsigned type;
  439. if ( lua_isnumber(L, 1) ){
  440. type = lua_tointeger(L, 1);
  441. if ( type != NONE_SLEEP_T && type != LIGHT_SLEEP_T && type != MODEM_SLEEP_T )
  442. return luaL_error( L, "wrong arg type" );
  443. if(!wifi_set_sleep_type(type)){
  444. lua_pushnil(L);
  445. return 1;
  446. }
  447. }
  448. type = wifi_get_sleep_type();
  449. lua_pushinteger( L, type );
  450. return 1;
  451. }
  452. // Lua: wifi.sta.getmac()
  453. static int wifi_station_getmac( lua_State* L ){
  454. return wifi_getmac(L, STATION_IF);
  455. }
  456. // Lua: wifi.sta.setmac()
  457. static int wifi_station_setmac( lua_State* L ){
  458. return wifi_setmac(L, STATION_IF);
  459. }
  460. // Lua: wifi.sta.getip()
  461. static int wifi_station_getip( lua_State* L ){
  462. return wifi_getip(L, STATION_IF);
  463. }
  464. // Lua: wifi.sta.setip()
  465. static int wifi_station_setip( lua_State* L ){
  466. return wifi_setip(L, STATION_IF);
  467. }
  468. // Lua: wifi.sta.getbroadcast()
  469. static int wifi_station_getbroadcast( lua_State* L ){
  470. return wifi_getbroadcast(L, STATION_IF);
  471. }
  472. /**
  473. * wifi.sta.getconfig()
  474. * Description:
  475. * Get current Station configuration.
  476. * Note: if bssid_set==1 STATION is configured to connect to specified BSSID
  477. * if bssid_set==0 specified BSSID address is irrelevant.
  478. * Syntax:
  479. * ssid, pwd, bssid_set, bssid=wifi.sta.getconfig()
  480. * Parameters:
  481. * none
  482. * Returns:
  483. * SSID, Password, BSSID_set, BSSID
  484. */
  485. static int wifi_station_getconfig( lua_State* L )
  486. {
  487. struct station_config sta_conf;
  488. char bssid[17];
  489. wifi_station_get_config(&sta_conf);
  490. if(sta_conf.ssid==0)
  491. {
  492. lua_pushnil(L);
  493. return 1;
  494. }
  495. else
  496. {
  497. lua_pushstring( L, sta_conf.ssid );
  498. lua_pushstring( L, sta_conf.password );
  499. lua_pushinteger( L, sta_conf.bssid_set);
  500. c_sprintf(bssid, MACSTR, MAC2STR(sta_conf.bssid));
  501. lua_pushstring( L, bssid);
  502. return 4;
  503. }
  504. }
  505. /**
  506. * wifi.sta.config()
  507. * Description:
  508. * Set current Station configuration.
  509. * Note: If there are multiple APs with the same ssid, you can connect to a specific one by entering it's MAC address into the "bssid" field.
  510. * Syntax:
  511. * wifi.sta.getconfig(ssid, password) --Set STATION configuration, Auto-connect by default, Connects to any BSSID
  512. * wifi.sta.getconfig(ssid, password, Auto_connect) --Set STATION configuration, Auto-connect(0 or 1), Connects to any BSSID
  513. * wifi.sta.getconfig(ssid, password, bssid) --Set STATION configuration, Auto-connect by default, Connects to specific BSSID
  514. * wifi.sta.getconfig(ssid, password, Auto_connect, bssid) --Set STATION configuration, Auto-connect(0 or 1), Connects to specific BSSID
  515. * Parameters:
  516. * ssid: string which is less than 32 bytes.
  517. * Password: string which is less than 64 bytes.
  518. * Auto_connect: 0 (disable Auto-connect) or 1 (to enable Auto-connect).
  519. * bssid: MAC address of Access Point you would like to connect to.
  520. * Returns:
  521. * Nothing.
  522. *
  523. * Example:
  524. --Connect to Access Point automatically when in range
  525. wifi.sta.getconfig("myssid", "password")
  526. --Connect to Access Point, User decides when to connect/disconnect to/from AP
  527. wifi.sta.getconfig("myssid", "mypassword", 0)
  528. wifi.sta.connect()
  529. --do some wifi stuff
  530. wifi.sta.disconnect()
  531. --Connect to specific Access Point automatically when in range
  532. wifi.sta.getconfig("myssid", "mypassword", "12:34:56:78:90:12")
  533. --Connect to specific Access Point, User decides when to connect/disconnect to/from AP
  534. wifi.sta.getconfig("myssid", "mypassword", 0)
  535. wifi.sta.connect()
  536. --do some wifi stuff
  537. wifi.sta.disconnect()
  538. *
  539. */
  540. static int wifi_station_config( lua_State* L )
  541. {
  542. size_t sl, pl, ml;
  543. struct station_config sta_conf;
  544. int auto_connect=0;
  545. const char *ssid = luaL_checklstring( L, 1, &sl );
  546. if (sl>32 || ssid == NULL)
  547. return luaL_error( L, "ssid:<32" );
  548. const char *password = luaL_checklstring( L, 2, &pl );
  549. if ((pl!=0 && (pl<8 || pl>64)) || password == NULL)
  550. return luaL_error( L, "pwd:0,8~64" );
  551. if(lua_isnumber(L, 3))
  552. {
  553. auto_connect=luaL_checkinteger( L, 3 );;
  554. if ( auto_connect != 0 && auto_connect != 1)
  555. return luaL_error( L, "wrong arg type" );
  556. }
  557. else if (lua_isstring(L, 3)&& !(lua_isnumber(L, 3)))
  558. {
  559. lua_pushnil(L);
  560. lua_insert(L, 3);
  561. auto_connect=1;
  562. }
  563. else
  564. {
  565. if(lua_isnil(L, 3))
  566. return luaL_error( L, "wrong arg type" );
  567. auto_connect=1;
  568. }
  569. if(lua_isnumber(L, 4))
  570. {
  571. sta_conf.bssid_set = 0;
  572. c_memset(sta_conf.bssid, 0, 6);
  573. }
  574. else
  575. {
  576. if (lua_isstring(L, 4))
  577. {
  578. const char *macaddr = luaL_checklstring( L, 4, &ml );
  579. luaL_argcheck(L, ml==17, 1, INVALID_MAC_STR);
  580. c_memset(sta_conf.bssid, 0, 6);
  581. ets_str2macaddr(sta_conf.bssid, macaddr);
  582. sta_conf.bssid_set = 1;
  583. }
  584. else
  585. {
  586. sta_conf.bssid_set = 0;
  587. c_memset(sta_conf.bssid, 0, 6);
  588. }
  589. }
  590. c_memset(sta_conf.ssid, 0, 32);
  591. c_memset(sta_conf.password, 0, 64);
  592. c_memcpy(sta_conf.ssid, ssid, sl);
  593. c_memcpy(sta_conf.password, password, pl);
  594. NODE_DBG(sta_conf.ssid);
  595. NODE_DBG(" %d\n", sl);
  596. NODE_DBG(sta_conf.password);
  597. NODE_DBG(" %d\n", pl);
  598. NODE_DBG(" %d\n", sta_conf.bssid_set);
  599. NODE_DBG( MACSTR, MAC2STR(sta_conf.bssid));
  600. NODE_DBG("\n");
  601. wifi_station_disconnect();
  602. wifi_station_set_config(&sta_conf);
  603. if(auto_connect==0)
  604. {
  605. wifi_station_set_auto_connect(false);
  606. }
  607. else
  608. {
  609. wifi_station_set_auto_connect(true);
  610. wifi_station_connect();
  611. }
  612. // station_check_connect(0);
  613. return 0;
  614. }
  615. // Lua: wifi.sta.connect()
  616. static int wifi_station_connect4lua( lua_State* L )
  617. {
  618. wifi_station_connect();
  619. return 0;
  620. }
  621. // Lua: wifi.sta.disconnect()
  622. static int wifi_station_disconnect4lua( lua_State* L )
  623. {
  624. wifi_station_disconnect();
  625. return 0;
  626. }
  627. // Lua: wifi.sta.auto(true/false)
  628. static int wifi_station_setauto( lua_State* L )
  629. {
  630. unsigned a;
  631. a = luaL_checkinteger( L, 1 );
  632. luaL_argcheck(L, ( a == 0 || a == 1 ), 1, "0 or 1");
  633. wifi_station_set_auto_connect(a);
  634. return 0;
  635. }
  636. /**
  637. * wifi.sta.listap()
  638. * Description:
  639. * scan and get ap list as a lua table into callback function.
  640. * Syntax:
  641. * wifi.sta.getap(function(table))
  642. * wifi.sta.getap(format, function(table))
  643. * wifi.sta.getap(cfg, function(table))
  644. * wifi.sta.getap(cfg, format, function(table))
  645. * Parameters:
  646. * cfg: table that contains scan configuration
  647. * Format:Select output table format.
  648. * 0 for the old format (SSID : Authmode, RSSI, BSSID, Channel) (Default)
  649. * 1 for the new format (BSSID : SSID, RSSI, Authmode, Channel)
  650. * function(table): a callback function to receive ap table when scan is done
  651. this function receive a table, the key is the ssid,
  652. value is other info in format: authmode,rssi,bssid,channel
  653. * Returns:
  654. * nil
  655. *
  656. * Example:
  657. --original function left intact to preserve backward compatibility
  658. wifi.sta.getap(function(T) for k,v in pairs(T) do print(k..":"..v) end end)
  659. --if no scan configuration is desired cfg can be set to nil or previous example can be used
  660. wifi.sta.getap(nil, function(T) for k,v in pairs(T) do print(k..":"..v) end end)
  661. --scan configuration
  662. scan_cfg={}
  663. scan_cfg.ssid="myssid" --if set to nil, ssid is not filtered
  664. scan_cfg.bssid="AA:AA:AA:AA:AA:AA" --if set to nil, MAC address is not filtered
  665. scan_cfg.channel=0 --if set to nil, channel will default to 0(scans all channels), if set scan will be faster
  666. scan_cfg.show_hidden=1 --if set to nil, show_hidden will default to 0
  667. wifi.sta.getap(scan_cfg, function(T) for k,v in pairs(T) do print(k..":"..v) end end)
  668. */
  669. static int wifi_station_listap( lua_State* L )
  670. {
  671. if(wifi_get_opmode() == SOFTAP_MODE)
  672. {
  673. return luaL_error( L, "Can't list ap in SOFTAP mode" );
  674. }
  675. struct scan_config scan_cfg;
  676. getap_output_format=0;
  677. if (lua_type(L, 1)==LUA_TTABLE)
  678. {
  679. char ssid[32];
  680. char bssid[6];
  681. uint8 channel=0;
  682. uint8 show_hidden=0;
  683. size_t len;
  684. lua_getfield(L, 1, "ssid");
  685. if (!lua_isnil(L, -1)){ /* found? */
  686. if( lua_isstring(L, -1) ) // deal with the ssid string
  687. {
  688. const char *ssidstr = luaL_checklstring( L, -1, &len );
  689. if(len>32)
  690. return luaL_error( L, "ssid:<32" );
  691. c_memset(ssid, 0, 32);
  692. c_memcpy(ssid, ssidstr, len);
  693. scan_cfg.ssid=ssid;
  694. NODE_DBG(scan_cfg.ssid);
  695. NODE_DBG("\n");
  696. }
  697. else
  698. return luaL_error( L, "wrong arg type" );
  699. }
  700. else
  701. scan_cfg.ssid=NULL;
  702. lua_getfield(L, 1, "bssid");
  703. if (!lua_isnil(L, -1)){ /* found? */
  704. if( lua_isstring(L, -1) ) // deal with the ssid string
  705. {
  706. const char *macaddr = luaL_checklstring( L, -1, &len );
  707. luaL_argcheck(L, len==17, 1, INVALID_MAC_STR);
  708. c_memset(bssid, 0, 6);
  709. ets_str2macaddr(bssid, macaddr);
  710. scan_cfg.bssid=bssid;
  711. NODE_DBG(MACSTR, MAC2STR(scan_cfg.bssid));
  712. NODE_DBG("\n");
  713. }
  714. else
  715. return luaL_error( L, "wrong arg type" );
  716. }
  717. else
  718. scan_cfg.bssid=NULL;
  719. lua_getfield(L, 1, "channel");
  720. if (!lua_isnil(L, -1)){ /* found? */
  721. if( lua_isnumber(L, -1) ) // deal with the ssid string
  722. {
  723. channel = luaL_checknumber( L, -1);
  724. if(!(channel>=0 && channel<=13))
  725. return luaL_error( L, "channel: 0 or 1-13" );
  726. scan_cfg.channel=channel;
  727. NODE_DBG("%d\n", scan_cfg.channel);
  728. }
  729. else
  730. return luaL_error( L, "wrong arg type" );
  731. }
  732. else
  733. scan_cfg.channel=0;
  734. lua_getfield(L, 1, "show_hidden");
  735. if (!lua_isnil(L, -1)){ /* found? */
  736. if( lua_isnumber(L, -1) ) // deal with the ssid string
  737. {
  738. show_hidden = luaL_checknumber( L, -1);
  739. if(show_hidden!=0 && show_hidden!=1)
  740. return luaL_error( L, "show_hidden: 0 or 1" );
  741. scan_cfg.show_hidden=show_hidden;
  742. NODE_DBG("%d\n", scan_cfg.show_hidden);
  743. }
  744. else
  745. return luaL_error( L, "wrong arg type" );
  746. }
  747. else
  748. scan_cfg.show_hidden=0;
  749. if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION)
  750. {
  751. lua_pushnil(L);
  752. lua_insert(L, 2);
  753. }
  754. lua_pop(L, -4);
  755. }
  756. else if (lua_type(L, 1) == LUA_TNUMBER)
  757. {
  758. lua_pushnil(L);
  759. lua_insert(L, 1);
  760. }
  761. else if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION)
  762. {
  763. lua_pushnil(L);
  764. lua_insert(L, 1);
  765. lua_pushnil(L);
  766. lua_insert(L, 1);
  767. }
  768. else if(lua_isnil(L, 1))
  769. {
  770. if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION)
  771. {
  772. lua_pushnil(L);
  773. lua_insert(L, 2);
  774. }
  775. }
  776. else
  777. {
  778. return luaL_error( L, "wrong arg type" );
  779. }
  780. if (lua_type(L, 2) == LUA_TNUMBER) //this section changes the output format
  781. {
  782. getap_output_format=luaL_checkinteger( L, 2 );
  783. if ( getap_output_format != 0 && getap_output_format != 1)
  784. return luaL_error( L, "wrong arg type" );
  785. }
  786. NODE_DBG("Use alternate output format: %d\n", getap_output_format);
  787. if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION)
  788. {
  789. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  790. register_lua_cb(L, &wifi_scan_succeed);
  791. if (lua_type(L, 1)==LUA_TTABLE)
  792. {
  793. wifi_station_scan(&scan_cfg,wifi_scan_done);
  794. }
  795. else
  796. {
  797. wifi_station_scan(NULL,wifi_scan_done);
  798. }
  799. }
  800. else
  801. {
  802. unregister_lua_cb(L, &wifi_scan_succeed);
  803. }
  804. }
  805. // Lua: wifi.sta.gethostname()
  806. static int wifi_sta_gethostname( lua_State* L )
  807. {
  808. char* hostname = wifi_station_get_hostname();
  809. lua_pushstring(L, hostname);
  810. return 1;
  811. }
  812. static bool wifi_sta_sethostname(const char *hostname, size_t len)
  813. {
  814. //this function follows RFC 952 & RFC 1123 host name standards.
  815. //the hostname must be 32 chars or less and first and last char must be alphanumeric
  816. if (!isalnum(hostname[0]) || !isalnum(hostname[len-1]) || len > 32)
  817. {
  818. return false;
  819. }
  820. for (int i=1; i<len; i++)
  821. {
  822. //characters in the middle of the host name can be alphanumeric or a hyphen(-) only
  823. if (!(isalnum(hostname[i]) || hostname[i]=='-'))
  824. {
  825. return false;
  826. }
  827. }
  828. return wifi_station_set_hostname((char*)hostname);
  829. }
  830. static int wifi_sta_sethostname_lua( lua_State* L )
  831. {
  832. size_t len;
  833. const char *hostname = luaL_checklstring(L, 1, &len);
  834. luaL_argcheck(L, wifi_sta_sethostname(hostname, len), 1, "Invalid hostname");
  835. return 0;
  836. }
  837. // Lua: wifi.sta.status()
  838. static int wifi_station_status( lua_State* L )
  839. {
  840. uint8_t status = wifi_station_get_connect_status();
  841. lua_pushinteger( L, status );
  842. return 1;
  843. }
  844. // Lua: wifi.sta.getrssi()
  845. static int wifi_station_getrssi( lua_State* L ){
  846. sint8 rssival=wifi_station_get_rssi();
  847. NODE_DBG("\n\tRSSI is %i\n", rssival);
  848. if (rssival<10)
  849. {
  850. lua_pushinteger(L, rssival);
  851. }
  852. else
  853. {
  854. lua_pushnil(L);
  855. }
  856. return 1;
  857. }
  858. //Lua: wifi.ap.deauth()
  859. static int wifi_ap_deauth( lua_State* L )
  860. {
  861. uint8_t mac[6];
  862. unsigned len = 0;
  863. if(lua_isstring(L, 1))
  864. {
  865. const char *macaddr = luaL_checklstring( L, 1, &len );
  866. luaL_argcheck(L, len==17, 1, INVALID_MAC_STR);
  867. ets_str2macaddr(mac, macaddr);
  868. }
  869. else
  870. {
  871. c_memset(&mac, 0xFF, sizeof(mac));
  872. }
  873. lua_pushboolean(L,wifi_softap_deauth(mac));
  874. return 1;
  875. }
  876. // Lua: wifi.ap.getmac()
  877. static int wifi_ap_getmac( lua_State* L ){
  878. return wifi_getmac(L, SOFTAP_IF);
  879. }
  880. // Lua: wifi.ap.setmac()
  881. static int wifi_ap_setmac( lua_State* L ){
  882. return wifi_setmac(L, SOFTAP_IF);
  883. }
  884. // Lua: wifi.ap.getip()
  885. static int wifi_ap_getip( lua_State* L ){
  886. return wifi_getip(L, SOFTAP_IF);
  887. }
  888. // Lua: wifi.ap.setip()
  889. static int wifi_ap_setip( lua_State* L ){
  890. return wifi_setip(L, SOFTAP_IF);
  891. }
  892. // Lua: wifi.ap.getbroadcast()
  893. static int wifi_ap_getbroadcast( lua_State* L ){
  894. return wifi_getbroadcast(L, SOFTAP_IF);
  895. }
  896. // Lua: wifi.ap.getconfig()
  897. static int wifi_ap_getconfig( lua_State* L )
  898. {
  899. struct softap_config config;
  900. wifi_softap_get_config(&config);
  901. lua_pushstring( L, config.ssid );
  902. if(config.authmode == AUTH_OPEN)
  903. lua_pushnil(L);
  904. else
  905. lua_pushstring( L, config.password );
  906. return 2;
  907. }
  908. // Lua: wifi.ap.config(table)
  909. static int wifi_ap_config( lua_State* L )
  910. {
  911. if (!lua_istable(L, 1))
  912. return luaL_error( L, "wrong arg type" );
  913. struct softap_config config;
  914. wifi_softap_get_config(&config);
  915. size_t len;
  916. lua_getfield(L, 1, "ssid");
  917. if (!lua_isnil(L, -1)){ /* found? */
  918. if( lua_isstring(L, -1) ) // deal with the ssid string
  919. {
  920. const char *ssid = luaL_checklstring( L, -1, &len );
  921. if(len<1 || len>32 || ssid == NULL)
  922. return luaL_error( L, "ssid:1~32" );
  923. c_memset(config.ssid, 0, 32);
  924. c_memcpy(config.ssid, ssid, len);
  925. NODE_DBG(config.ssid);
  926. NODE_DBG("\n");
  927. config.ssid_len = len;
  928. config.ssid_hidden = 0;
  929. }
  930. else
  931. return luaL_error( L, "wrong arg type" );
  932. }
  933. else
  934. return luaL_error( L, "ssid required" );
  935. lua_getfield(L, 1, "pwd");
  936. if (!lua_isnil(L, -1)){ /* found? */
  937. if( lua_isstring(L, -1) ) // deal with the password string
  938. {
  939. const char *pwd = luaL_checklstring( L, -1, &len );
  940. if(len<8 || len>64 || pwd == NULL)
  941. return luaL_error( L, "pwd:8~64" );
  942. c_memset(config.password, 0, 64);
  943. c_memcpy(config.password, pwd, len);
  944. NODE_DBG(config.password);
  945. NODE_DBG("\n");
  946. config.authmode = AUTH_WPA_WPA2_PSK;
  947. }
  948. else
  949. return luaL_error( L, "wrong arg type" );
  950. }
  951. else{
  952. config.authmode = AUTH_OPEN;
  953. }
  954. lua_getfield(L, 1, "auth");
  955. if (!lua_isnil(L, -1))
  956. {
  957. config.authmode = (uint8_t)luaL_checkinteger(L, -1);
  958. NODE_DBG("%d\n", config.authmode);
  959. }
  960. else
  961. {
  962. // keep whatever value resulted from "pwd" logic above
  963. }
  964. lua_getfield(L, 1, "channel");
  965. if (!lua_isnil(L, -1))
  966. {
  967. unsigned channel = luaL_checkinteger(L, -1);
  968. if (channel < 1 || channel > 13)
  969. return luaL_error( L, "channel:1~13" );
  970. config.channel = (uint8_t)channel;
  971. NODE_DBG("%d\n", config.channel);
  972. }
  973. else
  974. {
  975. config.channel = 6;
  976. }
  977. lua_getfield(L, 1, "hidden");
  978. if (!lua_isnil(L, -1))
  979. {
  980. config.ssid_hidden = (uint8_t)luaL_checkinteger(L, -1);
  981. NODE_DBG("%d\n", config.ssid_hidden);
  982. NODE_DBG("\n");
  983. }
  984. else
  985. {
  986. config.ssid_hidden = 0;
  987. }
  988. lua_getfield(L, 1, "max");
  989. if (!lua_isnil(L, -1))
  990. {
  991. unsigned max = luaL_checkinteger(L, -1);
  992. if (max < 1 || max > 4)
  993. return luaL_error( L, "max:1~4" );
  994. config.max_connection = (uint8_t)max;
  995. NODE_DBG("%d\n", config.max_connection);
  996. }
  997. else
  998. {
  999. config.max_connection = 4;
  1000. }
  1001. lua_getfield(L, 1, "beacon");
  1002. if (!lua_isnil(L, -1))
  1003. {
  1004. unsigned beacon = luaL_checkinteger(L, -1);
  1005. if (beacon < 100 || beacon > 60000)
  1006. return luaL_error( L, "beacon:100~60000" );
  1007. config.beacon_interval = (uint16_t)beacon;
  1008. NODE_DBG("%d\n", config.beacon_interval);
  1009. }
  1010. else
  1011. {
  1012. config.beacon_interval = 100;
  1013. }
  1014. wifi_softap_set_config(&config);
  1015. // system_restart();
  1016. return 0;
  1017. }
  1018. // Lua: table = wifi.ap.getclient()
  1019. static int wifi_ap_listclient( lua_State* L )
  1020. {
  1021. if (wifi_get_opmode() == STATION_MODE)
  1022. {
  1023. return luaL_error( L, "Can't list client in STATION_MODE mode" );
  1024. }
  1025. char temp[64];
  1026. lua_newtable(L);
  1027. struct station_info * station = wifi_softap_get_station_info();
  1028. struct station_info * next_station;
  1029. while (station != NULL)
  1030. {
  1031. c_sprintf(temp, MACSTR, MAC2STR(station->bssid));
  1032. wifi_add_sprintf_field(L, temp, IPSTR, IP2STR(&station->ip));
  1033. next_station = STAILQ_NEXT(station, next);
  1034. c_free(station);
  1035. station = next_station;
  1036. }
  1037. return 1;
  1038. }
  1039. // Lua: ip = wifi.ap.dhcp.config()
  1040. static int wifi_ap_dhcp_config( lua_State* L )
  1041. {
  1042. if (!lua_istable(L, 1))
  1043. return luaL_error( L, "wrong arg type" );
  1044. struct dhcps_lease lease;
  1045. uint32_t ip;
  1046. ip = parse_key(L, "start");
  1047. if (ip == 0)
  1048. return luaL_error( L, "wrong arg type" );
  1049. lease.start_ip.addr = ip;
  1050. NODE_DBG(IPSTR, IP2STR(&lease.start_ip));
  1051. NODE_DBG("\n");
  1052. // use configured max_connection to determine end
  1053. struct softap_config config;
  1054. wifi_softap_get_config(&config);
  1055. lease.end_ip = lease.start_ip;
  1056. ip4_addr4(&lease.end_ip) += config.max_connection - 1;
  1057. char temp[64];
  1058. c_sprintf(temp, IPSTR, IP2STR(&lease.start_ip));
  1059. lua_pushstring(L, temp);
  1060. c_sprintf(temp, IPSTR, IP2STR(&lease.end_ip));
  1061. lua_pushstring(L, temp);
  1062. // note: DHCP max range = 101 from start_ip to end_ip
  1063. wifi_softap_dhcps_stop();
  1064. wifi_softap_set_dhcps_lease(&lease);
  1065. wifi_softap_dhcps_start();
  1066. return 2;
  1067. }
  1068. // Lua: wifi.ap.dhcp.start()
  1069. static int wifi_ap_dhcp_start( lua_State* L )
  1070. {
  1071. lua_pushboolean(L, wifi_softap_dhcps_start());
  1072. return 1;
  1073. }
  1074. // Lua: wifi.ap.dhcp.stop()
  1075. static int wifi_ap_dhcp_stop( lua_State* L )
  1076. {
  1077. lua_pushboolean(L, wifi_softap_dhcps_stop());
  1078. return 1;
  1079. }
  1080. // Module function map
  1081. static const LUA_REG_TYPE wifi_station_map[] = {
  1082. { LSTRKEY( "getconfig" ), LFUNCVAL( wifi_station_getconfig ) },
  1083. { LSTRKEY( "config" ), LFUNCVAL( wifi_station_config ) },
  1084. { LSTRKEY( "connect" ), LFUNCVAL( wifi_station_connect4lua ) },
  1085. { LSTRKEY( "disconnect" ), LFUNCVAL( wifi_station_disconnect4lua ) },
  1086. { LSTRKEY( "autoconnect" ), LFUNCVAL( wifi_station_setauto ) },
  1087. { LSTRKEY( "getip" ), LFUNCVAL( wifi_station_getip ) },
  1088. { LSTRKEY( "setip" ), LFUNCVAL( wifi_station_setip ) },
  1089. { LSTRKEY( "getbroadcast" ), LFUNCVAL( wifi_station_getbroadcast) },
  1090. { LSTRKEY( "getmac" ), LFUNCVAL( wifi_station_getmac ) },
  1091. { LSTRKEY( "setmac" ), LFUNCVAL( wifi_station_setmac ) },
  1092. { LSTRKEY( "getap" ), LFUNCVAL( wifi_station_listap ) },
  1093. { LSTRKEY( "sethostname" ), LFUNCVAL( wifi_sta_sethostname_lua ) },
  1094. { LSTRKEY( "gethostname" ), LFUNCVAL( wifi_sta_gethostname ) },
  1095. { LSTRKEY( "getrssi" ), LFUNCVAL( wifi_station_getrssi ) },
  1096. { LSTRKEY( "status" ), LFUNCVAL( wifi_station_status ) },
  1097. #if defined(WIFI_STATION_STATUS_MONITOR_ENABLE)
  1098. { LSTRKEY( "eventMonReg" ), LFUNCVAL( wifi_station_event_mon_reg ) }, //declared in wifi_eventmon.c
  1099. { LSTRKEY( "eventMonStart" ), LFUNCVAL( wifi_station_event_mon_start ) }, //declared in wifi_eventmon.c
  1100. { LSTRKEY( "eventMonStop" ), LFUNCVAL( wifi_station_event_mon_stop ) }, //declared in wifi_eventmon.c
  1101. #endif
  1102. { LNILKEY, LNILVAL }
  1103. };
  1104. static const LUA_REG_TYPE wifi_ap_dhcp_map[] = {
  1105. { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_dhcp_config ) },
  1106. { LSTRKEY( "start" ), LFUNCVAL( wifi_ap_dhcp_start ) },
  1107. { LSTRKEY( "stop" ), LFUNCVAL( wifi_ap_dhcp_stop ) },
  1108. { LNILKEY, LNILVAL }
  1109. };
  1110. static const LUA_REG_TYPE wifi_ap_map[] = {
  1111. { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_config ) },
  1112. { LSTRKEY( "deauth" ), LFUNCVAL( wifi_ap_deauth ) },
  1113. { LSTRKEY( "getip" ), LFUNCVAL( wifi_ap_getip ) },
  1114. { LSTRKEY( "setip" ), LFUNCVAL( wifi_ap_setip ) },
  1115. { LSTRKEY( "getbroadcast" ), LFUNCVAL( wifi_ap_getbroadcast) },
  1116. { LSTRKEY( "getmac" ), LFUNCVAL( wifi_ap_getmac ) },
  1117. { LSTRKEY( "setmac" ), LFUNCVAL( wifi_ap_setmac ) },
  1118. { LSTRKEY( "getclient" ), LFUNCVAL( wifi_ap_listclient ) },
  1119. { LSTRKEY( "getconfig" ), LFUNCVAL( wifi_ap_getconfig ) },
  1120. { LSTRKEY( "dhcp" ), LROVAL( wifi_ap_dhcp_map ) },
  1121. //{ LSTRKEY( "__metatable" ), LROVAL( wifi_ap_map ) },
  1122. { LNILKEY, LNILVAL }
  1123. };
  1124. static const LUA_REG_TYPE wifi_map[] = {
  1125. { LSTRKEY( "setmode" ), LFUNCVAL( wifi_setmode ) },
  1126. { LSTRKEY( "getmode" ), LFUNCVAL( wifi_getmode ) },
  1127. { LSTRKEY( "getchannel" ), LFUNCVAL( wifi_getchannel ) },
  1128. { LSTRKEY( "setphymode" ), LFUNCVAL( wifi_setphymode ) },
  1129. { LSTRKEY( "getphymode" ), LFUNCVAL( wifi_getphymode ) },
  1130. { LSTRKEY( "sleep" ), LFUNCVAL( wifi_sleep ) },
  1131. { LSTRKEY( "nullmodesleep" ), LFUNCVAL( wifi_null_mode_auto_sleep ) },
  1132. #ifdef WIFI_SMART_ENABLE
  1133. { LSTRKEY( "startsmart" ), LFUNCVAL( wifi_start_smart ) },
  1134. { LSTRKEY( "stopsmart" ), LFUNCVAL( wifi_exit_smart ) },
  1135. #endif
  1136. { LSTRKEY( "sleeptype" ), LFUNCVAL( wifi_sleeptype ) },
  1137. { LSTRKEY( "sta" ), LROVAL( wifi_station_map ) },
  1138. { LSTRKEY( "ap" ), LROVAL( wifi_ap_map ) },
  1139. #if defined(WIFI_SDK_EVENT_MONITOR_ENABLE)
  1140. { LSTRKEY( "eventmon" ), LROVAL( wifi_event_monitor_map ) }, //declared in wifi_eventmon.c
  1141. #endif
  1142. { LSTRKEY( "NULLMODE" ), LNUMVAL( NULL_MODE ) },
  1143. { LSTRKEY( "STATION" ), LNUMVAL( STATION_MODE ) },
  1144. { LSTRKEY( "SOFTAP" ), LNUMVAL( SOFTAP_MODE ) },
  1145. { LSTRKEY( "STATIONAP" ), LNUMVAL( STATIONAP_MODE ) },
  1146. { LSTRKEY( "PHYMODE_B" ), LNUMVAL( PHY_MODE_11B ) },
  1147. { LSTRKEY( "PHYMODE_G" ), LNUMVAL( PHY_MODE_11G ) },
  1148. { LSTRKEY( "PHYMODE_N" ), LNUMVAL( PHY_MODE_11N ) },
  1149. { LSTRKEY( "NONE_SLEEP" ), LNUMVAL( NONE_SLEEP_T ) },
  1150. { LSTRKEY( "LIGHT_SLEEP" ), LNUMVAL( LIGHT_SLEEP_T ) },
  1151. { LSTRKEY( "MODEM_SLEEP" ), LNUMVAL( MODEM_SLEEP_T ) },
  1152. { LSTRKEY( "OPEN" ), LNUMVAL( AUTH_OPEN ) },
  1153. //{ LSTRKEY( "WEP" ), LNUMVAL( AUTH_WEP ) },
  1154. { LSTRKEY( "WPA_PSK" ), LNUMVAL( AUTH_WPA_PSK ) },
  1155. { LSTRKEY( "WPA2_PSK" ), LNUMVAL( AUTH_WPA2_PSK ) },
  1156. { LSTRKEY( "WPA_WPA2_PSK" ), LNUMVAL( AUTH_WPA_WPA2_PSK ) },
  1157. { LSTRKEY( "STA_IDLE" ), LNUMVAL( STATION_IDLE ) },
  1158. { LSTRKEY( "STA_CONNECTING" ), LNUMVAL( STATION_CONNECTING ) },
  1159. { LSTRKEY( "STA_WRONGPWD" ), LNUMVAL( STATION_WRONG_PASSWORD ) },
  1160. { LSTRKEY( "STA_APNOTFOUND" ), LNUMVAL( STATION_NO_AP_FOUND ) },
  1161. { LSTRKEY( "STA_FAIL" ), LNUMVAL( STATION_CONNECT_FAIL ) },
  1162. { LSTRKEY( "STA_GOTIP" ), LNUMVAL( STATION_GOT_IP ) },
  1163. { LSTRKEY( "__metatable" ), LROVAL( wifi_map ) },
  1164. { LNILKEY, LNILVAL }
  1165. };
  1166. void wifi_change_default_host_name(void)
  1167. {
  1168. uint8 opmode_temp=wifi_get_opmode();
  1169. wifi_set_opmode_current(STATION_MODE);
  1170. #ifndef WIFI_STA_HOSTNAME
  1171. char temp[32];
  1172. uint8_t mac[6];
  1173. wifi_get_macaddr(STATION_IF, mac);
  1174. c_sprintf(temp, "NODE-%X%X%X", (mac)[3], (mac)[4], (mac)[5]);
  1175. wifi_sta_sethostname((const char*)temp, strlen(temp));
  1176. #elif defined(WIFI_STA_HOSTNAME) && !defined(WIFI_STA_HOSTNAME_APPEND_MAC)
  1177. if(!wifi_sta_sethostname(WIFI_STA_HOSTNAME, strlen(WIFI_STA_HOSTNAME)))
  1178. {
  1179. char temp[32];
  1180. uint8_t mac[6];
  1181. wifi_get_macaddr(STATION_IF, mac);
  1182. c_sprintf(temp, "NODE-%X%X%X", (mac)[3], (mac)[4], (mac)[5]);
  1183. wifi_sta_sethostname((const char*)temp, strlen(temp));
  1184. }
  1185. #elif defined(WIFI_STA_HOSTNAME) && defined(WIFI_STA_HOSTNAME_APPEND_MAC)
  1186. char temp[32];
  1187. uint8_t mac[6];
  1188. wifi_get_macaddr(STATION_IF, mac);
  1189. c_sprintf(temp, "%s%X%X%X", WIFI_STA_HOSTNAME, (mac)[3], (mac)[4], (mac)[5]);
  1190. if(!wifi_sta_sethostname(temp, strlen(temp)))
  1191. {
  1192. c_sprintf(temp, "NODE-%X%X%X", (mac)[3], (mac)[4], (mac)[5]);
  1193. wifi_sta_sethostname((const char*)temp, strlen(temp));
  1194. }
  1195. #endif
  1196. if(opmode_temp!=wifi_get_opmode())
  1197. {
  1198. wifi_set_opmode_current(opmode_temp);
  1199. }
  1200. }
  1201. int luaopen_wifi( lua_State *L )
  1202. {
  1203. wifi_fpm_auto_sleep_set_in_null_mode(1);
  1204. //if esp is already in NULL_MODE, auto sleep setting won't take effect until next wifi_set_opmode(NULL_MODE) call.
  1205. if(wifi_get_opmode()==NULL_MODE)
  1206. {
  1207. wifi_set_opmode_current(NULL_MODE);
  1208. }
  1209. #if defined(WIFI_SDK_EVENT_MONITOR_ENABLE)
  1210. wifi_eventmon_init();
  1211. #endif
  1212. return 0;
  1213. }
  1214. NODEMCU_MODULE(WIFI, "wifi", wifi_map, luaopen_wifi);