OpenHarmony设备开发(四)-WIFI_AP开发

数据库 MySQL
本文章主要讲轻量化系统的WIFI的AP连接,即是打开WIFI的热点。本文适用于OpenHarmony3.1的轻量化系统设备.

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

前言

本文章主要讲轻量化系统的WIFI的AP连接,即是打开WIFI的热点。本文适用于OpenHarmony3.1的轻量化系统设备。

设计流程

wifiAPTask主线程函数

  1. 注册wifi事件的回调函数RegisterWifiEvent(WifiEvent* event)。
  2. 初始化wifi热点相关配置SetHotspotConfig(const HotspotConfig* config)。
  3. 启动wifi热点模式EnableHotspot()。
  4. 检查热点是否正确地启用IsHotspotActive()。
  5. 启动DHCP。
  • 查找接口:netifapi_netif_find()。
  • 改变IP_add的配置:netifapi_netif_set_addr()。
  • 启动dhcp服务:netifapi_dhcps_start()。

线程创建

static void Wifi_AP_Demo(void)
{
osThreadAttr_t attr;
attr.name = "WifiAPTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = 25;
if (osThreadNew((osThreadFunc_t)WifiAPTask, NULL, &attr) == NULL)
{
printf("Falied to create WifiAPTask!\r\n");
}
}
SYS_RUN(Wifi_AP_Demo);

代码分析

1、注册wifi事件的回调函数

首先要创建一个指向wifi事件回调的指针,用于热点连接、断开或扫描时调用回调函数,便于相对应时刻的操作。(若不需要回调函数,设置该指针为NULL)。

WifiEvent g_wifiEventHandler = {0};

再来介绍一下wifEvent结构体对象,.OnHotspotStaJoin是绑定STA站点加入时的回调函数,.OnHotspotStaLeave是STA退出时的回调函数,.OnHotspotStateChanged是状态改变回调函数,我们通常设置这三个回调函数即可。

typedef struct {
/** Connection state change */
void (*OnWifiConnectionChanged)(int state, WifiLinkedInfo *info);
/** Scan state change */
void (*OnWifiScanStateChanged)(int state, int size);
/** Hotspot state change */
void (*OnHotspotStateChanged)(int state);
/** Station connected */
void (*OnHotspotStaJoin)(StationInfo *info);
/** Station disconnected */
void (*OnHotspotStaLeave)(StationInfo *info);
} WifiEvent;

最后使用RegisterWifiEvent函数调用g_wifiEventHandler指针的数据,指定wifi回调函数,该函数返回值为0即是成功。

//注册wifi事件的回调函数
g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
//指定WiFi回调函数
RegisterWifiEvent(&g_wifiEventHandler);

具体的回调函数在文章后面细述。

2、初始化wifi热点相关配置

首先创建一个配置热点的config指针,并初始化该指针。

//设置指定的热点配置
HotspotConfig config = {0};
//初始化热点相关配置
strcpy(config.ssid, "FSR_hispark"); //设置热点的SSID
strcpy(config.preSharedKey, "12345678"); //设置热点的密钥
config.securityType = WIFI_SEC_TYPE_PSK; //加密模式为PSK
config.band = HOTSPOT_BAND_TYPE_2G; //设置频段为2.4GHz
config.channelNum = 7; //热点的信道数

随后使用 SetHotspotConfig函数配置wifi热点,该函数返回值为0即是成功。

//配置wifi热点
SetHotspotConfig(&config);

3、启动和检查wifi

启动wifi:

error = EnableHotspot();
if (error != WIFI_SUCCESS) //返回值为0即是开启成功
{
printf("EnableHotspot failed, error = %d.\r\n", error);
return -1;
}

使用IsHotspotActive();函数,确认热点模式是否使能成功。

if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE)   //若不成功
{
printf("Wifi station is not actived.\r\n");
return -1;
}
printf("Wifi station is actived!\r\n");

4、启动DHCP

第一步先创建dhcp对象。

static struct netif *g_lwip_netif = NULL;
g_lwip_netif = netifapi_netif_find("ap0"); //查找网络接口

第二步是初始化dhcp相关配置,然后使用netifapi_netif_set_addr函数配置dhcp。

ip4_addr_t bp_gw;
ip4_addr_t bp_ipaddr;
ip4_addr_t bp_netmask;
IP4_ADDR(&bp_gw, 192, 168, 1, 1); /* 网关 */
IP4_ADDR(&bp_ipaddr, 192, 168, 1, 1); /* IP */
IP4_ADDR(&bp_netmask, 255, 255, 255, 0); /* 网络掩码 */
err_t ret = netifapi_netif_set_addr(g_lwip_netif, &bp_ipaddr, &bp_netmask, &bp_gw);
//ret为0即成功

