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

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

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

所使用的API接口有:

API

接口说明

WifiErrorCode EnableWifi(void);

开启STA

WifiErrorCode DisableWifi(void);

关闭STA

int IsWifiActive(void);

查询STA是否已开启

WifiErrorCode Scan(void);

触发扫描

WifiErrorCode GetScanInfoList(WifiScanInfo* result, unsigned int* size);

获取扫描结果

WifiErrorCode AddDeviceConfig(const WifiDeviceConfig* config, int* result);

添加热点配置,成功会通过result传出netld

WifiErrorCode GetDeviceConfigs(WifiDeviceConfig* result, unsigned int* size);

获取本机所有热点配置

WifiErrorCode RemoveDevice(int networkId);

删除热点配置

WifiErrorCode ConnectTo(int networkId);

连接到热点

WifiErrorCode Disconnect(void);

断开热点连接

WifiErrorCode GetLinkedInfo(WifiLinkedInfo* result);

获取当前连接热点信息

WifiErrorCode RegisterWifiEvent(WifiEvent* event);

注册事件监听

WifiErrorCode UnRegisterWifiEvent(const WifiEvent* event);

解除事件监听

WifiErrorCode GetDeviceMacAddress(unsigned char* result);

获取Mac地址

WifiErrorCode AdvanceScan(WifiScanParams *params);

高级搜索

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",
    "wifi_10_sta:wifi_sta_example",
  ]
}

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

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\wifi_10_sta\wifi_sta_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\wifi_10_sta\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_sta_example") {
    sources = [
        "wifi_sta_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_device.h"

#include "lwip/netifapi.h"
#include "lwip/api_shell.h"

#define STACK_SIZE     10240
#define AP_SSID        "HarmonyOS"
#define AP_SKEY        "1234567890"

#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_50     (50)
#define DELAY_TICKS_100    (100)
#define DELAY_TICKS_200    (200)

#define COUNTER            (60)

static void PrintLinkedInfo(WifiLinkedInfo* info)
{
    if (!info) return;

    static char macAddress[32] = {0};
    unsigned char* mac = info->bssid;
    // 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("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %s\r\n",
        macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
}

static int g_connected = 0;
static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
{
    if (!info) return;

    printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
    PrintLinkedInfo(info);

    if (state == WIFI_STATE_AVALIABLE) {
        g_connected = 1;
    } else {
        g_connected = 0;
    }
}

static void OnWifiScanStateChanged(int state, int size)
{
    printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
}

static void WifiConnectTask(void)
{
    WifiErrorCode errCode;
    WifiEvent eventListener = {
        .OnWifiConnectionChanged = OnWifiConnectionChanged,
        .OnWifiScanStateChanged = OnWifiScanStateChanged
    };
    WifiDeviceConfig apConfig = {};
    int netId = -1;

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

    // setup your AP params
    // strcpy(apConfig.ssid, "ABCD");
    // strcpy(apConfig.preSharedKey, "147258369");
    strcpy_s(apConfig.ssid, WIFI_MAX_SSID_LEN, AP_SSID);
    strcpy_s(apConfig.preSharedKey, WIFI_MAX_KEY_LEN, AP_SKEY);
    apConfig.securityType = WIFI_SEC_TYPE_PSK;

    int timeoutCounter = COUNTER * COUNTER;
    while (timeoutCounter--) {
        errCode = EnableWifi();
        printf("EnableWifi: %d\r\n", errCode);
        osDelay(DELAY_TICKS_10);

        errCode = AddDeviceConfig(&apConfig, &netId);
        printf("AddDeviceConfig: %d\r\n", errCode);

        g_connected = 0;
        errCode = ConnectTo(netId);
        printf("ConnectTo(%d): %d\r\n", netId, errCode);

        while (!g_connected) {
            osDelay(DELAY_TICKS_10);
        }
        printf("g_connected: %d\r\n", g_connected);
        osDelay(DELAY_TICKS_50);

        // 联网业务开始
        struct netif* iface = netifapi_netif_find("wlan0");
        if (iface) {
            err_t ret = netifapi_dhcp_start(iface);
            printf("netifapi_dhcp_start: %d\r\n", ret);

            osDelay(DELAY_TICKS_200); // wait DHCP server give me IP
            ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL);
            printf("netifapi_netif_common: %d\r\n", ret);
        }

        // 模拟一段时间的联网业务
        int timeout = COUNTER;
        while (timeout--) {
            osDelay(DELAY_TICKS_100);
            printf("after %d seconds, I'll disconnect WiFi!\n", timeout);
        }

        // 联网业务结束
        err_t ret = netifapi_dhcp_stop(iface);
        printf("netifapi_dhcp_stop: %d\r\n", ret);

        Disconnect(); // disconnect with your AP

        RemoveDevice(netId); // remove AP config

        errCode = DisableWifi();
        printf("DisableWifi: %d\r\n", errCode);
        osDelay(DELAY_TICKS_200);
    }
}

