Hi3861 OpenHarmony嵌入式应用入门--wifi hotspot

鸿蒙WiFi AP模式相关的API接口文件路径

foundation/communication/interfaces/kits/wifi_lite/wifiservice/wifi_hotspot_config.h

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

所使用的API接口有:

|--------------------------------------------------------------------------|---------------|
| API | 接口说明 |
| WifiErrorCode EnableHotspot(void); | 打开Wifi AP 模式 |
| WifiErrorCode DisableHotspot(void); | 关闭Wifi AP 模式 |
| WifiErrorCode SetHotspotConfig(const HotspotConfig* config); | 设置当前AP热点的配置参数 |
| WifiErrorCode GetHotspotConfig(HotspotConfig* result); | 获取当前AP热点的配置参数 |
| int IsHotspotActive(void); | 查询AP是否已经开启 |
| WifiErrorCode GetStationList(StationInfo* result, unsigned int* size); | 获取接入的设备列表 |
| int GetSignalLevel(int rssi, int band); | 获取信号强度等级 |
| WifiErrorCode SetBand(int band); | 设置当前频段 |
| WifiErrorCode GetBand(int* result); | 获取当前频段 |

Hi3861 SDK的DHCP客户端接口:

|---------------------|-----------|
| API | 描述 |
| netifapi_netif_find | 按名称查找网络接口 |
| netifapi_dhcp_start | 启动DHCP客户端 |
| netifapi_dhcp_stop | 停止DHCP客户端 |

代码编写

修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件

# Copyright (c) 2023 Beijing HuaQing YuanJian Education 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("demo") {
  features = [
    #"base_00_helloworld:base_helloworld_example",
    #"base_01_led:base_led_example",
    #"base_02_loopkey:base_loopkey_example",
    #"base_03_irqkey:base_irqkey_example",
    #"base_04_adc:base_adc_example",
    #"base_05_pwm:base_pwm_example",
    #"base_06_ssd1306:base_ssd1306_example",
    #"kernel_01_task:kernel_task_example",
    #"kernel_02_timer:kernel_timer_example",
    #"kernel_03_event:kernel_event_example",
    #"kernel_04_mutex:kernel_mutex_example",
    #"kernel_05_semaphore_as_mutex:kernel_semaphore_as_mutex_example",
    #"kernel_06_semaphore_for_sync:kernel_semaphore_for_sync_example",
    #"kernel_07_semaphore_for_count:kernel_semaphore_for_count_example",
    #"kernel_08_message_queue:kernel_message_queue_example",
    "wifi_09_hotspot:wifi_hotspot_example",
  ]
}

创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\wifi_09_hotspot文件夹

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\wifi_09_hotspot\wifi_hotspot_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\wifi_09_hotspot\BUILD.gn文件

# Copyright (c) 2023 Beijing HuaQing YuanJian Education 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. 

