[DeviceDriver][wlan] add new wlan framework
This commit is contained in:
parent
2c6c4a747b
commit
324bfc5897
|
@ -194,32 +194,74 @@ config RT_USING_WDT
|
|||
bool "Using Watch Dog device drivers"
|
||||
default n
|
||||
|
||||
config RT_USING_WIFI
|
||||
bool "Using Wi-Fi network"
|
||||
default n
|
||||
|
||||
if RT_USING_WIFI
|
||||
config RT_USING_WLAN_STA
|
||||
bool "Using station mode"
|
||||
default y
|
||||
|
||||
config RT_USING_WLAN_AP
|
||||
bool "Using ap mode"
|
||||
default n
|
||||
|
||||
config WIFI_DEVICE_STA_NAME
|
||||
string "the wifi device name for station"
|
||||
default "w0"
|
||||
|
||||
config WIFI_DEVICE_AP_NAME
|
||||
string "the wifi device name for ap"
|
||||
default "ap"
|
||||
endif
|
||||
|
||||
config RT_USING_AUDIO
|
||||
bool "Using Audio device drivers"
|
||||
default n
|
||||
|
||||
menu "Using WiFi"
|
||||
config RT_USING_WIFI
|
||||
bool "Using Wi-Fi framework"
|
||||
default n
|
||||
|
||||
if RT_USING_WIFI
|
||||
config RT_WLAN_DEVICE_STA_NAME
|
||||
string "the WiFi device name for station"
|
||||
default "wlan0"
|
||||
|
||||
config RT_WLAN_DEVICE_AP_NAME
|
||||
string "the WiFi device name for ap"
|
||||
default "wlan1"
|
||||
|
||||
config RT_WLAN_DEFAULT_PROT
|
||||
string "Default transport protocol"
|
||||
default "lwip"
|
||||
|
||||
config RT_WLAN_SCAN_WAIT_MS
|
||||
int "Scan timeout time"
|
||||
default 10000;
|
||||
|
||||
config RT_WLAN_CONNECT_WAIT_MS
|
||||
int "connect timeout time"
|
||||
default 10000;
|
||||
|
||||
config RT_WLAN_SSID_MAX_LENGTH
|
||||
int "SSID name maximum length"
|
||||
default 32
|
||||
|
||||
config RT_WLAN_PASSWORD_MAX_LENGTH
|
||||
int "Maximum password length"
|
||||
default 32
|
||||
|
||||
config RT_WLAN_SCAN_SORT
|
||||
bool "Automatic sorting of scan results"
|
||||
default y
|
||||
|
||||
config RT_WLAN_CFG_INFO_MAX
|
||||
int "Maximum number of WiFi information automatically saved"
|
||||
default 3
|
||||
|
||||
config RT_WLAN_WORKQUEUE_THREAD_NAME
|
||||
string "WiFi work queue thread name"
|
||||
default "wlan_job"
|
||||
|
||||
config RT_WLAN_WORKQUEUE_THREAD_SIZE
|
||||
int "wifi work queue thread size"
|
||||
default 2048
|
||||
|
||||
config RT_WLAN_WORKQUEUE_THREAD_PRIO
|
||||
int "WiFi work queue thread priority"
|
||||
default 22
|
||||
|
||||
config RT_WLAN_DEV_EVENT_NUM
|
||||
int "Maximum number of driver events"
|
||||
default 2
|
||||
|
||||
config RT_WLAN_PROT_LWIP_PBUF_FORCE
|
||||
bool "Forced use of PBUF transmission"
|
||||
default n
|
||||
endif
|
||||
endmenu
|
||||
|
||||
menu "Using USB"
|
||||
config RT_USING_USB_HOST
|
||||
bool "Using USB host"
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-09-15 tyx the first version
|
||||
*/
|
||||
|
||||
#ifndef __WLAN_H__
|
||||
#define __WLAN_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <wlan_dev.h>
|
||||
#include <wlan_cfg.h>
|
||||
#include <wlan_mgnt.h>
|
||||
#include <wlan_prot.h>
|
||||
#include <wlan_workqueue.h>
|
||||
|
||||
#endif
|
|
@ -117,6 +117,10 @@ extern "C" {
|
|||
#include "drivers/rt_drv_pwm.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_WIFI
|
||||
#include "drivers/wlan.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,455 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-06 tyx the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <wlan_cfg.h>
|
||||
|
||||
#define DBG_ENABLE
|
||||
#define DBG_LEVEL DBG_INFO
|
||||
#define DBG_SECTION_NAME "WLAN.cfg"
|
||||
#define DBG_COLOR
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct cfg_save_info_head
|
||||
{
|
||||
rt_uint32_t magic;
|
||||
rt_uint32_t len;
|
||||
rt_uint32_t num;
|
||||
rt_uint32_t crc;
|
||||
};
|
||||
|
||||
struct rt_wlan_cfg_des
|
||||
{
|
||||
rt_uint32_t num;
|
||||
struct rt_wlan_cfg_info *cfg_info;
|
||||
};
|
||||
|
||||
#define WLAN_CFG_LOCK() (rt_mutex_take(&cfg_mutex, RT_WAITING_FOREVER))
|
||||
#define WLAN_CFG_UNLOCK() (rt_mutex_release(&cfg_mutex))
|
||||
|
||||
static struct rt_wlan_cfg_des *cfg_cache;
|
||||
static const struct rt_wlan_cfg_ops *cfg_ops;
|
||||
static struct rt_mutex cfg_mutex;
|
||||
|
||||
/*
|
||||
* CRC16_CCITT
|
||||
*/
|
||||
static rt_uint16_t rt_wlan_cal_crc(rt_uint8_t *buff, int len)
|
||||
{
|
||||
rt_uint16_t wCRCin = 0x0000;
|
||||
rt_uint16_t wCPoly = 0x1021;
|
||||
rt_uint8_t wChar = 0;
|
||||
|
||||
while (len--)
|
||||
{
|
||||
wChar = *(buff++);
|
||||
wCRCin ^= (wChar << 8);
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
if (wCRCin & 0x8000)
|
||||
wCRCin = (wCRCin << 1) ^ wCPoly;
|
||||
else
|
||||
wCRCin = wCRCin << 1;
|
||||
}
|
||||
}
|
||||
return wCRCin;
|
||||
}
|
||||
|
||||
void rt_wlan_cfg_init(void)
|
||||
{
|
||||
/* init cache memory */
|
||||
if (cfg_cache == RT_NULL)
|
||||
{
|
||||
cfg_cache = rt_malloc(sizeof(struct rt_wlan_cfg_des));
|
||||
if (cfg_cache != RT_NULL)
|
||||
{
|
||||
rt_memset(cfg_cache, 0, sizeof(struct rt_wlan_cfg_des));
|
||||
}
|
||||
/* init mutex lock */
|
||||
rt_mutex_init(&cfg_mutex, "wlan_cfg", RT_IPC_FLAG_FIFO);
|
||||
}
|
||||
}
|
||||
|
||||
void rt_wlan_cfg_set_ops(const struct rt_wlan_cfg_ops *ops)
|
||||
{
|
||||
rt_wlan_cfg_init();
|
||||
|
||||
WLAN_CFG_LOCK();
|
||||
/* save ops pointer */
|
||||
cfg_ops = ops;
|
||||
WLAN_CFG_UNLOCK();
|
||||
}
|
||||
|
||||
/* save config data */
|
||||
rt_err_t rt_wlan_cfg_cache_save(void)
|
||||
{
|
||||
rt_err_t err = RT_EOK;
|
||||
struct cfg_save_info_head *info_pkg;
|
||||
int len = 0;
|
||||
|
||||
if ((cfg_ops == RT_NULL) || (cfg_ops->write_cfg == RT_NULL))
|
||||
return RT_EOK;
|
||||
|
||||
WLAN_CFG_LOCK();
|
||||
len = sizeof(struct cfg_save_info_head) + sizeof(struct rt_wlan_cfg_info) * cfg_cache->num;
|
||||
info_pkg = rt_malloc(len);
|
||||
if (info_pkg == RT_NULL)
|
||||
{
|
||||
WLAN_CFG_UNLOCK();
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
info_pkg->magic = RT_WLAN_CFG_MAGIC;
|
||||
info_pkg->len = len;
|
||||
info_pkg->num = cfg_cache->num;
|
||||
/* CRC */
|
||||
info_pkg->crc = rt_wlan_cal_crc((rt_uint8_t *)cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * cfg_cache->num);
|
||||
rt_memcpy(((rt_uint8_t *)info_pkg) + sizeof(struct cfg_save_info_head),
|
||||
cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * cfg_cache->num);
|
||||
if (cfg_ops->write_cfg(info_pkg, len) != len)
|
||||
err = -RT_ERROR;
|
||||
rt_free(info_pkg);
|
||||
WLAN_CFG_UNLOCK();
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_cfg_cache_refresh(void)
|
||||
{
|
||||
int len = 0, i, j;
|
||||
struct cfg_save_info_head *head;
|
||||
void *data;
|
||||
struct rt_wlan_cfg_info *t_info, *cfg_info;
|
||||
rt_uint32_t crc;
|
||||
rt_bool_t equal_flag;
|
||||
|
||||
/* cache is full! exit */
|
||||
if (cfg_cache == RT_NULL || cfg_cache->num >= RT_WLAN_CFG_INFO_MAX)
|
||||
return -RT_ERROR;
|
||||
|
||||
/* check callback */
|
||||
if ((cfg_ops == RT_NULL) ||
|
||||
(cfg_ops->get_len == RT_NULL) ||
|
||||
(cfg_ops->read_cfg == RT_NULL))
|
||||
return -RT_ERROR;
|
||||
|
||||
WLAN_CFG_LOCK();
|
||||
/* get data len */
|
||||
if ((len = cfg_ops->get_len()) <= 0)
|
||||
{
|
||||
WLAN_CFG_UNLOCK();
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
head = rt_malloc(len);
|
||||
if (head == RT_NULL)
|
||||
{
|
||||
WLAN_CFG_UNLOCK();
|
||||
return -RT_ERROR;
|
||||
}
|
||||
/* get data */
|
||||
if (cfg_ops->read_cfg(head, len) != len)
|
||||
{
|
||||
rt_free(head);
|
||||
WLAN_CFG_UNLOCK();
|
||||
return -RT_ERROR;
|
||||
}
|
||||
/* get config */
|
||||
data = ((rt_uint8_t *)head) + sizeof(struct cfg_save_info_head);
|
||||
crc = rt_wlan_cal_crc((rt_uint8_t *)data, len - sizeof(struct cfg_save_info_head));
|
||||
LOG_D("head->magic:0x%08x RT_WLAN_CFG_MAGIC:0x%08x", head->magic, RT_WLAN_CFG_MAGIC);
|
||||
LOG_D("head->len:%d len:%d", head->len, len);
|
||||
LOG_D("head->num:%d num:%d", head->num, (len - sizeof(struct cfg_save_info_head)) / sizeof(struct rt_wlan_cfg_info));
|
||||
LOG_D("hred->crc:0x%04x crc:0x%04x", head->crc, crc);
|
||||
/* check */
|
||||
if ((head->magic != RT_WLAN_CFG_MAGIC) ||
|
||||
(head->len != len) ||
|
||||
(head->num != (len - sizeof(struct cfg_save_info_head)) / sizeof(struct rt_wlan_cfg_info)) ||
|
||||
(head->crc != crc))
|
||||
{
|
||||
rt_free(head);
|
||||
WLAN_CFG_UNLOCK();
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* remove duplicate config */
|
||||
cfg_info = (struct rt_wlan_cfg_info *)data;
|
||||
for (i = 0; i < head->num; i++)
|
||||
{
|
||||
equal_flag = RT_FALSE;
|
||||
for (j = 0; j < cfg_cache->num; j++)
|
||||
{
|
||||
if ((cfg_cache->cfg_info[j].info.ssid.len == cfg_info[i].info.ssid.len) &&
|
||||
(rt_memcmp(&cfg_cache->cfg_info[j].info.ssid.val[0], &cfg_info[i].info.ssid.val[0],
|
||||
cfg_cache->cfg_info[j].info.ssid.len) == 0) &&
|
||||
(rt_memcmp(&cfg_cache->cfg_info[j].info.bssid[0], &cfg_info[i].info.bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0))
|
||||
{
|
||||
equal_flag = RT_TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (cfg_cache->num >= RT_WLAN_CFG_INFO_MAX)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (equal_flag == RT_FALSE)
|
||||
{
|
||||
t_info = rt_realloc(cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * (cfg_cache->num + 1));
|
||||
if (t_info == RT_NULL)
|
||||
{
|
||||
rt_free(head);
|
||||
WLAN_CFG_UNLOCK();
|
||||
return -RT_ERROR;
|
||||
}
|
||||
cfg_cache->cfg_info = t_info;
|
||||
cfg_cache->cfg_info[cfg_cache->num] = cfg_info[i];
|
||||
cfg_cache->num ++;
|
||||
}
|
||||
}
|
||||
|
||||
rt_free(head);
|
||||
WLAN_CFG_UNLOCK();
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
int rt_wlan_cfg_get_num(void)
|
||||
{
|
||||
rt_wlan_cfg_init();
|
||||
|
||||
return cfg_cache->num;
|
||||
}
|
||||
|
||||
int rt_wlan_cfg_read(struct rt_wlan_cfg_info *cfg_info, int num)
|
||||
{
|
||||
rt_wlan_cfg_init();
|
||||
|
||||
if (num <= 0)
|
||||
return 0;
|
||||
/* copy data */
|
||||
WLAN_CFG_LOCK();
|
||||
num = cfg_cache->num > num ? num : cfg_cache->num;
|
||||
rt_memcpy(&cfg_cache->cfg_info[0], cfg_info, sizeof(struct rt_wlan_cfg_info) * num);
|
||||
WLAN_CFG_UNLOCK();
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_cfg_save(struct rt_wlan_cfg_info *cfg_info)
|
||||
{
|
||||
rt_err_t err = RT_EOK;
|
||||
struct rt_wlan_cfg_info *t_info;
|
||||
int idx = -1, i = 0;
|
||||
|
||||
rt_wlan_cfg_init();
|
||||
|
||||
/* parameter check */
|
||||
if ((cfg_info == RT_NULL) || (cfg_info->info.ssid.len == 0))
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
/* if (iteam == cache) exit */
|
||||
WLAN_CFG_LOCK();
|
||||
for (i = 0; i < cfg_cache->num; i++)
|
||||
{
|
||||
if ((cfg_cache->cfg_info[i].info.ssid.len == cfg_info->info.ssid.len) &&
|
||||
(rt_memcmp(&cfg_cache->cfg_info[i].info.ssid.val[0], &cfg_info->info.ssid.val[0],
|
||||
cfg_cache->cfg_info[i].info.ssid.len) == 0) &&
|
||||
(rt_memcmp(&cfg_cache->cfg_info[i].info.bssid[0], &cfg_info->info.bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0))
|
||||
{
|
||||
idx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((idx == 0) && (cfg_cache->cfg_info[i].key.len == cfg_info->key.len) &&
|
||||
(rt_memcmp(&cfg_cache->cfg_info[i].key.val[0], &cfg_info->key.val[0], cfg_info->key.len) == 0))
|
||||
{
|
||||
WLAN_CFG_UNLOCK();
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/* not find iteam with cache, Add iteam to the head */
|
||||
if ((idx == -1) && (cfg_cache->num < RT_WLAN_CFG_INFO_MAX))
|
||||
{
|
||||
t_info = rt_realloc(cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * (cfg_cache->num + 1));
|
||||
if (t_info == RT_NULL)
|
||||
{
|
||||
WLAN_CFG_UNLOCK();
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
cfg_cache->cfg_info = t_info;
|
||||
cfg_cache->num ++;
|
||||
}
|
||||
|
||||
/* move cache info */
|
||||
i = (i >= RT_WLAN_CFG_INFO_MAX ? RT_WLAN_CFG_INFO_MAX - 1 : i);
|
||||
for (; i; i--)
|
||||
{
|
||||
cfg_cache->cfg_info[i] = cfg_cache->cfg_info[i - 1];
|
||||
}
|
||||
/* add iteam to head */
|
||||
cfg_cache->cfg_info[i] = *cfg_info;
|
||||
WLAN_CFG_UNLOCK();
|
||||
|
||||
/* save info to flash */
|
||||
err = rt_wlan_cfg_cache_save();
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int rt_wlan_cfg_read_index(struct rt_wlan_cfg_info *cfg_info, int index)
|
||||
{
|
||||
rt_wlan_cfg_init();
|
||||
|
||||
if (index < 0)
|
||||
return 0;
|
||||
|
||||
WLAN_CFG_LOCK();
|
||||
if (index >= cfg_cache->num)
|
||||
{
|
||||
WLAN_CFG_UNLOCK();
|
||||
return 0;
|
||||
}
|
||||
/* copy data */
|
||||
*cfg_info = cfg_cache->cfg_info[index];
|
||||
WLAN_CFG_UNLOCK();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int rt_wlan_cfg_delete_index(int index)
|
||||
{
|
||||
struct rt_wlan_cfg_info *cfg_info;
|
||||
int i;
|
||||
|
||||
rt_wlan_cfg_init();
|
||||
|
||||
if (index < 0)
|
||||
return -1;
|
||||
|
||||
WLAN_CFG_LOCK();
|
||||
if (index >= cfg_cache->num)
|
||||
{
|
||||
WLAN_CFG_UNLOCK();
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* malloc new mem */
|
||||
cfg_info = rt_malloc(sizeof(struct rt_wlan_cfg_info) * (cfg_cache->num - 1));
|
||||
if (cfg_info == RT_NULL)
|
||||
{
|
||||
WLAN_CFG_UNLOCK();
|
||||
return -1;
|
||||
}
|
||||
/* copy data to new mem */
|
||||
for (i = 0; i < cfg_cache->num; i++)
|
||||
{
|
||||
if (i < index)
|
||||
{
|
||||
cfg_info[i] = cfg_cache->cfg_info[i];
|
||||
}
|
||||
else if (i > index)
|
||||
{
|
||||
cfg_info[i - 1] = cfg_cache->cfg_info[i];
|
||||
}
|
||||
}
|
||||
rt_free(cfg_cache->cfg_info);
|
||||
cfg_cache->cfg_info = cfg_info;
|
||||
cfg_cache->num --;
|
||||
WLAN_CFG_UNLOCK();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rt_wlan_cfg_delete_all(void)
|
||||
{
|
||||
rt_wlan_cfg_init();
|
||||
|
||||
/* delete all iteam */
|
||||
WLAN_CFG_LOCK();
|
||||
cfg_cache->num = 0;
|
||||
rt_free(cfg_cache->cfg_info);
|
||||
cfg_cache->cfg_info = RT_NULL;
|
||||
WLAN_CFG_UNLOCK();
|
||||
}
|
||||
|
||||
void rt_wlan_cfg_dump(void)
|
||||
{
|
||||
int index = 0;
|
||||
struct rt_wlan_info *info;
|
||||
struct rt_wlan_key *key;
|
||||
char *security;
|
||||
|
||||
rt_wlan_cfg_init();
|
||||
|
||||
rt_kprintf(" SSID PASSWORD MAC security chn\n");
|
||||
rt_kprintf("------------------------------- ------------------------------- ----------------- -------------- ---\n");
|
||||
for (index = 0; index < cfg_cache->num; index ++)
|
||||
{
|
||||
info = &cfg_cache->cfg_info[index].info;
|
||||
key = &cfg_cache->cfg_info[index].key;
|
||||
|
||||
if (info->ssid.len)
|
||||
rt_kprintf("%-32.32s", &info->ssid.val[0]);
|
||||
else
|
||||
rt_kprintf("%-32.32s", " ");
|
||||
|
||||
if (key->len)
|
||||
rt_kprintf("%-32.32s", &key->val[0]);
|
||||
else
|
||||
rt_kprintf("%-32.32s", " ");
|
||||
|
||||
rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x ",
|
||||
info->bssid[0],
|
||||
info->bssid[1],
|
||||
info->bssid[2],
|
||||
info->bssid[3],
|
||||
info->bssid[4],
|
||||
info->bssid[5]
|
||||
);
|
||||
switch (info->security)
|
||||
{
|
||||
case SECURITY_OPEN:
|
||||
security = "OPEN";
|
||||
break;
|
||||
case SECURITY_WEP_PSK:
|
||||
security = "WEP_PSK";
|
||||
break;
|
||||
case SECURITY_WEP_SHARED:
|
||||
security = "WEP_SHARED";
|
||||
break;
|
||||
case SECURITY_WPA_TKIP_PSK:
|
||||
security = "WPA_TKIP_PSK";
|
||||
break;
|
||||
case SECURITY_WPA_AES_PSK:
|
||||
security = "WPA_AES_PSK";
|
||||
break;
|
||||
case SECURITY_WPA2_AES_PSK:
|
||||
security = "WPA2_AES_PSK";
|
||||
break;
|
||||
case SECURITY_WPA2_TKIP_PSK:
|
||||
security = "WPA2_TKIP_PSK";
|
||||
break;
|
||||
case SECURITY_WPA2_MIXED_PSK:
|
||||
security = "WPA2_MIXED_PSK";
|
||||
break;
|
||||
case SECURITY_WPS_OPEN:
|
||||
security = "WPS_OPEN";
|
||||
break;
|
||||
case SECURITY_WPS_SECURE:
|
||||
security = "WPS_SECURE";
|
||||
break;
|
||||
default:
|
||||
security = "UNKNOWN";
|
||||
break;
|
||||
}
|
||||
rt_kprintf("%-14.14s ", security);
|
||||
rt_kprintf("%3d \n", info->channel);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-06 tyx the first version
|
||||
*/
|
||||
|
||||
#ifndef __WLAN_CFG_H__
|
||||
#define __WLAN_CFG_H__
|
||||
|
||||
#include <wlan_dev.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_CFG_INFO_MAX
|
||||
#define RT_WLAN_CFG_INFO_MAX (3) /* min is 1 */
|
||||
#endif
|
||||
|
||||
#if RT_WLAN_CFG_INFO_MAX < 1
|
||||
#error "The minimum configuration is 1"
|
||||
#endif
|
||||
|
||||
#define RT_WLAN_CFG_MAGIC (0x426f6d62)
|
||||
|
||||
struct rt_wlan_cfg_info
|
||||
{
|
||||
struct rt_wlan_info info;
|
||||
struct rt_wlan_key key;
|
||||
};
|
||||
|
||||
typedef int (*rt_wlan_wr)(void *buff, int len);
|
||||
|
||||
struct rt_wlan_cfg_ops
|
||||
{
|
||||
int (*read_cfg)(void *buff, int len);
|
||||
int (*get_len)(void);
|
||||
int (*write_cfg)(void *buff, int len);
|
||||
};
|
||||
|
||||
void rt_wlan_cfg_init(void);
|
||||
|
||||
void rt_wlan_cfg_set_ops(const struct rt_wlan_cfg_ops *ops);
|
||||
|
||||
int rt_wlan_cfg_get_num(void);
|
||||
|
||||
int rt_wlan_cfg_read(struct rt_wlan_cfg_info *cfg_info, int num);
|
||||
|
||||
int rt_wlan_cfg_read_index(struct rt_wlan_cfg_info *cfg_info, int index);
|
||||
|
||||
rt_err_t rt_wlan_cfg_save(struct rt_wlan_cfg_info *cfg_info);
|
||||
|
||||
rt_err_t rt_wlan_cfg_cache_refresh(void);
|
||||
|
||||
rt_err_t rt_wlan_cfg_cache_save(void);
|
||||
|
||||
int rt_wlan_cfg_delete_index(int index);
|
||||
|
||||
void rt_wlan_cfg_delete_all(void);
|
||||
|
||||
void rt_wlan_cfg_dump(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
|
@ -1,50 +0,0 @@
|
|||
/*
|
||||
* File : wlan_cmd.h
|
||||
* Wi-Fi common commands
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* 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
|
||||
* 2018-06-05 RT-Thread first version
|
||||
*/
|
||||
|
||||
#ifndef WLAN_CMD_H__
|
||||
#define WLAN_CMD_H__
|
||||
|
||||
struct netif;
|
||||
|
||||
int wifi_get_mode(void);
|
||||
int wifi_set_mode(int mode);
|
||||
|
||||
/* do the wifi default action: read wifi setting and then join or start soft-AP */
|
||||
int wifi_default(void);
|
||||
/* setup netif for soft-ap */
|
||||
int wifi_softap_setup_netif(struct netif *netif);
|
||||
|
||||
int wifi_set_setting(const char *ssid, const char *pwd);
|
||||
|
||||
#ifdef PKG_USING_CJSON
|
||||
int wifi_read_cfg(const char *filename);
|
||||
int wifi_save_cfg(const char *filename);
|
||||
#endif
|
||||
/* save wifi setting with default storage file */
|
||||
int wifi_save_setting(void);
|
||||
|
||||
extern struct rt_wlan_info info;
|
||||
|
||||
#endif
|
|
@ -1,328 +1,745 @@
|
|||
/*
|
||||
* RT-Thread Wi-Fi Device
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* COPYRIGHT (C) 2014 - 2018, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2014-09-11 Bernard the first verion
|
||||
* 2018-08-03 tyx the first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include <wlan_dev.h>
|
||||
#include <wlan_prot.h>
|
||||
|
||||
#include "wlan_dev.h"
|
||||
#include "wlan_cmd.h"
|
||||
#define DBG_ENABLE
|
||||
#define DBG_LEVEL DBG_INFO
|
||||
#define DBG_SECTION_NAME "WLAN.dev"
|
||||
#define DBG_COLOR
|
||||
#include <rtdbg.h>
|
||||
|
||||
#define NIOCTL_SADDR 0x02
|
||||
#ifndef RT_DEVICE
|
||||
#define RT_DEVICE(__device) ((rt_device_t)__device)
|
||||
#endif
|
||||
|
||||
void rt_wlan_info_init(struct rt_wlan_info *info, rt_wlan_mode_t mode, rt_wlan_security_t security,
|
||||
char *ssid)
|
||||
#define WLAN_DEV_LOCK(_wlan) (rt_mutex_take(&(_wlan)->lock, RT_WAITING_FOREVER))
|
||||
#define WLAN_DEV_UNLOCK(_wlan) (rt_mutex_release(&(_wlan)->lock))
|
||||
|
||||
rt_err_t rt_wlan_dev_init(struct rt_wlan_device *device, rt_wlan_mode_t mode)
|
||||
{
|
||||
if (info == RT_NULL) return ;
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
memset(info, 0x0, sizeof(struct rt_wlan_info));
|
||||
info->mode = mode;
|
||||
info->security = security;
|
||||
if (ssid)
|
||||
/* init wlan device */
|
||||
LOG_D("F:%s L:%d is run device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode);
|
||||
if ((device == RT_NULL) || (mode >= RT_WLAN_MODE_MAX))
|
||||
{
|
||||
info->ssid = rt_malloc(strlen((char *)ssid) + 1);
|
||||
if (info->ssid)
|
||||
{
|
||||
strncpy((char *)info->ssid, (char *)ssid, strlen((char *)ssid) + 1);
|
||||
}
|
||||
}
|
||||
LOG_E("F:%s L:%d Parameter Wrongful device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
void rt_wlan_info_deinit(struct rt_wlan_info *info)
|
||||
result = rt_device_init(RT_DEVICE(device));
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
if (info->ssid)
|
||||
{
|
||||
rt_free(info->ssid);
|
||||
info->ssid = RT_NULL;
|
||||
LOG_E("L:%d wlan init failed", __LINE__);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
memset(info, 0x0, sizeof(struct rt_wlan_info));
|
||||
}
|
||||
|
||||
int rt_wlan_init(struct rt_wlan_device *device, rt_wlan_mode_t mode)
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_MODE, (void *)&mode);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
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;
|
||||
LOG_E("L:%d wlan config mode failed", __LINE__);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
device->info = info;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), WIFI_INIT, (void *)&mode);
|
||||
|
||||
device->mode = mode;
|
||||
return result;
|
||||
}
|
||||
|
||||
int rt_wlan_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, char *password)
|
||||
rt_err_t rt_wlan_dev_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len)
|
||||
{
|
||||
int result = 0;
|
||||
rt_err_t result = RT_EOK;
|
||||
struct rt_sta_info sta_info;
|
||||
|
||||
if (device == RT_NULL) return -RT_EIO;
|
||||
|
||||
if (info != RT_NULL)
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
rt_wlan_set_info(device, info);
|
||||
return -RT_EIO;
|
||||
}
|
||||
if (info == RT_NULL)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1);
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), WIFI_EASYJOIN, (void *)password);
|
||||
if ((password_len > RT_WLAN_PASSWORD_MAX_LENGTH) ||
|
||||
(info->ssid.len > RT_WLAN_SSID_MAX_LENGTH))
|
||||
{
|
||||
LOG_E("L:%d password or ssid is to long", __LINE__);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_memset(&sta_info, 0, sizeof(struct rt_sta_info));
|
||||
rt_memcpy(&sta_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
|
||||
rt_memcpy(sta_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
|
||||
if (password != RT_NULL)
|
||||
{
|
||||
rt_memcpy(sta_info.key.val, password, password_len);
|
||||
sta_info.key.len = password_len;
|
||||
}
|
||||
sta_info.channel = info->channel;
|
||||
sta_info.security = info->security;
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_JOIN, &sta_info);
|
||||
return result;
|
||||
}
|
||||
|
||||
int rt_wlan_softap(struct rt_wlan_device *device, struct rt_wlan_info *info, char *password)
|
||||
rt_err_t rt_wlan_dev_disconnect(struct rt_wlan_device *device)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
if (device == RT_NULL) return -RT_EIO;
|
||||
|
||||
if (info != RT_NULL)
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
rt_wlan_set_info(device, info);
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
if (password == NULL)
|
||||
{
|
||||
memset(device->key, 0, sizeof(device->key));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rt_strlen(password) > sizeof(device->key) - 1)
|
||||
{
|
||||
rt_kprintf("WARN input password is longer than %d bytes.", sizeof(device->key) - 1);
|
||||
}
|
||||
rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1);
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), WIFI_SOFTAP, (void *)password);
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_DISCONNECT, RT_NULL);
|
||||
return result;
|
||||
}
|
||||
|
||||
int rt_wlan_disconnect(struct rt_wlan_device *device)
|
||||
rt_err_t rt_wlan_dev_ap_start(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len)
|
||||
{
|
||||
int result = 0;
|
||||
rt_err_t result = RT_EOK;
|
||||
struct rt_ap_info ap_info;
|
||||
|
||||
if (device == RT_NULL) return -RT_EIO;
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
if (info == RT_NULL)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* save event handler */
|
||||
result = rt_device_control(RT_DEVICE(device), WIFI_DISCONNECT, RT_NULL);
|
||||
if ((password_len >= RT_WLAN_PASSWORD_MAX_LENGTH) ||
|
||||
(info->ssid.len >= RT_WLAN_SSID_MAX_LENGTH))
|
||||
{
|
||||
LOG_E("L:%d password or ssid is to long", __LINE__);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_memset(&ap_info, 0, sizeof(struct rt_ap_info));
|
||||
rt_memcpy(&ap_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
|
||||
rt_memcpy(ap_info.key.val, password, password_len);
|
||||
ap_info.key.len = password_len;
|
||||
ap_info.hidden = info->hidden;
|
||||
ap_info.channel = info->channel;
|
||||
ap_info.security = info->security;
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SOFTAP, &ap_info);
|
||||
return result;
|
||||
}
|
||||
|
||||
int rt_wlan_set_info(struct rt_wlan_device *device, struct rt_wlan_info *info)
|
||||
rt_err_t rt_wlan_dev_ap_stop(struct rt_wlan_device *device)
|
||||
{
|
||||
if (device == RT_NULL) return -RT_EIO;
|
||||
if (device->info == RT_NULL) return -RT_EIO;
|
||||
rt_err_t result = 0;
|
||||
|
||||
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;
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_STOP, RT_NULL);
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_ap_deauth(struct rt_wlan_device *device, rt_uint8_t mac[6])
|
||||
{
|
||||
rt_err_t result = 0;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_DEAUTH, mac);
|
||||
return result;
|
||||
}
|
||||
|
||||
int rt_wlan_dev_get_rssi(struct rt_wlan_device *device)
|
||||
{
|
||||
int rssi = 0;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_RSSI, &rssi);
|
||||
return rssi;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_get_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
|
||||
{
|
||||
rt_err_t result = 0;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_MAC, &mac[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_set_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_MAC, &mac[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_enable_powersave(struct rt_wlan_device *device)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
int enable = 1;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_POWERSAVE, &enable);
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_disable_powersave(struct rt_wlan_device *device)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
int enable = 0;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_POWERSAVE, &enable);
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_register_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler, void *parameter)
|
||||
{
|
||||
int i = 0;
|
||||
rt_base_t level;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
if (event >= RT_WLAN_DEV_EVT_MAX)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
|
||||
{
|
||||
if (device->handler_table[event][i].handler == RT_NULL)
|
||||
{
|
||||
device->handler_table[event][i].handler = handler;
|
||||
device->handler_table[event][i].parameter = parameter;
|
||||
rt_hw_interrupt_enable(level);
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* No space found */
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler)
|
||||
{
|
||||
int i = 0;
|
||||
rt_base_t level;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
if (event >= RT_WLAN_DEV_EVT_MAX)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
|
||||
{
|
||||
if (device->handler_table[event][i].handler == handler)
|
||||
{
|
||||
rt_memset(&device->handler_table[event][i], 0, sizeof(struct rt_wlan_dev_event_desc));
|
||||
rt_exit_critical();
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
rt_hw_interrupt_enable(level);
|
||||
/* not find iteam */
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
void rt_wlan_dev_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff)
|
||||
{
|
||||
void *parameter[RT_WLAN_DEV_EVENT_NUM];
|
||||
rt_wlan_dev_event_handler handler[RT_WLAN_DEV_EVENT_NUM];
|
||||
int i;
|
||||
rt_base_t level;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (event >= RT_WLAN_DEV_EVT_MAX)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* get callback handle */
|
||||
level = rt_hw_interrupt_disable();
|
||||
for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
|
||||
{
|
||||
handler[i] = device->handler_table[event][i].handler;
|
||||
parameter[i] = device->handler_table[event][i].parameter;
|
||||
}
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* run callback */
|
||||
for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
|
||||
{
|
||||
if (handler[i] != RT_NULL)
|
||||
{
|
||||
handler[i](device, event, buff, parameter[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_enter_pormisc(struct rt_wlan_device *device)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
int enable = 1;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_exit_pormisc(struct rt_wlan_device *device)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
int enable = 0;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_set_pormisc_callback(struct rt_wlan_device *device, rt_wlan_pormisc_callback_t callback)
|
||||
{
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
device->pormisc_callback = callback;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device *device)
|
||||
void rt_wlan_dev_pormisc_handler(struct rt_wlan_device *device, void *data, int len)
|
||||
{
|
||||
struct rt_wlan_info *info = RT_NULL;
|
||||
rt_wlan_pormisc_callback_t callback;
|
||||
|
||||
if (device != RT_NULL)
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
info = device->info;
|
||||
return;
|
||||
}
|
||||
|
||||
return info;
|
||||
callback = device->pormisc_callback;
|
||||
|
||||
if (callback != RT_NULL)
|
||||
{
|
||||
callback(device, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
int rt_wlan_scan(struct rt_wlan_device *device, struct rt_wlan_scan_result **scan_result)
|
||||
rt_err_t rt_wlan_dev_cfg_filter(struct rt_wlan_device *device, struct rt_wlan_filter *filter)
|
||||
{
|
||||
int result;
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), WIFI_SCAN, scan_result);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
if (filter == RT_NULL)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_FILTER, filter);
|
||||
return result;
|
||||
}
|
||||
|
||||
int rt_wlan_get_rssi(struct rt_wlan_device *device)
|
||||
rt_err_t rt_wlan_dev_set_channel(struct rt_wlan_device *device, int channel)
|
||||
{
|
||||
int rssi;
|
||||
int result;
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
if (device == RT_NULL) return 0;
|
||||
result = rt_device_control(RT_DEVICE(device), WIFI_GET_RSSI, (void *)&rssi);
|
||||
|
||||
if (result == RT_EOK) return rssi;
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
if (channel < 0)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_CHANNEL, &channel);
|
||||
return result;
|
||||
}
|
||||
|
||||
int rt_wlan_get_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6])
|
||||
rt_err_t rt_wlan_dev_get_channel(struct rt_wlan_device *device)
|
||||
{
|
||||
int result;
|
||||
if (device == RT_NULL) return 0;
|
||||
result = rt_device_control(RT_DEVICE(device), NIOCTL_GADDR, (void *)hwaddr);
|
||||
return result;
|
||||
rt_err_t result = RT_EOK;
|
||||
int channel;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
int rt_wlan_set_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6])
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_CHANNEL, &channel);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
int result;
|
||||
if (device == RT_NULL) return 0;
|
||||
result = rt_device_control(RT_DEVICE(device), NIOCTL_SADDR, (void *)hwaddr);
|
||||
return result;
|
||||
return -1;
|
||||
}
|
||||
|
||||
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_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_SET_CHANNEL, (void *)&channel);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int rt_wlan_get_channel(struct rt_wlan_device *device)
|
||||
{
|
||||
int channel = 0;
|
||||
|
||||
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)
|
||||
rt_err_t rt_wlan_dev_set_country(struct rt_wlan_device *device, rt_country_code_t country_code)
|
||||
{
|
||||
int i, ap_num;
|
||||
struct rt_wlan_scan_result *_scan_result;
|
||||
int result = RT_EOK;
|
||||
|
||||
if (*scan_result != RT_NULL)
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
_scan_result = *scan_result;
|
||||
ap_num = _scan_result->ap_num;
|
||||
for (i = 0; i < ap_num; i++)
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_COUNTRY, &country_code);
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_country_code_t rt_wlan_dev_get_country(struct rt_wlan_device *device)
|
||||
{
|
||||
if (_scan_result->ap_table[i].ssid != RT_NULL)
|
||||
int result = 0;
|
||||
rt_country_code_t country_code = RT_COUNTRY_UNKNOWN;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
rt_free(_scan_result->ap_table[i].ssid);
|
||||
_scan_result->ap_table[i].ssid = RT_NULL;
|
||||
return country_code;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_COUNTRY, &country_code);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
return RT_COUNTRY_UNKNOWN;
|
||||
}
|
||||
_scan_result->ap_num = 0;
|
||||
rt_free(_scan_result->ap_table);
|
||||
_scan_result->ap_table = RT_NULL;
|
||||
|
||||
return country_code;
|
||||
}
|
||||
rt_free(*scan_result);
|
||||
*scan_result = RT_NULL;
|
||||
scan_result = RT_NULL;
|
||||
|
||||
rt_err_t rt_wlan_dev_scan(struct rt_wlan_device *device, struct rt_wlan_info *info)
|
||||
{
|
||||
struct rt_scan_info scan_info = { 0 };
|
||||
struct rt_scan_info *p_scan_info = RT_NULL;
|
||||
rt_err_t result = 0;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
if (info != RT_NULL)
|
||||
{
|
||||
if (info->ssid.len >= RT_WLAN_SSID_MAX_LENGTH)
|
||||
{
|
||||
LOG_E("L:%d ssid is to long", __LINE__);
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
rt_memcpy(&scan_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
|
||||
rt_memcpy(scan_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
|
||||
scan_info.channel_min = -1;
|
||||
scan_info.channel_max = -1;
|
||||
p_scan_info = &scan_info;
|
||||
}
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN, p_scan_info);
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_scan_stop(struct rt_wlan_device *device)
|
||||
{
|
||||
rt_err_t result = 0;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN_STOP, RT_NULL);
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_report_data(struct rt_wlan_device *device, void *buff, int len)
|
||||
{
|
||||
return rt_wlan_dev_transfer_prot(device, buff, len);
|
||||
}
|
||||
|
||||
static rt_err_t _rt_wlan_dev_init(rt_device_t dev)
|
||||
{
|
||||
struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
rt_mutex_init(&wlan->lock, "wlan_dev", RT_IPC_FLAG_FIFO);
|
||||
|
||||
if (wlan->ops->wlan_init)
|
||||
result = wlan->ops->wlan_init(wlan);
|
||||
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
LOG_I("wlan init success");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_I("wlan init failed");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args)
|
||||
{
|
||||
struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
WLAN_DEV_LOCK(wlan);
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case RT_WLAN_CMD_MODE:
|
||||
{
|
||||
rt_wlan_mode_t mode = *((rt_wlan_mode_t *)args);
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_MODE, "RT_WLAN_CMD_MODE");
|
||||
if (wlan->ops->wlan_mode)
|
||||
err = wlan->ops->wlan_mode(wlan, mode);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_SCAN:
|
||||
{
|
||||
struct rt_scan_info *scan_info = args;
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN, "RT_WLAN_CMD_SCAN");
|
||||
if (wlan->ops->wlan_scan)
|
||||
err = wlan->ops->wlan_scan(wlan, scan_info);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_JOIN:
|
||||
{
|
||||
struct rt_sta_info *sta_info = args;
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_JOIN, "RT_WLAN_CMD_JOIN");
|
||||
if (wlan->ops->wlan_join)
|
||||
err = wlan->ops->wlan_join(wlan, sta_info);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_SOFTAP:
|
||||
{
|
||||
struct rt_ap_info *ap_info = args;
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SOFTAP, "RT_WLAN_CMD_SOFTAP");
|
||||
if (wlan->ops->wlan_softap)
|
||||
err = wlan->ops->wlan_softap(wlan, ap_info);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_DISCONNECT:
|
||||
{
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_DISCONNECT, "RT_WLAN_CMD_DISCONNECT");
|
||||
if (wlan->ops->wlan_disconnect)
|
||||
err = wlan->ops->wlan_disconnect(wlan);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_AP_STOP:
|
||||
{
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_STOP, "RT_WLAN_CMD_AP_STOP");
|
||||
if (wlan->ops->wlan_ap_stop)
|
||||
err = wlan->ops->wlan_ap_stop(wlan);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_AP_DEAUTH:
|
||||
{
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_DEAUTH, "RT_WLAN_CMD_AP_DEAUTH");
|
||||
if (wlan->ops->wlan_ap_deauth)
|
||||
err = wlan->ops->wlan_ap_deauth(wlan, args);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_SCAN_STOP:
|
||||
{
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN_STOP, "RT_WLAN_CMD_SCAN_STOP");
|
||||
if (wlan->ops->wlan_scan_stop)
|
||||
err = wlan->ops->wlan_scan_stop(wlan);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_GET_RSSI:
|
||||
{
|
||||
int *rssi = args;
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_RSSI, "RT_WLAN_CMD_GET_RSSI");
|
||||
if (wlan->ops->wlan_get_rssi)
|
||||
*rssi = wlan->ops->wlan_get_rssi(wlan);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_POWERSAVE:
|
||||
{
|
||||
rt_bool_t enable = *((rt_bool_t *)args);
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_POWERSAVE, "RT_WLAN_CMD_POWERSAVE");
|
||||
if (wlan->ops->wlan_powersave)
|
||||
wlan->ops->wlan_powersave(wlan, enable);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_CFG_PROMISC:
|
||||
{
|
||||
rt_bool_t start = *((rt_bool_t *)args);
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_PROMISC, "RT_WLAN_CMD_CFG_PROMISC");
|
||||
if (wlan->ops->wlan_cfg_promisc)
|
||||
wlan->ops->wlan_cfg_promisc(wlan, start);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_CFG_FILTER:
|
||||
{
|
||||
struct rt_wlan_filter *filter = args;
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_FILTER, "RT_WLAN_CMD_CFG_FILTER");
|
||||
if (wlan->ops->wlan_cfg_filter)
|
||||
wlan->ops->wlan_cfg_filter(wlan, filter);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_SET_CHANNEL:
|
||||
{
|
||||
int channel = *(int *)args;
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_CHANNEL, "RT_WLAN_CMD_SET_CHANNEL");
|
||||
if (wlan->ops->wlan_set_channel)
|
||||
wlan->ops->wlan_set_channel(wlan, channel);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_GET_CHANNEL:
|
||||
{
|
||||
int *channel = args;
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_CHANNEL, "RT_WLAN_CMD_GET_CHANNEL");
|
||||
if (wlan->ops->wlan_get_channel)
|
||||
*channel = wlan->ops->wlan_get_channel(wlan);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_SET_COUNTRY:
|
||||
{
|
||||
rt_country_code_t country = *(rt_country_code_t *)args;
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_COUNTRY, "RT_WLAN_CMD_SET_COUNTRY");
|
||||
if (wlan->ops->wlan_set_country)
|
||||
wlan->ops->wlan_set_country(wlan, country);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_GET_COUNTRY:
|
||||
{
|
||||
rt_country_code_t *country = args;
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_COUNTRY, "RT_WLAN_CMD_GET_COUNTRY");
|
||||
if (wlan->ops->wlan_get_country)
|
||||
*country = wlan->ops->wlan_get_country(wlan);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_SET_MAC:
|
||||
{
|
||||
rt_uint8_t *mac = args;
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_MAC, "RT_WLAN_CMD_SET_MAC");
|
||||
if (wlan->ops->wlan_set_mac)
|
||||
wlan->ops->wlan_set_mac(wlan, mac);
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_CMD_GET_MAC:
|
||||
{
|
||||
rt_uint8_t *mac = args;
|
||||
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_MAC, "RT_WLAN_CMD_GET_MAC");
|
||||
if (wlan->ops->wlan_get_mac)
|
||||
wlan->ops->wlan_get_mac(wlan, mac);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, -1, "UNKUOWN");
|
||||
break;
|
||||
}
|
||||
|
||||
WLAN_DEV_UNLOCK(wlan);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
struct rt_wlan_device *rt_wlan_dev_register(const char *name, const struct rt_wlan_dev_ops *ops, rt_uint32_t flag, void *user_data)
|
||||
{
|
||||
struct rt_wlan_device *wlan;
|
||||
|
||||
if (name == RT_NULL || ops == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d parameter Wrongful", __FUNCTION__, __LINE__);
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
wlan = rt_malloc(sizeof(struct rt_wlan_device));
|
||||
if (wlan == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d", __FUNCTION__, __LINE__);
|
||||
return RT_NULL;
|
||||
}
|
||||
rt_memset(wlan, 0, sizeof(struct rt_wlan_device));
|
||||
|
||||
wlan->device.init = _rt_wlan_dev_init;
|
||||
wlan->device.open = RT_NULL;
|
||||
wlan->device.close = RT_NULL;
|
||||
wlan->device.read = RT_NULL;
|
||||
wlan->device.write = RT_NULL;
|
||||
wlan->device.control = _rt_wlan_dev_control;
|
||||
wlan->device.user_data = RT_NULL;
|
||||
|
||||
wlan->device.type = RT_Device_Class_NetIf;
|
||||
|
||||
wlan->ops = ops;
|
||||
wlan->user_data = user_data;
|
||||
|
||||
wlan->flags = flag;
|
||||
rt_device_register(&wlan->device, name, RT_DEVICE_FLAG_RDWR);
|
||||
|
||||
LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
|
||||
|
||||
return wlan;
|
||||
}
|
||||
|
|
|
@ -1,72 +1,65 @@
|
|||
/*
|
||||
* RT-Thread Wi-Fi Device
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* COPYRIGHT (C) 2014 - 2018, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2014-09-11 Bernard the first verion
|
||||
* 2018-08-03 tyx the first version
|
||||
*/
|
||||
|
||||
#ifndef WIFI_DEVICE_H__
|
||||
#define WIFI_DEVICE_H__
|
||||
#ifndef __WLAN_DEVICE_H__
|
||||
#define __WLAN_DEVICE_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <netif/ethernetif.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WIFI_NONE,
|
||||
WIFI_STATION,
|
||||
WIFI_AP,
|
||||
RT_WLAN_NONE,
|
||||
RT_WLAN_STATION,
|
||||
RT_WLAN_AP,
|
||||
RT_WLAN_MODE_MAX
|
||||
} rt_wlan_mode_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WIFI_INIT = 0x10,
|
||||
WIFI_SCAN, /* trigger scanning (list cells) */
|
||||
WIFI_JOIN,
|
||||
WIFI_EASYJOIN, /* join network with less information */
|
||||
WIFI_SOFTAP, /* start soft-AP */
|
||||
WIFI_DISCONNECT,
|
||||
WIFI_GET_RSSI, /* get sensitivity (dBm) */
|
||||
WIFI_ENTER_POWERSAVE,
|
||||
WIFI_CFG_MONITOR, /* start/stop minitor */
|
||||
WIFI_SET_CHANNEL,
|
||||
WIFI_GET_CHANNEL,
|
||||
WIFI_SET_MONITOR_CALLBACK,
|
||||
RT_WLAN_CMD_MODE = 0x10,
|
||||
RT_WLAN_CMD_SCAN, /* trigger scanning (list cells) */
|
||||
RT_WLAN_CMD_JOIN,
|
||||
RT_WLAN_CMD_SOFTAP, /* start soft-AP */
|
||||
RT_WLAN_CMD_DISCONNECT,
|
||||
RT_WLAN_CMD_AP_STOP, /* stop soft-AP */
|
||||
RT_WLAN_CMD_AP_DEAUTH,
|
||||
RT_WLAN_CMD_SCAN_STOP,
|
||||
RT_WLAN_CMD_GET_RSSI, /* get sensitivity (dBm) */
|
||||
RT_WLAN_CMD_POWERSAVE,
|
||||
RT_WLAN_CMD_CFG_PROMISC, /* start/stop minitor */
|
||||
RT_WLAN_CMD_CFG_FILTER,
|
||||
RT_WLAN_CMD_SET_CHANNEL,
|
||||
RT_WLAN_CMD_GET_CHANNEL,
|
||||
RT_WLAN_CMD_SET_COUNTRY,
|
||||
RT_WLAN_CMD_GET_COUNTRY,
|
||||
RT_WLAN_CMD_SET_MAC,
|
||||
RT_WLAN_CMD_GET_MAC
|
||||
} rt_wlan_cmd_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WIFI_PWR_OFF,
|
||||
WIFI_PWR_SLEEP,
|
||||
WIFI_PWR_NORMAL
|
||||
} rt_wlan_powersave_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WIFI_MONITOR_START,
|
||||
WIFI_MONITOR_STOP
|
||||
} rt_wlan_monitor_opition_t;
|
||||
RT_WLAN_DEV_EVT_INIT_DONE = 0,
|
||||
RT_WLAN_DEV_EVT_CONNECT,
|
||||
RT_WLAN_DEV_EVT_CONNECT_FAIL,
|
||||
RT_WLAN_DEV_EVT_DISCONNECT,
|
||||
RT_WLAN_DEV_EVT_AP_START,
|
||||
RT_WLAN_DEV_EVT_AP_STOP,
|
||||
RT_WLAN_DEV_EVT_AP_ASSOCIATED,
|
||||
RT_WLAN_DEV_EVT_AP_DISASSOCIATED,
|
||||
RT_WLAN_DEV_EVT_AP_ASSOCIATE_FAILED,
|
||||
RT_WLAN_DEV_EVT_SCAN_REPORT,
|
||||
RT_WLAN_DEV_EVT_SCAN_DONE,
|
||||
RT_WLAN_DEV_EVT_MAX,
|
||||
} rt_wlan_dev_event_t;
|
||||
|
||||
#define SHARED_ENABLED 0x00008000
|
||||
#define WPA_SECURITY 0x00200000
|
||||
|
@ -77,144 +70,506 @@ typedef enum
|
|||
#define AES_ENABLED 0x0004
|
||||
#define WSEC_SWFLAG 0x0008
|
||||
|
||||
#define KEY_ARRAY_SIZE 32
|
||||
#define SSID_LENGTH_MAX_SIZE 32 + 1
|
||||
#define RT_WLAN_FLAG_STA_ONLY (0x1 << 0)
|
||||
#define RT_WLAN_FLAG_AP_ONLY (0x1 << 1)
|
||||
|
||||
#ifndef RT_WLAN_SSID_MAX_LENGTH
|
||||
#define RT_WLAN_SSID_MAX_LENGTH (32) /* SSID MAX LEN */
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_BSSID_MAX_LENGTH
|
||||
#define RT_WLAN_BSSID_MAX_LENGTH (6) /* BSSID MAX LEN (default is 6) */
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_PASSWORD_MAX_LENGTH
|
||||
#define RT_WLAN_PASSWORD_MAX_LENGTH (32) /* PASSWORD MAX LEN*/
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_DEV_EVENT_NUM
|
||||
#define RT_WLAN_DEV_EVENT_NUM (2) /* EVENT GROUP MAX NUM */
|
||||
#endif
|
||||
|
||||
#if RT_WLAN_SSID_MAX_LENGTH < 1
|
||||
#error "SSID length is too short"
|
||||
#endif
|
||||
|
||||
#if RT_WLAN_BSSID_MAX_LENGTH < 1
|
||||
#error "BSSID length is too short"
|
||||
#endif
|
||||
|
||||
#if RT_WLAN_PASSWORD_MAX_LENGTH < 1
|
||||
#error "password length is too short"
|
||||
#endif
|
||||
|
||||
#if RT_WLAN_DEV_EVENT_NUM < 2
|
||||
#error "dev num Too little"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Enumeration of Wi-Fi security modes
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
SECURITY_OPEN = 0, /**< Open security */
|
||||
SECURITY_WEP_PSK = WEP_ENABLED, /**< WEP Security with open authentication */
|
||||
SECURITY_WEP_SHARED = (WEP_ENABLED | SHARED_ENABLED), /**< WEP Security with shared authentication */
|
||||
SECURITY_WPA_TKIP_PSK = (WPA_SECURITY | TKIP_ENABLED), /**< WPA Security with TKIP */
|
||||
SECURITY_WPA_AES_PSK = (WPA_SECURITY | AES_ENABLED), /**< WPA Security with AES */
|
||||
SECURITY_WPA2_AES_PSK = (WPA2_SECURITY | AES_ENABLED), /**< WPA2 Security with AES */
|
||||
SECURITY_WPA2_TKIP_PSK = (WPA2_SECURITY | TKIP_ENABLED), /**< WPA2 Security with TKIP */
|
||||
SECURITY_WPA2_MIXED_PSK = (WPA2_SECURITY | AES_ENABLED | TKIP_ENABLED), /**< WPA2 Security with AES & TKIP */
|
||||
SECURITY_WPS_OPEN = WPS_ENABLED, /**< WPS with open security */
|
||||
SECURITY_WPS_SECURE = (WPS_ENABLED | AES_ENABLED), /**< WPS with AES security */
|
||||
SECURITY_UNKNOWN = -1, /**< May be returned by scan function if security is unknown.
|
||||
SECURITY_OPEN = 0, /* Open security */
|
||||
SECURITY_WEP_PSK = WEP_ENABLED, /* WEP Security with open authentication */
|
||||
SECURITY_WEP_SHARED = (WEP_ENABLED | SHARED_ENABLED), /* WEP Security with shared authentication */
|
||||
SECURITY_WPA_TKIP_PSK = (WPA_SECURITY | TKIP_ENABLED), /* WPA Security with TKIP */
|
||||
SECURITY_WPA_AES_PSK = (WPA_SECURITY | AES_ENABLED), /* WPA Security with AES */
|
||||
SECURITY_WPA2_AES_PSK = (WPA2_SECURITY | AES_ENABLED), /* WPA2 Security with AES */
|
||||
SECURITY_WPA2_TKIP_PSK = (WPA2_SECURITY | TKIP_ENABLED), /* WPA2 Security with TKIP */
|
||||
SECURITY_WPA2_MIXED_PSK = (WPA2_SECURITY | AES_ENABLED | TKIP_ENABLED), /* WPA2 Security with AES & TKIP */
|
||||
SECURITY_WPS_OPEN = WPS_ENABLED, /* WPS with open security */
|
||||
SECURITY_WPS_SECURE = (WPS_ENABLED | AES_ENABLED), /* WPS with AES security */
|
||||
SECURITY_UNKNOWN = -1, /* May be returned by scan function if security is unknown.
|
||||
Do not pass this to the join function! */
|
||||
} rt_wlan_security_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WIFI_EVT_INIT_DONE = 0,
|
||||
WIFI_EVT_LINK_DOWN,
|
||||
WIFI_EVT_LINK_UP,
|
||||
WIFI_EVT_CONNECT,
|
||||
WIFI_EVT_DISCONNECT,
|
||||
WIFI_EVT_AP_START,
|
||||
WIFI_EVT_AP_STOP,
|
||||
WIFI_EVENT_STA_ASSOC,
|
||||
WIFI_EVENT_STA_DISASSOC,
|
||||
WIFI_EVT_SCAN_DONE,
|
||||
WIFI_EVT_MAX,
|
||||
} rt_wlan_event_t;
|
||||
RT_802_11_BAND_5GHZ = 0, /* Denotes 5GHz radio band */
|
||||
RT_802_11_BAND_2_4GHZ = 1, /* Denotes 2.4GHz radio band */
|
||||
RT_802_11_BAND_UNKNOWN = 0x7fffffff, /* unknown */
|
||||
} rt_802_11_band_t;
|
||||
|
||||
/* wifi network information */
|
||||
struct rt_wlan_info
|
||||
typedef enum
|
||||
{
|
||||
rt_wlan_mode_t mode; /* wifi mode */
|
||||
rt_wlan_security_t security;
|
||||
|
||||
char *ssid;
|
||||
uint8_t bssid[6];
|
||||
|
||||
/* maximal data rate */
|
||||
uint32_t datarate;
|
||||
/* radio channel */
|
||||
uint16_t channel;
|
||||
/* signal strength */
|
||||
int16_t rssi;
|
||||
};
|
||||
|
||||
struct rt_wlan_info_request
|
||||
{
|
||||
uint16_t req_number; /* the number of information item for request */
|
||||
uint16_t rsp_number; /* the number of information item for response */
|
||||
|
||||
struct rt_wlan_info *infos;/* the array of information to save response */
|
||||
};
|
||||
|
||||
typedef struct rt_wlan_scan_result
|
||||
{
|
||||
char ap_num;
|
||||
struct rt_wlan_info *ap_table;
|
||||
} rt_wlan_scan_result_t;
|
||||
RT_COUNTRY_AFGHANISTAN,
|
||||
RT_COUNTRY_ALBANIA,
|
||||
RT_COUNTRY_ALGERIA,
|
||||
RT_COUNTRY_AMERICAN_SAMOA,
|
||||
RT_COUNTRY_ANGOLA,
|
||||
RT_COUNTRY_ANGUILLA,
|
||||
RT_COUNTRY_ANTIGUA_AND_BARBUDA,
|
||||
RT_COUNTRY_ARGENTINA,
|
||||
RT_COUNTRY_ARMENIA,
|
||||
RT_COUNTRY_ARUBA,
|
||||
RT_COUNTRY_AUSTRALIA,
|
||||
RT_COUNTRY_AUSTRIA,
|
||||
RT_COUNTRY_AZERBAIJAN,
|
||||
RT_COUNTRY_BAHAMAS,
|
||||
RT_COUNTRY_BAHRAIN,
|
||||
RT_COUNTRY_BAKER_ISLAND,
|
||||
RT_COUNTRY_BANGLADESH,
|
||||
RT_COUNTRY_BARBADOS,
|
||||
RT_COUNTRY_BELARUS,
|
||||
RT_COUNTRY_BELGIUM,
|
||||
RT_COUNTRY_BELIZE,
|
||||
RT_COUNTRY_BENIN,
|
||||
RT_COUNTRY_BERMUDA,
|
||||
RT_COUNTRY_BHUTAN,
|
||||
RT_COUNTRY_BOLIVIA,
|
||||
RT_COUNTRY_BOSNIA_AND_HERZEGOVINA,
|
||||
RT_COUNTRY_BOTSWANA,
|
||||
RT_COUNTRY_BRAZIL,
|
||||
RT_COUNTRY_BRITISH_INDIAN_OCEAN_TERRITORY,
|
||||
RT_COUNTRY_BRUNEI_DARUSSALAM,
|
||||
RT_COUNTRY_BULGARIA,
|
||||
RT_COUNTRY_BURKINA_FASO,
|
||||
RT_COUNTRY_BURUNDI,
|
||||
RT_COUNTRY_CAMBODIA,
|
||||
RT_COUNTRY_CAMEROON,
|
||||
RT_COUNTRY_CANADA,
|
||||
RT_COUNTRY_CAPE_VERDE,
|
||||
RT_COUNTRY_CAYMAN_ISLANDS,
|
||||
RT_COUNTRY_CENTRAL_AFRICAN_REPUBLIC,
|
||||
RT_COUNTRY_CHAD,
|
||||
RT_COUNTRY_CHILE,
|
||||
RT_COUNTRY_CHINA,
|
||||
RT_COUNTRY_CHRISTMAS_ISLAND,
|
||||
RT_COUNTRY_COLOMBIA,
|
||||
RT_COUNTRY_COMOROS,
|
||||
RT_COUNTRY_CONGO,
|
||||
RT_COUNTRY_CONGO_THE_DEMOCRATIC_REPUBLIC_OF_THE,
|
||||
RT_COUNTRY_COSTA_RICA,
|
||||
RT_COUNTRY_COTE_DIVOIRE,
|
||||
RT_COUNTRY_CROATIA,
|
||||
RT_COUNTRY_CUBA,
|
||||
RT_COUNTRY_CYPRUS,
|
||||
RT_COUNTRY_CZECH_REPUBLIC,
|
||||
RT_COUNTRY_DENMARK,
|
||||
RT_COUNTRY_DJIBOUTI,
|
||||
RT_COUNTRY_DOMINICA,
|
||||
RT_COUNTRY_DOMINICAN_REPUBLIC,
|
||||
RT_COUNTRY_DOWN_UNDER,
|
||||
RT_COUNTRY_ECUADOR,
|
||||
RT_COUNTRY_EGYPT,
|
||||
RT_COUNTRY_EL_SALVADOR,
|
||||
RT_COUNTRY_EQUATORIAL_GUINEA,
|
||||
RT_COUNTRY_ERITREA,
|
||||
RT_COUNTRY_ESTONIA,
|
||||
RT_COUNTRY_ETHIOPIA,
|
||||
RT_COUNTRY_FALKLAND_ISLANDS_MALVINAS,
|
||||
RT_COUNTRY_FAROE_ISLANDS,
|
||||
RT_COUNTRY_FIJI,
|
||||
RT_COUNTRY_FINLAND,
|
||||
RT_COUNTRY_FRANCE,
|
||||
RT_COUNTRY_FRENCH_GUINA,
|
||||
RT_COUNTRY_FRENCH_POLYNESIA,
|
||||
RT_COUNTRY_FRENCH_SOUTHERN_TERRITORIES,
|
||||
RT_COUNTRY_GABON,
|
||||
RT_COUNTRY_GAMBIA,
|
||||
RT_COUNTRY_GEORGIA,
|
||||
RT_COUNTRY_GERMANY,
|
||||
RT_COUNTRY_GHANA,
|
||||
RT_COUNTRY_GIBRALTAR,
|
||||
RT_COUNTRY_GREECE,
|
||||
RT_COUNTRY_GRENADA,
|
||||
RT_COUNTRY_GUADELOUPE,
|
||||
RT_COUNTRY_GUAM,
|
||||
RT_COUNTRY_GUATEMALA,
|
||||
RT_COUNTRY_GUERNSEY,
|
||||
RT_COUNTRY_GUINEA,
|
||||
RT_COUNTRY_GUINEA_BISSAU,
|
||||
RT_COUNTRY_GUYANA,
|
||||
RT_COUNTRY_HAITI,
|
||||
RT_COUNTRY_HOLY_SEE_VATICAN_CITY_STATE,
|
||||
RT_COUNTRY_HONDURAS,
|
||||
RT_COUNTRY_HONG_KONG,
|
||||
RT_COUNTRY_HUNGARY,
|
||||
RT_COUNTRY_ICELAND,
|
||||
RT_COUNTRY_INDIA,
|
||||
RT_COUNTRY_INDONESIA,
|
||||
RT_COUNTRY_IRAN_ISLAMIC_REPUBLIC_OF,
|
||||
RT_COUNTRY_IRAQ,
|
||||
RT_COUNTRY_IRELAND,
|
||||
RT_COUNTRY_ISRAEL,
|
||||
RT_COUNTRY_ITALY,
|
||||
RT_COUNTRY_JAMAICA,
|
||||
RT_COUNTRY_JAPAN,
|
||||
RT_COUNTRY_JERSEY,
|
||||
RT_COUNTRY_JORDAN,
|
||||
RT_COUNTRY_KAZAKHSTAN,
|
||||
RT_COUNTRY_KENYA,
|
||||
RT_COUNTRY_KIRIBATI,
|
||||
RT_COUNTRY_KOREA_REPUBLIC_OF,
|
||||
RT_COUNTRY_KOSOVO,
|
||||
RT_COUNTRY_KUWAIT,
|
||||
RT_COUNTRY_KYRGYZSTAN,
|
||||
RT_COUNTRY_LAO_PEOPLES_DEMOCRATIC_REPUBIC,
|
||||
RT_COUNTRY_LATVIA,
|
||||
RT_COUNTRY_LEBANON,
|
||||
RT_COUNTRY_LESOTHO,
|
||||
RT_COUNTRY_LIBERIA,
|
||||
RT_COUNTRY_LIBYAN_ARAB_JAMAHIRIYA,
|
||||
RT_COUNTRY_LIECHTENSTEIN,
|
||||
RT_COUNTRY_LITHUANIA,
|
||||
RT_COUNTRY_LUXEMBOURG,
|
||||
RT_COUNTRY_MACAO,
|
||||
RT_COUNTRY_MACEDONIA_FORMER_YUGOSLAV_REPUBLIC_OF,
|
||||
RT_COUNTRY_MADAGASCAR,
|
||||
RT_COUNTRY_MALAWI,
|
||||
RT_COUNTRY_MALAYSIA,
|
||||
RT_COUNTRY_MALDIVES,
|
||||
RT_COUNTRY_MALI,
|
||||
RT_COUNTRY_MALTA,
|
||||
RT_COUNTRY_MAN_ISLE_OF,
|
||||
RT_COUNTRY_MARTINIQUE,
|
||||
RT_COUNTRY_MAURITANIA,
|
||||
RT_COUNTRY_MAURITIUS,
|
||||
RT_COUNTRY_MAYOTTE,
|
||||
RT_COUNTRY_MEXICO,
|
||||
RT_COUNTRY_MICRONESIA_FEDERATED_STATES_OF,
|
||||
RT_COUNTRY_MOLDOVA_REPUBLIC_OF,
|
||||
RT_COUNTRY_MONACO,
|
||||
RT_COUNTRY_MONGOLIA,
|
||||
RT_COUNTRY_MONTENEGRO,
|
||||
RT_COUNTRY_MONTSERRAT,
|
||||
RT_COUNTRY_MOROCCO,
|
||||
RT_COUNTRY_MOZAMBIQUE,
|
||||
RT_COUNTRY_MYANMAR,
|
||||
RT_COUNTRY_NAMIBIA,
|
||||
RT_COUNTRY_NAURU,
|
||||
RT_COUNTRY_NEPAL,
|
||||
RT_COUNTRY_NETHERLANDS,
|
||||
RT_COUNTRY_NETHERLANDS_ANTILLES,
|
||||
RT_COUNTRY_NEW_CALEDONIA,
|
||||
RT_COUNTRY_NEW_ZEALAND,
|
||||
RT_COUNTRY_NICARAGUA,
|
||||
RT_COUNTRY_NIGER,
|
||||
RT_COUNTRY_NIGERIA,
|
||||
RT_COUNTRY_NORFOLK_ISLAND,
|
||||
RT_COUNTRY_NORTHERN_MARIANA_ISLANDS,
|
||||
RT_COUNTRY_NORWAY,
|
||||
RT_COUNTRY_OMAN,
|
||||
RT_COUNTRY_PAKISTAN,
|
||||
RT_COUNTRY_PALAU,
|
||||
RT_COUNTRY_PANAMA,
|
||||
RT_COUNTRY_PAPUA_NEW_GUINEA,
|
||||
RT_COUNTRY_PARAGUAY,
|
||||
RT_COUNTRY_PERU,
|
||||
RT_COUNTRY_PHILIPPINES,
|
||||
RT_COUNTRY_POLAND,
|
||||
RT_COUNTRY_PORTUGAL,
|
||||
RT_COUNTRY_PUETO_RICO,
|
||||
RT_COUNTRY_QATAR,
|
||||
RT_COUNTRY_REUNION,
|
||||
RT_COUNTRY_ROMANIA,
|
||||
RT_COUNTRY_RUSSIAN_FEDERATION,
|
||||
RT_COUNTRY_RWANDA,
|
||||
RT_COUNTRY_SAINT_KITTS_AND_NEVIS,
|
||||
RT_COUNTRY_SAINT_LUCIA,
|
||||
RT_COUNTRY_SAINT_PIERRE_AND_MIQUELON,
|
||||
RT_COUNTRY_SAINT_VINCENT_AND_THE_GRENADINES,
|
||||
RT_COUNTRY_SAMOA,
|
||||
RT_COUNTRY_SANIT_MARTIN_SINT_MARTEEN,
|
||||
RT_COUNTRY_SAO_TOME_AND_PRINCIPE,
|
||||
RT_COUNTRY_SAUDI_ARABIA,
|
||||
RT_COUNTRY_SENEGAL,
|
||||
RT_COUNTRY_SERBIA,
|
||||
RT_COUNTRY_SEYCHELLES,
|
||||
RT_COUNTRY_SIERRA_LEONE,
|
||||
RT_COUNTRY_SINGAPORE,
|
||||
RT_COUNTRY_SLOVAKIA,
|
||||
RT_COUNTRY_SLOVENIA,
|
||||
RT_COUNTRY_SOLOMON_ISLANDS,
|
||||
RT_COUNTRY_SOMALIA,
|
||||
RT_COUNTRY_SOUTH_AFRICA,
|
||||
RT_COUNTRY_SPAIN,
|
||||
RT_COUNTRY_SRI_LANKA,
|
||||
RT_COUNTRY_SURINAME,
|
||||
RT_COUNTRY_SWAZILAND,
|
||||
RT_COUNTRY_SWEDEN,
|
||||
RT_COUNTRY_SWITZERLAND,
|
||||
RT_COUNTRY_SYRIAN_ARAB_REPUBLIC,
|
||||
RT_COUNTRY_TAIWAN_PROVINCE_OF_CHINA,
|
||||
RT_COUNTRY_TAJIKISTAN,
|
||||
RT_COUNTRY_TANZANIA_UNITED_REPUBLIC_OF,
|
||||
RT_COUNTRY_THAILAND,
|
||||
RT_COUNTRY_TOGO,
|
||||
RT_COUNTRY_TONGA,
|
||||
RT_COUNTRY_TRINIDAD_AND_TOBAGO,
|
||||
RT_COUNTRY_TUNISIA,
|
||||
RT_COUNTRY_TURKEY,
|
||||
RT_COUNTRY_TURKMENISTAN,
|
||||
RT_COUNTRY_TURKS_AND_CAICOS_ISLANDS,
|
||||
RT_COUNTRY_TUVALU,
|
||||
RT_COUNTRY_UGANDA,
|
||||
RT_COUNTRY_UKRAINE,
|
||||
RT_COUNTRY_UNITED_ARAB_EMIRATES,
|
||||
RT_COUNTRY_UNITED_KINGDOM,
|
||||
RT_COUNTRY_UNITED_STATES,
|
||||
RT_COUNTRY_UNITED_STATES_REV4,
|
||||
RT_COUNTRY_UNITED_STATES_NO_DFS,
|
||||
RT_COUNTRY_UNITED_STATES_MINOR_OUTLYING_ISLANDS,
|
||||
RT_COUNTRY_URUGUAY,
|
||||
RT_COUNTRY_UZBEKISTAN,
|
||||
RT_COUNTRY_VANUATU,
|
||||
RT_COUNTRY_VENEZUELA,
|
||||
RT_COUNTRY_VIET_NAM,
|
||||
RT_COUNTRY_VIRGIN_ISLANDS_BRITISH,
|
||||
RT_COUNTRY_VIRGIN_ISLANDS_US,
|
||||
RT_COUNTRY_WALLIS_AND_FUTUNA,
|
||||
RT_COUNTRY_WEST_BANK,
|
||||
RT_COUNTRY_WESTERN_SAHARA,
|
||||
RT_COUNTRY_WORLD_WIDE_XX,
|
||||
RT_COUNTRY_YEMEN,
|
||||
RT_COUNTRY_ZAMBIA,
|
||||
RT_COUNTRY_ZIMBABWE,
|
||||
RT_COUNTRY_UNKNOWN
|
||||
} rt_country_code_t;
|
||||
|
||||
struct rt_wlan_device;
|
||||
typedef void (*rt_wlan_event_handler)(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data);
|
||||
typedef void (*rt_wlan_monitor_callback_t)(uint8_t *data, int len, void *user_data);
|
||||
struct rt_wlan_device
|
||||
struct rt_wlan_buff;
|
||||
|
||||
typedef void (*rt_wlan_dev_event_handler)(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff, void *parameter);
|
||||
|
||||
typedef void (*rt_wlan_pormisc_callback_t)(struct rt_wlan_device *device, void *data, int len);
|
||||
|
||||
struct rt_wlan_ssid
|
||||
{
|
||||
struct eth_device parent;
|
||||
rt_uint8_t len;
|
||||
rt_uint8_t val[RT_WLAN_SSID_MAX_LENGTH + 1];
|
||||
};
|
||||
typedef struct rt_wlan_ssid rt_wlan_ssid_t;
|
||||
|
||||
struct rt_wlan_info *info;
|
||||
char key[KEY_ARRAY_SIZE + 1];
|
||||
struct rt_wlan_key
|
||||
{
|
||||
rt_uint8_t len;
|
||||
rt_uint8_t val[RT_WLAN_PASSWORD_MAX_LENGTH + 1];
|
||||
};
|
||||
typedef struct rt_wlan_key rt_wlan_key_t;
|
||||
|
||||
rt_wlan_event_handler handler[WIFI_EVT_MAX];
|
||||
void *user_data;
|
||||
int interface;
|
||||
#define INVALID_INFO(_info) do { \
|
||||
rt_memset((_info), 0, sizeof(struct rt_wlan_info)); \
|
||||
(_info)->band = RT_802_11_BAND_UNKNOWN; \
|
||||
(_info)->security = SECURITY_UNKNOWN; \
|
||||
(_info)->channel = -1; \
|
||||
} while(0)
|
||||
|
||||
struct rt_wlan_info
|
||||
{
|
||||
/* security type */
|
||||
rt_wlan_security_t security;
|
||||
/* 2.4G/5G */
|
||||
rt_802_11_band_t band;
|
||||
/* maximal data rate */
|
||||
rt_uint32_t datarate;
|
||||
/* radio channel */
|
||||
rt_int16_t channel;
|
||||
/* signal strength */
|
||||
rt_int16_t rssi;
|
||||
/* ssid */
|
||||
rt_wlan_ssid_t ssid;
|
||||
/* hwaddr */
|
||||
rt_uint8_t bssid[RT_WLAN_BSSID_MAX_LENGTH];
|
||||
rt_uint8_t hidden;
|
||||
};
|
||||
|
||||
/*
|
||||
* Wi-Fi Information APIs
|
||||
*/
|
||||
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_deinit(struct rt_wlan_info *info);
|
||||
struct rt_wlan_buff
|
||||
{
|
||||
void *data;
|
||||
rt_int32_t len;
|
||||
};
|
||||
|
||||
/*
|
||||
* Wi-Fi Manager APIs
|
||||
*/
|
||||
int rt_wlan_init(struct rt_wlan_device *device, rt_wlan_mode_t mode);
|
||||
struct rt_filter_pattern
|
||||
{
|
||||
rt_uint16_t offset; /* Offset in bytes to start filtering (referenced to the start of the ethernet packet) */
|
||||
rt_uint16_t mask_size; /* Size of the mask in bytes */
|
||||
rt_uint8_t *mask; /* Pattern mask bytes to be ANDed with the pattern eg. "\xff00" (must be in network byte order) */
|
||||
rt_uint8_t *pattern; /* Pattern bytes used to filter eg. "\x0800" (must be in network byte order) */
|
||||
};
|
||||
|
||||
int rt_wlan_connect(struct rt_wlan_device *device, struct rt_wlan_info *info,
|
||||
char *password);
|
||||
int rt_wlan_disconnect(struct rt_wlan_device *device);
|
||||
typedef enum
|
||||
{
|
||||
RT_POSITIVE_MATCHING = 0, /* Receive the data matching with this pattern and discard the other data */
|
||||
RT_NEGATIVE_MATCHING = 1 /* Discard the data matching with this pattern and receive the other data */
|
||||
} rt_filter_rule_t;
|
||||
|
||||
int rt_wlan_softap(struct rt_wlan_device *device, struct rt_wlan_info *info,
|
||||
char *password);
|
||||
struct rt_wlan_filter
|
||||
{
|
||||
struct rt_filter_pattern patt;
|
||||
rt_filter_rule_t rule;
|
||||
rt_uint8_t enable;
|
||||
};
|
||||
|
||||
/* set wifi information for AP */
|
||||
int rt_wlan_set_info(struct rt_wlan_device *device, struct rt_wlan_info *info);
|
||||
/* get wifi information for AP */
|
||||
struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device *device);
|
||||
struct rt_wlan_dev_event_desc
|
||||
{
|
||||
rt_wlan_dev_event_handler handler;
|
||||
void *parameter;
|
||||
};
|
||||
|
||||
/* get the AP result which were scaned in station */
|
||||
int rt_wlan_scan(struct rt_wlan_device *device, struct rt_wlan_scan_result **scan_result);
|
||||
struct rt_wlan_device
|
||||
{
|
||||
struct rt_device device;
|
||||
rt_wlan_mode_t mode;
|
||||
struct rt_mutex lock;
|
||||
struct rt_wlan_dev_event_desc handler_table[RT_WLAN_DEV_EVT_MAX][RT_WLAN_DEV_EVENT_NUM];
|
||||
rt_wlan_pormisc_callback_t pormisc_callback;
|
||||
const struct rt_wlan_dev_ops *ops;
|
||||
rt_uint32_t flags;
|
||||
void *prot;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
/* get rssi */
|
||||
int rt_wlan_get_rssi(struct rt_wlan_device *device);
|
||||
/* Get/Set MAC */
|
||||
int rt_wlan_get_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]);
|
||||
struct rt_sta_info
|
||||
{
|
||||
rt_wlan_ssid_t ssid;
|
||||
rt_wlan_key_t key;
|
||||
rt_uint8_t bssid[6];
|
||||
rt_uint16_t channel;
|
||||
rt_wlan_security_t security;
|
||||
};
|
||||
|
||||
/* enter power save level */
|
||||
int rt_wlan_enter_powersave(struct rt_wlan_device *device, int level);
|
||||
struct rt_ap_info
|
||||
{
|
||||
rt_wlan_ssid_t ssid;
|
||||
rt_wlan_key_t key;
|
||||
rt_bool_t hidden;
|
||||
rt_uint16_t channel;
|
||||
rt_wlan_security_t security;
|
||||
};
|
||||
|
||||
/* register the event handler */
|
||||
int rt_wlan_register_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event,
|
||||
rt_wlan_event_handler handler);
|
||||
struct rt_scan_info
|
||||
{
|
||||
rt_wlan_ssid_t ssid;
|
||||
rt_uint8_t bssid[6];
|
||||
rt_int16_t channel_min;
|
||||
rt_int16_t channel_max;
|
||||
};
|
||||
|
||||
/* un-register the event handler */
|
||||
int rt_wlan_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event);
|
||||
struct rt_wlan_dev_ops
|
||||
{
|
||||
rt_err_t (*wlan_init)(struct rt_wlan_device *wlan);
|
||||
rt_err_t (*wlan_mode)(struct rt_wlan_device *wlan, rt_wlan_mode_t mode);
|
||||
rt_err_t (*wlan_scan)(struct rt_wlan_device *wlan, struct rt_scan_info *scan_info);
|
||||
rt_err_t (*wlan_join)(struct rt_wlan_device *wlan, struct rt_sta_info *sta_info);
|
||||
rt_err_t (*wlan_softap)(struct rt_wlan_device *wlan, struct rt_ap_info *ap_info);
|
||||
rt_err_t (*wlan_disconnect)(struct rt_wlan_device *wlan);
|
||||
rt_err_t (*wlan_ap_stop)(struct rt_wlan_device *wlan);
|
||||
rt_err_t (*wlan_ap_deauth)(struct rt_wlan_device *wlan, rt_uint8_t mac[]);
|
||||
rt_err_t (*wlan_scan_stop)(struct rt_wlan_device *wlan);
|
||||
int (*wlan_get_rssi)(struct rt_wlan_device *wlan);
|
||||
rt_err_t (*wlan_powersave)(struct rt_wlan_device *wlan, rt_bool_t enable);
|
||||
rt_err_t (*wlan_cfg_promisc)(struct rt_wlan_device *wlan, rt_bool_t start);
|
||||
rt_err_t (*wlan_cfg_filter)(struct rt_wlan_device *wlan, struct rt_wlan_filter *filter);
|
||||
rt_err_t (*wlan_set_channel)(struct rt_wlan_device *wlan, int channel);
|
||||
int (*wlan_get_channel)(struct rt_wlan_device *wlan);
|
||||
rt_err_t (*wlan_set_country)(struct rt_wlan_device *wlan, rt_country_code_t country_code);
|
||||
rt_country_code_t (*wlan_get_country)(struct rt_wlan_device *wlan);
|
||||
rt_err_t (*wlan_set_mac)(struct rt_wlan_device *wlan, rt_uint8_t mac[]);
|
||||
rt_err_t (*wlan_get_mac)(struct rt_wlan_device *wlan, rt_uint8_t mac[]);
|
||||
int (*wlan_recv)(struct rt_wlan_device *wlan, void *buff, int len);
|
||||
int (*wlan_send)(struct rt_wlan_device *wlan, void *buff, int len);
|
||||
};
|
||||
|
||||
/* wlan driver indicate event to upper layer through wifi_indication. */
|
||||
int rt_wlan_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_event_t event,
|
||||
void *user_data);
|
||||
rt_err_t rt_wlan_dev_init(struct rt_wlan_device *device, rt_wlan_mode_t mode);
|
||||
|
||||
/* start or stop monitor */
|
||||
int rt_wlan_cfg_monitor(struct rt_wlan_device *device, rt_wlan_monitor_opition_t opition);
|
||||
rt_err_t rt_wlan_dev_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len);
|
||||
|
||||
/* set callback function for monitor mode*/
|
||||
int rt_wlan_set_monitor_callback(struct rt_wlan_device *device, rt_wlan_monitor_callback_t callback);
|
||||
rt_err_t rt_wlan_dev_disconnect(struct rt_wlan_device *device);
|
||||
|
||||
/* Set the monitor channel */
|
||||
int rt_wlan_set_channel(struct rt_wlan_device *device, int channel);
|
||||
rt_err_t rt_wlan_dev_ap_start(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len);
|
||||
|
||||
void rt_wlan_release_scan_result(struct rt_wlan_scan_result **scan_result);
|
||||
rt_err_t rt_wlan_dev_ap_stop(struct rt_wlan_device *device);
|
||||
|
||||
rt_err_t rt_wlan_dev_ap_deauth(struct rt_wlan_device *device, rt_uint8_t mac[6]);
|
||||
|
||||
int rt_wlan_dev_get_rssi(struct rt_wlan_device *device);
|
||||
|
||||
rt_err_t rt_wlan_dev_get_mac(struct rt_wlan_device *device, rt_uint8_t mac[6]);
|
||||
|
||||
rt_err_t rt_wlan_dev_set_mac(struct rt_wlan_device *device, rt_uint8_t mac[6]);
|
||||
|
||||
rt_err_t rt_wlan_dev_enable_powersave(struct rt_wlan_device *device);
|
||||
|
||||
rt_err_t rt_wlan_dev_disable_powersave(struct rt_wlan_device *device);
|
||||
|
||||
rt_err_t rt_wlan_dev_register_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler, void *parameter);
|
||||
|
||||
rt_err_t rt_wlan_dev_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler);
|
||||
|
||||
void rt_wlan_dev_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff);
|
||||
|
||||
rt_err_t rt_wlan_dev_enter_pormisc(struct rt_wlan_device *device);
|
||||
|
||||
rt_err_t rt_wlan_dev_exit_pormisc(struct rt_wlan_device *device);
|
||||
|
||||
rt_err_t rt_wlan_dev_set_pormisc_callback(struct rt_wlan_device *device, rt_wlan_pormisc_callback_t callback);
|
||||
|
||||
void rt_wlan_dev_pormisc_handler(struct rt_wlan_device *device, void *data, int len);
|
||||
|
||||
rt_err_t rt_wlan_dev_cfg_filter(struct rt_wlan_device *device, struct rt_wlan_filter *filter);
|
||||
|
||||
rt_err_t rt_wlan_dev_set_channel(struct rt_wlan_device *device, int channel);
|
||||
|
||||
rt_err_t rt_wlan_dev_get_channel(struct rt_wlan_device *device);
|
||||
|
||||
rt_err_t rt_wlan_dev_set_country(struct rt_wlan_device *device, rt_country_code_t country_code);
|
||||
|
||||
rt_country_code_t rt_wlan_dev_get_country(struct rt_wlan_device *device);
|
||||
|
||||
rt_err_t rt_wlan_dev_scan(struct rt_wlan_device *device, struct rt_wlan_info *info);
|
||||
|
||||
rt_err_t rt_wlan_dev_scan_stop(struct rt_wlan_device *device);
|
||||
|
||||
rt_err_t rt_wlan_dev_report_data(struct rt_wlan_device *device, void *buff, int len);
|
||||
|
||||
// void rt_wlan_dev_data_ready(struct rt_wlan_device *device, int len);
|
||||
|
||||
struct rt_wlan_device *rt_wlan_dev_register(const char *name, const struct rt_wlan_dev_ops *ops, rt_uint32_t flag, void *user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,478 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-14 tyx the first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <wlan_dev.h>
|
||||
#include <wlan_prot.h>
|
||||
#include <wlan_workqueue.h>
|
||||
#ifdef RT_USING_LWIP
|
||||
#include <netif/ethernetif.h>
|
||||
#include <lwip/netifapi.h>
|
||||
#ifdef LWIP_USING_DHCPD
|
||||
#include <dhcp_server.h>
|
||||
#endif
|
||||
|
||||
#define DBG_ENABLE
|
||||
#define DBG_LEVEL DBG_INFO
|
||||
#define DBG_SECTION_NAME "WLAN.lwip"
|
||||
#define DBG_COLOR
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct lwip_prot_des
|
||||
{
|
||||
struct rt_wlan_prot prot;
|
||||
struct eth_device eth;
|
||||
rt_int8_t connected_flag;
|
||||
struct rt_timer timer;
|
||||
struct rt_work work;
|
||||
};
|
||||
|
||||
static void netif_is_ready(struct rt_work *work, void *parameter)
|
||||
{
|
||||
ip_addr_t ip_addr_zero = { 0 };
|
||||
struct rt_wlan_device *wlan = parameter;
|
||||
struct lwip_prot_des *lwip_prot = (struct lwip_prot_des *)wlan->prot;
|
||||
struct eth_device *eth_dev = &lwip_prot->eth;
|
||||
char str[IP4ADDR_STRLEN_MAX];
|
||||
rt_base_t level;
|
||||
struct rt_wlan_buff buff;
|
||||
rt_uint32_t ip_addr[4];
|
||||
|
||||
rt_timer_stop(&lwip_prot->timer);
|
||||
if (ip_addr_cmp(&(eth_dev->netif->ip_addr), &ip_addr_zero) != 0)
|
||||
{
|
||||
rt_timer_start(&lwip_prot->timer);
|
||||
goto exit;
|
||||
}
|
||||
rt_memset(&ip_addr, 0, sizeof(ip_addr));
|
||||
#if LWIP_IPV4 && LWIP_IPV6
|
||||
if (eth_dev->netif->ip_addr.type == IPADDR_TYPE_V4)
|
||||
{
|
||||
ip_addr[0] = ip4_addr_get_u32(ð_dev->netif->ip_addr.u_addr.ip4);
|
||||
buff.data = &ip_addr[0];
|
||||
buff.len = sizeof(ip_addr[0]);
|
||||
}
|
||||
else if (eth_dev->netif->ip_addr.type == IPADDR_TYPE_V6)
|
||||
{
|
||||
*(ip6_addr_t *)(&ip_addr[0]) = eth_dev->netif->ip_addr.u_addr.ip6;
|
||||
buff.data = ip_addr;
|
||||
buff.len = sizeof(ip_addr);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_W("F:%s L:%d ip addr type not support", __FUNCTION__, __LINE__);
|
||||
}
|
||||
#else
|
||||
#if LWIP_IPV4
|
||||
ip_addr[0] = ip4_addr_get_u32(ð_dev->netif->ip_addr);
|
||||
buff.data = &ip_addr[0];
|
||||
buff.len = sizeof(ip_addr[0]);
|
||||
#else
|
||||
*(ip_addr_t *)(&ip_addr[0]) = eth_dev->netif->ip_addr;
|
||||
buff.data = ip_addr;
|
||||
buff.len = sizeof(ip_addr);
|
||||
#endif
|
||||
#endif
|
||||
if (rt_wlan_prot_ready(wlan, &buff) != 0)
|
||||
{
|
||||
rt_timer_start(&lwip_prot->timer);
|
||||
goto exit;
|
||||
}
|
||||
rt_memset(str, 0, IP4ADDR_STRLEN_MAX);
|
||||
rt_enter_critical();
|
||||
rt_memcpy(str, ipaddr_ntoa(&(eth_dev->netif->ip_addr)), IP4ADDR_STRLEN_MAX);
|
||||
rt_exit_critical();
|
||||
LOG_I("Got IP address : %s", str);
|
||||
exit:
|
||||
level = rt_hw_interrupt_disable();
|
||||
rt_memset(work, 0, sizeof(struct rt_work));
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
|
||||
static void timer_callback(void *parameter)
|
||||
{
|
||||
struct rt_workqueue *workqueue;
|
||||
struct rt_wlan_device *wlan = parameter;
|
||||
struct lwip_prot_des *lwip_prot = (struct lwip_prot_des *)wlan->prot;
|
||||
struct rt_work *work = &lwip_prot->work;
|
||||
rt_base_t level;
|
||||
|
||||
workqueue = rt_wlan_get_workqueue();
|
||||
if (workqueue != RT_NULL)
|
||||
{
|
||||
level = rt_hw_interrupt_disable();
|
||||
rt_work_init(work, netif_is_ready, parameter);
|
||||
rt_hw_interrupt_enable(level);
|
||||
if (rt_workqueue_dowork(workqueue, work) != RT_EOK)
|
||||
{
|
||||
level = rt_hw_interrupt_disable();
|
||||
rt_memset(work, 0, sizeof(struct rt_work));
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void netif_set_connected(void *parameter)
|
||||
{
|
||||
struct rt_wlan_device *wlan = parameter;
|
||||
struct lwip_prot_des *lwip_prot = wlan->prot;
|
||||
struct eth_device *eth_dev = &lwip_prot->eth;
|
||||
|
||||
if (lwip_prot->connected_flag)
|
||||
{
|
||||
if (wlan->mode == RT_WLAN_STATION)
|
||||
{
|
||||
LOG_D("F:%s L:%d dhcp start run", __FUNCTION__, __LINE__);
|
||||
netifapi_netif_set_link_up(eth_dev->netif);
|
||||
#ifdef RT_LWIP_DHCP
|
||||
dhcp_start(eth_dev->netif);
|
||||
#endif
|
||||
rt_timer_start(&lwip_prot->timer);
|
||||
}
|
||||
else if (wlan->mode == RT_WLAN_AP)
|
||||
{
|
||||
LOG_D("F:%s L:%d dhcpd start run", __FUNCTION__, __LINE__);
|
||||
|
||||
netifapi_netif_set_link_up(eth_dev->netif);
|
||||
#ifdef LWIP_USING_DHCPD
|
||||
{
|
||||
char netif_name[8];
|
||||
int i;
|
||||
|
||||
rt_memset(netif_name, 0, sizeof(netif_name));
|
||||
for (i = 0; i < sizeof(eth_dev->netif->name); i++)
|
||||
{
|
||||
netif_name[i] = eth_dev->netif->name[i];
|
||||
}
|
||||
dhcpd_start(netif_name);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (wlan->mode == RT_WLAN_STATION)
|
||||
{
|
||||
LOG_D("F:%s L:%d dhcp stop run", __FUNCTION__, __LINE__);
|
||||
netifapi_netif_set_link_down(eth_dev->netif);
|
||||
#ifdef RT_LWIP_DHCP
|
||||
{
|
||||
ip4_addr_t ip_addr = { 0 };
|
||||
dhcp_stop(eth_dev->netif);
|
||||
netif_set_addr(eth_dev->netif, &ip_addr, &ip_addr, &ip_addr);
|
||||
}
|
||||
#endif
|
||||
rt_timer_stop(&lwip_prot->timer);
|
||||
}
|
||||
else if (wlan->mode == RT_WLAN_AP)
|
||||
{
|
||||
LOG_D("F:%s L:%d dhcpd stop run", __FUNCTION__, __LINE__);
|
||||
netifapi_netif_set_link_down(eth_dev->netif);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void rt_wlan_lwip_event_handle(struct rt_wlan_prot *port, struct rt_wlan_device *wlan, int event)
|
||||
{
|
||||
struct lwip_prot_des *lwip_prot = (struct lwip_prot_des *)wlan->prot;
|
||||
rt_bool_t flag_old;
|
||||
|
||||
flag_old = lwip_prot->connected_flag;
|
||||
|
||||
switch (event)
|
||||
{
|
||||
case RT_WLAN_PROT_EVT_CONNECT:
|
||||
{
|
||||
LOG_D("event: CONNECT");
|
||||
lwip_prot->connected_flag = RT_TRUE;
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_PROT_EVT_DISCONNECT:
|
||||
{
|
||||
LOG_D("event: DISCONNECT");
|
||||
lwip_prot->connected_flag = RT_FALSE;
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_PROT_EVT_AP_START:
|
||||
{
|
||||
LOG_D("event: AP_START");
|
||||
lwip_prot->connected_flag = RT_TRUE;
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_PROT_EVT_AP_STOP:
|
||||
{
|
||||
LOG_D("event: AP_STOP");
|
||||
lwip_prot->connected_flag = RT_FALSE;
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_PROT_EVT_AP_ASSOCIATED:
|
||||
{
|
||||
LOG_D("event: ASSOCIATED");
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_PROT_EVT_AP_DISASSOCIATED:
|
||||
{
|
||||
LOG_D("event: DISASSOCIATED");
|
||||
break;
|
||||
}
|
||||
default :
|
||||
{
|
||||
LOG_D("event: UNKNOWN");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag_old != lwip_prot->connected_flag)
|
||||
{
|
||||
rt_wlan_workqueue_dowork(netif_set_connected, wlan);
|
||||
// netif_set_connected(wlan);
|
||||
}
|
||||
}
|
||||
|
||||
static rt_err_t rt_wlan_lwip_protocol_control(rt_device_t device, int cmd, void *args)
|
||||
{
|
||||
struct eth_device *eth_dev = (struct eth_device *)device;
|
||||
struct rt_wlan_device *wlan;
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
RT_ASSERT(eth_dev != RT_NULL);
|
||||
|
||||
LOG_D("F:%s L:%d device:0x%08x user_data:0x%08x", __FUNCTION__, __LINE__, eth_dev, eth_dev->parent.user_data);
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case NIOCTL_GADDR:
|
||||
/* get MAC address */
|
||||
wlan = eth_dev->parent.user_data;
|
||||
err = rt_device_control((rt_device_t)wlan, RT_WLAN_CMD_GET_MAC, args);
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t rt_wlan_lwip_protocol_recv(struct rt_wlan_device *wlan, void *buff, int len)
|
||||
{
|
||||
struct eth_device *eth_dev = &((struct lwip_prot_des *)wlan->prot)->eth;
|
||||
struct pbuf *p = RT_NULL;
|
||||
|
||||
LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
|
||||
|
||||
if (eth_dev == RT_NULL)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE
|
||||
{
|
||||
p = buff;
|
||||
if ((eth_dev->netif->input(p, eth_dev->netif)) != ERR_OK)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
#else
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
while (p == RT_NULL)
|
||||
{
|
||||
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
|
||||
if (p != RT_NULL)
|
||||
break;
|
||||
|
||||
p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
|
||||
if (p != RT_NULL)
|
||||
break;
|
||||
|
||||
LOG_D("F:%s L:%d wait for pbuf_alloc!", __FUNCTION__, __LINE__);
|
||||
rt_thread_delay(1);
|
||||
count++;
|
||||
|
||||
//wait for 10ms or give up!!
|
||||
if (count >= 10)
|
||||
{
|
||||
LOG_W("F:%s L:%d pbuf allocate fail!!!", __FUNCTION__, __LINE__);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
}
|
||||
/*copy data dat -> pbuf*/
|
||||
pbuf_take(p, buff, len);
|
||||
if ((eth_dev->netif->input(p, eth_dev->netif)) != ERR_OK)
|
||||
{
|
||||
LOG_D("F:%s L:%d IP input error", __FUNCTION__, __LINE__);
|
||||
pbuf_free(p);
|
||||
p = RT_NULL;
|
||||
}
|
||||
LOG_D("F:%s L:%d netif iput success! len:%d", __FUNCTION__, __LINE__, len);
|
||||
return RT_EOK;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static rt_err_t rt_wlan_lwip_protocol_send(rt_device_t device, struct pbuf *p)
|
||||
{
|
||||
struct rt_wlan_device *wlan = ((struct eth_device *)device)->parent.user_data;
|
||||
|
||||
LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
|
||||
|
||||
if (wlan == RT_NULL)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE
|
||||
{
|
||||
rt_wlan_prot_transfer_dev(wlan, p, p->tot_len);
|
||||
return RT_EOK;
|
||||
}
|
||||
#else
|
||||
{
|
||||
rt_uint8_t *frame;
|
||||
|
||||
/* sending data directly */
|
||||
if (p->len == p->tot_len)
|
||||
{
|
||||
frame = (rt_uint8_t *)p->payload;
|
||||
rt_wlan_prot_transfer_dev(wlan, frame, p->tot_len);
|
||||
LOG_D("F:%s L:%d run len:%d", __FUNCTION__, __LINE__, p->tot_len);
|
||||
return RT_EOK;
|
||||
}
|
||||
frame = rt_malloc(p->tot_len);
|
||||
if (frame == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d malloc out_buf fail\n", __FUNCTION__, __LINE__);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
/*copy pbuf -> data dat*/
|
||||
pbuf_copy_partial(p, frame, p->tot_len, 0);
|
||||
/* send data */
|
||||
rt_wlan_prot_transfer_dev(wlan, frame, p->tot_len);
|
||||
LOG_D("F:%s L:%d run len:%d", __FUNCTION__, __LINE__, p->tot_len);
|
||||
rt_free(frame);
|
||||
return RT_EOK;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static struct rt_wlan_prot *rt_wlan_lwip_protocol_register(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan)
|
||||
{
|
||||
struct eth_device *eth = RT_NULL;
|
||||
static rt_uint8_t id = 0;
|
||||
char eth_name[4], timer_name[16];
|
||||
rt_device_t device = RT_NULL;
|
||||
struct lwip_prot_des *lwip_prot;
|
||||
|
||||
if (wlan == RT_NULL || prot == RT_NULL)
|
||||
return RT_NULL;;
|
||||
|
||||
LOG_D("F:%s L:%d is run wlan:0x%08x", __FUNCTION__, __LINE__, wlan);
|
||||
|
||||
do
|
||||
{
|
||||
/* find ETH device name */
|
||||
eth_name[0] = 'w';
|
||||
eth_name[1] = '0' + id++;
|
||||
eth_name[2] = '\0';
|
||||
device = rt_device_find(eth_name);
|
||||
}
|
||||
while (device);
|
||||
|
||||
if (id > 9)
|
||||
{
|
||||
LOG_E("F:%s L:%d not find Empty name", __FUNCTION__, __LINE__, eth_name);
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
if (rt_device_open((rt_device_t)wlan, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
|
||||
{
|
||||
LOG_E("F:%s L:%d open wlan failed", __FUNCTION__, __LINE__);
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
lwip_prot = rt_malloc(sizeof(struct lwip_prot_des));
|
||||
if (lwip_prot == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d malloc mem failed", __FUNCTION__, __LINE__);
|
||||
rt_device_close((rt_device_t)wlan);
|
||||
return RT_NULL;
|
||||
}
|
||||
rt_memset(lwip_prot, 0, sizeof(struct lwip_prot_des));
|
||||
|
||||
eth = &lwip_prot->eth;
|
||||
eth->parent.init = RT_NULL;
|
||||
eth->parent.open = RT_NULL;
|
||||
eth->parent.close = RT_NULL;
|
||||
eth->parent.read = RT_NULL;
|
||||
eth->parent.write = RT_NULL;
|
||||
eth->parent.control = rt_wlan_lwip_protocol_control;
|
||||
eth->parent.user_data = wlan;
|
||||
eth->eth_rx = RT_NULL;
|
||||
eth->eth_tx = rt_wlan_lwip_protocol_send;
|
||||
|
||||
/* register ETH device */
|
||||
if (eth_device_init(eth, eth_name) != RT_EOK)
|
||||
{
|
||||
LOG_E("eth device init failed");
|
||||
rt_device_close((rt_device_t)wlan);
|
||||
rt_free(lwip_prot);
|
||||
return RT_NULL;
|
||||
}
|
||||
rt_memcpy(&lwip_prot->prot, prot, sizeof(struct rt_wlan_prot));
|
||||
if (wlan->mode == RT_WLAN_STATION)
|
||||
{
|
||||
rt_sprintf(timer_name, "timer_%s", eth_name);
|
||||
rt_timer_init(&lwip_prot->timer, timer_name, timer_callback, wlan, rt_tick_from_millisecond(1000),
|
||||
RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT);
|
||||
}
|
||||
LOG_I("eth device init ok name:%s", eth_name);
|
||||
|
||||
return &lwip_prot->prot;
|
||||
}
|
||||
|
||||
static void rt_wlan_lwip_protocol_unregister(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan)
|
||||
{
|
||||
/*TODO*/
|
||||
LOG_D("F:%s L:%d is run wlan:0x%08x", __FUNCTION__, __LINE__, wlan);
|
||||
}
|
||||
|
||||
static struct rt_wlan_prot_ops ops =
|
||||
{
|
||||
rt_wlan_lwip_protocol_recv,
|
||||
rt_wlan_lwip_protocol_register,
|
||||
rt_wlan_lwip_protocol_unregister
|
||||
};
|
||||
|
||||
int rt_wlan_lwip_init(void)
|
||||
{
|
||||
static struct rt_wlan_prot prot;
|
||||
rt_wlan_prot_event_t event;
|
||||
|
||||
rt_memset(&prot, 0, sizeof(prot));
|
||||
rt_strncpy(&prot.name[0], RT_WLAN_PROT_LWIP, RT_WLAN_PROT_NAME_LEN);
|
||||
prot.ops = &ops;
|
||||
|
||||
if (rt_wlan_prot_regisetr(&prot) != RT_EOK)
|
||||
{
|
||||
LOG_E("F:%s L:%d protocol regisetr failed", __FUNCTION__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (event = RT_WLAN_PROT_EVT_INIT_DONE; event < RT_WLAN_PROT_EVT_MAX; event++)
|
||||
{
|
||||
rt_wlan_prot_event_register(&prot, event, rt_wlan_lwip_event_handle);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
INIT_PREV_EXPORT(rt_wlan_lwip_init);
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
|
@ -1,34 +1,154 @@
|
|||
/*
|
||||
* RT-Thread Wi-Fi Device
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* COPYRIGHT (C) 2014 - 2018, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-02-27 EvalZero the first verion
|
||||
* 2018-08-06 tyx the first version
|
||||
*/
|
||||
|
||||
#ifndef __WLAN_MGNT_H__
|
||||
#define __WLAN_MGNT_H__
|
||||
|
||||
int rt_wlan_mgnt_attach(struct rt_wlan_device *device, void *user_data);
|
||||
#include <wlan_dev.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_SCAN_WAIT_MS
|
||||
#define RT_WLAN_SCAN_WAIT_MS (10 * 1000)
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_SCAN_CACHE_NUM
|
||||
#define RT_WLAN_SCAN_CACHE_NUM (50)
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_CONNECT_WAIT_MS
|
||||
#define RT_WLAN_CONNECT_WAIT_MS (10 * 1000)
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_START_AP_WAIT_MS
|
||||
#define RT_WLAN_START_AP_WAIT_MS (10 * 1000)
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_EBOX_NUM
|
||||
#define RT_WLAN_EBOX_NUM (10)
|
||||
#endif
|
||||
|
||||
#if RT_WLAN_EBOX_NUM < 1
|
||||
#error "event box num Too little"
|
||||
#endif
|
||||
|
||||
/*state fot station*/
|
||||
#define RT_WLAN_STATE_CONNECT (0x1 << 0)
|
||||
#define RT_WLAN_STATE_CONNECTING (0x1 << 1)
|
||||
#define RT_WLAN_STATE_READY (0x1 << 2)
|
||||
#define RT_WLAN_STATE_POWERSAVE (0x1 << 3)
|
||||
|
||||
/*flags fot station*/
|
||||
#define RT_WLAN_STATE_AUTOEN (0x1 << 0)
|
||||
|
||||
/*state fot ap*/
|
||||
#define RT_WLAN_STATE_ACTIVE (0x1 << 0)
|
||||
|
||||
typedef enum
|
||||
{
|
||||
RT_WLAN_EVT_READY = 0, /* connect and prot is ok, You can send data*/
|
||||
RT_WLAN_EVT_SCAN_DONE, /* Scan a info */
|
||||
RT_WLAN_EVT_SCAN_REPORT, /* Scan end */
|
||||
RT_WLAN_EVT_STA_CONNECTED, /* connect success */
|
||||
RT_WLAN_EVT_STA_CONNECTED_FAIL, /* connection failed */
|
||||
RT_WLAN_EVT_STA_DISCONNECTED, /* disconnect */
|
||||
RT_WLAN_EVT_AP_START, /* AP start */
|
||||
RT_WLAN_EVT_AP_STOP, /* AP stop */
|
||||
RT_WLAN_EVT_AP_ASSOCIATED, /* sta associated */
|
||||
RT_WLAN_EVT_AP_DISASSOCIATED, /* sta disassociated */
|
||||
RT_WLAN_EVT_MAX
|
||||
} rt_wlan_event_t;
|
||||
|
||||
typedef void (*rt_wlan_event_handler)(int event, struct rt_wlan_buff *buff, void *parameter);
|
||||
|
||||
struct rt_wlan_scan_result
|
||||
{
|
||||
rt_int32_t num;
|
||||
struct rt_wlan_info *info;
|
||||
};
|
||||
|
||||
/*
|
||||
* wifi init interface
|
||||
*/
|
||||
int rt_wlan_init(void);
|
||||
rt_err_t rt_wlan_set_mode(const char *dev_name, rt_wlan_mode_t mode);
|
||||
rt_wlan_mode_t rt_wlan_get_mode(const char *dev_name);
|
||||
|
||||
/*
|
||||
* wifi station mode interface
|
||||
*/
|
||||
rt_err_t rt_wlan_connect(const char *ssid, const char *password);
|
||||
rt_err_t rt_wlan_connect_adv(struct rt_wlan_info *info, const char *password);
|
||||
rt_err_t rt_wlan_disconnect(void);
|
||||
int rt_wlan_is_connected(void);
|
||||
int rt_wlan_is_ready(void);
|
||||
rt_err_t rt_wlan_set_mac(rt_uint8_t *mac);
|
||||
rt_err_t rt_wlan_get_mac(rt_uint8_t *mac);
|
||||
rt_err_t rt_wlan_get_info(struct rt_wlan_info *info);
|
||||
int rt_wlan_get_rssi(void);
|
||||
|
||||
/*
|
||||
* wifi ap mode interface
|
||||
*/
|
||||
rt_err_t rt_wlan_start_ap(const char *ssid, const char *password);
|
||||
rt_err_t rt_wlan_start_ap_adv(struct rt_wlan_info *info, const char *password);
|
||||
int rt_wlan_ap_is_active(void);
|
||||
rt_err_t rt_wlan_ap_stop(void);
|
||||
rt_err_t rt_wlan_ap_get_info(struct rt_wlan_info *info);
|
||||
int rt_wlan_ap_get_sta_num(void);
|
||||
int rt_wlan_ap_get_sta_info(struct rt_wlan_info *info, int num);
|
||||
rt_err_t rt_wlan_ap_deauth_sta(rt_uint8_t *mac);
|
||||
rt_err_t rt_wlan_ap_set_country(rt_country_code_t country_code);
|
||||
rt_country_code_t rt_wlan_ap_get_country(void);
|
||||
|
||||
/*
|
||||
* wifi scan interface
|
||||
*/
|
||||
rt_err_t rt_wlan_scan(void);
|
||||
struct rt_wlan_scan_result *rt_wlan_scan_sync(void);
|
||||
struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info);
|
||||
int rt_wlan_scan_get_info_num(void);
|
||||
int rt_wlan_scan_get_info(struct rt_wlan_info *info, int num);
|
||||
struct rt_wlan_scan_result *rt_wlan_scan_get_result(void);
|
||||
void rt_wlan_scan_result_clean(void);
|
||||
int rt_wlan_scan_find_cache(struct rt_wlan_info *info, struct rt_wlan_info *out_info, int num);
|
||||
rt_bool_t rt_wlan_find_best_by_cache(const char *ssid, struct rt_wlan_info *info);
|
||||
|
||||
/*
|
||||
* wifi auto connect interface
|
||||
*/
|
||||
void rt_wlan_config_autoreconnect(rt_bool_t enable);
|
||||
rt_bool_t rt_wlan_get_autoreconnect_mode(void);
|
||||
|
||||
/*
|
||||
* wifi power management interface
|
||||
*/
|
||||
rt_err_t rt_wlan_enable_powersave(void);
|
||||
rt_err_t rt_wlan_disable_powersave(void);
|
||||
|
||||
/*
|
||||
* wifi event management interface
|
||||
*/
|
||||
rt_err_t rt_wlan_register_event_handler(rt_wlan_event_t event, rt_wlan_event_handler handler, void *parameter);
|
||||
rt_err_t rt_wlan_unregister_event_handler(rt_wlan_event_t event);
|
||||
|
||||
/*
|
||||
* wifi management lock interface
|
||||
*/
|
||||
void rt_wlan_mgnt_lock(void);
|
||||
void rt_wlan_mgnt_unlock(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,356 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-14 tyx the first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <wlan_dev.h>
|
||||
#include <wlan_prot.h>
|
||||
|
||||
#define DBG_ENABLE
|
||||
#define DBG_LEVEL DBG_INFO
|
||||
#define DBG_SECTION_NAME "WLAN.prot"
|
||||
#define DBG_COLOR
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct rt_wlan_prot_event_des
|
||||
{
|
||||
rt_wlan_prot_event_handler handler;
|
||||
struct rt_wlan_prot *prot;
|
||||
};
|
||||
|
||||
static struct rt_wlan_prot *_prot[RT_WLAN_PROT_MAX];
|
||||
|
||||
static struct rt_wlan_prot_event_des prot_event_tab[RT_WLAN_PROT_EVT_MAX][RT_WLAN_PROT_MAX];
|
||||
|
||||
static void rt_wlan_prot_event_handle(struct rt_wlan_device *wlan, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff, void *parameter)
|
||||
{
|
||||
int i;
|
||||
struct rt_wlan_prot *wlan_prot;
|
||||
struct rt_wlan_prot *prot;
|
||||
rt_wlan_prot_event_handler handler;
|
||||
rt_wlan_prot_event_t prot_event;
|
||||
|
||||
LOG_D("F:%s L:%d event:%d", __FUNCTION__, __LINE__, event);
|
||||
|
||||
wlan_prot = wlan->prot;
|
||||
handler = RT_NULL;
|
||||
prot = RT_NULL;
|
||||
switch (event)
|
||||
{
|
||||
case RT_WLAN_DEV_EVT_INIT_DONE:
|
||||
{
|
||||
LOG_D("L%d event: INIT_DONE", __LINE__);
|
||||
prot_event = RT_WLAN_PROT_EVT_INIT_DONE;
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_DEV_EVT_CONNECT:
|
||||
{
|
||||
LOG_D("L%d event: CONNECT", __LINE__);
|
||||
prot_event = RT_WLAN_PROT_EVT_CONNECT;
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_DEV_EVT_DISCONNECT:
|
||||
{
|
||||
LOG_D("L%d event: DISCONNECT", __LINE__);
|
||||
prot_event = RT_WLAN_PROT_EVT_DISCONNECT;
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_DEV_EVT_AP_START:
|
||||
{
|
||||
LOG_D("L%d event: AP_START", __LINE__);
|
||||
prot_event = RT_WLAN_PROT_EVT_AP_START;
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_DEV_EVT_AP_STOP:
|
||||
{
|
||||
LOG_D("L%d event: AP_STOP", __LINE__);
|
||||
prot_event = RT_WLAN_PROT_EVT_AP_STOP;
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_DEV_EVT_AP_ASSOCIATED:
|
||||
{
|
||||
LOG_D("L%d event: AP_ASSOCIATED", __LINE__);
|
||||
prot_event = RT_WLAN_PROT_EVT_AP_ASSOCIATED;
|
||||
break;
|
||||
}
|
||||
case RT_WLAN_DEV_EVT_AP_DISASSOCIATED:
|
||||
{
|
||||
LOG_D("L%d event: AP_DISASSOCIATED", __LINE__);
|
||||
prot_event = RT_WLAN_PROT_EVT_AP_DISASSOCIATED;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < RT_WLAN_PROT_MAX; i++)
|
||||
{
|
||||
if ((prot_event_tab[prot_event][i].handler != RT_NULL) &&
|
||||
(prot_event_tab[prot_event][i].prot->id == wlan_prot->id))
|
||||
{
|
||||
handler = prot_event_tab[prot_event][i].handler;
|
||||
prot = prot_event_tab[prot_event][i].prot;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (handler != RT_NULL)
|
||||
{
|
||||
handler(prot, wlan, prot_event);
|
||||
}
|
||||
}
|
||||
|
||||
static struct rt_wlan_device *rt_wlan_prot_find_by_name(const char *name)
|
||||
{
|
||||
rt_device_t device;
|
||||
|
||||
if (name == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d Parameter Wrongful", __FUNCTION__, __LINE__);
|
||||
return RT_NULL;
|
||||
}
|
||||
device = rt_device_find(name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d not find wlan dev!! name:%s", __FUNCTION__, __LINE__, name);
|
||||
return RT_NULL;
|
||||
}
|
||||
return (struct rt_wlan_device *)device;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_prot_attach(const char *dev_name, const char *prot_name)
|
||||
{
|
||||
struct rt_wlan_device *wlan;
|
||||
|
||||
wlan = rt_wlan_prot_find_by_name(dev_name);
|
||||
if (wlan == RT_NULL)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return rt_wlan_prot_attach_dev(wlan, prot_name);
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_prot_detach(const char *name)
|
||||
{
|
||||
struct rt_wlan_device *wlan;
|
||||
|
||||
wlan = rt_wlan_prot_find_by_name(name);
|
||||
if (wlan == RT_NULL)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return rt_wlan_prot_detach_dev(wlan);
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_prot_attach_dev(struct rt_wlan_device *wlan, const char *prot_name)
|
||||
{
|
||||
int i = 0;
|
||||
struct rt_wlan_prot *prot = wlan->prot;
|
||||
rt_wlan_dev_event_t event;
|
||||
|
||||
if (wlan == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d wlan is null", __FUNCTION__, __LINE__);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (prot != RT_NULL &&
|
||||
(rt_strcmp(prot->name, prot_name) == 0))
|
||||
{
|
||||
LOG_D("prot is register");
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/* if prot not NULL */
|
||||
if (prot != RT_NULL)
|
||||
rt_wlan_prot_detach_dev(wlan);
|
||||
|
||||
#ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE
|
||||
if (rt_strcmp(RT_WLAN_PROT_LWIP, prot_name) != 0)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#endif
|
||||
/* find prot */
|
||||
for (i = 0; i < RT_WLAN_PROT_MAX; i++)
|
||||
{
|
||||
if ((_prot[i] != RT_NULL) && (rt_strcmp(_prot[i]->name, prot_name) == 0))
|
||||
{
|
||||
/* attach prot */
|
||||
wlan->prot = _prot[i]->ops->dev_reg_callback(_prot[i], wlan);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i >= RT_WLAN_PROT_MAX)
|
||||
{
|
||||
LOG_E("F:%s L:%d not find wlan protocol", __FUNCTION__, __LINE__);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
for (event = RT_WLAN_DEV_EVT_INIT_DONE; event < RT_WLAN_DEV_EVT_MAX; event ++)
|
||||
{
|
||||
if (rt_wlan_dev_register_event_handler(wlan, event, rt_wlan_prot_event_handle, RT_NULL) != RT_EOK)
|
||||
{
|
||||
LOG_E("prot register event filed:%d", event);
|
||||
}
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_prot_detach_dev(struct rt_wlan_device *wlan)
|
||||
{
|
||||
struct rt_wlan_prot *prot = wlan->prot;
|
||||
rt_wlan_dev_event_t event;
|
||||
|
||||
if (prot == RT_NULL)
|
||||
return RT_EOK;
|
||||
|
||||
for (event = RT_WLAN_DEV_EVT_INIT_DONE; event < RT_WLAN_DEV_EVT_MAX; event ++)
|
||||
{
|
||||
rt_wlan_dev_unregister_event_handler(wlan, event, rt_wlan_prot_event_handle);
|
||||
}
|
||||
|
||||
/* detach prot */
|
||||
prot->ops->dev_unreg_callback(prot, wlan);
|
||||
wlan->prot = RT_NULL;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_prot_regisetr(struct rt_wlan_prot *prot)
|
||||
{
|
||||
int i;
|
||||
rt_uint32_t id;
|
||||
static rt_uint8_t num;
|
||||
|
||||
/* Parameter checking */
|
||||
if ((prot == RT_NULL) ||
|
||||
(prot->ops->prot_recv == RT_NULL) ||
|
||||
(prot->ops->dev_reg_callback == RT_NULL))
|
||||
{
|
||||
LOG_E("F:%s L:%d Parameter Wrongful", __FUNCTION__, __LINE__);
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
/* save prot */
|
||||
for (i = 0; i < RT_WLAN_PROT_MAX; i++)
|
||||
{
|
||||
if (_prot[i] == RT_NULL)
|
||||
{
|
||||
id = (RT_LWAN_ID_PREFIX << 16) | num;
|
||||
prot->id = id;
|
||||
_prot[i] = prot;
|
||||
num ++;
|
||||
break;
|
||||
}
|
||||
else if (rt_strcmp(_prot[i]->name, prot->name) == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* is full */
|
||||
if (i >= RT_WLAN_PROT_MAX)
|
||||
{
|
||||
LOG_E("F:%s L:%d Space full", __FUNCTION__, __LINE__);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_prot_event_register(struct rt_wlan_prot *prot, rt_wlan_prot_event_t event, rt_wlan_prot_event_handler handler)
|
||||
{
|
||||
int i;
|
||||
|
||||
if ((prot == RT_NULL) || (handler == RT_NULL))
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
for (i = 0; i < RT_WLAN_PROT_MAX; i++)
|
||||
{
|
||||
if (prot_event_tab[event][i].handler == RT_NULL)
|
||||
{
|
||||
prot_event_tab[event][i].handler = handler;
|
||||
prot_event_tab[event][i].prot = prot;
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_prot_event_unregister(struct rt_wlan_prot *prot, rt_wlan_prot_event_t event)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (prot == RT_NULL)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
for (i = 0; i < RT_WLAN_PROT_MAX; i++)
|
||||
{
|
||||
if ((prot_event_tab[event][i].handler != RT_NULL) &&
|
||||
(prot_event_tab[event][i].prot == prot))
|
||||
{
|
||||
rt_memset(&prot_event_tab[event][i], 0, sizeof(struct rt_wlan_prot_event_des));
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_prot_transfer_dev(struct rt_wlan_device *wlan, void *buff, int len)
|
||||
{
|
||||
if (wlan->ops->wlan_send != RT_NULL)
|
||||
{
|
||||
return wlan->ops->wlan_send(wlan, buff, len);
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_dev_transfer_prot(struct rt_wlan_device *wlan, void *buff, int len)
|
||||
{
|
||||
struct rt_wlan_prot *prot = wlan->prot;
|
||||
|
||||
if (prot != RT_NULL)
|
||||
{
|
||||
return prot->ops->prot_recv(wlan, buff, len);
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
extern int rt_wlan_prot_ready_event(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff);
|
||||
int rt_wlan_prot_ready(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff)
|
||||
{
|
||||
return rt_wlan_prot_ready_event(wlan, buff);
|
||||
}
|
||||
|
||||
void rt_wlan_prot_dump(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
rt_kprintf(" name id \n");
|
||||
rt_kprintf("-------- --------\n");
|
||||
for (i = 0; i < RT_WLAN_PROT_MAX; i++)
|
||||
{
|
||||
if (_prot[i] != RT_NULL)
|
||||
{
|
||||
rt_kprintf("%-8.8s ", _prot[i]->name);
|
||||
rt_kprintf("%08x\n", _prot[i]->id);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-14 tyx the first version
|
||||
*/
|
||||
|
||||
#ifndef __WLAN_PROT_H__
|
||||
#define __WLAN_PROT_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_PROT_NAME_LEN
|
||||
#define RT_WLAN_PROT_NAME_LEN (8)
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_PROT_MAX
|
||||
#define RT_WLAN_PROT_MAX (1)
|
||||
#endif
|
||||
|
||||
#define RT_LWAN_ID_PREFIX (0x5054)
|
||||
|
||||
#if RT_WLAN_PROT_NAME_LEN < 4
|
||||
#error "The name is too short"
|
||||
#endif
|
||||
|
||||
#define RT_WLAN_PROT_LWIP ("lwip")
|
||||
|
||||
typedef enum
|
||||
{
|
||||
RT_WLAN_PROT_EVT_INIT_DONE = 0,
|
||||
RT_WLAN_PROT_EVT_CONNECT,
|
||||
RT_WLAN_PROT_EVT_DISCONNECT,
|
||||
RT_WLAN_PROT_EVT_AP_START,
|
||||
RT_WLAN_PROT_EVT_AP_STOP,
|
||||
RT_WLAN_PROT_EVT_AP_ASSOCIATED,
|
||||
RT_WLAN_PROT_EVT_AP_DISASSOCIATED,
|
||||
RT_WLAN_PROT_EVT_MAX,
|
||||
} rt_wlan_prot_event_t;
|
||||
|
||||
struct rt_wlan_prot;
|
||||
struct rt_wlan_prot_ops
|
||||
{
|
||||
rt_err_t (*prot_recv)(struct rt_wlan_device *wlan, void *buff, int len);
|
||||
struct rt_wlan_prot *(*dev_reg_callback)(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan);
|
||||
void (*dev_unreg_callback)(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan);
|
||||
};
|
||||
|
||||
struct rt_wlan_prot
|
||||
{
|
||||
char name[RT_WLAN_PROT_NAME_LEN];
|
||||
rt_uint32_t id;
|
||||
const struct rt_wlan_prot_ops *ops;
|
||||
};
|
||||
|
||||
typedef void (*rt_wlan_prot_event_handler)(struct rt_wlan_prot *port, struct rt_wlan_device *wlan, int event);
|
||||
|
||||
rt_err_t rt_wlan_prot_attach(const char *dev_name, const char *prot_name);
|
||||
|
||||
rt_err_t rt_wlan_prot_attach_dev(struct rt_wlan_device *wlan, const char *prot_name);
|
||||
|
||||
rt_err_t rt_wlan_prot_detach(const char *dev_name);
|
||||
|
||||
rt_err_t rt_wlan_prot_detach_dev(struct rt_wlan_device *wlan);
|
||||
|
||||
rt_err_t rt_wlan_prot_regisetr(struct rt_wlan_prot *prot);
|
||||
|
||||
rt_err_t rt_wlan_prot_transfer_dev(struct rt_wlan_device *wlan, void *buff, int len);
|
||||
|
||||
rt_err_t rt_wlan_dev_transfer_prot(struct rt_wlan_device *wlan, void *buff, int len);
|
||||
|
||||
rt_err_t rt_wlan_prot_event_register(struct rt_wlan_prot *prot, rt_wlan_prot_event_t event, rt_wlan_prot_event_handler handler);
|
||||
|
||||
rt_err_t rt_wlan_prot_event_unregister(struct rt_wlan_prot *prot, rt_wlan_prot_event_t event);
|
||||
|
||||
int rt_wlan_prot_ready(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff);
|
||||
|
||||
void rt_wlan_prot_dump(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-19 tyx the first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <wlan_workqueue.h>
|
||||
#include <ipc/workqueue.h>
|
||||
|
||||
#define DBG_ENABLE
|
||||
#define DBG_LEVEL DBG_INFO
|
||||
#define DBG_SECTION_NAME "WLAN.work"
|
||||
#define DBG_COLOR
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct rt_wlan_work
|
||||
{
|
||||
struct rt_work work;
|
||||
void (*fun)(void *parameter);
|
||||
void *parameter;
|
||||
};
|
||||
|
||||
static struct rt_workqueue *wlan_workqueue;
|
||||
|
||||
static void rt_wlan_workqueue_fun(struct rt_work *work, void *work_data)
|
||||
{
|
||||
struct rt_wlan_work *wlan_work = work_data;
|
||||
|
||||
wlan_work->fun(wlan_work->parameter);
|
||||
rt_free(wlan_work);
|
||||
}
|
||||
|
||||
struct rt_workqueue *rt_wlan_get_workqueue(void)
|
||||
{
|
||||
return wlan_workqueue;
|
||||
}
|
||||
|
||||
rt_err_t rt_wlan_workqueue_dowork(void (*func)(void *parameter), void *parameter)
|
||||
{
|
||||
struct rt_wlan_work *wlan_work;
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
LOG_D("F:%s is run", __FUNCTION__);
|
||||
if (func == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d func is null", __FUNCTION__, __LINE__);
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
if (wlan_workqueue == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d not init wlan work queue", __FUNCTION__, __LINE__);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
wlan_work = rt_malloc(sizeof(struct rt_wlan_work));
|
||||
if (wlan_work == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d create work failed", __FUNCTION__, __LINE__);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
wlan_work->fun = func;
|
||||
wlan_work->parameter = parameter;
|
||||
rt_work_init(&wlan_work->work, rt_wlan_workqueue_fun, wlan_work);
|
||||
err = rt_workqueue_dowork(wlan_workqueue, &wlan_work->work);
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
LOG_E("F:%s L:%d do work failed", __FUNCTION__, __LINE__);
|
||||
rt_free(wlan_work);
|
||||
return err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int rt_wlan_workqueue_init(void)
|
||||
{
|
||||
static rt_int8_t _init_flag = 0;
|
||||
|
||||
if (_init_flag == 0)
|
||||
{
|
||||
wlan_workqueue = rt_workqueue_create(RT_WLAN_WORKQUEUE_THREAD_NAME, RT_WLAN_WORKQUEUE_THREAD_SIZE,
|
||||
RT_WLAN_WORKQUEUE_THREAD_PRIO);
|
||||
if (wlan_workqueue == RT_NULL)
|
||||
{
|
||||
LOG_E("F:%s L:%d wlan work queue create failed", __FUNCTION__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
_init_flag = 1;
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
INIT_PREV_EXPORT(rt_wlan_workqueue_init);
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-19 tyx the first version
|
||||
*/
|
||||
|
||||
#ifndef __WLAN_WORKQUEUE_H__
|
||||
#define __WLAN_WORKQUEUE_H__
|
||||
|
||||
#include <ipc/workqueue.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_WORKQUEUE_THREAD_NAME
|
||||
#define RT_WLAN_WORKQUEUE_THREAD_NAME ("wlan_job")
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_WORKQUEUE_THREAD_SIZE
|
||||
#define RT_WLAN_WORKQUEUE_THREAD_SIZE (2048)
|
||||
#endif
|
||||
|
||||
#ifndef RT_WLAN_WORKQUEUE_THREAD_PRIO
|
||||
#define RT_WLAN_WORKQUEUE_THREAD_PRIO (20)
|
||||
#endif
|
||||
|
||||
int rt_wlan_workqueue_init(void);
|
||||
|
||||
rt_err_t rt_wlan_workqueue_dowork(void (*func)(void *parameter), void *parameter);
|
||||
|
||||
struct rt_workqueue *rt_wlan_get_workqueue(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue