luat_wlan_idf5.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. #include "nvs.h"
  15. #define LUAT_LOG_TAG "wlan"
  16. #include "luat_log.h"
  17. #include "lwip/netif.h"
  18. #include "luat_network_adapter.h"
  19. #include "luat_timer.h"
  20. #include "net_lwip.h"
  21. #include "lwip/tcp.h"
  22. void net_lwip_set_link_state(uint8_t adapter_index, uint8_t updown);
  23. static uint8_t wlan_inited = 0;
  24. static uint8_t wlan_is_ready = 0;
  25. static smartconfig_event_got_ssid_pswd_t *sc_evt;
  26. static char sta_connected_bssid[6];
  27. char luat_sta_hostname[32];
  28. static uint8_t smartconfig_state = 0; // 0 - idle, 1 - running
  29. static uint8_t auto_reconnection = 0;
  30. static uint8_t dhcp_enable = 1;
  31. // static uint8_t ap_stack_inited = 0;
  32. static esp_netif_t* wifiAP;
  33. static esp_netif_t* wifiSTA;
  34. // 从nvs恢复mac地址
  35. #if 1
  36. static void macaddr_restore(int id) {
  37. nvs_handle_t handle;
  38. uint8_t mac[16] = {0};
  39. uint8_t mac2[16] = {0};
  40. int ret = 0;
  41. int8_t len = 6;
  42. // LLOGW("尝试恢复mac %d", id);
  43. ret = nvs_open("macaddr", NVS_READONLY, &handle);
  44. if (ret) {
  45. // LLOGD("未自定义mac地址");
  46. return;
  47. }
  48. if (id == 0) {
  49. ret = nvs_get_blob(handle, "sta", mac, &len);
  50. if (ret == 0 && wifiSTA) {
  51. esp_netif_get_mac(wifiSTA, (uint8_t*)mac2);
  52. // LLOGD("恢复sta的mac地址为 %02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  53. ret = esp_netif_set_mac(wifiSTA, (uint8_t*)mac);
  54. if (ret) {
  55. LLOGE("恢复sta的自定义mac地址失败");
  56. }
  57. }
  58. else {
  59. // LLOGD("没有sta的自定义mac地址");
  60. }
  61. }
  62. else if (id == 1) {
  63. ret = nvs_get_blob(handle, "ap", mac, &len);
  64. if (ret == 0 && wifiAP) {
  65. esp_netif_get_mac(wifiAP, (uint8_t*)mac2);
  66. // LLOGD("恢复ap的mac地址为 %02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  67. ret = esp_netif_set_mac(wifiAP, (uint8_t*)mac);
  68. if (ret) {
  69. LLOGE("恢复ap的自定义mac地址失败");
  70. }
  71. }
  72. else {
  73. // LLOGD("没有ap的自定义mac地址");
  74. }
  75. }
  76. // LLOGD("恢复mac结束 %d", id);
  77. nvs_close(handle);
  78. }
  79. static void macaddr_set(int id, uint8_t* mac) {
  80. nvs_handle_t handle;
  81. int ret = 0;
  82. // LLOGD("保存mac地址到nvs %d", id);
  83. ret = nvs_open("macaddr", NVS_READWRITE, &handle);
  84. if (ret) {
  85. // LLOGD("未自定义mac地址");
  86. return;
  87. }
  88. if (id == 0) {
  89. if (mac == NULL) {
  90. nvs_erase_key(handle, "sta");
  91. return;
  92. }
  93. // LLOGD("存储sta的mac地址为 %02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  94. ret = nvs_set_blob(handle, "sta", mac, 6);
  95. if (ret) {
  96. LLOGE("存储sta的自定义mac地址失败");
  97. }
  98. else {
  99. nvs_commit(handle);
  100. }
  101. }
  102. else if (id == 1) {
  103. if (mac == NULL) {
  104. nvs_erase_key(handle, "ap");
  105. return;
  106. }
  107. // LLOGD("存储ap的mac地址为 %02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  108. ret = nvs_set_blob(handle, "ap", mac, 6);
  109. if (ret) {
  110. LLOGE("存储ap的自定义mac地址失败");
  111. }
  112. else {
  113. nvs_commit(handle);
  114. }
  115. }
  116. // LLOGD("保存结果 %d", ret);
  117. nvs_close(handle);
  118. }
  119. #else
  120. static void macaddr_restore(int id) {}
  121. static void macaddr_set(int id, uint8_t* mac) {}
  122. #endif
  123. static int l_wlan_handler(lua_State *L, void* ptr) {
  124. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  125. int32_t event_id = msg->arg1;
  126. int32_t event_tp = msg->arg2;
  127. char buff[64] = {0};
  128. lua_getglobal(L, "sys_pub");
  129. if (event_tp == 0) {
  130. switch (event_id)
  131. {
  132. case WIFI_EVENT_WIFI_READY:
  133. LLOGD("wifi protocol is ready");
  134. lua_getglobal(L, "sys_pub");
  135. lua_pushstring(L, "WLAN_READY");
  136. lua_pushinteger(L, 1);
  137. //esp_netif_get_ip_info(ESP_IF_WIFI_STA,&ip_info);
  138. lua_call(L, 2, 0);
  139. break;
  140. case WIFI_EVENT_STA_CONNECTED:
  141. LLOGD("wifi connected!!!");
  142. lua_pushstring(L, "WLAN_STA_CONNECTED");
  143. lua_call(L, 1, 0);
  144. if (!dhcp_enable) {
  145. lua_getglobal(L, "sys_pub");
  146. lua_pushstring(L, "IP_READY");
  147. luat_wlan_get_ip(0, buff);
  148. lua_pushstring(L, buff);
  149. lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  150. lua_call(L, 3, 0);
  151. }
  152. break;
  153. case WIFI_EVENT_STA_DISCONNECTED:
  154. LLOGD("wifi disconnected!!!");
  155. lua_pushstring(L, "WLAN_STA_DISCONNECTED");
  156. lua_call(L, 1, 0);
  157. lua_getglobal(L, "sys_pub");
  158. lua_pushstring(L, "WLAN_READY");
  159. lua_pushinteger(L, 0);
  160. lua_call(L, 2, 0);
  161. if (auto_reconnection && smartconfig_state == 0) {
  162. LLOGD("auto reconnecting ...");
  163. esp_wifi_connect();
  164. }
  165. break;
  166. case WIFI_EVENT_STA_START:
  167. LLOGD("wifi station start");
  168. macaddr_restore(0);
  169. break;
  170. case WIFI_EVENT_STA_STOP:
  171. LLOGD("wifi station stop");
  172. break;
  173. case WIFI_EVENT_SCAN_DONE:
  174. LLOGD("wifi scan done");
  175. lua_pushstring(L, "WLAN_SCAN_DONE");
  176. lua_call(L, 1, 0);
  177. break;
  178. case WIFI_EVENT_AP_START:
  179. LLOGD("wifi ap start");
  180. macaddr_restore(1);
  181. break;
  182. case WIFI_EVENT_AP_STOP:
  183. LLOGD("wifi ap stop");
  184. break;
  185. case WIFI_EVENT_AP_STACONNECTED :
  186. LLOGD("wifi ap sta connected");
  187. lua_pushstring(L, "WLAN_AP_CONNECTED");
  188. lua_call(L, 1, 0);
  189. break;
  190. case WIFI_EVENT_AP_STADISCONNECTED :
  191. LLOGD("wifi ap sta disconnected");
  192. lua_pushstring(L, "WLAN_AP_DISCONNECTED");
  193. lua_call(L, 1, 0);
  194. break;
  195. default:
  196. LLOGI("unkown event %d", event_id);
  197. break;
  198. }
  199. }
  200. else if (event_tp == 1) {
  201. if (event_id == IP_EVENT_STA_GOT_IP) {
  202. luat_wlan_get_ip(0, buff);
  203. LLOGD("sta_ip %s", buff);
  204. lua_pushstring(L, "IP_READY");
  205. lua_pushstring(L, buff);
  206. lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  207. lua_call(L, 3, 0);
  208. }
  209. else if (event_id == IP_EVENT_AP_STAIPASSIGNED) {
  210. LLOGD("wifi ap sta connected");
  211. lua_pushstring(L, "WLAN_AP_STA_IP_READY");
  212. char tmp[32];
  213. esp_ip4_addr_t ip4 = {
  214. .addr = (uint32_t)ptr
  215. };
  216. sprintf(tmp, IPSTR, IP2STR(&ip4));
  217. lua_pushstring(L, tmp);
  218. lua_call(L, 2, 0);
  219. }
  220. }
  221. else if (event_tp == 2) {
  222. if (event_id == SC_EVENT_SCAN_DONE) {
  223. LLOGD("smartconfig Scan done");
  224. }
  225. else if (event_id == SC_EVENT_SCAN_DONE) {
  226. LLOGD("smartconfig Found channel");
  227. }
  228. else if (event_id == SC_EVENT_GOT_SSID_PSWD) {
  229. LLOGD("smartconfig Got ssid and password");
  230. smartconfig_event_got_ssid_pswd_t *evt = sc_evt;
  231. wifi_config_t wifi_config;
  232. uint8_t ssid[33] = { 0 };
  233. uint8_t password[65] = { 0 };
  234. // uint8_t rvd_data[33] = { 0 };
  235. bzero(&wifi_config, sizeof(wifi_config_t));
  236. memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
  237. memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
  238. wifi_config.sta.bssid_set = evt->bssid_set;
  239. if (wifi_config.sta.bssid_set == true) {
  240. memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
  241. }
  242. memcpy(ssid, evt->ssid, sizeof(evt->ssid));
  243. memcpy(password, evt->password, sizeof(evt->password));
  244. LLOGD("SSID [%s] PASSWORD [%s]", ssid, password);
  245. // ESP_LOGI(TAG, "SSID:%s", ssid);
  246. // ESP_LOGI(TAG, "PASSWORD:%s", password);
  247. // if (evt->type == SC_TYPE_ESPTOUCH_V2) {
  248. // ESP_ERROR_CHECK( esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)) );
  249. // ESP_LOGI(TAG, "RVD_DATA:");
  250. // for (int i=0; i<33; i++) {
  251. // printf("%02x ", rvd_data[i]);
  252. // }
  253. // printf("\n");
  254. // }
  255. esp_wifi_disconnect();
  256. esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
  257. esp_wifi_connect();
  258. lua_pushstring(L, "SC_RESULT");
  259. lua_pushstring(L, (const char*)ssid);
  260. lua_pushstring(L, (const char*)password);
  261. lua_call(L, 3, 0);
  262. }
  263. else if (event_id == SC_EVENT_SEND_ACK_DONE) {
  264. esp_smartconfig_stop();
  265. smartconfig_state = 0;
  266. }
  267. }
  268. return 0;
  269. }
  270. static void wifi_event_handler(void *arg, esp_event_base_t event_base,
  271. int32_t event_id, void *event_data) {
  272. rtos_msg_t msg = {0};
  273. msg.handler = l_wlan_handler;
  274. msg.arg1 = event_id;
  275. LLOGD("wifi event %d", event_id);
  276. switch (event_id) {
  277. case WIFI_EVENT_STA_DISCONNECTED: {
  278. wlan_is_ready = 0;
  279. #ifdef LUAT_USE_NETWORK
  280. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_STA, 0);
  281. #endif
  282. // wifi_event_sta_disconnected_t* sta = (wifi_event_sta_disconnected_t*)event_data;
  283. memset(sta_connected_bssid, 0, sizeof(sta_connected_bssid));
  284. break;
  285. }
  286. case WIFI_EVENT_STA_CONNECTED: {
  287. wifi_event_sta_connected_t *sta = (wifi_event_sta_connected_t*)event_data;
  288. memcpy(sta_connected_bssid, sta->bssid, 6);
  289. break;
  290. }
  291. case WIFI_EVENT_AP_START: {
  292. #ifdef LUAT_USE_NETWORK
  293. net_lwip_set_netif(esp_netif_get_netif_impl(wifiAP), NW_ADAPTER_INDEX_LWIP_WIFI_AP);
  294. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 1);
  295. #endif
  296. break;
  297. }
  298. case WIFI_EVENT_AP_STOP: {
  299. #ifdef LUAT_USE_NETWORK
  300. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 0);
  301. #endif
  302. break;
  303. }
  304. case WIFI_EVENT_STA_BEACON_TIMEOUT : {
  305. LLOGD("wifi sta beacon timeout");
  306. return;
  307. }
  308. }
  309. luat_msgbus_put(&msg, 0);
  310. }
  311. static void ip_event_handler(void *arg, esp_event_base_t event_base,
  312. int32_t event_id, void *event_data) {
  313. rtos_msg_t msg = {0};
  314. msg.handler = l_wlan_handler;
  315. msg.arg1 = event_id;
  316. msg.arg2 = 1;
  317. // ip_event_got_ip_t *event;
  318. ip_event_ap_staipassigned_t* ap_sta_event;
  319. LLOGD("ip event %d", event_id);
  320. if (event_id == IP_EVENT_STA_GOT_IP) {
  321. wlan_is_ready = 1;
  322. // event = (ip_event_got_ip_t*)event_data;
  323. // sprintf(sta_ip, IPSTR, IP2STR(&event->ip_info.ip));
  324. // sprintf(sta_gw, IPSTR, IP2STR(&event->ip_info.gw));
  325. #ifdef LUAT_USE_NETWORK
  326. net_lwip_set_netif(netif_get_by_index(2), NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  327. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_STA, 1);
  328. #endif
  329. }
  330. else if (event_id == IP_EVENT_AP_STAIPASSIGNED) {
  331. ap_sta_event = (ip_event_ap_staipassigned_t*)event_data;
  332. char tmp[32];
  333. sprintf(tmp, IPSTR, IP2STR(&ap_sta_event->ip));
  334. LLOGD("ap sta ip %s", tmp);
  335. msg.ptr = (void*)ap_sta_event->ip.addr;
  336. }
  337. luat_msgbus_put(&msg, 0);
  338. // LLOGD("ip_event_handler is done");
  339. }
  340. static void sc_event_handler(void *arg, esp_event_base_t event_base,
  341. int32_t event_id, void *event_data) {
  342. rtos_msg_t msg = {0};
  343. msg.handler = l_wlan_handler;
  344. msg.arg1 = event_id;
  345. msg.arg2 = 2;
  346. if (event_id == SC_EVENT_GOT_SSID_PSWD && sc_evt != NULL) {
  347. memcpy(sc_evt, event_data, sizeof(smartconfig_event_got_ssid_pswd_t));
  348. }
  349. LLOGD("sc event %d", event_id);
  350. luat_msgbus_put(&msg, 0);
  351. }
  352. extern void luat_ntp_autosync(void);
  353. int luat_wlan_init(luat_wlan_config_t *conf) {
  354. int ret = 0;
  355. if (wlan_inited == 0) {
  356. esp_netif_init();
  357. esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL);
  358. esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL);
  359. esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &sc_event_handler, NULL);
  360. wifiSTA = esp_netif_create_default_wifi_sta();
  361. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  362. cfg.static_rx_buf_num = 2;
  363. cfg.static_tx_buf_num = 2;
  364. ret = esp_wifi_init(&cfg);
  365. if (ret)
  366. LLOGD("esp_wifi_init ret %d", ret);
  367. esp_wifi_set_mode(WIFI_MODE_STA);
  368. ret = esp_netif_set_hostname(wifiSTA, luat_wlan_get_hostname(0));
  369. #ifdef LUAT_USE_NETWORK
  370. net_lwip_register_adapter(NW_ADAPTER_INDEX_LWIP_WIFI_AP);
  371. net_lwip_register_adapter(NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  372. #endif
  373. if (ret)
  374. LLOGD("esp_netif_set_hostname ret %d", ret);
  375. if (dhcp_enable) {
  376. // LLOGD("自动启动dhcp");
  377. ret = esp_netif_dhcpc_start(wifiSTA);
  378. if (ret)
  379. LLOGD("esp_netif_dhcpc_start ret %d", ret);
  380. }
  381. else {
  382. // LLOGD("禁用dhcp");
  383. }
  384. }
  385. #ifdef LUAT_USE_NIMBLE
  386. #if CONFIG_BT_ENABLED
  387. //esp_wifi_set_ps(WIFI_PS_NONE);
  388. #endif
  389. #endif
  390. ret = esp_wifi_start();
  391. if (ret)
  392. LLOGD("esp_wifi_start ret %d", ret);
  393. wlan_inited = 1;
  394. // 是否自动开启ntp
  395. #ifndef LUAT_USE_SNTP_NOT_AUTO
  396. luat_ntp_autosync();
  397. #endif
  398. return 0;
  399. }
  400. // 设置wifi模式
  401. int luat_wlan_mode(luat_wlan_config_t *conf) {
  402. switch (conf->mode)
  403. {
  404. case LUAT_WLAN_MODE_NULL:
  405. esp_wifi_set_mode(WIFI_MODE_NULL);
  406. break;
  407. case LUAT_WLAN_MODE_STA:
  408. esp_wifi_set_mode(WIFI_MODE_STA);
  409. break;
  410. case LUAT_WLAN_MODE_AP:
  411. esp_wifi_set_mode(WIFI_MODE_AP);
  412. break;
  413. case LUAT_WLAN_MODE_APSTA:
  414. esp_wifi_set_mode(WIFI_MODE_APSTA);
  415. break;
  416. }
  417. return 0;
  418. }
  419. // 是否已经连上wifi
  420. int luat_wlan_ready(void) {
  421. return wlan_is_ready;
  422. }
  423. int luat_wlan_connect(luat_wlan_conninfo_t* info) {
  424. int ret = 0;
  425. wifi_config_t cfg = {0};
  426. // 允许重用一起的ssid和passwd数据直接重连
  427. if (strlen(info->ssid)) {
  428. // LLOGD("connect %s %s", info->ssid, info->password);
  429. memcpy(cfg.sta.ssid, info->ssid, strlen(info->ssid));
  430. memcpy(cfg.sta.password, info->password, strlen(info->password));
  431. esp_wifi_set_config(WIFI_IF_STA, &cfg);
  432. }
  433. auto_reconnection = 1;
  434. ret = esp_wifi_connect();
  435. if (ret)
  436. LLOGD("esp_wifi_connect ret %d", ret);
  437. return 0;
  438. }
  439. int luat_wlan_disconnect(void) {
  440. int ret = 0;
  441. auto_reconnection = 0;
  442. ret = esp_wifi_disconnect();
  443. if (ret)
  444. LLOGD("esp_wifi_disconnect ret %d", ret);
  445. return 0;
  446. }
  447. int luat_wlan_scan(void) {
  448. static const wifi_scan_config_t conf = {0};
  449. return esp_wifi_scan_start(&conf, false);
  450. }
  451. int luat_wlan_scan_get_result(luat_wlan_scan_result_t *results, size_t ap_limit) {
  452. uint16_t num = ap_limit;
  453. luat_wlan_scan_result_t *tmp = results;
  454. wifi_ap_record_t *ap_info = luat_heap_malloc(sizeof(wifi_ap_record_t) * ap_limit);
  455. if (ap_info == NULL)
  456. return 0;
  457. esp_wifi_scan_get_ap_records(&num, ap_info);
  458. for (size_t i = 0; i < num; i++)
  459. {
  460. memcpy(tmp[i].ssid, ap_info[i].ssid, strlen((const char*)ap_info[i].ssid));
  461. memcpy(tmp[i].bssid, ap_info[i].bssid, 6);
  462. tmp[i].rssi = ap_info[i].rssi;
  463. }
  464. luat_heap_free(ap_info);
  465. return num;
  466. }
  467. int luat_wlan_smartconfig_start(int tp) {
  468. if (smartconfig_state != 0) {
  469. LLOGI("smartconfig is running");
  470. return 0;
  471. }
  472. switch (tp)
  473. {
  474. case LUAT_SC_TYPE_ESPTOUCH:
  475. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH);
  476. break;
  477. case LUAT_SC_TYPE_ESPTOUCH_AIRKISS:
  478. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS);
  479. break;
  480. case LUAT_SC_TYPE_AIRKISS:
  481. esp_smartconfig_set_type(SC_TYPE_AIRKISS);
  482. break;
  483. case LUAT_SC_TYPE_ESPTOUCH_V2:
  484. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_V2);
  485. break;
  486. default:
  487. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH);
  488. break;
  489. }
  490. if (sc_evt == NULL) {
  491. sc_evt = luat_heap_malloc(sizeof(smartconfig_event_got_ssid_pswd_t));
  492. }
  493. smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
  494. esp_smartconfig_start(&cfg);
  495. smartconfig_state = 1;
  496. return 0;
  497. }
  498. int luat_wlan_smartconfig_stop(void) {
  499. esp_smartconfig_stop();
  500. smartconfig_state = 0;
  501. return 0;
  502. }
  503. int luat_wlan_get_mac(int id, char* mac) {
  504. // LLOGD("尝试读取MAC %d", id);
  505. int ret = -1;
  506. if (id == 0 && wifiSTA != NULL) {
  507. ret = esp_netif_get_mac(wifiSTA, (uint8_t*)mac);
  508. }
  509. if (id == 1 && wifiAP != NULL) {
  510. ret = esp_netif_get_mac(wifiAP, (uint8_t*)mac);
  511. }
  512. return ret;
  513. }
  514. int luat_wlan_set_mac(int id, const char* mac) {
  515. const char emac[6] = {0,0,0,0,0,0};
  516. // LLOGD("设置MAC地址 %d %02X%02X%02X%02X%02X%02X", id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  517. int ret = -1;
  518. if (id == 0 && wifiSTA != NULL) {
  519. if (!memcmp(mac, emac, 6)) {
  520. // 清除自定义mac
  521. macaddr_set(0, NULL);
  522. return 0;
  523. }
  524. ret = esp_netif_set_mac(wifiSTA, (uint8_t*)mac);
  525. if (ret == 0) {
  526. macaddr_set(0, (uint8_t*)mac);
  527. }
  528. }
  529. if (id == 1 && wifiAP != NULL) {
  530. if (!memcmp(mac, emac, 6)) {
  531. // 清除自定义mac
  532. macaddr_set(1, NULL);
  533. return 0;
  534. }
  535. ret = esp_netif_set_mac(wifiAP, (uint8_t*)mac);
  536. if (ret == 0) {
  537. macaddr_set(1, (uint8_t*)mac);
  538. }
  539. }
  540. if (ret)
  541. LLOGE("设置结果 %d", ret);
  542. return ret;
  543. }
  544. static inline uint32_t to_esp_ipv4(uint8_t* data) {
  545. return data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24);
  546. }
  547. int luat_wlan_ap_start(luat_wlan_apinfo_t *apinfo) {
  548. wifi_mode_t mode = 0;
  549. int ret = 0;
  550. wifi_config_t cfg = {0};
  551. cfg.ap.channel = 1;
  552. cfg.ap.max_connection = 5;
  553. memcpy(cfg.ap.ssid, apinfo->ssid, strlen(apinfo->ssid));
  554. cfg.ap.ssid_len = strlen(apinfo->ssid);
  555. if (strlen(apinfo->password) >= 6) {
  556. memcpy(cfg.ap.password, apinfo->password, strlen(apinfo->password));
  557. cfg.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
  558. }
  559. else {
  560. cfg.ap.authmode = WIFI_AUTH_OPEN;
  561. }
  562. if (apinfo->channel) {
  563. cfg.ap.channel = apinfo->channel;
  564. }
  565. LLOGD("softap %s %s", apinfo->ssid, apinfo->password);
  566. ret = esp_wifi_stop();
  567. if (ret)
  568. LLOGD("esp_wifi_stop ret %d", ret);
  569. if (wifiAP == NULL) {
  570. wifiAP = esp_netif_create_default_wifi_ap();
  571. }
  572. esp_netif_ip_info_t ipInfo = {0};
  573. if (apinfo->gateway[0]) {
  574. ipInfo.ip.addr = apinfo->gateway[0] + (apinfo->gateway[1] << 8) + (apinfo->gateway[2] << 16) + (apinfo->gateway[3] << 24);
  575. ipInfo.gw.addr = apinfo->gateway[0] + (apinfo->gateway[1] << 8) + (apinfo->gateway[2] << 16) + (apinfo->gateway[3] << 24);
  576. ipInfo.netmask.addr = apinfo->netmask[0] + (apinfo->netmask[1] << 8) + (apinfo->netmask[2] << 16) + (apinfo->netmask[3] << 24);
  577. ret = esp_netif_dhcps_stop(wifiAP);
  578. if (ret)
  579. LLOGD("esp_netif_dhcps_stop ret %d", ret);
  580. ret = esp_netif_set_ip_info(wifiAP, &ipInfo);
  581. if (ret)
  582. LLOGD("esp_netif_set_ip_info ret %d", ret);
  583. ret = esp_netif_dhcps_start(wifiAP);
  584. if (ret)
  585. LLOGD("esp_netif_dhcps_start ret %d", ret);
  586. }
  587. else {
  588. LLOGD("default gw 192.168.4.1 mask 255.255.255.0");
  589. }
  590. ret = esp_wifi_get_mode(&mode);
  591. if (mode == WIFI_MODE_NULL || mode == WIFI_MODE_STA) {
  592. LLOGI("auto set to APSTA mode");
  593. ret = esp_wifi_set_mode(WIFI_MODE_APSTA);
  594. if (ret)
  595. LLOGD("esp_wifi_set_mode ret %d", ret);
  596. }
  597. ret = esp_wifi_set_config(WIFI_IF_AP, &cfg);
  598. if (ret)
  599. LLOGD("esp_wifi_set_config ret %d", ret);
  600. ret = esp_wifi_start();
  601. if (ret)
  602. LLOGD("esp_wifi_start ret %d", ret);
  603. return ret;
  604. }
  605. int luat_wlan_get_ip(int type, char* data) {
  606. // memcpy(data, sta_ip, strlen(sta_ip) + 1);
  607. struct netif* sta = netif_get_by_index(2);
  608. if (sta == NULL) {
  609. data[0] = 0;
  610. return 0;
  611. }
  612. sprintf(data, IPSTR, IP2STR(&sta->ip_addr));
  613. return 0;
  614. }
  615. int luat_wlan_set_ps(int mode) {
  616. if (mode == 0) {
  617. esp_wifi_set_ps(WIFI_PS_NONE);
  618. }
  619. else if (mode == 1) {
  620. esp_wifi_set_ps(WIFI_PS_MIN_MODEM);
  621. }
  622. else if (mode == 2) {
  623. esp_wifi_set_ps(WIFI_PS_MAX_MODEM);
  624. }
  625. else {
  626. LLOGE("unkown wifi ps mode %d", mode);
  627. return -1;
  628. }
  629. return 0;
  630. }
  631. int luat_wlan_get_ps(void) {
  632. wifi_ps_type_t mode = WIFI_PS_NONE;
  633. esp_wifi_get_ps(&mode);
  634. return mode;
  635. }
  636. int luat_wlan_get_ap_bssid(char* buff) {
  637. memcpy(buff, sta_connected_bssid, 6);
  638. return 0;
  639. }
  640. int luat_wlan_get_ap_rssi(void) {
  641. wifi_ap_record_t ap;
  642. esp_wifi_sta_get_ap_info(&ap);
  643. return ap.rssi;
  644. }
  645. int luat_wlan_get_ap_gateway(char* buff) {
  646. esp_netif_ip_info_t ipInfo = {0};
  647. buff[0] = 0x00;
  648. if (wifiSTA == NULL) {
  649. return 0;
  650. }
  651. int ret = esp_netif_get_ip_info(wifiSTA, &ipInfo);
  652. if (ret == 0) {
  653. sprintf(buff, IPSTR, IP2STR(&ipInfo.gw));
  654. }
  655. return 0;
  656. }
  657. const char* luat_wlan_get_hostname(int id) {
  658. if (luat_sta_hostname[0] == 0) {
  659. char mac[6];
  660. esp_read_mac((uint8_t*)mac, 0);
  661. sprintf_(luat_sta_hostname, "LUATOS_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  662. }
  663. return (const char*)luat_sta_hostname;
  664. }
  665. int luat_wlan_set_hostname(int id, const char* hostname) {
  666. if (hostname == NULL || hostname[0] == 0) {
  667. return 0;
  668. }
  669. memcpy(luat_sta_hostname, hostname, strlen(hostname) + 1);
  670. esp_netif_set_hostname(wifiSTA, luat_sta_hostname);
  671. return 0;
  672. }
  673. int luat_wlan_ap_stop(void) {
  674. esp_wifi_stop();
  675. return 0;
  676. }
  677. int luat_wlan_set_station_ip(luat_wlan_station_info_t *info) {
  678. esp_netif_ip_info_t ipInfo = {0};
  679. int ret = 0;
  680. if (wifiSTA == NULL) {
  681. LLOGE("call wlan.init() first");
  682. return -1;
  683. }
  684. dhcp_enable = info->dhcp_enable;
  685. if (!info->dhcp_enable) {
  686. esp_netif_dhcpc_stop(wifiSTA);
  687. ipInfo.ip.addr = to_esp_ipv4(info->ipv4_addr);
  688. ipInfo.gw.addr = to_esp_ipv4(info->ipv4_gateway);
  689. ipInfo.netmask.addr = to_esp_ipv4(info->ipv4_netmask);
  690. ret = esp_netif_set_ip_info(wifiSTA, &ipInfo);
  691. if (ret)
  692. LLOGD("esp_netif_set_ip_info ret %d", ret);
  693. LLOGD("dhcp is disabled");
  694. LLOGD("Sta IP %d.%d.%d.%d MASK %d.%d.%d.%d GW %d.%d.%d.%d",
  695. info->ipv4_addr[0], info->ipv4_addr[1], info->ipv4_addr[2], info->ipv4_addr[3],
  696. info->ipv4_netmask[0],info->ipv4_netmask[1],info->ipv4_netmask[2],info->ipv4_netmask[3],
  697. info->ipv4_gateway[0],info->ipv4_gateway[1],info->ipv4_gateway[2],info->ipv4_gateway[3]
  698. );
  699. }
  700. else {
  701. ret = esp_netif_dhcpc_start(wifiSTA);
  702. if (ret)
  703. LLOGD("esp_netif_dhcpc_start ret %d", ret);
  704. LLOGD("dhcp enabled");
  705. }
  706. return ret;
  707. }