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

news2024/11/19 20:16:58

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.cn
    IP4_ADDR(&g_ntpServerList[1], 114, 118, 7, 163);  // ntp.ntsc.ac.cn
    IP4_ADDR(&g_ntpServerList[2], 120, 25, 108, 11);   // time.pool.aliyun.com
    IP4_ADDR(&g_ntpServerList[3], 119, 28, 206, 193); // cn.pool.ntp.org

    for (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.cn
    IP4_ADDR(&g_ntpServerList[1], 114, 118, 7, 163);  // ntp.ntsc.ac.cn
    IP4_ADDR(&g_ntpServerList[2], 120, 25, 108, 11);   // time.pool.aliyun.com
    IP4_ADDR(&g_ntpServerList[3], 119, 28, 206, 193); // cn.pool.ntp.org

    for (size_t i = 0; i < SNTP_SERVERS; i++) {
        sntp_setserver(i, (ip_addr_t*)&g_ntpServerList[i]);
    }
}
#endif

static 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_DNS
    ip4_addr_t dnsServerAddr;
    IP4_ADDR(&dnsServerAddr, 202, 98, 0, 68);
    dns_setserver(0, (struct ip_addr *)&dnsServerAddr);
    dns_init();

    SntpSetServernames();
#else
    SntpSetServers();
#endif

    sntp_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.coloradmin.cn/o/1896300.html

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

相关文章

基于布雷格曼偏差校正技术的全变分一维时间序列信号降噪方法(MATLAB R2018A)

信号降噪是信号处理的重要步骤之一&#xff0c;目的是提高所获得信号数据的质量&#xff0c;以达到更高的定性和定量分析精度。信号降噪能提升信号处理其他环节的性能和人们对信息识别的准确率&#xff0c;给信号处理工作提供更可靠的保证。信号降噪的难点是降低噪声的同时也会…

如何压缩视频大小,怎么压缩视频

随着科技的进步&#xff0c;我们拍摄的视频质量越来越高。然而&#xff0c;视频带来的一个问题是文件体积巨大&#xff0c;这不仅占用大量存储空间&#xff0c;而且在传输和分享时也造成了不小的困扰。别担心&#xff0c;本文将为你详细介绍几种实用的视频压缩方法&#xff0c;…

xmind2testcase工具将测试用例从Xmind转为CSV导入禅道

使用xmind编写测试用例&#xff0c;使用xmind2testcase工具将测试用例从Xmind转为CSV导入禅道&#xff0c;便于管理。 1.工具准备 第一步&#xff1a;安装python 第二步&#xff1a;安装xmind2testcase工具 运行-cmd-打开命令提示符弹窗&#xff0c;输入安装命令 安装命令&…

粉丝问:教育机构首页UI,安排

教育机构的首页UI应该展现以下内容&#xff1a; 机构简介&#xff1a;在首页上展示教育机构的简介和核心价值观&#xff0c;包括机构的使命、愿景和教育理念。这有助于让访问者了解机构的背景和宗旨。课程和项目&#xff1a;展示机构提供的课程和项目&#xff0c;包括学科课程…

AGI|Transformer自注意力机制超全扫盲攻略,建议收藏!

一、前言 2017年&#xff0c;谷歌团队推出一篇神经网络的论文&#xff0c;首次提出将“自注意力”机制引入深度学习中&#xff0c;这一机制可以根据输入数据各部分重要性的不同而分配不同的权重。当ChatGPT震惊世人时&#xff0c;Transformer也随之进入大众视野。一夜之间&…

PMP--知识卡片--波士顿矩阵

文章目录 记忆黑话概念作用图示 记忆 一说到波士顿就联想到波士顿龙虾&#xff0c;所以波士顿矩阵跟动物有关&#xff0c;狗&#xff0c;牛。 黑话 你公司的现金牛业务&#xff0c;正在逐渐变成瘦狗&#xff0c;应尽快采取收割策略&#xff1b;问题业务的储备太少&#xff0…

测量和检测二合一的劳易测解决方案

劳易测ODT3CL系列是一款基于TOF原理的具有测量功能的传感器。它具有很好的检测性能&#xff0c;能够非常可靠的检测亮色、深色以及高反光的物体。ODT 3CL系列的检测距离可达2000毫米&#xff0c;在量程范围内对目标物体的颜色检测不敏感&#xff0c;检测距离稳定输出。 针对工业…

浅析基于量子成像的下一代甚高灵敏度图像传感器技术

高灵敏度探测成像是空间遥感应用中的一个重要技术领域&#xff0c;如全天时对地观测、空间暗弱目标跟踪识别等应用&#xff0c;对于甚高灵敏度图像传感器的需求日益强烈。随着固态图像传感器技术水平的不断提高&#xff0c;尤其背照式及埋沟道等工艺的突破&#xff0c;使得固态…

相机光学(二十四)——CRA角度

