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

news2024/7/4 8:59:17

鸿蒙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 ...

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1886362.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

图片转pdf,图片转pdf在线转换,在线图片转pdf,图片转pdf格式怎么弄

在数字化时代&#xff0c;图片和PDF文件成为了我们日常生活中不可或缺的元素。有时候&#xff0c;我们需要将图片转换成PDF格式以便更好地分享、存储或打印。那么&#xff0c;图片格式怎么转换成PDF呢&#xff1f;本文将为您详细介绍简单实用的转换方法。 方法一、使用pdf转换软…

Python容器 之 字典--字典的常用操作方法

1.增加和修改 字典[键] 值 键 存在, 修改 键 不存在, 添加 # 定义非空字典, 姓名, 年龄, 身高, 性别 my_dict {"name": "小明", "age": 18, "height": 1.78, "isMen": True} print(my_dict) # {name: 小明, age: 18, h…

【知识】DGL中graph默认的稀疏矩阵格式和coo格式不对的坑

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;欢迎[点赞、收藏、关注]哦~ 目录 先给结论 源码解读 代码验证 网上没找到相关的讨论&#xff0c;因此只能从源码上一步步查。 先给结论 对于自己使用dgl.graph接口创建的图&am…

【HTML入门】列表与表格

文章目录 前言一、列表与表格是什么&#xff1f;列表表格 二、使用标签列表标签表格标签 三、组合情况列表的组合表格的组合 四、示例代码总结 好的&#xff0c;以下是一个关于HTML列表与表格的文章示例&#xff1a; 前言 随着网页开发的普及&#xff0c;HTML成为了构建网页的…

LVS FILTER UNUSED OPTION

我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧? 拾陆楼知识星球入口 过滤一些版图与spice网表对不上的器件。 一般后端遇不到这个问题,因为通常是需要写到网表中的decap没有写出来造成的,如下图。

来不及啦!怎样找回删除的照片,2个解决方案【安卓通用】

一不小心手滑&#xff0c;手机里那些珍贵的照片竟然全都被删了&#xff01;现在心急如焚&#xff0c;怎样找回删除的照片呢&#xff1f;别担心&#xff0c;小编我这就为你带来2个快速有效的解决方案&#xff0c;让你在分分钟找回那些失去的照片&#xff01;快来学习一下吧&…

Java语法系列 小白入门参考资料 方法

方法的概念及使用 方法概念 方法出现的原因 在编程中&#xff0c;某段功能的代码可能频繁使用到&#xff0c;如果在每个位置都重新实现一遍&#xff0c;会&#xff1a; 1. 使程序变得繁琐 2. 开发效率低下&#xff0c;做了大量重复性的工作 3. 不利于维护&#xff0c;需要…

自动驾驶水泥搅拌车在梁场的应用(下)

自动驾驶水泥搅拌车在梁场的应用&#xff08;下&#xff09; 北京渡众机器人科技有限公司的自动驾驶水泥搅拌车在梁场&#xff08;也称为预制梁场&#xff09;的应用可以带来多方面的优势和效益&#xff1a; 1. 自动化搅拌和运输 在梁场中&#xff0c;通常需要大量的混凝土搅…

探索大型语言模型自动评估 LLM 输出长句准确性的方法

LLM现在能够自动评估较长文本中的事实真实性 源码地址&#xff1a;https://github.com/google-deepmind/long-form-factuality 论文地址&#xff1a;https://arxiv.org/pdf/2403.18802.pdf 这篇论文是关于谷歌DeepMind的&#xff0c;提出了新的数据集、评估方法和衡量标准&am…

零基础学习MySQL---MySQL入门

顾得泉&#xff1a;个人主页 个人专栏&#xff1a;《Linux操作系统》 《C从入门到精通》 《LeedCode刷题》 键盘敲烂&#xff0c;年薪百万&#xff01; 一、什么是数据库 问&#xff1a;存储数据用文件就可以了&#xff0c;为什么还要弄个数据库呢&#xff1f; 这就不得不提…

收集了很久的全网好用的磁力搜索站列表分享

