luat_wlan_idf5.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #include "luat_base.h"
  2. #include "luat_msgbus.h"
  3. #include "luat_wlan.h"
  4. #include "luat_timer.h"
  5. #include "esp_attr.h"
  6. #include "esp_netif.h"
  7. #include "esp_event.h"
  8. #include "esp_system.h"
  9. #include "esp_wifi.h"
  10. #include "esp_wifi_types.h"
  11. #include "esp_smartconfig.h"
  12. #include "esp_mac.h"
  13. #include "esp_netif_net_stack.h"
  14. #define LUAT_LOG_TAG "wlan"
  15. #include "luat_log.h"
  16. #include "lwip/netif.h"
  17. #include "luat_network_adapter.h"
  18. #include "luat_timer.h"
  19. #include "net_lwip.h"
  20. #include "lwip/tcp.h"
  21. void net_lwip_set_link_state(uint8_t adapter_index, uint8_t updown);
  22. static uint8_t wlan_inited = 0;
  23. static uint8_t wlan_is_ready = 0;
  24. static smartconfig_event_got_ssid_pswd_t *sc_evt;
  25. static char sta_ip[32];
  26. // static char sta_gw[32];
  27. static char sta_connected_bssid[6];
  28. char luat_sta_hostname[32];
  29. static uint8_t smartconfig_state = 0; // 0 - idle, 1 - running
  30. static uint8_t auto_reconnection = 0;
  31. // static uint8_t ap_stack_inited = 0;
  32. static esp_netif_t* wifiAP;
  33. static esp_netif_t* wifiSTA;
  34. static int l_wlan_handler(lua_State *L, void* ptr) {
  35. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  36. int32_t event_id = msg->arg1;
  37. int32_t event_tp = msg->arg2;
  38. //esp_netif_ip_info_t ip_info;
  39. lua_getglobal(L, "sys_pub");
  40. if (event_tp == 0) {
  41. switch (event_id)
  42. {
  43. case WIFI_EVENT_WIFI_READY:
  44. LLOGD("wifi protocol is ready");
  45. lua_getglobal(L, "sys_pub");
  46. lua_pushstring(L, "WLAN_READY");
  47. lua_pushinteger(L, 1);
  48. //esp_netif_get_ip_info(ESP_IF_WIFI_STA,&ip_info);
  49. lua_call(L, 2, 0);
  50. break;
  51. case WIFI_EVENT_STA_CONNECTED:
  52. LLOGD("wifi connected!!!");
  53. lua_pushstring(L, "WLAN_STA_CONNECTED");
  54. lua_call(L, 1, 0);
  55. break;
  56. case WIFI_EVENT_STA_DISCONNECTED:
  57. LLOGD("wifi disconnected!!!");
  58. lua_pushstring(L, "WLAN_STA_DISCONNECTED");
  59. lua_call(L, 1, 0);
  60. lua_getglobal(L, "sys_pub");
  61. lua_pushstring(L, "WLAN_READY");
  62. lua_pushinteger(L, 0);
  63. lua_call(L, 2, 0);
  64. if (auto_reconnection && smartconfig_state == 0) {
  65. LLOGD("auto reconnecting ...");
  66. esp_wifi_connect();
  67. }
  68. break;
  69. case WIFI_EVENT_STA_START:
  70. LLOGD("wifi station start");
  71. break;
  72. case WIFI_EVENT_STA_STOP:
  73. LLOGD("wifi station stop");
  74. break;
  75. case WIFI_EVENT_SCAN_DONE:
  76. LLOGD("wifi scan done");
  77. lua_pushstring(L, "WLAN_SCAN_DONE");
  78. lua_call(L, 1, 0);
  79. break;
  80. case WIFI_EVENT_AP_START:
  81. LLOGD("wifi ap start");
  82. break;
  83. case WIFI_EVENT_AP_STOP:
  84. LLOGD("wifi ap stop");
  85. break;
  86. case WIFI_EVENT_AP_STACONNECTED :
  87. LLOGD("wifi ap sta connected");
  88. lua_pushstring(L, "WLAN_AP_CONNECTED");
  89. lua_call(L, 1, 0);
  90. break;
  91. case WIFI_EVENT_AP_STADISCONNECTED :
  92. LLOGD("wifi ap sta disconnected");
  93. lua_pushstring(L, "WLAN_AP_DISCONNECTED");
  94. lua_call(L, 1, 0);
  95. break;
  96. default:
  97. LLOGI("unkown event %d", event_id);
  98. break;
  99. }
  100. }
  101. else if (event_tp == 1) {
  102. if (event_id == IP_EVENT_STA_GOT_IP) {
  103. LLOGD("sta_ip %s", sta_ip);
  104. lua_pushstring(L, "IP_READY");
  105. lua_pushstring(L, sta_ip);
  106. lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  107. lua_call(L, 3, 0);
  108. }
  109. else if (event_id == IP_EVENT_AP_STAIPASSIGNED) {
  110. LLOGD("wifi ap sta connected");
  111. lua_pushstring(L, "WLAN_AP_STA_IP_READY");
  112. char tmp[32];
  113. esp_ip4_addr_t ip4 = {
  114. .addr = (uint32_t)ptr
  115. };
  116. sprintf(tmp, IPSTR, IP2STR(&ip4));
  117. lua_pushstring(L, tmp);
  118. lua_call(L, 2, 0);
  119. }
  120. }
  121. else if (event_tp == 2) {
  122. if (event_id == SC_EVENT_SCAN_DONE) {
  123. LLOGD("smartconfig Scan done");
  124. }
  125. else if (event_id == SC_EVENT_SCAN_DONE) {
  126. LLOGD("smartconfig Found channel");
  127. }
  128. else if (event_id == SC_EVENT_GOT_SSID_PSWD) {
  129. LLOGD("smartconfig Got ssid and password");
  130. smartconfig_event_got_ssid_pswd_t *evt = sc_evt;
  131. wifi_config_t wifi_config;
  132. uint8_t ssid[33] = { 0 };
  133. uint8_t password[65] = { 0 };
  134. // uint8_t rvd_data[33] = { 0 };
  135. bzero(&wifi_config, sizeof(wifi_config_t));
  136. memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
  137. memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
  138. wifi_config.sta.bssid_set = evt->bssid_set;
  139. if (wifi_config.sta.bssid_set == true) {
  140. memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
  141. }
  142. memcpy(ssid, evt->ssid, sizeof(evt->ssid));
  143. memcpy(password, evt->password, sizeof(evt->password));
  144. LLOGD("SSID [%s] PASSWORD [%s]", ssid, password);
  145. // ESP_LOGI(TAG, "SSID:%s", ssid);
  146. // ESP_LOGI(TAG, "PASSWORD:%s", password);
  147. // if (evt->type == SC_TYPE_ESPTOUCH_V2) {
  148. // ESP_ERROR_CHECK( esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)) );
  149. // ESP_LOGI(TAG, "RVD_DATA:");
  150. // for (int i=0; i<33; i++) {
  151. // printf("%02x ", rvd_data[i]);
  152. // }
  153. // printf("\n");
  154. // }
  155. esp_wifi_disconnect();
  156. esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
  157. esp_wifi_connect();
  158. lua_pushstring(L, "SC_RESULT");
  159. lua_pushstring(L, (const char*)ssid);
  160. lua_pushstring(L, (const char*)password);
  161. lua_call(L, 3, 0);
  162. }
  163. else if (event_id == SC_EVENT_SEND_ACK_DONE) {
  164. esp_smartconfig_stop();
  165. smartconfig_state = 0;
  166. }
  167. }
  168. return 0;
  169. }
  170. static void wifi_event_handler(void *arg, esp_event_base_t event_base,
  171. int32_t event_id, void *event_data) {
  172. rtos_msg_t msg = {0};
  173. msg.handler = l_wlan_handler;
  174. msg.arg1 = event_id;
  175. LLOGD("wifi event %d", event_id);
  176. switch (event_id) {
  177. case WIFI_EVENT_STA_DISCONNECTED: {
  178. wlan_is_ready = 0;
  179. #ifdef LUAT_USE_NETWORK
  180. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_STA, 0);
  181. #endif
  182. // wifi_event_sta_disconnected_t* sta = (wifi_event_sta_disconnected_t*)event_data;
  183. memset(sta_connected_bssid, 0, sizeof(sta_connected_bssid));
  184. break;
  185. }
  186. case WIFI_EVENT_STA_CONNECTED: {
  187. wifi_event_sta_connected_t *sta = (wifi_event_sta_connected_t*)event_data;
  188. memcpy(sta_connected_bssid, sta->bssid, 6);
  189. break;
  190. }
  191. case WIFI_EVENT_AP_START: {
  192. #ifdef LUAT_USE_NETWORK
  193. net_lwip_set_netif(esp_netif_get_netif_impl(wifiAP), NW_ADAPTER_INDEX_LWIP_WIFI_AP);
  194. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 1);
  195. #endif
  196. break;
  197. }
  198. case WIFI_EVENT_AP_STOP: {
  199. #ifdef LUAT_USE_NETWORK
  200. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 0);
  201. #endif
  202. break;
  203. }
  204. case WIFI_EVENT_STA_BEACON_TIMEOUT : {
  205. LLOGD("wifi sta beacon timeout");
  206. return;
  207. }
  208. }
  209. luat_msgbus_put(&msg, 0);
  210. }
  211. static void ip_event_handler(void *arg, esp_event_base_t event_base,
  212. int32_t event_id, void *event_data) {
  213. rtos_msg_t msg = {0};
  214. msg.handler = l_wlan_handler;
  215. msg.arg1 = event_id;
  216. msg.arg2 = 1;
  217. ip_event_got_ip_t *event;
  218. ip_event_ap_staipassigned_t* ap_sta_event;
  219. LLOGD("ip event %d", event_id);
  220. if (event_id == IP_EVENT_STA_GOT_IP) {
  221. wlan_is_ready = 1;
  222. event = (ip_event_got_ip_t*)event_data;
  223. sprintf(sta_ip, IPSTR, IP2STR(&event->ip_info.ip));
  224. // sprintf(sta_gw, IPSTR, IP2STR(&event->ip_info.gw));
  225. #ifdef LUAT_USE_NETWORK
  226. net_lwip_set_netif(netif_get_by_index(2), NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  227. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_STA, 1);
  228. #endif
  229. }
  230. else if (event_id == IP_EVENT_AP_STAIPASSIGNED) {
  231. ap_sta_event = (ip_event_ap_staipassigned_t*)event_data;
  232. char tmp[32];
  233. sprintf(tmp, IPSTR, IP2STR(&ap_sta_event->ip));
  234. LLOGD("ap sta ip %s", tmp);
  235. msg.ptr = (void*)ap_sta_event->ip.addr;
  236. }
  237. luat_msgbus_put(&msg, 0);
  238. // LLOGD("ip_event_handler is done");
  239. }
  240. static void sc_event_handler(void *arg, esp_event_base_t event_base,
  241. int32_t event_id, void *event_data) {
  242. rtos_msg_t msg = {0};
  243. msg.handler = l_wlan_handler;
  244. msg.arg1 = event_id;
  245. msg.arg2 = 2;
  246. if (event_id == SC_EVENT_GOT_SSID_PSWD && sc_evt != NULL) {
  247. memcpy(sc_evt, event_data, sizeof(smartconfig_event_got_ssid_pswd_t));
  248. }
  249. LLOGD("sc event %d", event_id);
  250. luat_msgbus_put(&msg, 0);
  251. }
  252. extern void luat_ntp_autosync(void);
  253. int luat_wlan_init(luat_wlan_config_t *conf) {
  254. int ret = 0;
  255. if (wlan_inited == 0) {
  256. esp_netif_init();
  257. esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL);
  258. esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL);
  259. esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &sc_event_handler, NULL);
  260. wifiSTA = esp_netif_create_default_wifi_sta();
  261. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  262. cfg.static_rx_buf_num = 2;
  263. cfg.static_tx_buf_num = 2;
  264. ret = esp_wifi_init(&cfg);
  265. if (ret)
  266. LLOGD("esp_wifi_init ret %d", ret);
  267. esp_wifi_set_mode(WIFI_MODE_STA);
  268. esp_netif_set_hostname(wifiSTA, luat_wlan_get_hostname(0));
  269. #ifdef LUAT_USE_NETWORK
  270. net_lwip_register_adapter(NW_ADAPTER_INDEX_LWIP_WIFI_AP);
  271. net_lwip_register_adapter(NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  272. #endif
  273. if (ret)
  274. LLOGD("esp_netif_set_hostname ret %d", ret);
  275. }
  276. #ifdef LUAT_USE_NIMBLE
  277. #if CONFIG_BT_ENABLED
  278. //esp_wifi_set_ps(WIFI_PS_NONE);
  279. #endif
  280. #endif
  281. ret = esp_wifi_start();
  282. if (ret)
  283. LLOGD("esp_wifi_start ret %d", ret);
  284. wlan_inited = 1;
  285. // 是否自动开启ntp
  286. #ifndef LUAT_USE_SNTP_NOT_AUTO
  287. luat_ntp_autosync();
  288. #endif
  289. return 0;
  290. }
  291. // 设置wifi模式
  292. int luat_wlan_mode(luat_wlan_config_t *conf) {
  293. switch (conf->mode)
  294. {
  295. case LUAT_WLAN_MODE_NULL:
  296. esp_wifi_set_mode(WIFI_MODE_NULL);
  297. break;
  298. case LUAT_WLAN_MODE_STA:
  299. esp_wifi_set_mode(WIFI_MODE_STA);
  300. break;
  301. case LUAT_WLAN_MODE_AP:
  302. esp_wifi_set_mode(WIFI_MODE_AP);
  303. break;
  304. case LUAT_WLAN_MODE_APSTA:
  305. esp_wifi_set_mode(WIFI_MODE_APSTA);
  306. break;
  307. }
  308. return 0;
  309. }
  310. // 是否已经连上wifi
  311. int luat_wlan_ready(void) {
  312. return wlan_is_ready;
  313. }
  314. int luat_wlan_connect(luat_wlan_conninfo_t* info) {
  315. int ret = 0;
  316. wifi_config_t cfg = {0};
  317. // 允许重用一起的ssid和passwd数据直接重连
  318. if (strlen(info->ssid)) {
  319. // LLOGD("connect %s %s", info->ssid, info->password);
  320. memcpy(cfg.sta.ssid, info->ssid, strlen(info->ssid));
  321. memcpy(cfg.sta.password, info->password, strlen(info->password));
  322. esp_wifi_set_config(WIFI_IF_STA, &cfg);
  323. }
  324. auto_reconnection = 1;
  325. ret = esp_wifi_connect();
  326. if (ret)
  327. LLOGD("esp_wifi_connect ret %d", ret);
  328. return 0;
  329. }
  330. int luat_wlan_disconnect(void) {
  331. int ret = 0;
  332. auto_reconnection = 0;
  333. ret = esp_wifi_disconnect();
  334. if (ret)
  335. LLOGD("esp_wifi_disconnect ret %d", ret);
  336. return 0;
  337. }
  338. int luat_wlan_scan(void) {
  339. static const wifi_scan_config_t conf = {0};
  340. return esp_wifi_scan_start(&conf, false);
  341. }
  342. int luat_wlan_scan_get_result(luat_wlan_scan_result_t *results, size_t ap_limit) {
  343. uint16_t num = ap_limit;
  344. luat_wlan_scan_result_t *tmp = results;
  345. wifi_ap_record_t *ap_info = luat_heap_malloc(sizeof(wifi_ap_record_t) * ap_limit);
  346. if (ap_info == NULL)
  347. return 0;
  348. esp_wifi_scan_get_ap_records(&num, ap_info);
  349. for (size_t i = 0; i < num; i++)
  350. {
  351. memcpy(tmp[i].ssid, ap_info[i].ssid, strlen((const char*)ap_info[i].ssid));
  352. memcpy(tmp[i].bssid, ap_info[i].bssid, 6);
  353. tmp[i].rssi = ap_info[i].rssi;
  354. }
  355. luat_heap_free(ap_info);
  356. return num;
  357. }
  358. int luat_wlan_smartconfig_start(int tp) {
  359. if (smartconfig_state != 0) {
  360. LLOGI("smartconfig is running");
  361. return 0;
  362. }
  363. switch (tp)
  364. {
  365. case LUAT_SC_TYPE_ESPTOUCH:
  366. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH);
  367. break;
  368. case LUAT_SC_TYPE_ESPTOUCH_AIRKISS:
  369. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS);
  370. break;
  371. case LUAT_SC_TYPE_AIRKISS:
  372. esp_smartconfig_set_type(SC_TYPE_AIRKISS);
  373. break;
  374. case LUAT_SC_TYPE_ESPTOUCH_V2:
  375. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_V2);
  376. break;
  377. default:
  378. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH);
  379. break;
  380. }
  381. if (sc_evt == NULL) {
  382. sc_evt = luat_heap_malloc(sizeof(smartconfig_event_got_ssid_pswd_t));
  383. }
  384. smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
  385. esp_smartconfig_start(&cfg);
  386. smartconfig_state = 1;
  387. return 0;
  388. }
  389. int luat_wlan_smartconfig_stop(void) {
  390. esp_smartconfig_stop();
  391. smartconfig_state = 0;
  392. return 0;
  393. }
  394. int luat_wlan_get_mac(int id, char* mac) {
  395. if (id >= 0 && id <= ESP_MAC_IEEE802154) {
  396. esp_read_mac((uint8_t*)mac, id);
  397. return 0;
  398. }
  399. else {
  400. LLOGW("no such mac id %d", id);
  401. return -1;
  402. }
  403. }
  404. int luat_wlan_set_mac(int id, const char* mac) {
  405. if (id >= 0 && id <= ESP_MAC_IEEE802154) {
  406. esp_base_mac_addr_set((uint8_t*)mac);
  407. return 0;
  408. }
  409. else {
  410. LLOGW("no such mac id %d", id);
  411. return -1;
  412. }
  413. }
  414. int luat_wlan_ap_start(luat_wlan_apinfo_t *apinfo) {
  415. wifi_mode_t mode = 0;
  416. int ret = 0;
  417. wifi_config_t cfg = {0};
  418. cfg.ap.channel = 1;
  419. cfg.ap.max_connection = 5;
  420. memcpy(cfg.ap.ssid, apinfo->ssid, strlen(apinfo->ssid));
  421. cfg.ap.ssid_len = strlen(apinfo->ssid);
  422. if (strlen(apinfo->password) >= 6) {
  423. memcpy(cfg.ap.password, apinfo->password, strlen(apinfo->password));
  424. cfg.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
  425. }
  426. else {
  427. cfg.ap.authmode = WIFI_AUTH_OPEN;
  428. }
  429. if (apinfo->channel) {
  430. cfg.ap.channel = apinfo->channel;
  431. }
  432. LLOGD("softap %s %s", apinfo->ssid, apinfo->password);
  433. ret = esp_wifi_stop();
  434. if (ret)
  435. LLOGD("esp_wifi_stop ret %d", ret);
  436. if (wifiAP == NULL) {
  437. wifiAP = esp_netif_create_default_wifi_ap();
  438. }
  439. esp_netif_ip_info_t ipInfo = {0};
  440. if (apinfo->gateway[0]) {
  441. ipInfo.ip.addr = apinfo->gateway[0] + (apinfo->gateway[1] << 8) + (apinfo->gateway[2] << 16) + (apinfo->gateway[3] << 24);
  442. ipInfo.gw.addr = apinfo->gateway[0] + (apinfo->gateway[1] << 8) + (apinfo->gateway[2] << 16) + (apinfo->gateway[3] << 24);
  443. ipInfo.netmask.addr = apinfo->netmask[0] + (apinfo->netmask[1] << 8) + (apinfo->netmask[2] << 16) + (apinfo->netmask[3] << 24);
  444. ret = esp_netif_dhcps_stop(wifiAP);
  445. if (ret)
  446. LLOGD("esp_netif_dhcps_stop ret %d", ret);
  447. ret = esp_netif_set_ip_info(wifiAP, &ipInfo);
  448. if (ret)
  449. LLOGD("esp_netif_set_ip_info ret %d", ret);
  450. ret = esp_netif_dhcps_start(wifiAP);
  451. if (ret)
  452. LLOGD("esp_netif_dhcps_start ret %d", ret);
  453. }
  454. else {
  455. LLOGD("default gw 192.168.4.1 mask 255.255.255.0");
  456. }
  457. ret = esp_wifi_get_mode(&mode);
  458. if (mode == WIFI_MODE_NULL || mode == WIFI_MODE_STA) {
  459. LLOGI("auto set to APSTA mode");
  460. ret = esp_wifi_set_mode(WIFI_MODE_APSTA);
  461. if (ret)
  462. LLOGD("esp_wifi_set_mode ret %d", ret);
  463. }
  464. ret = esp_wifi_set_config(WIFI_IF_AP, &cfg);
  465. if (ret)
  466. LLOGD("esp_wifi_set_config ret %d", ret);
  467. ret = esp_wifi_start();
  468. if (ret)
  469. LLOGD("esp_wifi_start ret %d", ret);
  470. return ret;
  471. }
  472. int luat_wlan_get_ip(int type, char* data) {
  473. memcpy(data, sta_ip, strlen(sta_ip) + 1);
  474. return 0;
  475. }
  476. int luat_wlan_set_ps(int mode) {
  477. if (mode == 0) {
  478. esp_wifi_set_ps(WIFI_PS_NONE);
  479. }
  480. else if (mode == 1) {
  481. esp_wifi_set_ps(WIFI_PS_MIN_MODEM);
  482. }
  483. else if (mode == 2) {
  484. esp_wifi_set_ps(WIFI_PS_MAX_MODEM);
  485. }
  486. else {
  487. LLOGE("unkown wifi ps mode %d", mode);
  488. return -1;
  489. }
  490. return 0;
  491. }
  492. int luat_wlan_get_ps(void) {
  493. wifi_ps_type_t mode = WIFI_PS_NONE;
  494. esp_wifi_get_ps(&mode);
  495. return mode;
  496. }
  497. int luat_wlan_get_ap_bssid(char* buff) {
  498. memcpy(buff, sta_connected_bssid, 6);
  499. return 0;
  500. }
  501. int luat_wlan_get_ap_rssi(void) {
  502. wifi_ap_record_t ap;
  503. esp_wifi_sta_get_ap_info(&ap);
  504. return ap.rssi;
  505. }
  506. int luat_wlan_get_ap_gateway(char* buff) {
  507. esp_netif_ip_info_t ipInfo = {0};
  508. buff[0] = 0x00;
  509. if (wifiSTA == NULL) {
  510. return 0;
  511. }
  512. int ret = esp_netif_get_ip_info(wifiSTA, &ipInfo);
  513. if (ret == 0) {
  514. sprintf(buff, IPSTR, IP2STR(&ipInfo.gw));
  515. }
  516. return 0;
  517. }
  518. const char* luat_wlan_get_hostname(int id) {
  519. if (luat_sta_hostname[0] == 0) {
  520. char mac[6];
  521. esp_read_mac((uint8_t*)mac, 0);
  522. sprintf_(luat_sta_hostname, "LUATOS_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  523. }
  524. return (const char*)luat_sta_hostname;
  525. }
  526. int luat_wlan_set_hostname(int id, const char* hostname) {
  527. if (hostname == NULL || hostname[0] == 0) {
  528. return 0;
  529. }
  530. memcpy(luat_sta_hostname, hostname, strlen(hostname) + 1);
  531. esp_netif_set_hostname(wifiSTA, luat_sta_hostname);
  532. return 0;
  533. }
  534. int luat_wlan_ap_stop(void) {
  535. esp_wifi_stop();
  536. return 0;
  537. }