luat_wlan_idf5.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "luat_base.h"
  2. #include "luat_msgbus.h"
  3. #include "luat_wlan.h"
  4. #include "esp_attr.h"
  5. #include "esp_netif.h"
  6. #include "esp_event.h"
  7. #include "esp_system.h"
  8. #include "esp_wifi.h"
  9. #include "esp_wifi_types.h"
  10. #include "esp_smartconfig.h"
  11. #define LUAT_LOG_TAG "wlan"
  12. #include "luat_log.h"
  13. static uint8_t wlan_inited = 0;
  14. static uint8_t wlan_is_ready = 0;
  15. static char sta_ip[32];
  16. static int l_wlan_handler(lua_State *L, void* ptr) {
  17. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  18. int32_t event_id = msg->arg1;
  19. esp_netif_ip_info_t ip_info;
  20. lua_getglobal(L, "sys_pub");
  21. if (msg->arg2 == 0) {
  22. switch (event_id)
  23. {
  24. case WIFI_EVENT_WIFI_READY:
  25. LLOGD("wifi protocol is ready");
  26. lua_getglobal(L, "sys_pub");
  27. lua_pushstring(L, "WLAN_READY");
  28. lua_pushinteger(L, 1);
  29. //esp_netif_get_ip_info(ESP_IF_WIFI_STA,&ip_info);
  30. lua_call(L, 2, 0);
  31. break;
  32. case WIFI_EVENT_STA_CONNECTED:
  33. LLOGD("wifi connected!!!");
  34. lua_pushstring(L, "WLAN_STA_CONNECTED");
  35. lua_call(L, 1, 0);
  36. break;
  37. case WIFI_EVENT_STA_DISCONNECTED:
  38. LLOGD("wifi disconnected!!!");
  39. lua_pushstring(L, "WLAN_STA_DISCONNECTED");
  40. lua_call(L, 1, 0);
  41. lua_getglobal(L, "sys_pub");
  42. lua_pushstring(L, "WLAN_READY");
  43. lua_pushinteger(L, 0);
  44. lua_call(L, 2, 0);
  45. break;
  46. case WIFI_EVENT_STA_START:
  47. //LLOGD("wifi station start");
  48. break;
  49. case WIFI_EVENT_STA_STOP:
  50. //LLOGD("wifi station stop");
  51. break;
  52. case WIFI_EVENT_SCAN_DONE:
  53. LLOGD("wifi scan done");
  54. break;
  55. default:
  56. LLOGI("unkown event %d", event_id);
  57. break;
  58. }
  59. }
  60. else if (msg->arg2 == 1) {
  61. if (event_id == IP_EVENT_STA_GOT_IP) {
  62. LLOGD("IP_EVENT_STA_GOT_IP %s", sta_ip);
  63. lua_pushstring(L, "IP_READY");
  64. lua_pushstring(L, sta_ip);
  65. lua_call(L, 2, 0);
  66. }
  67. }
  68. return 0;
  69. }
  70. static void wifi_event_handler(void *arg, esp_event_base_t event_base,
  71. int32_t event_id, void *event_data) {
  72. rtos_msg_t msg = {0};
  73. msg.handler = l_wlan_handler;
  74. msg.arg1 = event_id;
  75. LLOGD("wifi event %d", event_id);
  76. if (event_id == WIFI_EVENT_STA_DISCONNECTED) {
  77. wlan_is_ready = 0;
  78. }
  79. luat_msgbus_put(&msg, 0);
  80. }
  81. static void ip_event_handler(void *arg, esp_event_base_t event_base,
  82. int32_t event_id, void *event_data) {
  83. rtos_msg_t msg = {0};
  84. msg.handler = l_wlan_handler;
  85. msg.arg1 = event_id;
  86. msg.arg2 = 1;
  87. ip_event_got_ip_t *event;
  88. LLOGD("ip event %d", event_id);
  89. if (event_id == IP_EVENT_STA_GOT_IP) {
  90. wlan_is_ready = 1;
  91. event = (ip_event_got_ip_t*)event_data;
  92. sprintf(sta_ip, IPSTR, IP2STR(&event->ip_info.ip));
  93. }
  94. luat_msgbus_put(&msg, 0);
  95. }
  96. int luat_wlan_init(luat_wlan_config_t *conf) {
  97. if (wlan_inited != 0) {
  98. LLOGI("wlan is ready!!");
  99. return 0;
  100. }
  101. esp_netif_init();
  102. esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL);
  103. esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL);
  104. esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL);
  105. esp_netif_create_default_wifi_sta();
  106. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  107. cfg.static_rx_buf_num = 2;
  108. cfg.static_tx_buf_num = 2;
  109. int ret = esp_wifi_init(&cfg);
  110. esp_wifi_set_mode(WIFI_MODE_STA);
  111. LLOGD("esp_wifi_init ret %d", ret);
  112. ret = esp_wifi_start();
  113. LLOGD("esp_wifi_start ret %d", ret);
  114. wlan_inited = 1;
  115. return 0;
  116. }
  117. // 设置wifi模式
  118. int luat_wlan_mode(luat_wlan_config_t *conf) {
  119. switch (conf->mode)
  120. {
  121. case LUAT_WLAN_MODE_NULL:
  122. esp_wifi_set_mode(WIFI_MODE_NULL);
  123. break;
  124. case LUAT_WLAN_MODE_STA:
  125. esp_wifi_set_mode(WIFI_MODE_STA);
  126. break;
  127. case LUAT_WLAN_MODE_AP:
  128. esp_wifi_set_mode(WIFI_MODE_AP);
  129. break;
  130. case LUAT_WLAN_MODE_APSTA:
  131. esp_wifi_set_mode(WIFI_MODE_APSTA);
  132. break;
  133. }
  134. return 0;
  135. }
  136. // 是否已经连上wifi
  137. int luat_wlan_ready(void) {
  138. return wlan_is_ready;
  139. }
  140. int luat_wlan_connect(luat_wlan_conninfo_t* info) {
  141. int ret = 0;
  142. wifi_config_t cfg = {0};
  143. // LLOGD("connect %s %s", info->ssid, info->password);
  144. memcpy(cfg.sta.ssid, info->ssid, strlen(info->ssid));
  145. memcpy(cfg.sta.password, info->password, strlen(info->password));
  146. esp_wifi_set_config(WIFI_IF_STA, &cfg);
  147. ret = esp_wifi_connect();
  148. LLOGD("esp_wifi_connect ret %d", ret);
  149. return 0;
  150. }
  151. int luat_wlan_disconnect(void) {
  152. int ret = 0;
  153. ret = esp_wifi_disconnect();
  154. LLOGD("esp_wifi_disconnect ret %d", ret);
  155. return 0;
  156. }
  157. int luat_wlan_scan(void) {
  158. static const wifi_scan_config_t conf = {0};
  159. return esp_wifi_scan_start(&conf, false);
  160. }