luat_wlan_idf5.c 25 KB

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