Hi3861 OpenHarmony嵌入式应用入门--SNTP

sntp(Simple Network Time Protocol)是一种网络时间协议,它是NTP(Network Time Protocol)的一个简化版本。

本项目是从LwIP中抽取的SNTP代码;

`Hi3861 SDK`中已经包含了一份预编译的lwip,但没有开启SNTP功能(静态库无法修改);

需要保证模块连接的热点能够访问网络的。

修改网络参数

在Hi3861开发板上运行上述四个测试程序之前,需要根据你的无线路由、Linux系统IP修改 net_params.h文件的相关代码:

  • PARAM_HOTSPOT_SSID 修改为你的热点名称
  • PARAM_HOTSPOT_PSK 修改为你的热点密码;

可能需要修改校时服务器的ip地址,修改如下代码

void SntpSetServers(void)
{IP4_ADDR(&g_ntpServerList[0], 223, 113, 97, 98); // cn.ntp.org.cnIP4_ADDR(&g_ntpServerList[1], 114, 118, 7, 163);  // ntp.ntsc.ac.cnIP4_ADDR(&g_ntpServerList[2], 120, 25, 108, 11);   // time.pool.aliyun.comIP4_ADDR(&g_ntpServerList[3], 119, 28, 206, 193); // cn.pool.ntp.orgfor (size_t i = 0; i < SNTP_SERVERS; i++) {sntp_setserver(i, (ip_addr_t*)&g_ntpServerList[i]);}
}

方法就是用ping网址,通过返回的消息查看ip地址

代码编写

修改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",#"tcp_11_server:tcp_server_example",#"tcp_12_client:tcp_client_example",#"udp_13_server:udp_server_example",#"udp_14_client:udp_client_example",#"network_15_mqtt:network_mqtt_example","network_16_sntp:network_sntp_example",]
}

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

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

# 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.static_library("network_sntp_example") {sources = ["network_sntp_example.c","wifi_connecter.c","//third_party/sntp/src/sntp.c","//third_party/sntp/src/sntp_port.c","//third_party/sntp/src/sntp_debug.c"]defines = [#"SNTP_SERVER_DNS=1","LWIP_HAVE_INT64=0","LWIP_DEBUG","SNTP_DEBUG=0xA0","LWIP_DBG_TYPES_ON=LWIP_DBG_ON"]include_dirs = ["//third_party/sntp/include","//third_party/sntp/include/lwip/apps","//foundation/communication/wifi_lite/interfaces/wifiservice","//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/",]
}

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\network_16_sntp\wifi_connecter.h文件,该头文件包含wifi连接的宏。文件同tcp_12_client\wifi_connecter.h

额外添加如下宏定义

#ifndef PARAM_HOTSPOT_TYPE
#define PARAM_HOTSPOT_TYPE WIFI_SEC_TYPE_PSK /* defined in wifi_device_config.h */
#endif

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\network_16_sntp\wifi_connecter.c文件,文件同tcp_12_client\wifi_connecter.c

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\network_16_sntp\network_sntp_example.c文件