static_library("wifi_hotspot_example") {
    sources = [
        "wifi_hotspot_example.c"
    ]

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

/*
 * Copyright (c) 2020, HiHope Community.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

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

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_hotspot.h"
#include "lwip/netifapi.h"

#define STACK_SIZE     10240
#define AP_SSID        "ABCD"
#define AP_SKEY        "123456789"
#define IDX_0          0
#define IDX_1          1
#define IDX_2          2
#define IDX_3          3
#define IDX_4          4
#define IDX_5          5
#define DELAY_TICKS_10     (10)
#define DELAY_TICKS_100    (100)
#define COUNTER            (60)
#define CH_NUM             (7)

static volatile int g_hotspotStarted = 0;

static void OnHotspotStateChanged(int state)
{
    printf("OnHotspotStateChanged: %d.\r\n", state);
    if (state == WIFI_HOTSPOT_ACTIVE) {
        g_hotspotStarted = 1;
    } else {
        g_hotspotStarted = 0;
    }
}

static volatile int g_joinedStations = 0;

static void PrintStationInfo(StationInfo* info)
{
    if (!info) return;
    static char macAddress[32] = {0};
    unsigned char* mac = info->macAddress;
    // int ret = snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
    int ret = snprintf_s(macAddress, sizeof(macAddress), sizeof(macAddress) - 1, "%02X:%02X:%02X:%02X:%02X:%02X",
        mac[IDX_0], mac[IDX_1], mac[IDX_2], mac[IDX_3], mac[IDX_4], mac[IDX_5]);
    if (ret < 0) {
        return;
    }
    printf(" PrintStationInfo: mac=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
}

static void OnHotspotStaJoin(StationInfo* info)
{
    g_joinedStations++;
    PrintStationInfo(info);
    printf("+OnHotspotStaJoin: active stations = %d.\r\n", g_joinedStations);
}

static void OnHotspotStaLeave(StationInfo* info)
{
    g_joinedStations--;
    PrintStationInfo(info);
    printf("-OnHotspotStaLeave: active stations = %d.\r\n", g_joinedStations);
}

WifiEvent g_defaultWifiEventListener = {
    .OnHotspotStaJoin = OnHotspotStaJoin,
    .OnHotspotStaLeave = OnHotspotStaLeave,
    .OnHotspotStateChanged = OnHotspotStateChanged,
};

static struct netif* g_iface = NULL;

int StartHotspot(const HotspotConfig* config)
{
    WifiErrorCode errCode = WIFI_SUCCESS;

    errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
    printf("RegisterWifiEvent: %d\r\n", errCode);

    errCode = SetHotspotConfig(config);
    printf("SetHotspotConfig: %d\r\n", errCode);

    g_hotspotStarted = 0;
    errCode = EnableHotspot();
    printf("EnableHotspot: %d\r\n", errCode);

    while (!g_hotspotStarted) {
        osDelay(DELAY_TICKS_10);
    }
    printf("g_hotspotStarted = %d.\r\n", g_hotspotStarted);

    g_iface = netifapi_netif_find("ap0");
    if (g_iface) {
        ip4_addr_t ipaddr;
        ip4_addr_t gateway;
        ip4_addr_t netmask;

        IP4_ADDR(&ipaddr,  192, 168, 1, 1);     /* input your IP for example: 192 168 1 1 */
        IP4_ADDR(&gateway, 192, 168, 1, 1);     /* input your gateway for example: 192 168 1 1 */
        IP4_ADDR(&netmask, 255, 255, 255, 0);   /* input your netmask for example: 255 255 255 0 */
        err_t ret = netifapi_netif_set_addr(g_iface, &ipaddr, &netmask, &gateway);
        printf("netifapi_netif_set_addr: %d\r\n", ret);

        ret = netifapi_dhcps_stop(g_iface); // 海思扩展的HDCP服务接口
        printf("netifapi_dhcps_stop: %d\r\n", ret);

        ret = netifapi_dhcps_start(g_iface, 0, 0); // 海思扩展的HDCP服务接口
        printf("netifapi_dhcp_start: %d\r\n", ret);
    }
    return errCode;
}

void StopHotspot(void)
{
    if (g_iface) {
        err_t ret = netifapi_dhcps_stop(g_iface);  // 海思扩展的HDCP服务接口
        printf("netifapi_dhcps_stop: %d\r\n", ret);
    }

    WifiErrorCode errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
    printf("UnRegisterWifiEvent: %d\r\n", errCode);

    errCode = DisableHotspot();
    printf("EnableHotspot: %d\r\n", errCode);
}


static void WifiHotspotTask(void)
{
    WifiErrorCode errCode;
    HotspotConfig config = {0};

    // strcpy(config.ssid, "ABCD");
    // strcpy(config.preSharedKey, "12345678");
    strcpy_s(config.ssid, WIFI_MAX_SSID_LEN, AP_SSID);
    strcpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, AP_SKEY);
    config.securityType = WIFI_SEC_TYPE_PSK;
    config.band = HOTSPOT_BAND_TYPE_2G;
    config.channelNum = CH_NUM;

    osDelay(DELAY_TICKS_10);

    printf("starting AP ...\r\n");
    errCode = StartHotspot(&config);
    printf("StartHotspot: %d\r\n", errCode);

    int timeout = COUNTER;
    while (timeout--) {
        printf("After %d seconds Ap will turn off!\r\n", timeout);
        osDelay(DELAY_TICKS_100);
    }

    printf("stop AP ...\r\n");
    StopHotspot();
    printf("stop AP ...\r\n");
}

static void WifiHotspotDemo(void)
{
    osThreadAttr_t attr;

    attr.name = "WifiHotspotTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = STACK_SIZE;
    attr.priority = osPriorityNormal;

    if (osThreadNew(WifiHotspotTask, NULL, &attr) == NULL) {
        printf("[WifiHotspotDemo] Falied to create WifiHotspotTask!\n");
    }
}

APP_FEATURE_INIT(WifiHotspotDemo);

使用build,编译成功后,使用upload进行烧录。

---- 已打开串行端口 COM11 ----
ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
formatting spiffs...
FileSystem mount ok.
wifi init success!
hilog will init.


