HarmonyOS小熊派 | HarmonyOSWiFi编程开发--Wifi AP热点

举报
Yuchuan 发表于 2021/08/06 08:31:56 2021/08/06
【摘要】 编写一个创建Wifi热点程序

本示例将演示如何在BearPi-HM_Nano开发板上编写一个创建Wifi热点程序

BearPi-HM_Nano

Wifi API分析

本案例主要使用了以下几个API完成Wifi热点创建

依赖文件:

//foundation/communication/wifi_lite/interfaces/wifiservice/wifi_device.h

//foundation/communication/wifi_lite/interfaces/wifiservice/wifi_hotspot.h

1、RegisterWifiEvent()

/**
 * @brief Registers a callback for a specified Wi-Fi event.
 *
 * The registered callback will be invoked when the Wi-Fi event defined in {@link WifiEvent} occurs. \n
 *
 * @param event Indicates the event for which the callback is to be registered.
 * @return Returns {@link WIFI_SUCCESS} if the callback is registered successfully; returns an error code defined
 * in {@link WifiErrorCode} otherwise.
 * @since 1.0
 * @version 1.0
 */
WifiErrorCode RegisterWifiEvent(WifiEvent* event);

描述: 为指定的Wi-Fi事件注册回调函数。当WifiEvent中定义的Wi-Fi事件发生时,将调用已注册的回调函数 参数:

名字 描述
event 表示要注册回调的事件.

2、EnableHotspot()

/**
 * @brief Enables the hotspot mode.
 *
 * Before using this function, you need to invoke {@link SetHotspotConfig} and set at least the SSID, security type,
 * and key. \n
 *
 * @return Returns {@link WIFI_SUCCESS} if the hotspot mode is enabled; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 1.0
 * @version 1.0
 */
WifiErrorCode EnableHotspot(void);

描述:

启用Wifi热点模式

3、SetHotspotConfig()

/**
 * @brief Sets a specified hotspot configuration.
 *
 * The hotspot configuration includes the SSID, security type, and key. The configuration set overwrites the existing
 * configuration and takes effect after the hotspot mode is re-enabled. \n
 * Before enabling the hotspot mode for the first time, you must call this function. \n
 *
 * @param config Indicates the hotspot configuration to set.
 * @return Returns {@link WIFI_SUCCESS} if the hotspot configuration is set; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 1.0
 * @version 1.0 */
WifiErrorCode SetHotspotConfig(const HotspotConfig* config);

描述:

设置指定的热点配置

4、IsHotspotActive()

/**
 * @brief Checks whether the hotspot mode is enabled.
 *
 * @return Returns {@link WIFI_HOTSPOT_ACTIVE} if the hotspot mode is enabled; returns {@link WIFI_HOTSPOT_NOT_ACTIVE}
 * otherwise.
 * @since 1.0
 * @version 1.0
 */
int IsHotspotActive(void);

描述: 检查AP热点模式是否启用

5、GetStationList()

/**
 * @brief Obtains an array of stations connected to this hotspot.
 *
 * The station information is defined in {@link StationInfo}. \n
 *
 * @param result Indicates the array of stations connected to this hotspot. The array is requested and released by the
 * caller. The value must be greater than or equal to {@link WIFI_MAX_STA_NUM}.
 * @param size Indicates the size of the array.
 * @return Returns {@link WIFI_SUCCESS} if the array of stations connected to this hotspot is obtained; returns an error
 *  code defined in {@link WifiErrorCode} otherwise.
 * @since 1.0
 * @version 1.0
 */
WifiErrorCode GetStationList(StationInfo* result, unsigned int* size);

描述:

获取连接到该热点的一系列STA

参数:

名字 描述
result 表示连接到该热点的STA列表.
size 表示连接到该热点的STA数量

软件设计

主要代码分析

