luat_wlan_idf5.c 24 KB

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