static void WifiConnectDemo(void)
{
    osThreadAttr_t attr;

    attr.name = "WifiConnectTask";
    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(WifiConnectTask, NULL, &attr) == NULL) {
        printf("[WifiConnectDemo] Falied to create WifiConnectTask!\n");
    }
}

APP_FEATURE_INIT(WifiConnectDemo);

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

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.
RegisterWifiEvent: 0
EnableWifi: 0
AddDeviceConfig: 0
ConnectTo(1): 0
+NOTICE:SCANFINISH
+NOTICE:CONNECTED
OnWifiConnectionChanged 80, state = 1, info = 
bssid: 4A:AD:9A:1C:00:D9, rssi: 0, connState: 0, reason: 0, ssid: HarmonyOS
g_connected: 1
netifapi_dhcp_start: 0
server :
        server_id : 192.168.137.1
        mask : 255.255.255.0, 1
        gw : 192.168.137.1
        T0 : 604800
        T1 : 302400
        T2 : 453600
clients <1> :
        mac_idx mac             addr            state   lease   tries   rto     
        0       94c9601e15d0    192.168.137.66  10      0       1       2       
netifapi_netif_common: 0
after 59 seconds, I'll disconnect WiFi!
after 58 seconds, I'll disconnect WiFi!
after 57 seconds, I'll disconnect WiFi!
after 56 seconds, I'll disconnect WiFi!
after 55 seconds, I'll disconnect WiFi!
after 54 seconds, I'll disconnect WiFi!
after 53 seconds, I'll disconnect WiFi!
after 52 seconds, I'll disconnect WiFi!
after 51 seconds, I'll disconnect WiFi!
after 50 seconds, I'll disconnect WiFi!
after 49 seconds, I'll disconnect WiFi!
after 48 seconds, I'll disconnect WiFi!
after 47 seconds, I'll disconnect WiFi!
after 46 seconds, I'll disconnect WiFi!
after 45 seconds, I'll disconnect WiFi!
after 44 seconds, I'll disconnect WiFi!
after 43 seconds, I'll disconnect WiFi!
after 42 seconds, I'll disconnect WiFi!
after 41 seconds, I'll disconnect WiFi!
after 40 seconds, I'll disconnect WiFi!
after 39 seconds, I'll disconnect WiFi!
after 38 seconds, I'll disconnect WiFi!
after 37 seconds, I'll disconnect WiFi!
after 36 seconds, I'll disconnect WiFi!
after 35 seconds, I'll disconnect WiFi!
after 34 seconds, I'll disconnect WiFi!
after 33 seconds, I'll disconnect WiFi!
after 32 seconds, I'll disconnect WiFi!
after 31 seconds, I'll disconnect WiFi!
after 30 seconds, I'll disconnect WiFi!
after 29 seconds, I'll disconnect WiFi!
after 28 seconds, I'll disconnect WiFi!
after 27 seconds, I'll disconnect WiFi!
after 26 seconds, I'll disconnect WiFi!
after 25 seconds, I'll disconnect WiFi!
after 24 seconds, I'll disconnect WiFi!
after 23 seconds, I'll disconnect WiFi!
after 22 seconds, I'll disconnect WiFi!
after 21 seconds, I'll disconnect WiFi!
after 20 seconds, I'll disconnect WiFi!
after 19 seconds, I'll disconnect WiFi!
after 18 seconds, I'll disconnect WiFi!
after 17 seconds, I'll disconnect WiFi!
after 16 seconds, I'll disconnect WiFi!
after 15 seconds, I'll disconnect WiFi!
after 14 seconds, I'll disconnect WiFi!
after 13 seconds, I'll disconnect WiFi!
after 12 seconds, I'll disconnect WiFi!
after 11 seconds, I'll disconnect WiFi!
after 10 seconds, I'll disconnect WiFi!
after 9 seconds, I'll disconnect WiFi!
after 8 seconds, I'll disconnect WiFi!
after 7 seconds, I'll disconnect WiFi!
after 6 seconds, I'll disconnect WiFi!
after 5 seconds, I'll disconnect WiFi!
after 4 seconds, I'll disconnect WiFi!
after 3 seconds, I'll disconnect WiFi!
after 2 seconds, I'll disconnect WiFi!
after 1 seconds, I'll disconnect WiFi!
after 0 seconds, I'll disconnect WiFi!
netifapi_dhcp_stop: 0
+NOTICE:DISCONNECTED

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

