69 Star 68 Fork 994

OpenHarmony / drivers_peripheral

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README_zh.md 29.25 KB
一键复制 编辑 原始数据 按行查看 历史
郭园 提交于 2024-02-21 11:33 . peri coex

WLAN

简介

该仓下主要包含WLAN相关HDI和HAL接口与实现,提供主要功能包括:

  1. 创建和销毁IWiFi对象。
  2. 创建和销毁HAL层和WLAN驱动的通道。
  3. 设置MAC地址、设置国家码、获取网络设备信息等。

图 1 WLAN驱动模块架构图

目录

WLAN源代码目录结构如下所示:

/drivers/peripheral/wlan
├── client              # 实现用户态与内核态通信的client模块存放目录
│   └── include         # client相关头文件存放目录
│   └── src             # client代码存放目录
├── hal                 # hal层框架代码
│   └── include         # hal层相关头文件存放目录
│   └── src             # hal层代码存放目录
├── hdi_service         # hdi层框架代码
│   └── service_common  # hdi层通用接口存放目录
│   └── service_extend  # hdi扩展接口存放目录
├── interfaces          # hal接口的存放目录
│   └── include         # 对外提供的接口头文件存放目录

接口说明

WLAN驱动模块通过HAL/HDI层(HAL是直调,HDI是通过IPC调用)对Wi-Fi Service提供接口,主要功能有:创建/销毁IWiFi对象、设置MAC地址等。提供的接口说明如下表所示:

表 1 hal层和hdi层对外接口

头文件

HDI接口名称

头文件

HAL接口名称

功能描述

v1_3/iwlan_interface.h(V1_3为版本号,随着接口的增加而改变)

int32_t (*Start)(struct IWlanInterface *self);

wifi_hal.h

int32_t (*start)(struct IWiFi *);

创建HAL和驱动之间的通道及获取驱动支持的网卡信息。

int32_t (*Stop)(struct IWlanInterface *self);

int32_t (*stop)(struct IWiFi *);

销毁通道。

int32_t (*GetSupportFeature)(struct IWlanInterface *self, uint8_t* supType, uint32_t* supTypeLen);

int32_t (*getSupportFeature)(uint8_t *supType, uint32_t size);

获取该设备支持的WLAN特性(不考虑当前的使用状态)。

int32_t (*GetSupportCombo)(struct IWlanInterface *self, uint64_t* combo);

int32_t (*getSupportCombo)(uint64_t *combo, uint32_t size);

获取多网卡共存情况。

int32_t (*CreateFeature)(struct IWlanInterface *self, int32_t type, struct HdfFeatureInfo* ifeature);

int32_t (*createFeature)(int32_t type, struct IWiFiBaseFeature **ifeature);

根据输入类型创建对应的特性。

int32_t (*GetFeatureByIfName)(struct IWlanInterface *self, const char* ifName, struct HdfFeatureInfo* ifeature);

int32_t (*getFeatureByIfName)(const char *ifName, struct IWiFiBaseFeature **ifeature);

通过网络接口名字获取对应的特性。

int32_t (*RegisterEventCallback)(struct IWlanInterface *self, struct IWlanCallback* cbFunc, const char* ifName);

int32_t (*registerEventCallback)(CallbackFunc cbFunc);

注册IWiFi的回调函数,监听异步事件。

int32_t (*UnregisterEventCallback)(struct IWlanInterface *self, struct IWlanCallback* cbFunc, const char* ifName);

int32_t (*unRegisterEventCallback)(void);

去注册IWiFi的回调函数。

int32_t (*DestroyFeature)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature);

int32_t (*destroyFeature)(struct IWiFiBaseFeature *ifeature);

销毁对应的特性。

int32_t (*ResetDriver)(struct IWlanInterface *self, uint8_t chipId, const char* ifName);

int32_t (*resetDriver)(const uint8_t chipId);

WLAN驱动进行重置。

int32_t (*GetPowerMode)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, uint8_t* mode);

int32_t (*getPowerMode)(const char *ifName, uint8_t *mode);

获取正在使用的功率模式。

int32_t (*SetPowerMode)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, uint8_t mode);

int32_t (*setPowerMode)(const char *ifName, uint8_t mode);

设置功率模式。

int32_t (*StartChannelMeas)(struct IWlanInterface *self, const char* ifName, const struct MeasChannelParam* measChannelParam);

int32_t (*startChannelMeas)(const char *ifName, const struct MeasParam *measParam);

通过ioctl向底层驱动发送信道测量命令。

int32_t (*GetNetDevInfo)(struct IWlanInterface *self, struct HdfNetDeviceInfoResult* netDeviceInfoResult);