完成Wifi热点的扫描需要以下几步

  1. 通过 RegisterWifiEvent 接口向系统注册热点状态改变事件、STA站点加入事件、STA站点退出事件

    • OnHotspotStateChangedHandler 用于绑定热点状态改变事件,该回调函数有一个参数 state 

      • state表示是否开启AP模式,取值为0和1,0表示已启用Wifi AP模式,1表示已禁用Wifi AP模式;
    • OnHotspotStaLeaveHandler 用于绑定STA站点退出事件,当有STA站点退出,该回调函数会打印出退出站点的MAC地址;

    • OnHotspotStaJoinHandler 用于绑定STA站点加入事件,当有新的STA站点加入时,该回调函数会创建 HotspotStaJoinTask,在该任务中会调用 GetStationList 函数获取当前接入到该AP的所有STA站点信息,并打印出每个STA站点的MAC地址;

  2. 调用 SetHotspotConfig 接口,设置指定的热点配置;

  3. 调用 EnableHotspot 接口,使能 Wifi AP 模式;

  4. 调用 IsHotspotActive 接口,检查AP热点模式是否启用;

  5. 调用 netifapi_netif_set_addr 函数设置网卡信息;

  6. 调用 netifapi_dhcps_start 函数启动dhcp服务;

yuchuan_wifi_ap.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "wifi_device.h"
#include "wifi_error_code.h"
#include "wifi_hotspot.h"
#include "lwip/netifapi.h"

#define TASK_CB_SIZE 0U
#define TASK_STACK_SIZE 1024 * 4
#define TASK_PRIORITY 25
#define AP_SSID "YuHua"
#define AP_PSK "dw926711"

static void OnHotspotStaJoinHandler(StationInfo *info);
static void OnHotspotStaLeaveHandler(StationInfo *info);
static void OnHotspotStateChangedHandler(int state);

/* static void OnHotspotStaJoinHandler(StationInfo *info);
static void OnHotspotStateChangedHandler(int state);
static void OnHotspotStaLeaveHandler(StationInfo *info); */


static struct netif *g_lwip_netif = NULL;
static int g_apEnableSuccess = 0;

WifiEvent g_wifiEventHandler = {0};
// WifiEvent g_wifiEventHandler = {0};
WifiErrorCode error;

/* 改变热点的回调函数 */
static void OnHotspotStateChangedHandler(int state)
{
    printf("OnHotspotStateChangedHandler: sta is %d.\r\n", state);
    if (state == WIFI_HOTSPOT_ACTIVE)
    {
        printf("wifi hotspot active\r\n");
    }
    else
    {
        printf("wifi hotspot noactive\r\n");
    }
}

/* 离开热点的回调函数 */
static void OnHotspotStaLeaveHandler(StationInfo *info)
{
    if (info == NULL)
    {
        printf("OnHotspotStaLeaveHandler : 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("OnHotspotStaLeaveHandler: macAddress=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
        g_apEnableSuccess--;
    }
    return;
}

/* 热点加入站点的任务 */
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;

    // WifiErrorCode GetStationList(StationInfo* result, unsigned int* size);
    error = GetStationList(stainfo, &size);
    if (error != WIFI_SUCCESS)
    {
        printf("hotspotStaJoinTask: get station list falied error code is %d.\r\n", error);
        return;
    }
    sta_list_node = stainfo;
    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);
    }
    g_apEnableSuccess++;
}

/* 加入热点的回调函数 */
static void OnHotspotStaJoinHandler(StationInfo *info)
{
    if (info == NULL)
    {
        printf("OnHotspotStaJoinHandler : info is null!!!\r\n");
    }
    else
    {
        printf("New Sta Join \r\n");
        osThreadAttr_t attr;

        attr.attr_bits = 0U;
        attr.cb_mem = NULL;
        attr.cb_size = 0U;
        attr.stack_mem = NULL;
        attr.stack_size = 2048;
        attr.priority = 24;
        attr.name = "HotspotStaJoinTask";

        if (osThreadNew((osThreadFunc_t)hotspotStaJoinTask, NULL, &attr) == NULL)
        {
            printf("Falied To Create HotspotStaJoinTask task!!!\r\n");
        }
    }
    return;
}