《后端程序猿 · 基于 Lettuce 实现缓存容错策略》

&#x1f4e2; 大家好&#xff0c;我是 【战神刘玉栋】&#xff0c;有10多年的研发经验&#xff0c;致力于前后端技术栈的知识沉淀和传播。 &#x1f497; &#x1f33b; 近期刚转战 CSDN&#xff0c;会严格把控文章质量&#xff0c;绝不滥竽充数&#xff0c;如需交流&#xff…

大数据期末复习——hadoop、hive等基础知识

一、题型分析 1、Hadoop环境搭建 2、hadoop的三大组件 HDFS&#xff1a;NameNode&#xff0c;DataNode&#xff0c;SecondaryNameNode YARN&#xff1a;ResourceManager&#xff0c;NodeManager &#xff08;Yarn的工作原理&#xff09; MapReduce&#xff1a;Map&#xff0…

点云处理实操 点云平面拟合

目录 一、什么是平拟合 二、拟合步骤 三、数学原理 1、平面拟合 2、PCA过程 四、代码 一、什么是平拟合 平面拟合是指在三维空间中找到一个平面,使其尽可能接近给定的点云。最小二乘法是一种常用的拟合方法,通过最小化误差平方和来找到最优的拟合平面。 二、拟合步骤…

Kafka 为何如此之快?深度解析其背后的秘密

目录 前言 一、生产者 1. 异步发送 2. 多分区并行 3. 消息批量发送 4.支持消息压缩 二、存储端 1. 分区和副本 2. 页缓存 3. 磁盘顺序写入 4. 零拷贝技术 5. 稀疏索引 三、消费端 1. 消费者群组 2. 批量拉取 3. 高效的偏移量管理 4. 并行消费 总结 前言 Kafk…

观测云赋能「阿里云飞天企业版」,打造全方位监控观测解决方案

近日&#xff0c;观测云成功通过了「阿里云飞天企业版」的生态集成认证测试&#xff0c;并荣获阿里云颁发的产品生态集成认证证书。作为监控观测领域的领军者&#xff0c;观测云一直专注于提供统一的数据视角&#xff0c;助力用户构建起全球范围内的端到端全链路可观测服务。此…

SwanLinkOS首批实现与HarmonyOS NEXT互联互通,软通动力子公司鸿湖万联助力鸿蒙生态统一互联

在刚刚落下帷幕的华为开发者大会2024上&#xff0c;伴随全场景智能操作系统HarmonyOS Next的盛大发布&#xff0c;作为基于OpenHarmony的同根同源系统生态&#xff0c;软通动力子公司鸿湖万联全域智能操作系统SwanLinkOS首批实现与HarmonyOS NEXT互联互通&#xff0c;率先攻克基…

Appium adb 获取appActivity

方法一&#xff08;最简单有效的方法&#xff09; 通过cmd命令&#xff0c;前提是先打开手机中你要获取包名的APP adb devices -l 获取连接设备详细信息 adb shell dumpsys activity | grep mFocusedActivity 有时获取到的不是真实的Activity 方法二 adb shell monkey -p …

Java中反射的使用

