wlan_dev.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * RT-Thread Wi-Fi Device
  3. *
  4. * COPYRIGHT (C) 2014 - 2015, 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. * 2014-09-11 Bernard 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 NIOCTL_SADDR 0x02
  34. void rt_wlan_info_init(struct rt_wlan_info* info, rt_wlan_mode_t mode, rt_wlan_security_t security,
  35. char *ssid)
  36. {
  37. if (info == RT_NULL) return ;
  38. memset(info, 0x0, sizeof(struct rt_wlan_info));
  39. info->mode = mode;
  40. info->security = security;
  41. if (ssid)
  42. {
  43. info->ssid = rt_malloc(strlen((char*)ssid) + 1);
  44. if (info->ssid)
  45. {
  46. strncpy((char*)info->ssid, (char*)ssid, strlen((char*)ssid) + 1);
  47. }
  48. }
  49. }
  50. void rt_wlan_info_deinit(struct rt_wlan_info* info)
  51. {
  52. if (info->ssid)
  53. {
  54. rt_free(info->ssid);
  55. info->ssid = RT_NULL;
  56. }
  57. memset(info, 0x0, sizeof(struct rt_wlan_info));
  58. }
  59. int rt_wlan_init(struct rt_wlan_device* device, rt_wlan_mode_t mode)
  60. {
  61. int result;
  62. if (device == RT_NULL) return 0;
  63. result = rt_device_control(RT_DEVICE(device), WIFI_INIT, (void*)&mode);
  64. return result;
  65. }
  66. int rt_wlan_connect(struct rt_wlan_device* device, struct rt_wlan_info* info, char *password)
  67. {
  68. int result = 0;
  69. if (device == RT_NULL) return -RT_EIO;
  70. if (info != RT_NULL)
  71. {
  72. rt_wlan_set_info(device, info);
  73. }
  74. result = rt_device_control(RT_DEVICE(device), WIFI_EASYJOIN, (void*)password);
  75. if (result == RT_EOK)
  76. {
  77. struct netif *netif = device->parent.netif;
  78. netifapi_netif_set_up(netif);
  79. eth_device_linkchange(&(device->parent), RT_TRUE);
  80. #ifdef RT_LWIP_DHCP
  81. /* set DHCP flags */
  82. // netif->flags |= NETIF_FLAG_DHCP;
  83. /* start DHCP */
  84. dhcp_start(netif);
  85. #endif
  86. rt_strncpy((char*)device->key, (char*)password, sizeof(device->key) - 1);
  87. }
  88. return result;
  89. }
  90. int rt_wlan_softap(struct rt_wlan_device* device, struct rt_wlan_info* info, char *password)
  91. {
  92. int result = RT_EOK;
  93. if (device == RT_NULL) return -RT_EIO;
  94. if (info != RT_NULL)
  95. {
  96. rt_wlan_set_info(device, info);
  97. }
  98. result = rt_device_control(RT_DEVICE(device), WIFI_SOFTAP, (void*)password);
  99. if (result == RT_EOK)
  100. {
  101. rt_strncpy((char*)device->key, (char*)password, sizeof(device->key) - 1);
  102. netifapi_netif_set_up(device->parent.netif);
  103. eth_device_linkchange(&(device->parent), RT_TRUE);
  104. wifi_softap_setup_netif(device->parent.netif);
  105. }
  106. return result;
  107. }
  108. int rt_wlan_disconnect(struct rt_wlan_device* device)
  109. {
  110. int result = 0;
  111. if (device == RT_NULL) return -RT_EIO;
  112. /* save event handler */
  113. result = rt_device_control(RT_DEVICE(device), WIFI_DISCONNECT, RT_NULL);
  114. if (result == RT_EOK)
  115. {
  116. netifapi_netif_set_down(device->parent.netif);
  117. eth_device_linkchange(&(device->parent), RT_FALSE);
  118. }
  119. return result;
  120. }
  121. int rt_wlan_set_info(struct rt_wlan_device* device, struct rt_wlan_info* info)
  122. {
  123. if (device->info == info) return RT_EOK; /* same info */
  124. if (device->info != RT_NULL)
  125. {
  126. rt_wlan_info_deinit(device->info);
  127. rt_free(device->info);
  128. }
  129. device->info = info;
  130. return RT_EOK;
  131. }
  132. struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device* device)
  133. {
  134. struct rt_wlan_info* info = RT_NULL;
  135. if (device != RT_NULL)
  136. {
  137. info = device->info;
  138. }
  139. return info;
  140. }
  141. int rt_wlan_scan(struct rt_wlan_device* device, struct rt_wlan_info *infos, int item_sz)
  142. {
  143. int result;
  144. struct rt_wlan_info_request request;
  145. if (device == RT_NULL) return 0;
  146. request.req_number = item_sz;
  147. request.rsp_number = 0;
  148. request.infos = infos;
  149. result = rt_device_control(RT_DEVICE(device), WIFI_SCAN, (void*)&request);
  150. result = result; /* skip warning */
  151. return request.rsp_number;
  152. }
  153. int rt_wlan_get_rssi(struct rt_wlan_device* device)
  154. {
  155. int rssi;
  156. int result;
  157. if (device == RT_NULL) return 0;
  158. result = rt_device_control(RT_DEVICE(device), WIFI_GET_RSSI, (void*)&rssi);
  159. if (result == RT_EOK) return rssi;
  160. return result;
  161. }
  162. int rt_wlan_get_mac(struct rt_wlan_device* device, rt_uint8_t hwaddr[6])
  163. {
  164. int result;
  165. if (device == RT_NULL) return 0;
  166. result = rt_device_control(RT_DEVICE(device), NIOCTL_GADDR, (void*)hwaddr);
  167. return result;
  168. }
  169. int rt_wlan_set_mac(struct rt_wlan_device* device, rt_uint8_t hwaddr[6])
  170. {
  171. int result;
  172. if (device == RT_NULL) return 0;
  173. result = rt_device_control(RT_DEVICE(device), NIOCTL_SADDR, (void*)hwaddr);
  174. return result;
  175. }
  176. int rt_wlan_enter_powersave(struct rt_wlan_device* device, int level)
  177. {
  178. int result = 0;
  179. if (device == RT_NULL) return -RT_EIO;
  180. result = rt_device_control(RT_DEVICE(device), WIFI_ENTER_POWERSAVE, (void*)&level);
  181. return result;
  182. }
  183. void rt_wlan_set_event_callback(struct rt_wlan_device* device, rt_wlan_event_handler handler,
  184. void *user_data)
  185. {
  186. if (device == RT_NULL) return ;
  187. device->handler = handler;
  188. device->user_data = user_data;
  189. return ;
  190. }