wifi.c 35 KB

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