int32_t (*getNetDevInfo)(struct NetDeviceInfoResult *netDeviceInfoResult);

获取网络设备信息(设备索引、网卡名字、MAC等信息)。

int32_t (*GetChannelMeasResult)(struct IWlanInterface *self, const char* ifName, struct MeasChannelResult* measChannelResult);

int32_t (*getChannelMeasResult)(const char *ifName, struct MeasResult *measResult);

获取底层上报上来的测量结果。

int32_t (*GetAssociatedStas)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature,struct HdfStaInfo* staInfo, uint32_t* staInfoLen, uint32_t* num);

wifi_hal_ap_feature.h

int32_t (*getAssociatedStas)(const struct IWiFiAp *apFeature, struct StaInfo *staInfo, uint32_t count, uint32_t *num);

获取连接上的所有STA的信息(目前只包含MAC地址)。

int32_t (*SetCountryCode)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, const char* code, uint32_t len);

int32_t (*setCountryCode)(const struct IWiFiAp *apFeature, const char *code, uint32_t len);

设置国家码。

int32_t (*SetScanningMacAddress)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, const uint8_t* scanMac, uint32_t scanMacLen);

wifi_hal_sta_feature.h

int32_t (*setScanningMacAddres)(const struct IWiFiSta *staFeature, unsigned char *scanMac, uint8_t len);

设置扫描单个MAC地址。

int32_t (*StartScan)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, const struct HdfWifiScan* scan);

int32_t (*startScan)(const char *ifName, WifiScan *scan);

启动扫描。

int32_t (*GetNetworkIfaceName)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, char* ifName, uint32_t ifNameLen);

wifi_hal_base_feature.h

const char *(*getNetworkIfaceName)(const struct IWiFiBaseFeature *baseFeature);

获取网络接口的名字。

int32_t (*GetFeatureType)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, int32_t* featureType);

int32_t (*getFeatureType)(const struct IWiFiBaseFeature *);

获取特性的类型。

int32_t (*SetMacAddress)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, const uint8_t* mac, uint32_t macLen);

int32_t (*setMacAddress)(const struct IWiFiBaseFeature *, unsigned char *, uint8_t);

设置MAC地址。

int32_t (*GetDeviceMacAddress)(struct IWlanInterface *self, const struct HdfFeatureInfo *ifeature, uint8_t* mac, uint32_t* macLen, uint8_t len)

int32_t (*getDeviceMacAddress)(const struct IWiFiBaseFeature *, unsigned char *, uint8_t);

获取设备持久化的MAC地址。

int32_t (*GetFreqsWithBand)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, const struct HdfWifiInfo* wifiInfo, int32_t* freq, uint32_t* freqLen);

int32_t (*getValidFreqsWithBand)(const struct IWiFiBaseFeature *baseFeature, int32_t band, int32_t *freqs, uint32_t count, uint32_t *num);

获取指定频段(2.4G或者5G)下支持的频率。

int32_t (*SetTxPower)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, int32_t power);

int32_t (*setTxPower)(const struct IWiFiBaseFeature *, int32_t);

设置发射功率。

int32_t (*GetChipId)(struct IWlanInterface *self, const struct HdfFeatureInfo* ifeature, uint8_t* chipId);

int32_t (*getChipId)(const struct IWiFiBaseFeature *baseFeature, uint8_t *chipId);

获得当前驱动的芯片ID。

int32_t (*GetIfNamesByChipId)(struct IWlanInterface *self, uint8_t chipId, char* ifName, uint32_t ifNameLen, uint32_t* num);

int32_t (*getIfNamesByChipId)(const uint8_t chipId, char **ifNames, uint32_t *num);

通过芯片ID获得当前芯片所有的网卡名。

struct IWlanInterface *WlanInterfaceGetInstance(const char *serviceName);

获取WLAN服务对象。

struct IWlanCallback *WlanCallbackServiceGet(void);

获取WLAN回调服务。

void WlanInterfaceRelease(struct IWlanCallback *instance);

销毁WLAN服务对象。

void WlanCallbackServiceRelease(struct IWlanCallback *instance);

销毁WLAN回调服务。

使用说明(HAL接口)

HAL接口使用步骤:

  1. 使用WifiConstruct创建一个WiFi实体。
  2. 用创建的WiFi实体调用start开启HAL和驱动之间的通道,获得驱动网卡信息。
  3. 通过createFeature函数创建一个apFeature或者staFeature。后面可通过apFeature或者staFeature去调用具体的实现接口,下面代码示例基于创建的apFeature。
  4. 调用和使用相关接口:如setMacAddress设置MAC地址、getDeviceMacAddress获取设备的MAC地址等。
  5. 调用destroyFeature,销毁创建的Feature。
  6. 调用stop销毁创建的通道。
  7. 执行WifiDestruct销毁创建的WiFi实体。

