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进行烧录。