CRA角度 0.参考资料1.什么是CRA角度2.为什么 CRA 会导致luma shading3.为什么 CRA 会导致color shading4.CRA相差过大的具体表现5.CRA Matching6.怎样选择sensor的CRA 0.参考资料 1.芯片CRA角度与镜头的匹配关系&#xff08;一&#xff09;   2.芯片CRA角度与镜头选型的匹配关…

谷粒商城学习-07-虚拟机网络设置

文章目录 一&#xff0c;找到配置文件Vagrantfile二&#xff0c;查询虚拟机网卡地址1&#xff0c;查看虚拟机网络配置2&#xff0c;查看宿主机网络配置 三&#xff0c;修改配置文件下的IP配置四&#xff0c;重新启动虚拟机即可生效五&#xff0c;Vagrantfile 的作用1&#xff0…

护眼落地灯哪个牌子好?盘点五款必入不踩雷的护眼大路灯

护眼落地灯哪个牌子好&#xff1f;在这个快节奏的时代&#xff0c;护眼落地灯已经从一种高端选择转变为日常用眼生活中的必须品。不论是提升普通照明&#xff0c;还是针对孩子学习是改善光线质量环境&#xff0c;一款优秀的护眼落地灯都能成为我们生活中的照明神器。怎么选择一…

QT+OpenCV在Android上实现人脸实时检测与目标检测

一、功能介绍 在当今的移动应用领域&#xff0c;随着技术的飞速发展和智能设备的普及&#xff0c;将先进的计算机视觉技术集成到移动平台&#xff0c;特别是Android系统中&#xff0c;已成为提升用户体验、拓展应用功能的关键。其中&#xff0c;目标检测与人脸识别作为计算机视…

固相提取铕和铀

固相萃取&#xff08;Solid Phase Extraction&#xff0c;SPE&#xff09;是一种常用的化学分离技术&#xff0c;它利用固体吸附剂&#xff08;固定相&#xff09;与样品中的目标化合物&#xff08;流动相&#xff09;之间的相互作用力&#xff0c;将目标化合物从样品中分离出来…

【数据结构】(C语言):堆(二叉树的应用)

堆&#xff1a; 此处堆为二叉树的应用&#xff0c;不是计算机中用于管理动态内存的堆。形状是完全二叉树。堆分两种&#xff1a;最大堆&#xff0c;最小堆。最大堆&#xff1a;每个节点比子树所有节点的数值都大&#xff0c;根节点为最大值。最小堆&#xff1a;每个节点比子树…

推动高效能:东芝TB67H301FTG全桥直流电机驱动IC

在如今高度自动化的时代&#xff0c;电子产品的性能和效率成为了工程师们关注的焦点。东芝的TB67H301FTG全桥直流电机驱动IC应运而生&#xff0c;以其卓越的技术和可靠性&#xff0c;成为众多应用的理想选择。无论是在机器人、家用电器、工业自动化&#xff0c;还是在其他需要精…

面试篇-系统设计题总结

这里记录一些有趣的系统设计类的题目&#xff0c;一般大家比较喜欢出的设计类面试题目会和高可用系统相关比如秒杀和抢红包等。欢迎大家在评论中评论自己遇到的题目&#xff0c;本篇文章会持续更新。 1、设计一个抢红包系统 抢红包系统其实也是秒杀类中的一个场景&#xff0…

【Linux系统编程】深入剖析:四大IO模型机制与应用(阻塞、非阻塞、多路复用、信号驱动IO 全解读)

目录 概述&#xff1a; 1. 阻塞IO (Blocking IO) 2. 非阻塞IO (Non-blocking IO) 3. IO多路复用 (I/O Multiplexing) 4. 信号驱动IO (Signal-driven IO) 阻塞式IO 非阻塞式IO 信号驱动IO&#xff08;Signal-driven IO&#xff09; 信号IO实例&#xff1a; IO多路复用…

后端之路——最规范、便捷的spring boot工程配置

一、参数配置化 上一篇我们学了阿里云OSS的使用&#xff0c;那么我们为了方便使用OSS来上传文件&#xff0c;就创建了一个【util】类&#xff0c;里面有一个【AliOSSUtils】类&#xff0c;虽然本人觉得没啥不方便的&#xff0c;但是黑马视频又说这样还是存在不便维护和管理问题…

Java支付宝沙箱支付环境配置及简单测试

Java支付宝沙箱环境配置(测试) 1. 沙箱配置环境 沙箱应用 - 开放平台 (alipay.com) 2. 需要用到的基本信息 3. Pom文件添加依赖 <!--支付宝依赖 --><dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-easysdk</artifactId…

上海晋名室外危废品暂存柜第三次复购项目落地

近日又有一台SAVEST室外危废品暂存柜项目成功验收交付使用。 用户单位是一家致力于光伏电池银浆用电子级银粉的国产化&#xff0c;并开发MLCC、锂离子电池、半导体等领域使用的纳米级粉体材料的企业。 用户于2021年4月底订购1台室外危化品暂存柜和1台室外危废品暂存柜&#x…