wlan_mgnt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * RT-Thread Wi-Fi Device
  3. *
  4. * COPYRIGHT (C) 2014 - 2018, Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * This file is part of RT-Thread (http://www.rt-thread.org)
  7. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. *
  24. * Change Logs:
  25. * Date Author Notes
  26. * 2018-02-27 EvalZero the first verion
  27. */
  28. #include <rtthread.h>
  29. #include <rtdevice.h>
  30. #include <lwip/netifapi.h>
  31. #include "wlan_dev.h"
  32. #include "wlan_cmd.h"
  33. #define WLAN_MGNT_DEBUG 1
  34. #if WLAN_MGNT_DEBUG
  35. #define WLAN_MGNT_DBG(...) rt_kprintf("[WLAN_MGNT]"),rt_kprintf(__VA_ARGS__)
  36. #else
  37. #define WLAN_MGNT_DBG(...)
  38. #endif
  39. #ifndef WIFI_DEVICE_STA_NAME
  40. #define WIFI_DEVICE_STA_NAME "w0"
  41. #endif
  42. #ifndef WIFI_DEVICE_AP_NAME
  43. #define WIFI_DEVICE_AP_NAME "ap"
  44. #endif
  45. static void wlan_mgnt_init_done_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  46. {
  47. WLAN_MGNT_DBG("wlan init done event callback \n");
  48. }
  49. static void wlan_mgnt_link_up_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  50. {
  51. WLAN_MGNT_DBG("wlan link up event callback \n");
  52. }
  53. static void wlan_mgnt_link_down_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  54. {
  55. WLAN_MGNT_DBG("wlan link down event callback \n");
  56. }
  57. static void wlan_mgnt_sta_connect_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  58. {
  59. WLAN_MGNT_DBG("wlan sta connect event callback \n");
  60. struct netif *netif = device->parent.netif;
  61. netifapi_netif_set_up(netif);
  62. netifapi_netif_set_link_up(netif);
  63. #ifdef RT_LWIP_DHCP
  64. /* start DHCP */
  65. dhcp_start(netif);
  66. #endif
  67. }
  68. static void wlan_mgnt_sta_disconnect_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  69. {
  70. WLAN_MGNT_DBG("wlan sta disconnect event callback \n");
  71. netifapi_netif_set_down(device->parent.netif);
  72. netifapi_netif_set_link_down(device->parent.netif);
  73. rt_memset(&device->parent.netif->ip_addr, 0, sizeof(ip_addr_t));
  74. rt_memset(&device->parent.netif->netmask, 0, sizeof(ip_addr_t));
  75. rt_memset(&device->parent.netif->gw, 0, sizeof(ip_addr_t));
  76. #ifdef RT_LWIP_DHCP
  77. dhcp_stop(device->parent.netif);
  78. #endif
  79. }
  80. static void wlan_mgnt_ap_start_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  81. {
  82. WLAN_MGNT_DBG("wlan ap start event callback \n");
  83. netifapi_netif_set_up(device->parent.netif);
  84. netifapi_netif_set_link_up(device->parent.netif);
  85. wifi_softap_setup_netif(device->parent.netif);
  86. }
  87. static void wlan_mgnt_ap_stop_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  88. {
  89. WLAN_MGNT_DBG("wlan ap stop event callback \n");
  90. netifapi_netif_set_down(device->parent.netif);
  91. netifapi_netif_set_link_down(device->parent.netif);
  92. }
  93. static void wlan_mgnt_ap_associate_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  94. {
  95. WLAN_MGNT_DBG("wlan ap associate event callback \n");
  96. }
  97. static void wlan_mgnt_ap_disassociate_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  98. {
  99. WLAN_MGNT_DBG("wlan ap disassociate event callback \n");
  100. }
  101. static void wlan_mgnt_scan_done_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  102. {
  103. WLAN_MGNT_DBG("wlan scan done event callback \n");
  104. }
  105. int rt_wlan_mgnt_attach(struct rt_wlan_device *device, void *user_data)
  106. {
  107. RT_ASSERT(device != RT_NULL);
  108. rt_wlan_register_event_handler(device, WIFI_EVT_INIT_DONE, wlan_mgnt_init_done_event);
  109. rt_wlan_register_event_handler(device, WIFI_EVT_LINK_DOWN, wlan_mgnt_link_up_event);
  110. rt_wlan_register_event_handler(device, WIFI_EVT_LINK_UP, wlan_mgnt_link_down_event);
  111. rt_wlan_register_event_handler(device, WIFI_EVT_CONNECT, wlan_mgnt_sta_connect_event);
  112. rt_wlan_register_event_handler(device, WIFI_EVT_DISCONNECT, wlan_mgnt_sta_disconnect_event);
  113. rt_wlan_register_event_handler(device, WIFI_EVT_AP_START, wlan_mgnt_ap_start_event);
  114. rt_wlan_register_event_handler(device, WIFI_EVT_AP_STOP, wlan_mgnt_ap_stop_event);
  115. rt_wlan_register_event_handler(device, WIFI_EVENT_STA_ASSOC, wlan_mgnt_ap_associate_event);
  116. rt_wlan_register_event_handler(device, WIFI_EVENT_STA_DISASSOC, wlan_mgnt_ap_disassociate_event);
  117. rt_wlan_register_event_handler(device, WIFI_EVT_SCAN_DONE, wlan_mgnt_scan_done_event);
  118. return RT_EOK;
  119. }