luat_wlan_idf5.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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) || info->bssid[5]) {
  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. memcpy(cfg.sta.bssid, info->bssid, 6);
  438. esp_wifi_set_config(WIFI_IF_STA, &cfg);
  439. }
  440. auto_reconnection = 1;
  441. ret = esp_wifi_connect();
  442. if (ret)
  443. LLOGD("esp_wifi_connect ret %d", ret);
  444. return ret;
  445. }
  446. int luat_wlan_disconnect(void) {
  447. int ret = 0;
  448. auto_reconnection = 0;
  449. ret = esp_wifi_disconnect();
  450. if (ret)
  451. LLOGD("esp_wifi_disconnect ret %d", ret);
  452. return ret;
  453. }
  454. int luat_wlan_scan(void) {
  455. static const wifi_scan_config_t conf = {0};
  456. return esp_wifi_scan_start(&conf, false);
  457. }
  458. int luat_wlan_scan_get_result(luat_wlan_scan_result_t *results, size_t ap_limit) {
  459. uint16_t num = ap_limit;
  460. luat_wlan_scan_result_t *tmp = results;
  461. wifi_ap_record_t *ap_info = luat_heap_malloc(sizeof(wifi_ap_record_t) * ap_limit);
  462. if (ap_info == NULL)
  463. return 0;
  464. esp_wifi_scan_get_ap_records(&num, ap_info);
  465. for (size_t i = 0; i < num; i++)
  466. {
  467. memcpy(tmp[i].ssid, ap_info[i].ssid, strlen((const char*)ap_info[i].ssid));
  468. memcpy(tmp[i].bssid, ap_info[i].bssid, 6);
  469. tmp[i].rssi = ap_info[i].rssi;
  470. }
  471. luat_heap_free(ap_info);
  472. return num;
  473. }
  474. int luat_wlan_smartconfig_start(int tp) {
  475. if (smartconfig_state != 0) {
  476. LLOGI("smartconfig is running");
  477. return 0;
  478. }
  479. switch (tp)
  480. {
  481. case LUAT_SC_TYPE_ESPTOUCH:
  482. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH);
  483. break;
  484. case LUAT_SC_TYPE_ESPTOUCH_AIRKISS:
  485. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS);
  486. break;
  487. case LUAT_SC_TYPE_AIRKISS:
  488. esp_smartconfig_set_type(SC_TYPE_AIRKISS);
  489. break;
  490. case LUAT_SC_TYPE_ESPTOUCH_V2:
  491. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_V2);
  492. break;
  493. default:
  494. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH);
  495. break;
  496. }
  497. if (sc_evt == NULL) {
  498. sc_evt = luat_heap_malloc(sizeof(smartconfig_event_got_ssid_pswd_t));
  499. }
  500. smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
  501. esp_smartconfig_start(&cfg);
  502. smartconfig_state = 1;
  503. return 0;
  504. }
  505. int luat_wlan_smartconfig_stop(void) {
  506. esp_smartconfig_stop();
  507. smartconfig_state = 0;
  508. return 0;
  509. }
  510. int luat_wlan_get_mac(int id, char* mac) {
  511. // LLOGD("尝试读取MAC %d", id);
  512. int ret = -1;
  513. if (id == 0 && wifiSTA != NULL) {
  514. ret = esp_netif_get_mac(wifiSTA, (uint8_t*)mac);
  515. }
  516. if (id == 1 && wifiAP != NULL) {
  517. ret = esp_netif_get_mac(wifiAP, (uint8_t*)mac);
  518. }
  519. return ret;
  520. }
  521. int luat_wlan_set_mac(int id, const char* mac) {
  522. const char emac[6] = {0,0,0,0,0,0};
  523. // LLOGD("设置MAC地址 %d %02X%02X%02X%02X%02X%02X", id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  524. int ret = -1;
  525. if (id == 0 && wifiSTA != NULL) {
  526. if (!memcmp(mac, emac, 6)) {
  527. // 清除自定义mac
  528. macaddr_set(0, NULL);
  529. return 0;
  530. }
  531. ret = esp_netif_set_mac(wifiSTA, (uint8_t*)mac);
  532. if (ret == 0) {
  533. macaddr_set(0, (uint8_t*)mac);
  534. }
  535. }
  536. if (id == 1 && wifiAP != NULL) {
  537. if (!memcmp(mac, emac, 6)) {
  538. // 清除自定义mac
  539. macaddr_set(1, NULL);
  540. return 0;
  541. }
  542. ret = esp_netif_set_mac(wifiAP, (uint8_t*)mac);
  543. if (ret == 0) {
  544. macaddr_set(1, (uint8_t*)mac);
  545. }
  546. }
  547. if (ret)
  548. LLOGE("设置结果 %d", ret);
  549. return ret;
  550. }
  551. static inline uint32_t to_esp_ipv4(uint8_t* data) {
  552. return data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24);
  553. }
  554. int luat_wlan_ap_start(luat_wlan_apinfo_t *apinfo) {
  555. wifi_mode_t mode = 0;
  556. int ret = 0;
  557. wifi_config_t cfg = {0};
  558. cfg.ap.channel = 1;
  559. cfg.ap.max_connection = 5;
  560. memcpy(cfg.ap.ssid, apinfo->ssid, strlen(apinfo->ssid));
  561. cfg.ap.ssid_len = strlen(apinfo->ssid);
  562. if (strlen(apinfo->password) >= 6) {
  563. memcpy(cfg.ap.password, apinfo->password, strlen(apinfo->password));
  564. cfg.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
  565. }
  566. else {
  567. cfg.ap.authmode = WIFI_AUTH_OPEN;
  568. }
  569. if (apinfo->channel) {
  570. cfg.ap.channel = apinfo->channel;
  571. }
  572. if (apinfo->hidden) {
  573. cfg.ap.ssid_hidden = apinfo->hidden;
  574. }
  575. if (apinfo->max_conn) {
  576. cfg.ap.max_connection = apinfo->max_conn;
  577. }
  578. LLOGD("softap %s %s", apinfo->ssid, apinfo->password);
  579. ret = esp_wifi_stop();
  580. if (ret)
  581. LLOGD("esp_wifi_stop ret %d", ret);
  582. if (wifiAP == NULL) {
  583. wifiAP = esp_netif_create_default_wifi_ap();
  584. }
  585. esp_netif_ip_info_t ipInfo = {0};
  586. if (apinfo->gateway[0]) {
  587. ipInfo.ip.addr = apinfo->gateway[0] + (apinfo->gateway[1] << 8) + (apinfo->gateway[2] << 16) + (apinfo->gateway[3] << 24);
  588. ipInfo.gw.addr = apinfo->gateway[0] + (apinfo->gateway[1] << 8) + (apinfo->gateway[2] << 16) + (apinfo->gateway[3] << 24);
  589. ipInfo.netmask.addr = apinfo->netmask[0] + (apinfo->netmask[1] << 8) + (apinfo->netmask[2] << 16) + (apinfo->netmask[3] << 24);
  590. ret = esp_netif_dhcps_stop(wifiAP);
  591. if (ret)
  592. LLOGD("esp_netif_dhcps_stop ret %d", ret);
  593. ret = esp_netif_set_ip_info(wifiAP, &ipInfo);
  594. if (ret)
  595. LLOGD("esp_netif_set_ip_info ret %d", ret);
  596. ret = esp_netif_dhcps_start(wifiAP);
  597. if (ret)
  598. LLOGD("esp_netif_dhcps_start ret %d", ret);
  599. }
  600. else {
  601. LLOGD("default gw 192.168.4.1 mask 255.255.255.0");
  602. }
  603. ret = esp_wifi_get_mode(&mode);
  604. if (mode == WIFI_MODE_NULL || mode == WIFI_MODE_STA) {
  605. LLOGI("auto set to APSTA mode");
  606. ret = esp_wifi_set_mode(WIFI_MODE_APSTA);
  607. if (ret)
  608. LLOGD("esp_wifi_set_mode ret %d", ret);
  609. }
  610. ret = esp_wifi_set_config(WIFI_IF_AP, &cfg);
  611. if (ret)
  612. LLOGD("esp_wifi_set_config ret %d", ret);
  613. ret = esp_wifi_start();
  614. if (ret)
  615. LLOGD("esp_wifi_start ret %d", ret);
  616. return ret;
  617. }
  618. int luat_wlan_get_ip(int type, char* data) {
  619. struct netif* nt = NULL;
  620. if (type == 0) {
  621. if (wifiSTA != NULL)
  622. nt = esp_netif_get_netif_impl(wifiSTA);
  623. }
  624. else {
  625. if (wifiAP != NULL)
  626. nt = esp_netif_get_netif_impl(wifiAP);
  627. }
  628. if (nt == NULL) {
  629. data[0] = 0;
  630. return 0;
  631. }
  632. sprintf(data, IPSTR, IP2STR(&nt->ip_addr));
  633. return 0;
  634. }
  635. int luat_wlan_set_ps(int mode) {
  636. if (mode == 0) {
  637. esp_wifi_set_ps(WIFI_PS_NONE);
  638. }
  639. else if (mode == 1) {
  640. esp_wifi_set_ps(WIFI_PS_MIN_MODEM);
  641. }
  642. else if (mode == 2) {
  643. esp_wifi_set_ps(WIFI_PS_MAX_MODEM);
  644. }
  645. else {
  646. LLOGE("unkown wifi ps mode %d", mode);
  647. return -1;
  648. }
  649. return 0;
  650. }
  651. int luat_wlan_get_ps(void) {
  652. wifi_ps_type_t mode = WIFI_PS_NONE;
  653. esp_wifi_get_ps(&mode);
  654. return mode;
  655. }
  656. int luat_wlan_get_ap_bssid(char* buff) {
  657. memcpy(buff, sta_connected_bssid, 6);
  658. return 0;
  659. }
  660. int luat_wlan_get_ap_rssi(void) {
  661. wifi_ap_record_t ap;
  662. esp_wifi_sta_get_ap_info(&ap);
  663. return ap.rssi;
  664. }
  665. int luat_wlan_get_ap_gateway(char* buff) {
  666. esp_netif_ip_info_t ipInfo = {0};
  667. buff[0] = 0x00;
  668. if (wifiSTA == NULL) {
  669. return 0;
  670. }
  671. int ret = esp_netif_get_ip_info(wifiSTA, &ipInfo);
  672. if (ret == 0) {
  673. sprintf(buff, IPSTR, IP2STR(&ipInfo.gw));
  674. }
  675. return 0;
  676. }
  677. const char* luat_wlan_get_hostname(int id) {
  678. if (luat_sta_hostname[0] == 0) {
  679. char mac[6];
  680. esp_read_mac((uint8_t*)mac, 0);
  681. sprintf_(luat_sta_hostname, "LUATOS_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  682. }
  683. return (const char*)luat_sta_hostname;
  684. }
  685. int luat_wlan_set_hostname(int id, const char* hostname) {
  686. if (hostname == NULL || hostname[0] == 0) {
  687. return 0;
  688. }
  689. memcpy(luat_sta_hostname, hostname, strlen(hostname) + 1);
  690. esp_netif_set_hostname(wifiSTA, luat_sta_hostname);
  691. return 0;
  692. }
  693. int luat_wlan_ap_stop(void) {
  694. esp_wifi_stop();
  695. return 0;
  696. }
  697. int luat_wlan_set_station_ip(luat_wlan_station_info_t *info) {
  698. esp_netif_ip_info_t ipInfo = {0};
  699. int ret = 0;
  700. if (wifiSTA == NULL) {
  701. LLOGE("call wlan.init() first");
  702. return -1;
  703. }
  704. dhcp_enable = info->dhcp_enable;
  705. if (!info->dhcp_enable) {
  706. esp_netif_dhcpc_stop(wifiSTA);
  707. ipInfo.ip.addr = to_esp_ipv4(info->ipv4_addr);
  708. ipInfo.gw.addr = to_esp_ipv4(info->ipv4_gateway);
  709. ipInfo.netmask.addr = to_esp_ipv4(info->ipv4_netmask);
  710. ret = esp_netif_set_ip_info(wifiSTA, &ipInfo);
  711. if (ret)
  712. LLOGD("esp_netif_set_ip_info ret %d", ret);
  713. LLOGD("dhcp is disabled");
  714. LLOGD("Sta IP %d.%d.%d.%d MASK %d.%d.%d.%d GW %d.%d.%d.%d",
  715. info->ipv4_addr[0], info->ipv4_addr[1], info->ipv4_addr[2], info->ipv4_addr[3],
  716. info->ipv4_netmask[0],info->ipv4_netmask[1],info->ipv4_netmask[2],info->ipv4_netmask[3],
  717. info->ipv4_gateway[0],info->ipv4_gateway[1],info->ipv4_gateway[2],info->ipv4_gateway[3]
  718. );
  719. }
  720. else {
  721. ret = esp_netif_dhcpc_start(wifiSTA);
  722. if (ret)
  723. LLOGD("esp_netif_dhcpc_start ret %d", ret);
  724. LLOGD("dhcp enabled");
  725. }
  726. return ret;
  727. }