hiview init success.
starting AP ...
RegisterWifiEvent: 0
SetHotspotConfig: 0
OnHotspotStateChanged: 1.
EnableHotspot: 0
g_hotspotStarted = 1.
netifapi_netif_set_addr: 0
netifapi_dhcps_stop: 0
netifapi_dhcp_start: 0
StartHotspot: 0
After 59 seconds Ap will turn off!
After 58 seconds Ap will turn off!
After 57 seconds Ap will turn off!
After 56 seconds Ap will turn off!
After 55 seconds Ap will turn off!
After 54 seconds Ap will turn off!
After 53 seconds Ap will turn off!
After 52 seconds Ap will turn off!
After 51 seconds Ap will turn off!
After 50 seconds Ap will turn off!
After 49 seconds Ap will turn off!
After 48 seconds Ap will turn off!
After 47 seconds Ap will turn off!
After 46 seconds Ap will turn off!
After 45 seconds Ap will turn off!
After 44 seconds Ap will turn off!
After 43 seconds Ap will turn off!
After 42 seconds Ap will turn off!
After 41 seconds Ap will turn off!
After 40 seconds Ap will turn off!
After 39 seconds Ap will turn off!
After 38 seconds Ap will turn off!
After 37 seconds Ap will turn off!
After 36 seconds Ap will turn off!
After 35 seconds Ap will turn off!
After 34 seconds Ap will turn off!
After 33 seconds Ap will turn off!
After 32 seconds Ap will turn off!
After 31 seconds Ap will turn off!
After 30 seconds Ap will turn off!
After 29 seconds Ap will turn off!
After 28 seconds Ap will turn off!
After 27 seconds Ap will turn off!
+NOTICE:STA CONNECTED
 PrintStationInfo: mac=76:06:CF:47:79:B0, reason=0.
+OnHotspotStaJoin: active stations = 1.
After 26 seconds Ap will turn off!
After 25 seconds Ap will turn off!
After 24 seconds Ap will turn off!
After 23 seconds Ap will turn off!
After 22 seconds Ap will turn off!
After 21 seconds Ap will turn off!
After 20 seconds Ap will turn off!
After 19 seconds Ap will turn off!
After 18 seconds Ap will turn off!
After 17 seconds Ap will turn off!
After 16 seconds Ap will turn off!
After 15 seconds Ap will turn off!
After 14 seconds Ap will turn off!
After 13 seconds Ap will turn off!
After 12 seconds Ap will turn off!
After 11 seconds Ap will turn off!
After 10 seconds Ap will turn off!
After 9 seconds Ap will turn off!
After 8 seconds Ap will turn off!
After 7 seconds Ap will turn off!
After 6 seconds Ap will turn off!
After 5 seconds Ap will turn off!
After 4 seconds Ap will turn off!
After 3 seconds Ap will turn off!
After 2 seconds Ap will turn off!
After 1 seconds Ap will turn off!
After 0 seconds Ap will turn off!
stop AP ...
netifapi_dhcps_stop: 0
UnRegisterWifiEvent: 0
+NOTICE:STA DISCONNECTED
EnableHotspot: 0
stop AP ...
相关推荐
程序猿会指北2 天前
纯血鸿蒙APP实战开发——Text实现部分文本高亮和超链接样式
移动开发·harmonyos·arkts·openharmony·arkui·组件化·鸿蒙开发
程序猿会指北3 天前
纯血鸿蒙APP实战开发——应用新功能引导实现案例
移动开发·harmonyos·arkts·openharmony·arkui·组件化·鸿蒙开发
Hacker_Albert4 天前
OpenHarmony-3.HDF Display子系统(6)
openharmony
Hacker_Albert6 天前
OpenHarmony-4.HDI 框架
openharmony
神一样的老师9 天前
【鸿睿创智开发板试用】移植OpenCV 4到OpenHarmony 4.1
人工智能·opencv·计算机视觉·鸿蒙·openharmony
白羊@9 天前
多模块应用、发布使用第三方库(持续更新中)
服务器·前端·网络·harmonyos·鸿蒙·openharmony·第三方库
xianKOG13 天前
openHarHarmony学习
鸿蒙·openharmony
码莎拉蒂 .17 天前
NEXT开发应用质量建议与测试指南
openharmony
Industio_触觉智能22 天前
触觉智能亮相OpenHarmony人才生态大会2024
嵌入式硬件·openharmony·嵌入式开发·开源鸿蒙·鸿蒙开发·触觉智能·原生鸿蒙
软通动力24 天前
技术创新与人才培养并重 软通动力子公司鸿湖万联亮相OpenHarmony人才生态大会
openharmony