wifi.c 32 KB

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