static BOOL YuchuanWifiApTask(void)
{
    // 延时2s便于查看日志
    osDelay(200);

    // 1、注册wifi事件的回调函数
    g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
    g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
    g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;

    /* g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
    g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
    g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler; */

    // WifiErrorCode RegisterWifiEvent(WifiEvent* event);
    error = RegisterWifiEvent(&g_wifiEventHandler);
    if (error != WIFI_SUCCESS)
    {
        printf("Register Wifi Event Falied, Error is %d.\r\n", error);
        return -1;
    }
    printf("Register Wifi Event Succed!\r\n");

    // 2、设置指定的热点配置
    HotspotConfig config = {0};

    strcpy(config.ssid, AP_SSID);
    strcpy(config.preSharedKey, AP_PSK);
    config.securityType = WIFI_SEC_TYPE_PSK;
    config.band = HOTSPOT_BAND_TYPE_2G;
    config.channelNum = 7;

    // 设置热点配置
    // WifiErrorCode SetHotspotConfig(const HotspotConfig* config);
    error = SetHotspotConfig(&config);
    if (error != WIFI_SUCCESS)
    {
        printf("Set Hotspot Config Falied Errot Code is %d.\r\n", error);
        return -1;
    }
    printf("Set Hotspot Config Succeed!!!\r\n");

    // 3、启动wifi 热点模式
    // WifiErrorCode EnableHotspot(void);
    error = EnableHotspot();
    if (error != WIFI_SUCCESS)
    {
        printf("Enable Wifi Hotspot Falied! Error Code is %d.\r\n", error);
        return -1;
    }
    printf("Enable Wifi Hotspot Succeed!!!\r\n");

    // 4、检查Wifi热点是否使能
    // int IsHotspotActive(void);
    if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE)
    {
        printf("Wifi Station is not Active.\r\n");
        return -1;
    }
    printf("Wifi Station is Actived!!!\r\n");

    // 5、启动DHCP
    // struct netif *netifapi_netif_find(const char *name);
    g_lwip_netif = netifapi_netif_find("ap0");
    if (g_lwip_netif)
    {
        ip4_addr_t bp_gw;
        ip4_addr_t bp_ipaddr;
        ip4_addr_t bp_netmask;

        IP4_ADDR(&bp_gw, 192, 168, 137, 0);       /* input your gateway for example: 192.168.137.0 */
        IP4_ADDR(&bp_ipaddr, 192, 168, 137, 109); /* input your IP for example: 192.168.137.109 */
        IP4_ADDR(&bp_netmask, 255, 255, 255, 0);  /* input your netmask for example: 255.255.255.0 */

        // err_t netifapi_netif_set_addr(struct netif *netif,const ip4_addr_t *ipaddr,const ip4_addr_t *netmask,const ip4_addr_t *gw);
        err_t ret = netifapi_netif_set_addr(g_lwip_netif, &bp_ipaddr, &bp_netmask, &bp_netmask);
        if (ret != ERR_OK)
        {
            printf("netifapi_netif_set_addr Falied Error Code is %d.\r\n", ret);
            return -1;
        }
        printf("netifapi_netif_set_addr Succed!!!\r\n");

        // err_t netifapi_dhcps_start(struct netif *netif, char *start_ip, u16_t ip_num);
        ret = netifapi_dhcps_start(g_lwip_netif, 0, 0);
        if (ret != ERR_OK)
        {
            printf("netifapi_dhcps_start Falied Error Code is %d.\r\n", ret);
            return -1;
        }
        printf("netifapi_dhcps_start Succed!!!\r\n");
    }

    /****************以下为UDP服务器代码***************/
    /****************以下为UDP服务器代码***************/
    /* //在sock_fd 进行监听
    int sock_fd;
    //服务端地址信息
    struct sockaddr_in server_sock;

    //创建socket
    if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
        perror("socket is error.\r\n");
        return -1;
    }

    bzero(&server_sock, sizeof(server_sock));
    server_sock.sin_family = AF_INET;
    server_sock.sin_addr.s_addr = htonl(INADDR_ANY);
    server_sock.sin_port = htons(8888);

    //调用bind函数绑定socket和地址
    if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1)
    {
        perror("bind is error.\r\n");
        return -1;
    }

    int ret;
    char recvBuf[512] = {0};
    //客户端地址信息
    struct sockaddr_in client_addr;
    int size_client_addr = sizeof(struct sockaddr_in);
    while (1)
    {

        printf("Waiting to receive data...\r\n");
        memset(recvBuf, 0, sizeof(recvBuf));
        ret = recvfrom(sock_fd, recvBuf, sizeof(recvBuf), 0, (struct sockaddr *)&client_addr, (socklen_t *)&size_client_addr);
        if (ret < 0)
        {
            printf("UDP server receive failed!\r\n");
            return -1;
        }
        printf("receive %d bytes of data from ipaddr = %s, port = %d.\r\n", ret, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
        printf("data is %s\r\n", recvBuf);
        ret = sendto(sock_fd, recvBuf, strlen(recvBuf), 0, (struct sockaddr *)&client_addr, sizeof(client_addr));
        if (ret < 0)
        {
            printf("UDP server send failed!\r\n");
            return -1;
        }
    } */

    // 在sock_fd 进行监听
    int sock_fd;
    // 服务器地址信息
    struct sockaddr_in server_sock;

    // 创建sock
    if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
        perror("create socket is error!\r\n");
        return -1;
    }

    bzero(&server_sock, sizeof(server_sock));
    server_sock.sin_family = AF_INET;
    server_sock.sin_addr.s_addr = htonl(INADDR_ANY);
    server_sock.sin_port = htons(8888);

    // 调用bind函数绑定socket和地址
    if ((bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr))) == -1)
    {
        perror("bind is error!\r\n");
        return -1;
    }

    int ret;
    char recvBuff[512] = {0};
    // 客户端地址信息
    struct sockaddr_in client_addr;
    int size_client_addr = sizeof(struct sockaddr_in);
    while (1)
    {
        printf("Waiting to receive data...\r\n");
        memset(recvBuff, 0, sizeof(recvBuff));

        // 接收数据
        ret = recvfrom(sock_fd, recvBuff, sizeof(recvBuff), 0, (struct sockaddr*)&client_addr, (socklen_t *)&size_client_addr);
        if (ret < 0)
        {
            printf("UDP Server Recv Message Falied!!!\r\n");
            return -1;
        }
        printf("Receive %d bytes of data from ipaddr = %s, port = %d.\r\n", ret, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
        printf("data is %s.\r\n", recvBuff);

        // 发送数据
        ret = sendto(sock_fd, recvBuff, strlen(recvBuff), 0, (struct sockaddr *)&client_addr, sizeof(client_addr));
        if (ret < 0)
        {
            printf("UDP Server Send Message Falied!!!\r\n");
            return -1;
        }
    }
    /*********************END********************/
}

