3

【FFH】OpenHarmony设备开发(四)-WIFI_AP开发

 1 year ago
source link: https://blog.51cto.com/harmonyos/5707052
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【FFH】OpenHarmony设备开发(四)-WIFI_AP开发

推荐 原创

开源基础软件社区官方 2022-09-23 15:27:09 ©著作权

文章标签 南向开发 WIFI 文章分类 HarmonyOS 编程语言 阅读数316

Table of Contents

我们在第一篇文章中讲到了WIFI的STA连接,本文章主要讲轻量化系统的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/4.启动和检查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");

5.启动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开发_WIFI
【FFH】OpenHarmony设备开发(四)-WIFI_AP开发_南向开发_02

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

 51CTO 开源基础软件社区

 https://ost.51cto.com/#bkwz

  • 打赏
  • 1
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK