luat_wlan_idf5.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. luat_ntp_autosync();
  249. return 0;
  250. }
  251. // 设置wifi模式
  252. int luat_wlan_mode(luat_wlan_config_t *conf) {
  253. switch (conf->mode)
  254. {
  255. case LUAT_WLAN_MODE_NULL:
  256. esp_wifi_set_mode(WIFI_MODE_NULL);
  257. break;
  258. case LUAT_WLAN_MODE_STA:
  259. esp_wifi_set_mode(WIFI_MODE_STA);
  260. break;
  261. case LUAT_WLAN_MODE_AP:
  262. esp_wifi_set_mode(WIFI_MODE_AP);
  263. break;
  264. case LUAT_WLAN_MODE_APSTA:
  265. esp_wifi_set_mode(WIFI_MODE_APSTA);
  266. break;
  267. }
  268. return 0;
  269. }
  270. // 是否已经连上wifi
  271. int luat_wlan_ready(void) {
  272. return wlan_is_ready;
  273. }
  274. int luat_wlan_connect(luat_wlan_conninfo_t* info) {
  275. int ret = 0;
  276. wifi_config_t cfg = {0};
  277. // 允许重用一起的ssid和passwd数据直接重连
  278. if (strlen(info->ssid)) {
  279. // LLOGD("connect %s %s", info->ssid, info->password);
  280. memcpy(cfg.sta.ssid, info->ssid, strlen(info->ssid));
  281. memcpy(cfg.sta.password, info->password, strlen(info->password));
  282. esp_wifi_set_config(WIFI_IF_STA, &cfg);
  283. }
  284. auto_reconnection = 1;
  285. ret = esp_wifi_connect();
  286. LLOGD("esp_wifi_connect ret %d", ret);
  287. return 0;
  288. }
  289. int luat_wlan_disconnect(void) {
  290. int ret = 0;
  291. auto_reconnection = 0;
  292. ret = esp_wifi_disconnect();
  293. LLOGD("esp_wifi_disconnect ret %d", ret);
  294. return 0;
  295. }
  296. int luat_wlan_scan(void) {
  297. static const wifi_scan_config_t conf = {0};
  298. return esp_wifi_scan_start(&conf, false);
  299. }
  300. int luat_wlan_scan_get_result(luat_wlan_scan_result_t *results, int ap_limit) {
  301. uint16_t num = ap_limit;
  302. luat_wlan_scan_result_t *tmp = results;
  303. wifi_ap_record_t *ap_info = luat_heap_malloc(sizeof(wifi_ap_record_t) * ap_limit);
  304. if (ap_info == NULL)
  305. return 0;
  306. esp_wifi_scan_get_ap_records(&num, ap_info);
  307. for (size_t i = 0; i < num; i++)
  308. {
  309. memcpy(tmp[i].ssid, ap_info[i].ssid, strlen((const char*)ap_info[i].ssid));
  310. memcpy(tmp[i].bssid, ap_info[i].bssid, 6);
  311. tmp[i].rssi = ap_info[i].rssi;
  312. }
  313. luat_heap_free(ap_info);
  314. return num;
  315. }
  316. int luat_wlan_smartconfig_start(int tp) {
  317. if (smartconfig_state != 0) {
  318. LLOGI("smartconfig is running");
  319. return 0;
  320. }
  321. switch (tp)
  322. {
  323. case LUAT_SC_TYPE_ESPTOUCH:
  324. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH);
  325. break;
  326. case LUAT_SC_TYPE_ESPTOUCH_AIRKISS:
  327. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS);
  328. break;
  329. case LUAT_SC_TYPE_AIRKISS:
  330. esp_smartconfig_set_type(SC_TYPE_AIRKISS);
  331. break;
  332. case LUAT_SC_TYPE_ESPTOUCH_V2:
  333. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_V2);
  334. break;
  335. default:
  336. esp_smartconfig_set_type(SC_TYPE_ESPTOUCH);
  337. break;
  338. }
  339. if (sc_evt == NULL) {
  340. sc_evt = luat_heap_malloc(sizeof(smartconfig_event_got_ssid_pswd_t));
  341. }
  342. smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
  343. esp_smartconfig_start(&cfg);
  344. smartconfig_state = 1;
  345. return 0;
  346. }
  347. int luat_wlan_smartconfig_stop(void) {
  348. esp_smartconfig_stop();
  349. smartconfig_state = 0;
  350. return 0;
  351. }
  352. int luat_wlan_get_mac(int id, char* mac) {
  353. if (id >= 0 && id <= ESP_MAC_IEEE802154) {
  354. esp_read_mac((uint8_t*)mac, id);
  355. return 0;
  356. }
  357. else {
  358. LLOGW("no such mac id %d", id);
  359. return -1;
  360. }
  361. }
  362. int luat_wlan_set_mac(int id, char* mac) {
  363. if (id >= 0 && id <= ESP_MAC_IEEE802154) {
  364. esp_base_mac_addr_set((uint8_t*)mac);
  365. return 0;
  366. }
  367. else {
  368. LLOGW("no such mac id %d", id);
  369. return -1;
  370. }
  371. }
  372. static uint8_t ap_stack_inited = 0;
  373. int luat_wlan_ap_start(luat_wlan_apinfo_t *apinfo) {
  374. wifi_mode_t mode = 0;
  375. int ret = 0;
  376. wifi_config_t cfg = {0};
  377. cfg.ap.channel = 1;
  378. cfg.ap.max_connection = 5;
  379. memcpy(cfg.ap.ssid, apinfo->ssid, strlen(apinfo->ssid));
  380. cfg.ap.ssid_len = strlen(apinfo->ssid);
  381. if (strlen(apinfo->password) >= 6) {
  382. memcpy(cfg.ap.password, apinfo->password, strlen(apinfo->password));
  383. cfg.ap.authmode = WIFI_AUTH_WPA2_PSK;
  384. }
  385. else {
  386. cfg.ap.authmode = WIFI_AUTH_OPEN;
  387. }
  388. LLOGD("softap %s %s", apinfo->ssid, apinfo->password);
  389. ret = esp_wifi_stop();
  390. LLOGD("esp_wifi_stop ret %d", ret);
  391. if (ap_stack_inited == 0) {
  392. ap_stack_inited = 1;
  393. esp_netif_create_default_wifi_ap();
  394. }
  395. ret = esp_wifi_get_mode(&mode);
  396. if (mode == WIFI_MODE_NULL || mode == WIFI_MODE_STA) {
  397. LLOGI("auto set to APSTA mode");
  398. ret = esp_wifi_set_mode(WIFI_MODE_APSTA);
  399. LLOGD("esp_wifi_set_mode ret %d", ret);
  400. }
  401. ret = esp_wifi_set_config(WIFI_IF_AP, &cfg);
  402. LLOGD("esp_wifi_set_config ret %d", ret);
  403. ret = esp_wifi_start();
  404. LLOGD("esp_wifi_start ret %d", ret);
  405. return 0;
  406. }
  407. int luat_wlan_get_ip(int type, char* data) {
  408. memcpy(data, sta_ip, strlen(sta_ip) + 1);
  409. return 0;
  410. }
  411. int luat_wlan_set_ps(int mode) {
  412. if (mode == 0) {
  413. esp_wifi_set_ps(WIFI_PS_NONE);
  414. }
  415. else if (mode == 1) {
  416. esp_wifi_set_ps(WIFI_PS_MIN_MODEM);
  417. }
  418. else if (mode == 2) {
  419. esp_wifi_set_ps(WIFI_PS_MAX_MODEM);
  420. }
  421. else {
  422. LLOGE("unkown wifi ps mode %d", mode);
  423. return -1;
  424. }
  425. return 0;
  426. }
  427. int luat_wlan_get_ps(void) {
  428. wifi_ps_type_t mode = WIFI_PS_NONE;
  429. esp_wifi_get_ps(&mode);
  430. return mode;
  431. }
  432. int luat_wlan_get_ap_bssid(char* buff) {
  433. memcpy(buff, sta_connected_bssid, 6);
  434. return 0;
  435. }
  436. int luat_wlan_get_ap_rssi(void) {
  437. wifi_ap_record_t ap;
  438. esp_wifi_sta_get_ap_info(&ap);
  439. return ap.rssi;
  440. }
  441. int luat_wlan_get_ap_gateway(char* buff) {
  442. memcpy(buff, sta_gw, strlen(sta_gw) + 1);
  443. return 0;
  444. }