void YuchuanWifiApEntry(void)
{
    osThreadAttr_t threadAttr;
    threadAttr.attr_bits = 0U;
    threadAttr.cb_mem = NULL;
    threadAttr.cb_size = TASK_CB_SIZE;
    threadAttr.stack_mem = NULL;
    threadAttr.stack_size = TASK_STACK_SIZE;
    threadAttr.priority = TASK_PRIORITY;

    threadAttr.name = "YuchuanWifiApTask";

    if (osThreadNew((osThreadFunc_t)YuchuanWifiApTask, NULL, &threadAttr) == NULL)
    {
        printf("Falied To Create YuchuanWifiApTask !!!\n");
    }
}

APP_FEATURE_INIT(YuchuanWifiApEntry);

编译调试

BUILD.gn

static_library("yuchuanWifiAp"){
    sources = [
        "yuchuan_wifi_ap.c",
    ]

    cflags = ["-Wno-unused-variable"]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal/cmsis",
        "//foundation/communication/wifi_lite/interfaces/wifiservice"
    ]
}

修改 BUILD.gn 文件

修改 applications\BearPi\BearPi-HM_Nano\sample 路径下 BUILD.gn 文件,指定 yuchuanWifiAp 参与编译。

# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/lite/config/component/lite_component.gni")

