rt-thread-official/components/drivers/wlan/wlan_dev.c

318 lines
7.9 KiB
C
Raw Normal View History

2017-10-25 07:37:26 +08:00
/*
* RT-Thread Wi-Fi Device
*
* COPYRIGHT (C) 2014 - 2018, Shanghai Real-Thread Technology Co., Ltd
2017-10-25 07:37:26 +08:00
*
* This file is part of RT-Thread (http://www.rt-thread.org)
*
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2014-09-11 Bernard the first verion
*/
#include <rtthread.h>
#include <rtdevice.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)
2017-10-25 07:37:26 +08:00
{
if (info == RT_NULL) return ;
memset(info, 0x0, sizeof(struct rt_wlan_info));
info->mode = mode;
info->security = security;
if (ssid)
{
info->ssid = rt_malloc(strlen((char *)ssid) + 1);
2017-10-25 07:37:26 +08:00
if (info->ssid)
{
strncpy((char *)info->ssid, (char *)ssid, strlen((char *)ssid) + 1);
2017-10-25 07:37:26 +08:00
}
}
}
void rt_wlan_info_deinit(struct rt_wlan_info *info)
2017-10-25 07:37:26 +08:00
{
if (info->ssid)
{
rt_free(info->ssid);
info->ssid = RT_NULL;
}
memset(info, 0x0, sizeof(struct rt_wlan_info));
}
int rt_wlan_init(struct rt_wlan_device *device, rt_wlan_mode_t mode)
2017-10-25 07:37:26 +08:00
{
int result;
if (device == RT_NULL) return 0;
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);
2017-10-25 07:37:26 +08:00
return result;
}
int rt_wlan_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, char *password)
2017-10-25 07:37:26 +08:00
{
int result = 0;
if (device == RT_NULL) return -RT_EIO;
if (info != RT_NULL)
{
rt_wlan_set_info(device, info);
}
rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1);
2017-10-25 07:37:26 +08:00
result = rt_device_control(RT_DEVICE(device), WIFI_EASYJOIN, (void *)password);
2017-10-25 07:37:26 +08:00
return result;
}
int rt_wlan_softap(struct rt_wlan_device *device, struct rt_wlan_info *info, char *password)
2017-10-25 07:37:26 +08:00
{
int result = RT_EOK;
if (device == RT_NULL) return -RT_EIO;
if (info != RT_NULL)
{
rt_wlan_set_info(device, info);
}
rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1);
2017-10-25 07:37:26 +08:00
result = rt_device_control(RT_DEVICE(device), WIFI_SOFTAP, (void *)password);
2017-10-25 07:37:26 +08:00
return result;
}
int rt_wlan_disconnect(struct rt_wlan_device *device)
2017-10-25 07:37:26 +08:00
{
int result = 0;
if (device == RT_NULL) return -RT_EIO;
/* save event handler */
result = rt_device_control(RT_DEVICE(device), WIFI_DISCONNECT, RT_NULL);
return result;
}
int rt_wlan_set_info(struct rt_wlan_device *device, struct rt_wlan_info *info)
2017-10-25 07:37:26 +08:00
{
if (device == RT_NULL) return -RT_EIO;
if (device->info == RT_NULL) return -RT_EIO;
2017-10-25 07:37:26 +08:00
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;
2017-10-25 07:37:26 +08:00
return RT_EOK;
}
struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device *device)
2017-10-25 07:37:26 +08:00
{
struct rt_wlan_info *info = RT_NULL;
2017-10-25 07:37:26 +08:00
if (device != RT_NULL)
{
info = device->info;
}
return info;
}
int rt_wlan_scan(struct rt_wlan_device *device, struct rt_wlan_scan_result **scan_result)
2017-10-25 07:37:26 +08:00
{
int result;
result = rt_device_control(RT_DEVICE(device), WIFI_SCAN, scan_result);
2017-10-25 07:37:26 +08:00
return result;
2017-10-25 07:37:26 +08:00
}
int rt_wlan_get_rssi(struct rt_wlan_device *device)
2017-10-25 07:37:26 +08:00
{
int rssi;
int result;
if (device == RT_NULL) return 0;
result = rt_device_control(RT_DEVICE(device), WIFI_GET_RSSI, (void *)&rssi);
2017-10-25 07:37:26 +08:00
if (result == RT_EOK) return rssi;
return result;
}
int rt_wlan_get_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6])
2017-10-25 07:37:26 +08:00
{
int result;
if (device == RT_NULL) return 0;
result = rt_device_control(RT_DEVICE(device), NIOCTL_GADDR, (void *)hwaddr);
2017-10-25 07:37:26 +08:00
return result;
}
int rt_wlan_set_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6])
2017-10-25 07:37:26 +08:00
{
int result;
if (device == RT_NULL) return 0;
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);
2017-10-25 07:37:26 +08:00
return result;
}
int rt_wlan_set_channel(struct rt_wlan_device *device, int channel)
2017-10-25 07:37:26 +08:00
{
int result = 0;
if (device == RT_NULL) return -RT_EIO;
result = rt_device_control(RT_DEVICE(device), WIFI_SET_CHANNEL, (void *)&channel);
2017-10-25 07:37:26 +08:00
return result;
}
int rt_wlan_get_channel(struct rt_wlan_device *device)
2017-10-25 07:37:26 +08:00
{
int channel = 0;
2017-10-25 07:37:26 +08:00
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;
2017-10-25 07:37:26 +08:00
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;
2017-10-25 07:37:26 +08:00
}