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

news/2024/12/22 0:44:24/

鸿蒙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 CONNECTEDPrintStationInfo: 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.ppmy.cn/news/1473674.html

相关文章

邀请函 | 极限科技全新搜索引擎 INFINI Pizza 亮相 2024 可信数据库发展大会!

过去一年&#xff0c;在全球 AI 浪潮和国家数据局成立的推动下&#xff0c;数据库产业变革不断、热闹非凡。2024 年&#xff0c;站在中国数字经济产业升级和数据要素市场化建设的时代交汇点上&#xff0c;“2024 可信数据库发展大会” 将于 2024 年 7 月 16-17 日在北京悠唐皇冠…

Windows如何查看端口是否占用,并结束端口进程

需求与问题&#xff1a;前后端配置了跨域操作&#xff0c;但是仍然报错&#xff0c;可以考虑端口被两个程序占用&#xff0c;找不到正确端口或者后端接口书写是否规范&#xff0c;特别是利用Python Flask书写时要保证缩进是否正确&#xff01; Windows操作系统中&#xff0c;查…

昇思25天学习打卡营第16天|Diffusion扩散模型

导入必要的库函数 import math from functools import partial %matplotlib inline import matplotlib.pyplot as plt from tqdm.auto import tqdm import numpy as np from multiprocessing import cpu_count from download import downloadimport mindspore as ms import mi…

贵州建筑三类人员安全员2024年考试最新题库练习题

一、单选题 1.建设工程安全管理的方针是&#xff08;&#xff09;。 A.安全第一&#xff0c;预防为主&#xff0c;综合治理 B.质量第一&#xff0c;兼顾安全 C.安全至上 D.安全责任重于泰山 答案&#xff1a;A 2.安全生产管理的根本目的是&#xff08;&#xff09;。 A.…

ubuntu的screen会话,断开远程连接也能照样运行程序

文章目录 创建新的 screen 会话管理 screen 会话例子关闭某一个 screen 会话删除某一个 screen 会话 在 Ubuntu 中使用 screen 工具可以创建和管理多个终端会话。以下是创建 screen 会话的方法&#xff1a; 创建新的 screen 会话 启动一个新的 screen 会话&#xff1a; screen…

Java实现登录验证 -- JWT令牌实现

目录 1.实现登录验证的引出原因 2.JWT令牌2.1 使用JWT令牌时2.2 令牌的组成 3. JWT令牌&#xff08;token&#xff09;生成和校验3.1 引入JWT令牌的依赖3.2 使用Jar包中提供的API来实现JWT令牌的生成和校验3.3 使用JWT令牌验证登录3.4 令牌的优缺点 1.实现登录验证的引出 传统…

huggingface笔记:gpt2

0 使用的tips GPT-2是一个具有绝对位置嵌入的模型&#xff0c;因此通常建议在输入的右侧而不是左侧填充GPT-2是通过因果语言建模&#xff08;CLM&#xff09;目标进行训练的&#xff0c;因此在预测序列中的下一个标记方面非常强大 利用这一特性&#xff0c;GPT-2可以生成语法连…

VitePress美化

参考资料&#xff1a; https://blog.csdn.net/weixin_44803753/article/details/130903396 https://blog.csdn.net/qq_30678861/category_12467776.html 站点信息修改 首页部分的修改基本都在.vitepress/config.mts,这个文件内修改。 title 站点名称 description 描述 top…