wifi.c 50 KB

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