wlan_dev.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. if (password == NULL)
  98. {
  99. memset(device->key, 0, sizeof(device->key));
  100. }
  101. else
  102. {
  103. if (rt_strlen(password) > sizeof(device->key) - 1)
  104. {
  105. rt_kprintf("WARN input password is longer than %d bytes.", sizeof(device->key) - 1);
  106. }
  107. rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1);
  108. }
  109. result = rt_device_control(RT_DEVICE(device), WIFI_SOFTAP, (void *)password);
  110. return result;
  111. }
  112. int rt_wlan_disconnect(struct rt_wlan_device *device)
  113. {
  114. int result = 0;
  115. if (device == RT_NULL) return -RT_EIO;
  116. /* save event handler */
  117. result = rt_device_control(RT_DEVICE(device), WIFI_DISCONNECT, RT_NULL);
  118. return result;
  119. }
  120. int rt_wlan_set_info(struct rt_wlan_device *device, struct rt_wlan_info *info)
  121. {
  122. if (device == RT_NULL) return -RT_EIO;
  123. if (device->info == RT_NULL) return -RT_EIO;
  124. device->info->mode = info->mode;
  125. device->info->security = info->security;
  126. memset(device->info->ssid, 0, SSID_LENGTH_MAX_SIZE);
  127. memcpy(device->info->ssid, info->ssid, strlen(info->ssid));
  128. memcpy(device->info->bssid, info->bssid, 6);
  129. device->info->datarate = info->datarate;
  130. device->info->channel = info->channel;
  131. device->info->rssi = info->rssi;
  132. return RT_EOK;
  133. }
  134. struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device *device)
  135. {
  136. struct rt_wlan_info *info = RT_NULL;
  137. if (device != RT_NULL)
  138. {
  139. info = device->info;
  140. }
  141. return info;
  142. }
  143. int rt_wlan_scan(struct rt_wlan_device *device, struct rt_wlan_scan_result **scan_result)
  144. {
  145. int result;
  146. result = rt_device_control(RT_DEVICE(device), WIFI_SCAN, scan_result);
  147. return result;
  148. }
  149. int rt_wlan_get_rssi(struct rt_wlan_device *device)
  150. {
  151. int rssi;
  152. int result;
  153. if (device == RT_NULL) return 0;
  154. result = rt_device_control(RT_DEVICE(device), WIFI_GET_RSSI, (void *)&rssi);
  155. if (result == RT_EOK) return rssi;
  156. return result;
  157. }
  158. int rt_wlan_get_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6])
  159. {
  160. int result;
  161. if (device == RT_NULL) return 0;
  162. result = rt_device_control(RT_DEVICE(device), NIOCTL_GADDR, (void *)hwaddr);
  163. return result;
  164. }
  165. int rt_wlan_set_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6])
  166. {
  167. int result;
  168. if (device == RT_NULL) return 0;
  169. result = rt_device_control(RT_DEVICE(device), NIOCTL_SADDR, (void *)hwaddr);
  170. return result;
  171. }
  172. int rt_wlan_enter_powersave(struct rt_wlan_device *device, int level)
  173. {
  174. int result = 0;
  175. if (device == RT_NULL) return -RT_EIO;
  176. result = rt_device_control(RT_DEVICE(device), WIFI_ENTER_POWERSAVE, (void *)&level);
  177. return result;
  178. }
  179. int rt_wlan_register_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event,
  180. rt_wlan_event_handler handler)
  181. {
  182. if (device == RT_NULL) return -RT_EIO;
  183. if (event >= WIFI_EVT_MAX) return -RT_EINVAL;
  184. device->handler[event] = handler;
  185. return RT_EOK;
  186. }
  187. int rt_wlan_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event)
  188. {
  189. if (device == RT_NULL) return -RT_EIO;
  190. if (event >= WIFI_EVT_MAX) return -RT_EINVAL;
  191. device->handler[event] = RT_NULL;
  192. return RT_EOK;
  193. }
  194. int rt_wlan_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
  195. {
  196. if (device == RT_NULL) return -RT_EIO;
  197. if (event >= WIFI_EVT_MAX) return -RT_EINVAL;
  198. if (device->handler[event] != RT_NULL)
  199. device->handler[event](device, event, user_data);
  200. return RT_EOK;
  201. }
  202. int rt_wlan_cfg_monitor(struct rt_wlan_device *device, rt_wlan_monitor_opition_t opition)
  203. {
  204. int result = 0;
  205. if (device == RT_NULL) return -RT_EIO;
  206. result = rt_device_control(RT_DEVICE(device), WIFI_CFG_MONITOR, (void *)&opition);
  207. return result;
  208. }
  209. int rt_wlan_set_monitor_callback(struct rt_wlan_device *device, rt_wlan_monitor_callback_t callback)
  210. {
  211. int result = 0;
  212. if (device == RT_NULL) return -RT_EIO;
  213. result = rt_device_control(RT_DEVICE(device), WIFI_SET_MONITOR_CALLBACK, (void *)callback);
  214. return result;
  215. }
  216. int rt_wlan_set_channel(struct rt_wlan_device *device, int channel)
  217. {
  218. int result = 0;
  219. if (device == RT_NULL) return -RT_EIO;
  220. result = rt_device_control(RT_DEVICE(device), WIFI_SET_CHANNEL, (void *)&channel);
  221. return result;
  222. }
  223. int rt_wlan_get_channel(struct rt_wlan_device *device)
  224. {
  225. int channel = 0;
  226. if (device == RT_NULL) return -RT_EIO;
  227. rt_device_control(RT_DEVICE(device), WIFI_GET_CHANNEL, &channel);
  228. return channel;
  229. }
  230. void rt_wlan_release_scan_result(struct rt_wlan_scan_result **scan_result)
  231. {
  232. int i, ap_num;
  233. struct rt_wlan_scan_result *_scan_result;
  234. if (*scan_result != RT_NULL)
  235. {
  236. _scan_result = *scan_result;
  237. ap_num = _scan_result->ap_num;
  238. for (i = 0; i < ap_num; i++)
  239. {
  240. if (_scan_result->ap_table[i].ssid != RT_NULL)
  241. {
  242. rt_free(_scan_result->ap_table[i].ssid);
  243. _scan_result->ap_table[i].ssid = RT_NULL;
  244. }
  245. }
  246. _scan_result->ap_num = 0;
  247. rt_free(_scan_result->ap_table);
  248. _scan_result->ap_table = RT_NULL;
  249. }
  250. rt_free(*scan_result);
  251. *scan_result = RT_NULL;
  252. scan_result = RT_NULL;
  253. }