|
@@ -1,7 +1,7 @@
|
|
|
/*
|
|
|
* RT-Thread Wi-Fi Device
|
|
|
*
|
|
|
- * COPYRIGHT (C) 2014 - 2015, Shanghai Real-Thread Technology Co., Ltd
|
|
|
+ * COPYRIGHT (C) 2014 - 2018, Shanghai Real-Thread Technology Co., Ltd
|
|
|
*
|
|
|
* This file is part of RT-Thread (http://www.rt-thread.org)
|
|
|
*
|
|
@@ -29,15 +29,13 @@
|
|
|
#include <rtthread.h>
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
-#include <lwip/netifapi.h>
|
|
|
-
|
|
|
#include "wlan_dev.h"
|
|
|
#include "wlan_cmd.h"
|
|
|
|
|
|
#define NIOCTL_SADDR 0x02
|
|
|
|
|
|
-void rt_wlan_info_init(struct rt_wlan_info* info, rt_wlan_mode_t mode, rt_wlan_security_t security,
|
|
|
- char *ssid)
|
|
|
+void rt_wlan_info_init(struct rt_wlan_info *info, rt_wlan_mode_t mode, rt_wlan_security_t security,
|
|
|
+ char *ssid)
|
|
|
{
|
|
|
if (info == RT_NULL) return ;
|
|
|
|
|
@@ -46,15 +44,15 @@ void rt_wlan_info_init(struct rt_wlan_info* info, rt_wlan_mode_t mode, rt_wlan_s
|
|
|
info->security = security;
|
|
|
if (ssid)
|
|
|
{
|
|
|
- info->ssid = rt_malloc(strlen((char*)ssid) + 1);
|
|
|
+ info->ssid = rt_malloc(strlen((char *)ssid) + 1);
|
|
|
if (info->ssid)
|
|
|
{
|
|
|
- strncpy((char*)info->ssid, (char*)ssid, strlen((char*)ssid) + 1);
|
|
|
+ strncpy((char *)info->ssid, (char *)ssid, strlen((char *)ssid) + 1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void rt_wlan_info_deinit(struct rt_wlan_info* info)
|
|
|
+void rt_wlan_info_deinit(struct rt_wlan_info *info)
|
|
|
{
|
|
|
if (info->ssid)
|
|
|
{
|
|
@@ -65,18 +63,32 @@ void rt_wlan_info_deinit(struct rt_wlan_info* info)
|
|
|
memset(info, 0x0, sizeof(struct rt_wlan_info));
|
|
|
}
|
|
|
|
|
|
-int rt_wlan_init(struct rt_wlan_device* device, rt_wlan_mode_t mode)
|
|
|
+int rt_wlan_init(struct rt_wlan_device *device, rt_wlan_mode_t mode)
|
|
|
{
|
|
|
int result;
|
|
|
|
|
|
if (device == RT_NULL) return 0;
|
|
|
|
|
|
- result = rt_device_control(RT_DEVICE(device), WIFI_INIT, (void*)&mode);
|
|
|
+ if (device->info == RT_NULL)
|
|
|
+ {
|
|
|
+ struct rt_wlan_info *info;
|
|
|
+ char *ssid;
|
|
|
+
|
|
|
+ info = rt_malloc(sizeof(struct rt_wlan_info));
|
|
|
+ if (info)
|
|
|
+ {
|
|
|
+ ssid = rt_malloc(SSID_LENGTH_MAX_SIZE);
|
|
|
+ info->ssid = ssid;
|
|
|
+ }
|
|
|
+ device->info = info;
|
|
|
+ }
|
|
|
+
|
|
|
+ result = rt_device_control(RT_DEVICE(device), WIFI_INIT, (void *)&mode);
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-int rt_wlan_connect(struct rt_wlan_device* device, struct rt_wlan_info* info, char *password)
|
|
|
+int rt_wlan_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, char *password)
|
|
|
{
|
|
|
int result = 0;
|
|
|
|
|
@@ -87,28 +99,14 @@ int rt_wlan_connect(struct rt_wlan_device* device, struct rt_wlan_info* info, ch
|
|
|
rt_wlan_set_info(device, info);
|
|
|
}
|
|
|
|
|
|
- result = rt_device_control(RT_DEVICE(device), WIFI_EASYJOIN, (void*)password);
|
|
|
- if (result == RT_EOK)
|
|
|
- {
|
|
|
- struct netif *netif = device->parent.netif;
|
|
|
-
|
|
|
- netifapi_netif_set_up(netif);
|
|
|
- eth_device_linkchange(&(device->parent), RT_TRUE);
|
|
|
+ rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1);
|
|
|
|
|
|
-#ifdef RT_LWIP_DHCP
|
|
|
- /* set DHCP flags */
|
|
|
- // netif->flags |= NETIF_FLAG_DHCP;
|
|
|
- /* start DHCP */
|
|
|
- dhcp_start(netif);
|
|
|
-#endif
|
|
|
-
|
|
|
- rt_strncpy((char*)device->key, (char*)password, sizeof(device->key) - 1);
|
|
|
- }
|
|
|
+ result = rt_device_control(RT_DEVICE(device), WIFI_EASYJOIN, (void *)password);
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-int rt_wlan_softap(struct rt_wlan_device* device, struct rt_wlan_info* info, char *password)
|
|
|
+int rt_wlan_softap(struct rt_wlan_device *device, struct rt_wlan_info *info, char *password)
|
|
|
{
|
|
|
int result = RT_EOK;
|
|
|
|
|
@@ -119,21 +117,14 @@ int rt_wlan_softap(struct rt_wlan_device* device, struct rt_wlan_info* info, cha
|
|
|
rt_wlan_set_info(device, info);
|
|
|
}
|
|
|
|
|
|
- result = rt_device_control(RT_DEVICE(device), WIFI_SOFTAP, (void*)password);
|
|
|
- if (result == RT_EOK)
|
|
|
- {
|
|
|
- rt_strncpy((char*)device->key, (char*)password, sizeof(device->key) - 1);
|
|
|
-
|
|
|
- netifapi_netif_set_up(device->parent.netif);
|
|
|
- eth_device_linkchange(&(device->parent), RT_TRUE);
|
|
|
+ rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1);
|
|
|
|
|
|
- wifi_softap_setup_netif(device->parent.netif);
|
|
|
- }
|
|
|
+ result = rt_device_control(RT_DEVICE(device), WIFI_SOFTAP, (void *)password);
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-int rt_wlan_disconnect(struct rt_wlan_device* device)
|
|
|
+int rt_wlan_disconnect(struct rt_wlan_device *device)
|
|
|
{
|
|
|
int result = 0;
|
|
|
|
|
@@ -141,33 +132,30 @@ int rt_wlan_disconnect(struct rt_wlan_device* device)
|
|
|
|
|
|
/* save event handler */
|
|
|
result = rt_device_control(RT_DEVICE(device), WIFI_DISCONNECT, RT_NULL);
|
|
|
- if (result == RT_EOK)
|
|
|
- {
|
|
|
- netifapi_netif_set_down(device->parent.netif);
|
|
|
- eth_device_linkchange(&(device->parent), RT_FALSE);
|
|
|
- }
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-int rt_wlan_set_info(struct rt_wlan_device* device, struct rt_wlan_info* info)
|
|
|
+int rt_wlan_set_info(struct rt_wlan_device *device, struct rt_wlan_info *info)
|
|
|
{
|
|
|
- if (device->info == info) return RT_EOK; /* same info */
|
|
|
-
|
|
|
- if (device->info != RT_NULL)
|
|
|
- {
|
|
|
- rt_wlan_info_deinit(device->info);
|
|
|
- rt_free(device->info);
|
|
|
- }
|
|
|
+ if (device == RT_NULL) return -RT_EIO;
|
|
|
+ if (device->info == RT_NULL) return -RT_EIO;
|
|
|
|
|
|
- device->info = info;
|
|
|
+ device->info->mode = info->mode;
|
|
|
+ device->info->security = info->security;
|
|
|
+ memset(device->info->ssid, 0, SSID_LENGTH_MAX_SIZE);
|
|
|
+ memcpy(device->info->ssid, info->ssid, strlen(info->ssid));
|
|
|
+ memcpy(device->info->bssid, info->bssid, 6);
|
|
|
+ device->info->datarate = info->datarate;
|
|
|
+ device->info->channel = info->channel;
|
|
|
+ device->info->rssi = info->rssi;
|
|
|
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
|
|
|
-struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device* device)
|
|
|
+struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device *device)
|
|
|
{
|
|
|
- struct rt_wlan_info* info = RT_NULL;
|
|
|
+ struct rt_wlan_info *info = RT_NULL;
|
|
|
|
|
|
if (device != RT_NULL)
|
|
|
{
|
|
@@ -177,70 +165,153 @@ struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device* device)
|
|
|
return info;
|
|
|
}
|
|
|
|
|
|
-int rt_wlan_scan(struct rt_wlan_device* device, struct rt_wlan_info *infos, int item_sz)
|
|
|
+int rt_wlan_scan(struct rt_wlan_device *device, struct rt_wlan_scan_result **scan_result)
|
|
|
{
|
|
|
int result;
|
|
|
- struct rt_wlan_info_request request;
|
|
|
|
|
|
- if (device == RT_NULL) return 0;
|
|
|
+ result = rt_device_control(RT_DEVICE(device), WIFI_SCAN, scan_result);
|
|
|
|
|
|
- request.req_number = item_sz;
|
|
|
- request.rsp_number = 0;
|
|
|
- request.infos = infos;
|
|
|
-
|
|
|
- result = rt_device_control(RT_DEVICE(device), WIFI_SCAN, (void*)&request);
|
|
|
- result = result; /* skip warning */
|
|
|
-
|
|
|
- return request.rsp_number;
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
-int rt_wlan_get_rssi(struct rt_wlan_device* device)
|
|
|
+int rt_wlan_get_rssi(struct rt_wlan_device *device)
|
|
|
{
|
|
|
int rssi;
|
|
|
int result;
|
|
|
|
|
|
if (device == RT_NULL) return 0;
|
|
|
- result = rt_device_control(RT_DEVICE(device), WIFI_GET_RSSI, (void*)&rssi);
|
|
|
+ result = rt_device_control(RT_DEVICE(device), WIFI_GET_RSSI, (void *)&rssi);
|
|
|
|
|
|
if (result == RT_EOK) return rssi;
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-int rt_wlan_get_mac(struct rt_wlan_device* device, rt_uint8_t hwaddr[6])
|
|
|
+int rt_wlan_get_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6])
|
|
|
{
|
|
|
int result;
|
|
|
if (device == RT_NULL) return 0;
|
|
|
- result = rt_device_control(RT_DEVICE(device), NIOCTL_GADDR, (void*)hwaddr);
|
|
|
+ result = rt_device_control(RT_DEVICE(device), NIOCTL_GADDR, (void *)hwaddr);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-int rt_wlan_set_mac(struct rt_wlan_device* device, rt_uint8_t hwaddr[6])
|
|
|
+int rt_wlan_set_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6])
|
|
|
{
|
|
|
int result;
|
|
|
if (device == RT_NULL) return 0;
|
|
|
- result = rt_device_control(RT_DEVICE(device), NIOCTL_SADDR, (void*)hwaddr);
|
|
|
+ result = rt_device_control(RT_DEVICE(device), NIOCTL_SADDR, (void *)hwaddr);
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+int rt_wlan_enter_powersave(struct rt_wlan_device *device, int level)
|
|
|
+{
|
|
|
+ int result = 0;
|
|
|
+
|
|
|
+ if (device == RT_NULL) return -RT_EIO;
|
|
|
+
|
|
|
+ result = rt_device_control(RT_DEVICE(device), WIFI_ENTER_POWERSAVE, (void *)&level);
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+int rt_wlan_register_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event,
|
|
|
+ rt_wlan_event_handler handler)
|
|
|
+{
|
|
|
+ if (device == RT_NULL) return -RT_EIO;
|
|
|
+ if (event >= WIFI_EVT_MAX) return -RT_EINVAL;
|
|
|
+
|
|
|
+ device->handler[event] = handler;
|
|
|
+
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+int rt_wlan_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event)
|
|
|
+{
|
|
|
+ if (device == RT_NULL) return -RT_EIO;
|
|
|
+ if (event >= WIFI_EVT_MAX) return -RT_EINVAL;
|
|
|
+
|
|
|
+ device->handler[event] = RT_NULL;
|
|
|
+
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+int rt_wlan_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data)
|
|
|
+{
|
|
|
+ if (device == RT_NULL) return -RT_EIO;
|
|
|
+ if (event >= WIFI_EVT_MAX) return -RT_EINVAL;
|
|
|
+
|
|
|
+ if (device->handler[event] != RT_NULL)
|
|
|
+ device->handler[event](device, event, user_data);
|
|
|
+
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+int rt_wlan_cfg_monitor(struct rt_wlan_device *device, rt_wlan_monitor_opition_t opition)
|
|
|
+{
|
|
|
+ int result = 0;
|
|
|
+
|
|
|
+ if (device == RT_NULL) return -RT_EIO;
|
|
|
+
|
|
|
+ result = rt_device_control(RT_DEVICE(device), WIFI_CFG_MONITOR, (void *)&opition);
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+int rt_wlan_set_monitor_callback(struct rt_wlan_device *device, rt_wlan_monitor_callback_t callback)
|
|
|
+{
|
|
|
+ int result = 0;
|
|
|
+
|
|
|
+ if (device == RT_NULL) return -RT_EIO;
|
|
|
+
|
|
|
+ result = rt_device_control(RT_DEVICE(device), WIFI_SET_MONITOR_CALLBACK, (void *)callback);
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-int rt_wlan_enter_powersave(struct rt_wlan_device* device, int level)
|
|
|
+int rt_wlan_set_channel(struct rt_wlan_device *device, int channel)
|
|
|
{
|
|
|
int result = 0;
|
|
|
|
|
|
if (device == RT_NULL) return -RT_EIO;
|
|
|
|
|
|
- result = rt_device_control(RT_DEVICE(device), WIFI_ENTER_POWERSAVE, (void*)&level);
|
|
|
+ result = rt_device_control(RT_DEVICE(device), WIFI_SET_CHANNEL, (void *)&channel);
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-void rt_wlan_set_event_callback(struct rt_wlan_device* device, rt_wlan_event_handler handler,
|
|
|
- void *user_data)
|
|
|
+int rt_wlan_get_channel(struct rt_wlan_device *device)
|
|
|
{
|
|
|
- if (device == RT_NULL) return ;
|
|
|
+ int channel = 0;
|
|
|
|
|
|
- device->handler = handler;
|
|
|
- device->user_data = user_data;
|
|
|
+ if (device == RT_NULL) return -RT_EIO;
|
|
|
+
|
|
|
+ rt_device_control(RT_DEVICE(device), WIFI_GET_CHANNEL, &channel);
|
|
|
+
|
|
|
+ return channel;
|
|
|
+}
|
|
|
+
|
|
|
+void rt_wlan_release_scan_result(struct rt_wlan_scan_result **scan_result)
|
|
|
+{
|
|
|
+ int i, ap_num;
|
|
|
+ struct rt_wlan_scan_result *_scan_result;
|
|
|
|
|
|
- return ;
|
|
|
+ if (*scan_result != RT_NULL)
|
|
|
+ {
|
|
|
+ _scan_result = *scan_result;
|
|
|
+ ap_num = _scan_result->ap_num;
|
|
|
+ for (i = 0; i < ap_num; i++)
|
|
|
+ {
|
|
|
+ if (_scan_result->ap_table[i].ssid != RT_NULL)
|
|
|
+ {
|
|
|
+ rt_free(_scan_result->ap_table[i].ssid);
|
|
|
+ _scan_result->ap_table[i].ssid = RT_NULL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ _scan_result->ap_num = 0;
|
|
|
+ rt_free(_scan_result->ap_table);
|
|
|
+ _scan_result->ap_table = RT_NULL;
|
|
|
+ }
|
|
|
+ rt_free(*scan_result);
|
|
|
+ *scan_result = RT_NULL;
|
|
|
+ scan_result = RT_NULL;
|
|
|
}
|