之前找资源的时候&#xff0c;收集了一波国内外大部分主流的磁力链接搜索站点。每一个站可能都有对应的优缺点&#xff0c;多试试&#xff0c;就能知道自己要哪个了。 全网好用的磁力链接 大部分的时候&#xff0c;我们用国内的就可以了&#xff0c;速度块&#xff0c;而且不…

Free Pascal语言基础学习:定义变量、数据类型、循环语句、case语句、条件判断、with语句、运算符

Pascal是一种结构化编程语言&#xff0c;而Free Pascal作为其现代编译器&#xff0c;不仅支持跨多种操作系统和处理器架构&#xff0c;还提供了高效的内存使用和函数重载等先进功能。Free Pascal继承了Pascal语言的核心特性&#xff0c;同时进行了扩展和优化&#xff0c;使其成…

操作系统真象还原:编写硬盘驱动程序

第13章-编写硬盘驱动程序 这是一个网站有所有小节的代码实现&#xff0c;同时也包含了Bochs等文件 13.1 硬盘及分区表 13.1.1 创建从盘及获取安装的磁盘数 要实现文件系统&#xff0c;必须先有个磁盘介质&#xff0c;虽然咱们己经有个虚拟磁盘 hd60M.img&#xff0c;但它只…

理解MySQL存储引擎:掌握数据存储与管理

在工作或学习过程中&#xff0c;作为一名数据库管理员或开发者&#xff0c;我们常常需处理大量数据&#xff0c;同时确保数据的可靠性与高效性。MySQL作为最受欢迎的开源数据库之一&#xff0c;其强大的性能和灵活性广为人知。而在MySQL背后的存储引擎则起到了至关重要的作用。…

深度学习项目GPU开发环境安装

注安装环境&#xff1a;ubuntu22.04, cuda 11.7, cudnn8.9 1.安装nvidia驱动 看可安装的Nvidia驱动版本&#xff1a;执行 ubuntu-drivers devices 安装合适版本的Nvidia驱动&#xff1a; sudo apt-get install nvidia-driver-515 注意&#xff1a;合适的版本需要尝试&#x…

【区块链+基础设施】银联云区块链服务 | FISCO BCOS应用案例

为了顺应区块链基础设施化的发展趋势&#xff0c;中国银联推出了银联云区块链服务——UPBaaS&#xff0c;为金融行业采用区块链 技术提出了解决方案&#xff0c;微众银行为平台提供 FISCO BCOS 区块链开源技术支持。通过银联云区块链服务&#xff0c;用户可 以用可视化的方式创…

ERROR: No matching distribution found for torch==2.0.1+cu117(比手动下载方便)

ERROR: No matching distribution found for torch2.0.1cu117 遇见这种报错可以把pip install -r requirements.txt修改为 pip install -r requirements.tx --extra-index-url https://download.pytorch.org/whl/cu117 -i https://pypi.tuna.tsinghua.edu.cn/simple或者直接…

ROS2仿真工具-gazebo

gazebo独立于ROS2&#xff0c;就像插件一样&#xff0c;需要安装。 1.安装 sudo apt install gazebo sudo apt install ros-humble-gazebo-* 2.运行测试demo gazebo /opt/ros/humble/share/gazebo_plugins/worlds/gazebo_ros_diff_drive_demo.world 查看所有话题 ros2 top…

vue3开发过程中遇到的一些问题记录

问题&#xff1a; vue3在使用 defineProps、defineEmits、defineExpose 时不需要import&#xff0c;但是 eslint会报错error defineProps is not defined no-undef 解决方法&#xff1a; 安装 vue-eslint-parser 插件&#xff0c;在 .eslintrc.js 文件中添加配置 parser: vue-e…

交叉编译tslib库和上机测试

目录 一、tslib 介绍 二、tslib 框架分析 三、交叉编译、测试 tslib 1.安装工具链 tslib &#xff08;1&#xff09;设置交叉编译工具链 &#xff08;2&#xff09;进入tslib目录 &#xff08;3&#xff09;安装工具链 &#xff08;4&#xff09;确定工具链中头文件、库…