/** 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 "ohos_init.h"
#include "cmsis_os2.h"#include "sntp.h"
#include "wifi_connecter.h"
#include "lwip/pbuf.h"
#include "lwip/dns.h"
#include "lwip/ip4_addr.h"#define SNTP_SERVERS 4
// #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
#define STACK_SIZE         (4096)
#define DELAY_TICKS_500    (500)static int g_netId = -1;#if SNTP_SERVER_DNS
static const char* g_ntpServerList[] = {// refers from https://dns.icoa.cn/ntp/#china"cn.ntp.org.cn",        // 中国 NTP 快速授时服务"ntp.ntsc.ac.cn",       // 国家授时中心 NTP 服务器"time.pool.aliyun.com", // 阿里云公共 NTP 服务器"cn.pool.ntp.org",      // 国际 NTP 快速授时服务
};
// #define SNTP_SERVERS ARRAY_SIZE(g_ntpServerList)void SntpSetServernames(void)
{for (size_t i = 0; i < SNTP_SERVERS; i++) {sntp_setservername(i, g_ntpServerList[i]);}
}#else// ip4_addr_t g_ntpServerList[SNTP_MAX_SERVERS];
ip4_addr_t g_ntpServerList[SNTP_SERVERS];void SntpSetServers(void)
{IP4_ADDR(&g_ntpServerList[0], 223, 113, 97, 98); // cn.ntp.org.cnIP4_ADDR(&g_ntpServerList[1], 114, 118, 7, 163);  // ntp.ntsc.ac.cnIP4_ADDR(&g_ntpServerList[2], 120, 25, 108, 11);   // time.pool.aliyun.comIP4_ADDR(&g_ntpServerList[3], 119, 28, 206, 193); // cn.pool.ntp.orgfor (size_t i = 0; i < SNTP_SERVERS; i++) {sntp_setserver(i, (ip_addr_t*)&g_ntpServerList[i]);}
}
#endifstatic void SntpTask(void)
{WifiDeviceConfig config = {0};// 准备AP的配置参数// strcpy(config.ssid, PARAM_HOTSPOT_SSID);// strcpy(config.preSharedKey, PARAM_HOTSPOT_PSK);strcpy_s(config.ssid, WIFI_MAX_SSID_LEN, PARAM_HOTSPOT_SSID);strcpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, PARAM_HOTSPOT_PSK);config.securityType = PARAM_HOTSPOT_TYPE;g_netId = ConnectToHotspot(&config);printf("netId = %d\r\n", g_netId);#if SNTP_SERVER_DNSip4_addr_t dnsServerAddr;IP4_ADDR(&dnsServerAddr, 202, 98, 0, 68);dns_setserver(0, (struct ip_addr *)&dnsServerAddr);dns_init();SntpSetServernames();
#elseSntpSetServers();
#endifsntp_setoperatingmode(SNTP_OPMODE_POLL);sntp_init();printf("sntp_enabled: %d\r\n", sntp_enabled());for (size_t i = 0; i < SNTP_SERVERS; i++) {printf("sntp_getreachability(%zu): %d\r\n", i, sntp_getreachability(i));}osDelay(DELAY_TICKS_500);for (size_t i = 0; i < SNTP_SERVERS; i++) {printf("sntp_getreachability(%zu): %d\r\n", i, sntp_getreachability(i));}
}static void SntpEntry(void)
{osThreadAttr_t attr = {0};attr.name = "SntpTask";attr.stack_size = STACK_SIZE;attr.priority = osPriorityNormal;if (osThreadNew(SntpTask, NULL, &attr) == NULL) {printf("[SntpEntry] create SntpTask failed!\n");}
}
SYS_RUN(SntpEntry);

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

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

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

相关文章

线程间的通信

文章目录 线程间的通讯技术就是通过等待和唤醒机制&#xff0c;来实现多个线程协同操作完成某一项任务&#xff0c;例如经典的生产者和消费者案例。等待唤醒机制其实就是让线程进入等待状态或者让线程从等待状态中唤醒&#xff0c;需要用到两种方法&#xff0c;如下&#xff1a…

红蓝对抗下的内网横向移动渗透技术详解

一、利用Windows计划任务横向移动 Windows计划任务是一个非常实用的功能&#xff0c;可以帮助我们自动完成一些重复性的任务。比如&#xff0c;我们可以设定一个计划任务来自动备份文件、更新软件、执行脚本等,本文主要介绍了如何利用Windows计划任务进行横向渗透。 &#xf…

C#委托事件的实现

1、事件 在C#中事件是一种特殊的委托类型&#xff0c;用于在对象之间提供一种基于观察者模式的通知机制。 1.1、事件的发送方定义了一个委托&#xff0c;委托类型的声明包含了事件的签名&#xff0c;即事件处理器方法的签名。 1.2、事件的订阅者可以通过运算符来注册事件处理器…

Python基础小知识问答系列-过滤列表元素

1. 问题&#xff1a; 如何根据单一条件过滤列表的元素&#xff1f; 如何根据复杂条件过滤列表的元素&#xff1f; 2. 解决方式&#xff1a; 可以使用推导式生成器&#xff0c;进行单一条件的列表元素过滤&#xff0c;尤其是列表内容较多时; 也可以使用filter函数进行列…

Linux Static Keys和jump label机制

文章目录 前言一、asm goto二、API使用2.1 低版本API2.2 高版本API 三、jump label四、源码分析4.1 数据结构4.2 static_key_false4.3 jump_label_init4.4 __jump_label_transform4.5 static_key_slow_inc/dec 五、__jump_table节5.1 内核5.2 内核模块 六、修改内存代码6.1 x86…

24西安电子科技大学数学与统计学院—考研录取情况

24西安电子科技大学—数学与统计学院—考研录取统计 01、数学与统计学院各个方向 02、24数学与统计学院近三年复试分数线对比 数统院24年院线相对于23年院线增加高达30分&#xff0c;确实增长浮动比较高&#xff0c;接近30分的水平&#xff0c;因此大家更需要好好去努力&#x…

GTest和Catch2单元测试学习(附Cmake测试代码库)

kevin_CTest CTest 单元测试学习 Gitee库&#xff1a; https://gitee.com/bigearrabbit/kevin_ctest.git 示例多是从网页文章上摘取的&#xff0c;大部分记录在下面&#xff0c;或者源码内。供学习参考。 CTest 学习Catch2 框架 单个文档的测试架构&#xff0c;使用方便&am…

vue3中svg图标的封装与使用

组件封装&#xff1a; <template><svg :class"svgClass" :style"{ width: size px, height: size px, color: color, verticalAlign:deviationem}" aria-hidden"true"><use :xlink:href"#icon-${name}" /></s…

FreeRTOS的任务间通信

文章目录 4 FreeRTOS任务间通信4.1 队列4.1.1 队列的使用4.1.2 队列的创建&#xff0c;删除&#xff0c;复位4.1.3 队列的发送&#xff0c;接收&#xff0c;查询 4.2 邮箱&#xff08;mailbox&#xff09;4.2.1 任务中读写邮箱4.2.2 中断中读写邮箱 4.3 队列集4.3.1 队列集的创…

Vue打包文件dist放在SpringBoot项目下运行(正确实现全过程)

项目开发中&#xff0c;一般我们都会使用SpringBootVue进行前后端开发。 在部署时&#xff0c;会后端启动一个服务&#xff0c;再启动一个nginx&#xff0c;nginx中配置前端打包文件dist进行项目访问。 实际上&#xff0c;我们也可以把打包好的dist目录放在SpringBoot项目下进…

Os-hackNos

下载地址 https://download.vulnhub.com/hacknos/Os-hackNos-1.ova 环境配置如果出现&#xff0c;扫描不到IP的情况&#xff0c;可以尝试vulnhub靶机检测不到IP地址解决办法_vulnhub靶机扫描不到ip-CSDN博客 信息收集 确定靶机地址&#xff1a; 探测到存活主机192.168.111.…

如何利用AI撰写短文案获客?分享6大平台和3大步骤!

从去年开始&#xff0c;很多大厂都在裁员&#xff0c;原因就是因为AI的火爆&#xff0c;替代了很多机械式的劳动力。以前很多人可以通过机械式的工作来摸鱼&#xff0c;现在AI完成的效率比人工的要高很多倍。 国内好用的AI平台非常多&#xff0c;有时候也可以使用几个AI平台结合…

软件设计之Java入门视频(11)

软件设计之Java入门视频(11) 视频教程来自B站尚硅谷&#xff1a; 尚硅谷Java入门视频教程&#xff0c;宋红康java基础视频 相关文件资料&#xff08;百度网盘&#xff09; 提取密码&#xff1a;8op3 idea 下载可以关注 软件管家 公众号 学习内容&#xff1a; 该视频共分为1-7…

STM32学习历程(day2)

GPIO解释 GPIO(General-purpose input/output) 可以配置为八种输入输出模式 引脚电平 0V-3.3V 部分引脚可容忍5v 输出模式可控制端口输出高低电平 用以驱动LED、控制蜂鸣器、模拟通信协议输出时序 输入模式可读取端口的高低电平或电压&#xff0c;用于读取按键输入、外界…

uniapp自定义富文本现实组件(支持查看和收起)

废话不多说上代码 CollapseText.vue <template><view v-if"descr"><scroll-view class"collapse-text" :style"{maxHeight: computedMaxHeight}"><!-- <slot></slot> --><rich-text :nodes"descr&q…

2-网页请求的原理

网页请求的原理 ​ 网络爬虫请求网页的过程可以理解为用户使用浏览器加载网页的过程&#xff0c;这个过程其实是向Web服务器发送请求的过程&#xff0c;即浏览器向Web服务器发送请求&#xff0c;Web服务器会将响应内容以网页形式返回给浏览器。因此&#xff0c;了解浏览器与We…

Android Studio下载Gradle特别慢,甚至超时,失败。。。解决方法

使用Android studio下载或更新gradle时超级慢怎么办&#xff1f; 切换服务器&#xff0c;立马解决。打开gradle配置文件 修改服务器路径 distributionUrlhttps\://mirrors.cloud.tencent.com/gradle/gradle-7.3.3-bin.zip 最后&#xff0c;同步&#xff0c;下载&#xff0c;速…

数据融合工具(1)指定路径下同名图层合并

情景再现&#xff0c;呼叫小编 ————数据合并时&#xff0c;你是否也经常碰到这些情况&#xff1f; 数据存在几何错误&#xff0c;合并失败&#xff01; 数据字段类型不一致&#xff0c;合并失败&#xff01; 合并工具运行有警告信息&#xff0c;不知道是否合并成功&…

2024年中国网络安全市场全景图 -百度下载

是自2018年开始&#xff0c;数说安全发布的第七版全景图。 企业数智化转型加速已经促使网络安全成为全社会关注的焦点&#xff0c;在网络安全边界不断扩大&#xff0c;新理念、新产品、新技术不断融合发展的进程中&#xff0c;数说安全始终秉承科学的方法论&#xff0c;以遵循…

航模插头篇

一、常见的电池插头&#xff08;电调端 是公头 电池端 是母头&#xff09; 电池总是被插的 1.XT60头 过流大 安全系数高 难插拔 2.T插 插拔轻松 过流比较小 容易发烫 电调端 是公头 电池端 是母头 3.香蕉头插孔 过流够 插拔轻松 但 容易插反 爆炸 4.TX90(和XT60差…