luat_wlan_idf5.c 16 KB

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