wlan_dev.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #ifndef WIFI_DEVICE_H__
  29. #define WIFI_DEVICE_H__
  30. #include <stdint.h>
  31. #include <netif/ethernetif.h>
  32. typedef enum
  33. {
  34. WIFI_NONE,
  35. WIFI_STATION,
  36. WIFI_AP,
  37. } rt_wlan_mode_t;
  38. typedef enum
  39. {
  40. WIFI_INIT = 0x10,
  41. WIFI_SCAN, /* trigger scanning (list cells) */
  42. WIFI_JOIN,
  43. WIFI_EASYJOIN, /* join network with less information */
  44. WIFI_SOFTAP, /* start soft-AP */
  45. WIFI_DISCONNECT,
  46. WIFI_GET_RSSI, /* get sensitivity (dBm) */
  47. WIFI_ENTER_POWERSAVE,
  48. WIFI_CFG_MONITOR, /* start/stop minitor */
  49. WIFI_SET_CHANNEL,
  50. WIFI_GET_CHANNEL,
  51. WIFI_SET_MONITOR_CALLBACK,
  52. } rt_wlan_cmd_t;
  53. typedef enum
  54. {
  55. WIFI_PWR_OFF,
  56. WIFI_PWR_SLEEP,
  57. WIFI_PWR_NORMAL
  58. } rt_wlan_powersave_t;
  59. typedef enum
  60. {
  61. WIFI_MONITOR_START,
  62. WIFI_MONITOR_STOP
  63. } rt_wlan_monitor_opition_t;
  64. #define SHARED_ENABLED 0x00008000
  65. #define WPA_SECURITY 0x00200000
  66. #define WPA2_SECURITY 0x00400000
  67. #define WPS_ENABLED 0x10000000
  68. #define WEP_ENABLED 0x0001
  69. #define TKIP_ENABLED 0x0002
  70. #define AES_ENABLED 0x0004
  71. #define WSEC_SWFLAG 0x0008
  72. #define KEY_ARRAY_SIZE 32
  73. #define SSID_LENGTH_MAX_SIZE 32 + 1
  74. /**
  75. * Enumeration of Wi-Fi security modes
  76. */
  77. typedef enum
  78. {
  79. SECURITY_OPEN = 0, /**< Open security */
  80. SECURITY_WEP_PSK = WEP_ENABLED, /**< WEP Security with open authentication */
  81. SECURITY_WEP_SHARED = (WEP_ENABLED | SHARED_ENABLED), /**< WEP Security with shared authentication */
  82. SECURITY_WPA_TKIP_PSK = (WPA_SECURITY | TKIP_ENABLED), /**< WPA Security with TKIP */
  83. SECURITY_WPA_AES_PSK = (WPA_SECURITY | AES_ENABLED), /**< WPA Security with AES */
  84. SECURITY_WPA2_AES_PSK = (WPA2_SECURITY | AES_ENABLED), /**< WPA2 Security with AES */
  85. SECURITY_WPA2_TKIP_PSK = (WPA2_SECURITY | TKIP_ENABLED), /**< WPA2 Security with TKIP */
  86. SECURITY_WPA2_MIXED_PSK = (WPA2_SECURITY | AES_ENABLED | TKIP_ENABLED), /**< WPA2 Security with AES & TKIP */
  87. SECURITY_WPS_OPEN = WPS_ENABLED, /**< WPS with open security */
  88. SECURITY_WPS_SECURE = (WPS_ENABLED | AES_ENABLED), /**< WPS with AES security */
  89. SECURITY_UNKNOWN = -1, /**< May be returned by scan function if security is unknown.
  90. Do not pass this to the join function! */
  91. } rt_wlan_security_t;
  92. typedef enum
  93. {
  94. WIFI_EVT_INIT_DONE = 0,
  95. WIFI_EVT_LINK_DOWN,
  96. WIFI_EVT_LINK_UP,
  97. WIFI_EVT_CONNECT,
  98. WIFI_EVT_DISCONNECT,
  99. WIFI_EVT_AP_START,
  100. WIFI_EVT_AP_STOP,
  101. WIFI_EVENT_STA_ASSOC,
  102. WIFI_EVENT_STA_DISASSOC,
  103. WIFI_EVT_SCAN_DONE,
  104. WIFI_EVT_MAX,
  105. } rt_wlan_event_t;
  106. /* wifi network information */
  107. struct rt_wlan_info
  108. {
  109. rt_wlan_mode_t mode; /* wifi mode */
  110. rt_wlan_security_t security;
  111. char *ssid;
  112. uint8_t bssid[6];
  113. /* maximal data rate */
  114. uint32_t datarate;
  115. /* radio channel */
  116. uint16_t channel;
  117. /* signal strength */
  118. int16_t rssi;
  119. };
  120. struct rt_wlan_info_request
  121. {
  122. uint16_t req_number; /* the number of information item for request */
  123. uint16_t rsp_number; /* the number of information item for response */
  124. struct rt_wlan_info *infos;/* the array of information to save response */
  125. };
  126. typedef struct rt_wlan_scan_result
  127. {
  128. char ap_num;
  129. struct rt_wlan_info *ap_table;
  130. } rt_wlan_scan_result_t;
  131. struct rt_wlan_device;
  132. typedef void (*rt_wlan_event_handler)(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data);
  133. typedef void (*rt_wlan_monitor_callback_t)(uint8_t *data, int len, void *user_data);
  134. struct rt_wlan_device
  135. {
  136. struct eth_device parent;
  137. struct rt_wlan_info *info;
  138. char key[KEY_ARRAY_SIZE + 1];
  139. rt_wlan_event_handler handler[WIFI_EVT_MAX];
  140. void *user_data;
  141. int interface;
  142. };
  143. /*
  144. * Wi-Fi Information APIs
  145. */
  146. void rt_wlan_info_init(struct rt_wlan_info *info, rt_wlan_mode_t mode, rt_wlan_security_t security,
  147. char *ssid);
  148. void rt_wlan_info_deinit(struct rt_wlan_info *info);
  149. /*
  150. * Wi-Fi Manager APIs
  151. */
  152. int rt_wlan_init(struct rt_wlan_device *device, rt_wlan_mode_t mode);
  153. int rt_wlan_connect(struct rt_wlan_device *device, struct rt_wlan_info *info,
  154. char *password);
  155. int rt_wlan_disconnect(struct rt_wlan_device *device);
  156. int rt_wlan_softap(struct rt_wlan_device *device, struct rt_wlan_info *info,
  157. char *password);
  158. /* set wifi information for AP */
  159. int rt_wlan_set_info(struct rt_wlan_device *device, struct rt_wlan_info *info);
  160. /* get wifi information for AP */
  161. struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device *device);
  162. /* get the AP result which were scaned in station */
  163. int rt_wlan_scan(struct rt_wlan_device *device, struct rt_wlan_scan_result **scan_result);
  164. /* get rssi */
  165. int rt_wlan_get_rssi(struct rt_wlan_device *device);
  166. /* Get/Set MAC */
  167. int rt_wlan_get_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6]);
  168. int rt_wlan_set_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6]);
  169. /* enter power save level */
  170. int rt_wlan_enter_powersave(struct rt_wlan_device *device, int level);
  171. /* register the event handler */
  172. int rt_wlan_register_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event,
  173. rt_wlan_event_handler handler);
  174. /* un-register the event handler */
  175. int rt_wlan_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event);
  176. /* wlan driver indicate event to upper layer through wifi_indication. */
  177. int rt_wlan_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_event_t event,
  178. void *user_data);
  179. /* start or stop monitor */
  180. int rt_wlan_cfg_monitor(struct rt_wlan_device *device, rt_wlan_monitor_opition_t opition);
  181. /* set callback function for monitor mode*/
  182. int rt_wlan_set_monitor_callback(struct rt_wlan_device *device, rt_wlan_monitor_callback_t callback);
  183. /* Set the monitor channel */
  184. int rt_wlan_set_channel(struct rt_wlan_device *device, int channel);
  185. void rt_wlan_release_scan_result(struct rt_wlan_scan_result **scan_result);
  186. #endif