wlan_dev.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. * 2014-09-11 Bernard the first verion
  27. */
  28. #include <rtthread.h>
  29. #include <rtdevice.h>
  30. #include "wlan_dev.h"
  31. #include "wlan_cmd.h"
  32. #define NIOCTL_SADDR 0x02
  33. void rt_wlan_info_init(struct rt_wlan_info *info, rt_wlan_mode_t mode, rt_wlan_security_t security,
  34. char *ssid)
  35. {
  36. if (info == RT_NULL) return ;
  37. memset(info, 0x0, sizeof(struct rt_wlan_info));
  38. info->mode = mode;
  39. info->security = security;
  40. if (ssid)
  41. {
  42. info->ssid = rt_malloc(strlen((char *)ssid) + 1);
  43. if (info->ssid)
  44. {
  45. strncpy((char *)info->ssid, (char *)ssid, strlen((char *)ssid) + 1);
  46. }
  47. }
  48. }
  49. void rt_wlan_info_deinit(struct rt_wlan_info *info)
  50. {
  51. if (info->ssid)
  52. {
  53. rt_free(info->ssid);
  54. info->ssid = RT_NULL;
  55. }
  56. memset(info, 0x0, sizeof(struct rt_wlan_info));
  57. }
  58. int rt_wlan_init(struct rt_wlan_device *device, rt_wlan_mode_t mode)
  59. {
  60. int result;
  61. if (device == RT_NULL) return 0;
  62. if (device->info == RT_NULL)
  63. {
  64. struct rt_wlan_info *info;
  65. char *ssid;
  66. info = rt_malloc(sizeof(struct rt_wlan_info));
  67. if (info)
  68. {
  69. ssid = rt_malloc(SSID_LENGTH_MAX_SIZE);
  70. info->ssid = ssid;
  71. }
  72. device->info = info;
  73. }
  74. result = rt_device_control(RT_DEVICE(device), WIFI_INIT, (void *)&mode);
  75. return result;
  76. }
  77. int rt_wlan_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, char *password)
  78. {
  79. int result = 0;
  80. if (device == RT_NULL) return -RT_EIO;
  81. if (info != RT_NULL)
  82. {
  83. rt_wlan_set_info(device, info);
  84. }
  85. rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1);
  86. result = rt_device_control(RT_DEVICE(device), WIFI_EASYJOIN, (void *)password);
  87. return result;
  88. }
  89. int rt_wlan_softap(struct rt_wlan_device *device, struct rt_wlan_info *info, char *password)
  90. {
  91. int result = RT_EOK;
  92. if (device == RT_NULL) return -RT_EIO;
  93. if (info != RT_NULL)
  94. {
  95. rt_wlan_set_info(device, info);
  96. }
  97. rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1);
  98. result = rt_device_control(RT_DEVICE(device), WIFI_SOFTAP, (void *)password);
  99. return result;
  100. }
  101. int rt_wlan_disconnect(struct rt_wlan_device *device)
  102. {
  103. int result = 0;
  104. if (device == RT_NULL) return -RT_EIO;
  105. /* save event handler */
  106. result = rt_device_control(RT_DEVICE(device), WIFI_DISCONNECT, RT_NULL);
  107. return result;
  108. }
  109. int rt_wlan_set_info(struct rt_wlan_device *device, struct rt_wlan_info *info)
  110. {
  111. if (device == RT_NULL) return -RT_EIO;
  112. if (device->info == RT_NULL) return -RT_EIO;
  113. device->info->mode = info->mode;
  114. device->info->security = info->security;
  115. memset(device->info->ssid, 0, SSID_LENGTH_MAX_SIZE);
  116. memcpy(device->info->ssid, info->ssid, strlen(info->ssid));
  117. memcpy(device->info->bssid, info->bssid, 6);
  118. device->info->datarate = info->datarate;
  119. device->info->channel = info->channel;
  120. device->info->rssi = info->rssi;
  121. return RT_EOK;
  122. }
  123. struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device *device)
  124. {
  125. struct rt_wlan_info *info = RT_NULL;
  126. if (device != RT_NULL)
  127. {
  128. info = device->info;
  129. }
  130. return info;
  131. }
  132. int rt_wlan_scan(struct rt_wlan_device *device, struct rt_wlan_scan_result **scan_result)
  133. {
  134. int result;
  135. result = rt_device_control(RT_DEVICE(device), WIFI_SCAN, scan_result);
  136. return result;
  137. }
  138. int rt_wlan_get_rssi(struct rt_wlan_device *device)
  139. {
  140. int rssi;
  141. int result;
  142. if (device == RT_NULL) return 0;
  143. result = rt_device_control(RT_DEVICE(device), WIFI_GET_RSSI, (void *)&rssi);
  144. if (result == RT_EOK) return rssi;
  145. return result;
  146. }
  147. int rt_wlan_get_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6])
  148. {
  149. int result;
  150. if (device == RT_NULL) return 0;
  151. result = rt_device_control(RT_DEVICE(device), NIOCTL_GADDR, (void *)hwaddr);
  152. return result;
  153. }
  154. int rt_wlan_set_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6])
  155. {
  156. int result;
  157. if (device == RT_NULL) return 0;
  158. result = rt_device_control(RT_DEVICE(device), NIOCTL_SADDR, (void *)hwaddr);
  159. return result;
  160. }
  161. int rt_wlan_enter_powersave(struct rt_wlan_device *device, int level)
  162. {
  163. int result = 0;
  164. if (device == RT_NULL) return -RT_EIO;
  165. result = rt_device_control(RT_DEVICE(device), WIFI_ENTER_POWERSAVE, (void *)&level);
  166. return result;
  167. }
  168. int rt_wlan_register_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event,
  169. rt_wlan_event_handler handler)
  170. {
  171. if (device == RT_NULL) return -RT_EIO;
  172. if (event >= WIFI_EVT_MAX) return -RT_EINVAL;
  173. device->handler[event] = handler;
  174. return RT_EOK;
  175. }
  176. int rt_wlan_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event)
  177. {
  178. if (device == RT_NULL) return -RT_EIO;
  179. if (event >= WIFI_EVT_MAX) return -RT_EINVAL;
  180. device->handler[event] = RT_NULL;
  181. return RT_EOK;
  182. }
  183. int rt_wlan_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  184. {
  185. if (device == RT_NULL) return -RT_EIO;
  186. if (event >= WIFI_EVT_MAX) return -RT_EINVAL;
  187. if (device->handler[event] != RT_NULL)
  188. device->handler[event](device, event, user_data);
  189. return RT_EOK;
  190. }
  191. int rt_wlan_cfg_monitor(struct rt_wlan_device *device, rt_wlan_monitor_opition_t opition)
  192. {
  193. int result = 0;
  194. if (device == RT_NULL) return -RT_EIO;
  195. result = rt_device_control(RT_DEVICE(device), WIFI_CFG_MONITOR, (void *)&opition);
  196. return result;
  197. }
  198. int rt_wlan_set_monitor_callback(struct rt_wlan_device *device, rt_wlan_monitor_callback_t callback)
  199. {
  200. int result = 0;
  201. if (device == RT_NULL) return -RT_EIO;
  202. result = rt_device_control(RT_DEVICE(device), WIFI_SET_MONITOR_CALLBACK, (void *)callback);
  203. return result;
  204. }
  205. int rt_wlan_set_channel(struct rt_wlan_device *device, int channel)
  206. {
  207. int result = 0;
  208. if (device == RT_NULL) return -RT_EIO;
  209. result = rt_device_control(RT_DEVICE(device), WIFI_SET_CHANNEL, (void *)&channel);
  210. return result;
  211. }
  212. int rt_wlan_get_channel(struct rt_wlan_device *device)
  213. {
  214. int channel = 0;
  215. if (device == RT_NULL) return -RT_EIO;
  216. rt_device_control(RT_DEVICE(device), WIFI_GET_CHANNEL, &channel);
  217. return channel;
  218. }
  219. void rt_wlan_release_scan_result(struct rt_wlan_scan_result **scan_result)
  220. {
  221. int i, ap_num;
  222. struct rt_wlan_scan_result *_scan_result;
  223. if (*scan_result != RT_NULL)
  224. {
  225. _scan_result = *scan_result;
  226. ap_num = _scan_result->ap_num;
  227. for (i = 0; i < ap_num; i++)
  228. {
  229. if (_scan_result->ap_table[i].ssid != RT_NULL)
  230. {
  231. rt_free(_scan_result->ap_table[i].ssid);
  232. _scan_result->ap_table[i].ssid = RT_NULL;
  233. }
  234. }
  235. _scan_result->ap_num = 0;
  236. rt_free(_scan_result->ap_table);
  237. _scan_result->ap_table = RT_NULL;
  238. }
  239. rt_free(*scan_result);
  240. *scan_result = RT_NULL;
  241. scan_result = RT_NULL;
  242. }