无参构造器 方法的调用 package com.studio;import java.lang.reflect.Method;class User {private String name;/*无参构造器*/public User() {}public String getName() {return name;}public void setName(String name) {this.name name;}Overridepublic String toString…

PHP景区旅游多商户版微信小程序系统源码

解锁景区新玩法&#xff01;​ 引言&#xff1a;一站式旅行新体验 厌倦了传统景区的单调游览&#xff1f;想要一次旅行就能体验多种风情&#xff1f;那么&#xff0c;“景区旅游多商户版”绝对是你的不二之选&#xff01;这个创新模式将景区内多个商户资源整合&#xff0c;为…

C# WPF自制批注工具(方便标记重点和演示)

在教学和演示中&#xff0c;我们通常需要对重点进行批注&#xff0c;下载安装第三方工具批注显得很麻烦。本篇使用WPF开发了一个批注工具&#xff0c;工具小巧&#xff0c;功能丰富&#xff0c;非常使用日常免费使用&#xff0c;或者进行再次开发。 自制批注工具具有以下功能特…

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

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

查询 条件列值用notepad++批量添加单引号和逗号

参考&#xff1a;Notepad批量添加引号_notepad字符串统一加引号-CSDN博客 我需要批量修改数据表中某一列值指定的部分列&#xff0c;比如某个编号为CP0408242321001到CP0408242321101的条件。 我从数据表中把这个条件的所有编号复制出来了粘贴到了notepad里面。 如下图所示 从…

数据开源|GigaSpeech 2:三万小时东南亚多语种语音识别开源数据集发布

“Giga”一词源于“gigantic”&#xff0c;互联网上具有海量音频资源&#xff0c;但语音质量良莠不齐&#xff0c;高质量音频文本对数据十分稀缺且标注成本高昂&#xff0c;特别是在小语种领域。GigaSpeech 是一个非常成功的英文开源数据集&#xff0c;以 YouTube 和 Podcast 为…

读书笔记-Java并发编程的艺术-第4章(Java并发编程基础)-第1节(线程简介)

文章目录 4.1 线程简介4.1.1 什么是线程4.1.2 为什么要使用多线程4.1.3 线程优先级4.1.4 线程的状态4.1.5 Daemon 线程 Java从诞生开始就明智地选择了内置对多线程的支持&#xff0c;这使得Java语言相比同一时期的其他语言具有明显的优势。线程作为操作系统调度的最小单元&…

DC/AC电源模块:为智能家居设备提供恒定的电力供应

BOSHIDA DC/AC电源模块&#xff1a;为智能家居设备提供恒定的电力供应 DC/AC电源模块是一种常见的电源转换器&#xff0c;它将直流电源&#xff08;DC&#xff09;转换为交流电源&#xff08;AC&#xff09;&#xff0c;为智能家居设备提供恒定的电力供应。在智能家居系统中&a…

Linux运维:mysql视图,用户及远程登录,用户密码的修改和破解

目 录 一、视图 二、用户 2.1 新建用户 三、创建远程登录用户test 3.1 远程登录mysql​编辑 3.1 7-1需要赋予权限 3.3 修改远程登录用户的密码 3.4 修改远程登录的用户名 3.5 删除用户&#xff1a;drop user lisi192.168.114.%; 四、修改用户密码 4.1 修改当前本地…

网安小贴士(3)网安协议

一、前言 网络安全协议是构建安全网络环境的基础&#xff0c;它们帮助保护网络通信免受各种威胁和攻击。 二、定义 网络安全协议是指在计算机网络中用于确保网络通信和数据传输安全的协议。它们定义了在网络通信过程中的安全机制、加密算法、认证和授权流程等&#xff0c;以保…

【论文阅读】自动驾驶光流任务 DeFlow: Decoder of Scene Flow Network in Autonomous Driving

再一次轮到讲自己的paper&#xff01;耶&#xff0c;宣传一下自己的工作&#xff0c;顺便完成中文博客的解读 方便大家讨论。 Title Picture Reference and pictures paper: https://arxiv.org/abs/2401.16122 code: https://github.com/KTH-RPL/DeFlow b站视频: https://www.b…

关于 Mybatis 的开启二级缓存返回对象不一致问题

做实验报告的时候&#xff0c;跟着学习&#xff0c;发现我已经将 开启 二级缓存的 配置都配置好了&#xff0c;但是返回值地址不一致&#xff0c;说明对象不一致&#xff0c;二级缓存命中失败。 跟着流程配置&#xff1a; mybatis-config <settings><!-- 启用 myba…

SpringBoot+Thymeleaf项目重定向到另一个系统HTTPS变成HTTP

SpringBootThymeleaf项目是一个简单的单体项目&#xff0c;只有一个页面。 重定向的是前后分离&#xff0c;前端用的vue。 浏览器看到重定向后 https成了http&#xff0c;F12控制台看到是 301 Moved Permanently 单体项目最开始写法&#xff1a; response.sendRedirect(url); …