代码示例:

#include "wifi_hal.h"
#include "wifi_hal_sta_feature.h"
#include "wifi_hal_ap_feature.h"
#include "wifi_hal_cmd.h"
#include "wifi_hal_event.h"

#define MAC_LEN 6
#define HDF_SUCCESS 0
#define HDF_FAILURE (-1)

static int32_t hal_main()
{
    int32_t ret;
    struct IWiFi *wifi;

    /* 创建一个WiFi实体 */
    ret = WifiConstruct(&wifi);
    if (ret != HDF_SUCCESS || wifi == NULL) {
        return HDF_FAILURE;
    }

    /* 开启HAL和驱动之间的通道 */
    ret = wifi->start(wifi);
    if (ret != HDF_SUCCESS) {
        return HDF_FAILURE;
    }

    /* 创建apFeature */
    ret = wifi->createFeature(PROTOCOL_80211_IFTYPE_AP, (struct IWiFiBaseFeature **)&apFeature);
    if (ret != HDF_SUCCESS) {
        return HDF_FAILURE;
    }

    /* 获取设备的MAC地址 */
    unsigned char mac[MAC_LEN] = {0};
    ret = apFeature->baseFeature.getDeviceMacAddress((struct IWiFiBaseFeature *)apFeature, mac, MAC_LEN);
    if (ret != HDF_SUCCESS) {
        return HDF_FAILURE;
    }

    /* 销毁掉创建的这个Feature */
    ret = wifi->destroyFeature((struct IWiFiBaseFeature *)apFeature);
    if (ret != HDF_SUCCESS) {
        return HDF_FAILURE;
    }

    /* 销毁创建的通道 */
    ret = wifi->stop(wifi);
    if (ret != HDF_SUCCESS) {
        return HDF_FAILURE;
    }

    /* 销毁创建的WiFi实体 */
    ret = WifiDestruct(&wifi);
    if (ret != HDF_SUCCESS) {
        return HDF_FAILURE;
    }
    return ret;
}

使用说明(HDI接口)

HDI接口使用步骤:

  1. 使用WlanInterfaceGetInstance获取WLAN服务对象。
  2. 使用Start创建HAL和驱动之间的通道。
  3. 通过GetSupportFeature获取该设备支持的WLAN特性。
  4. 调用Stop,销毁HAL和驱动之间的通道。
  5. 执行WlanInterfaceRelease销毁WLAN服务对象。

代码示例:

#include "v1_3/iwlan_interface.h"
#include "wlan_callback_impl.h"
#include "wlan_impl.h"

#define PROTOCOL_80211_IFTYPE_NUM 11
#define HDF_SUCCESS 0
#define HDF_FAILURE (-1)

static int32_t hdi_main()
{
	int32_t rc;
	const char *WLAN_SERVICE_NAME = "wlan_hal_c_service";
	static struct IWlanInterface *g_wlanObj = NULL;
	uint8_t supType[ PROTOCOL_80211_IFTYPE_NUM + 1 ] = {0};
    uint32_t supTypeLen = PROTOCOL_80211_IFTYPE_NUM + 1;
	
	/* 获取wlan服务对象。*/
	g_wlanObj = WlanInterfaceGetInstance(WLAN_SERVICE_NAME);
	if (g_wlanObj == NULL)
	{
		returnHDF_FAILURE;
	}
	
	/* 创建HAL和驱动之间的通道及获取驱动网卡信息。 */
	rc = g_wlanObj->Start(g_wlanObj);
	if (rc != HDF_SUCCESS)
	{
		return HDF_FAILURE;
	}
	
	/* 获取该设备支持的WLAN特性(不考虑当前的使用状态)。 */
	rc = g_wlanObj->GetSupportFeature(g_wlanObj, supType, &supTypeLen);
	if (rc != HDF_SUCCESS)
	{
		return HDF_FAILURE;
	}
	
	/* 销毁HAL和驱动之间的通道。 */
	rc = g_wlanObj->Stop(g_wlanObj);
	if (rc != HDF_SUCCESS)
	{
		return HDF_FAILURE;
	}
	
	/* 销毁wlan服务对象。 */
	WlanInterfaceRelease(g_wlanObj);
	
	return rc;
}

相关仓

驱动子系统

drivers_hdf_core

driver_interface

drivers_peripheral

1
https://gitee.com/openharmony/drivers_peripheral.git
git@gitee.com:openharmony/drivers_peripheral.git
openharmony
drivers_peripheral
drivers_peripheral
master

搜索帮助