第三步需要先将dhcp关闭!!!否则直接进行第四步开启dhcp会产生内存报错。

​​netifapi_dhcps_stop(g_lwip_netif);​​

第四步是开启dhcp

​​netifapi_dhcps_start(g_lwip_netif, 0, 0);​​
  • 参数一:dhcp对象。
  • 参数二:地址池的起始IP地址。
  • 参数三:需要加入IP地址池的IP地址数量。

回调函数

下面将介绍三个类型的回调函数。

状态改变回调函数

终端输出WIFI AP模式状态。

static void OnHotspotStateChangedHandler(int state)
{
printf("HotspotStateChanged:state is %d.\r\n", state);
if (state == WIFI_HOTSPOT_ACTIVE) // state=1表示已启用WIFI AP模式
{
printf("wifi hotspot active.\r\n");
}
else // state=0表示WIFI AP模式已禁用
{
printf("wifi hotspot noactive.\r\n");
}
}

STA退出回调函数

STA退出时打印mac地址。

static void OnHotspotStaLeaveHandler(StationInfo *info)
{
if (info == NULL)
{
printf("HotspotStaLeave:info is null.\r\n");
}
else
{
static char macAddress[32] = {0};
unsigned char *mac = info->macAddress;
snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("HotspotStaLeave: macAddress=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
//设备-1
g_apEnableSuccess--;
}
return;
}
绑定STA站点加入回调函数

打印出每个STA站点的MAC地址。

static void HotspotStaJoinTask(void)
{
static char macAddress[32] = {0};
StationInfo stainfo[WIFI_MAX_STA_NUM] = {0};
StationInfo *sta_list_node = NULL;
unsigned int size = WIFI_MAX_STA_NUM;

//获取当前接入到该AP的所有STA站点信息
error = GetStationList(stainfo, &size);
if (error != WIFI_SUCCESS)
{
printf("HotspotStaJoin:get list fail, error is %d.\r\n", error);
return;
}
sta_list_node = stainfo;
//打印出每个STA站点的MAC地址
for (uint32_t i = 0; i < size; i++, sta_list_node++)
{
unsigned char *mac = sta_list_node->macAddress;
snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("HotspotSta[%d]: macAddress=%s.\r\n", i, macAddress);
}
//记录设备+1
g_apEnableSuccess++;
}

//STA加入回调函数
static void OnHotspotStaJoinHandler(StationInfo *info)
{
if (info == NULL)
{
printf("HotspotStaJoin:info is null.\r\n");
}
else
{
//创建连接线程
printf("New Sta Join\n");
osThreadAttr_t attr;
attr.name = "HotspotStaJoinTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 2048;
attr.priority = 24;
if (osThreadNew((osThreadFunc_t)HotspotStaJoinTask, NULL, &attr) == NULL)
{
printf("HotspotStaJoin:create task fail!\r\n");
}
}
return;
}

效果图

【FFH】OpenHarmony设备开发(四)-WIFI_AP开发-开源基础软件社区

【FFH】OpenHarmony设备开发(四)-WIFI_AP开发-开源基础软件社区

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​​。

责任编辑:jianghua 来源: 51CTO开源基础软件社区
相关推荐

2022-09-06 15:25:22

Wifi设备开发

2022-10-24 14:54:29

LWIP协议鸿蒙

2022-10-25 14:51:11

设备开发鸿蒙

2022-02-15 14:06:36

OpenHarmon操作系统鸿蒙

2022-02-21 15:38:57

Openharmon操作系统鸿蒙

2022-09-07 15:35:49

设备开发鸿蒙

2022-07-29 14:29:24

设备开发鸿蒙

2022-02-15 14:45:14

OpenHarmo系统鸿蒙

2022-09-30 13:50:07

设备开发鸿蒙

2023-04-25 16:30:58

设备开发鸿蒙

2022-09-28 13:48:13

设备开发鸿蒙

2024-04-09 09:34:36

鸿蒙系统烧录操作系统

2022-02-09 19:25:49

Hi3861WiFi操作鸿蒙

2022-02-17 18:08:04

OpenHarmon应用开发鸿蒙

2016-09-27 10:51:43

2022-10-12 15:00:02

设备开发应用开发

2023-04-21 15:54:08

应用开发鸿蒙

2022-11-04 14:58:59

应用开发鸿蒙

2016-09-06 16:53:55

2022-05-19 15:40:37

配网开发设备开发
点赞
收藏

51CTO技术栈公众号