lite_component("sample") {
    features = [
        #"A1_kernal_thread:thread_example",
        #"A2_kernel_timer:timer_example",
        #"A3_kernel_event:event_example",
        #"A4_kernel_mutex:mutex_example",
        #"A5_kernel_semaphore:semaphore_example",
        #"A6_kernel_message:message_example",

        #"B1_basic_led_blink:led_example",
        #"B2_basic_button:button_example",
        #"B3_basic_pwm_led:pwm_example",
        #"B4_basic_adc:adc_example", 
        #"B5_basic_i2c_nfc:i2c_example",
        #"B6_basic_uart:uart_example",
        
        #"C1_e53_sf1_mq2:e53_sf1_example",
        #"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
        #"C3_e53_sc1_pls:e53_sc1_example",
        #"C4_e53_sc2_axis:e53_sc2_example",
        #"C5_e53_is1_infrared:e53_is1_example",

        #"D1_iot_wifi_ap:wifi_ap",
        #"D2_iot_wifi_sta_connect:wifi_sta_connect",        
        #"D3_iot_udp_client:udp_client",
        #"D4_iot_tcp_server:tcp_server",
        #"D5_iot_mqtt:iot_mqtt",        
        #"D6_iot_cloud_oc:oc_mqtt",
        #"D7_iot_cloud_onenet:onenet_mqtt",
        #"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
        #"D9_iot_cloud_oc_light:cloud_oc_light",
        #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
        #"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
        #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
        #"D13_iot_cloud_oc_gps:cloud_oc_gps",
        #"A1_YuchuanThread:yuchuanThread",
        #"A2_YuchuanTimer:yuchuanTimer",
        #"A4_YuchuanMutex:yuchuanMutex",
        #"A5_YuchuanSemaphore:yuchuanSemaphore",
        #"A3_YuchuanEvent:yuchuanEvent",
        #"A6_YuchuanMessage:yuchuanMessageQueue",
        #"B3_YuchuanBasicPWM:yuchuanPWM",
        #"B4_YuchuanBasicADC:yuchuanBasicADC",
        #"B5_YuchuanNFCI2C:yuchuanNFCI2C",
        #"B6_YuchuanBasicUART:YuchuanBasicUART",
        #"YuchuanTest:YuchuanTestAPP",
        "D1_YuchuanWifiAp:yuchuanWifiAp",
    ]
}

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,使用手机去连接该热点,会打印出一下信息。

ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
formatting spiffs...
FileSystem mount ok.
wifi init success!

hiview init success.00 00:00:00 0 132 D 0/HIVIEW: log limit init success.
00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0da8 TaskPool:0xe4c3c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0db4 TaskPool:0xe4c5c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b1280 TaskPool:0xe4c7c
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b0db4 <time: 0ms> success!
00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b0da8 <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b1280 <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
Register Wifi Event Succed!
Set Hotspot Config Succeed!!!
OnHotspotStateChangedHandler: sta is 1.
wifi hotspot active
Enable Wifi Hotspot Succeed!!!
Wifi Station is Actived!!!
netifapi_netif_set_addr Succed!!!
netifapi_dhcps_start Succed!!!
Waiting to receive data...
+NOTICE:STA CONNECTED
New Sta Join 
+NOTICE:STA DISCONNECTED
OnHotspotStaLeaveHandler: macAddress=82:04:1F:4F:D8:60, reason=0.

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。