1. ping測驗
協議堆疊提供ping工具,其頭檔案為“net_tools/ping.h”,將其include進你的目標系統中即可使用這個工具,
……
#include "onps.h"
#include "net_tools/ping.h"
//* 回呼函式,收到目標地址的應答報文后ping工具會呼叫這個函式完成用戶的特定處理邏輯
//* 針對這個測驗,在這里就是簡單地列印出了應答報文的內容以及ping的回應時間
static void ping_recv_handler(USHORT usIdentifier, //* ping的標識id,回應報文與探測報文這個id應該一致
in_addr_t unFromAddr, //* 回應報文的源地址
USHORT usSeqNum, //* 回應報文序號,其與探測報文一致
UCHAR *pubEchoData, //* 回應報文攜帶的回應資料,其與探測報文一致
UCHAR ubEchoDataLen, //* 回應報文攜帶的資料長度
UCHAR ubTTL, //* ttl值
UCHAR ubElapsedMSecs) //* 回應時長,單位:秒,從發送探測報文開始計時到收到回應報文結束計時
{
CHAR szSrcAddr[20];
struct in_addr stInAddr;
stInAddr.s_addr = unFromAddr;
printf("<Fr>%s, recv %d bytes, ID=%d, Sequence=%d, Data='https://www.cnblogs.com/neo-T/p/%s', TTL=%d, time=%dms\r\n",
inet_ntoa_safe(stInAddr, szSrcAddr), //* 這是一個執行緒安全的ip地址轉ascii字串函式
(UINT)ubEchoDataLen,
usIdentifier,
usSeqNum,
pubEchoData,
(UINT)ubTTL,
(UINT)ubElapsedMSecs);
}
int main(void)
{
if(open_npstack_load(&enErr))
{
printf("The open source network protocol stack (ver %s) is loaded successfully. \r\n", ONPS_VER);
//* 協議堆疊加載成功,在這里初始化ethernet網卡或等待ppp鏈路就緒
#if 0
emac_init(); //* ethernet網卡初始化函式,并注冊網卡到協議堆疊
#else
while(!netif_is_ready("ppp0")) //* 等待ppp鏈路建立成功
os_sleep_secs(1);
#endif
}
else
{
printf("The open source network protocol stack failed to load, %s\r\n", onps_error(enErr));
return -1;
}
//* 啟動ping測驗
USHORT usSeqNum = 0;
UINT unErrCount = 0;
INT nPing = ping_start(&enErr);
if(nPing < 0)
{
//* 啟動失敗,輸出一條日志資訊
printf("ping_start() failed, %s\r\n", onps_error(enErr));
return -1;
}
while(TRUE && usSeqNum < 100)
{
//* ping目標地址
INT nRtnVal = ping(nPing, inet_addr("192.168.0.2"), usSeqNum++, 64, GetElapsedMSecs, ping_recv_handler, 3, &enErr);
if(nRtnVal <= 0) //* ping回傳一個錯誤
{
//* 累計ping錯誤數
unErrCount++;
//* 控制臺列印當前錯誤數
printf("no reply received, the current number of errors is %d, current error: %s\r\n", unErrCount, nRtnVal ? onps_error(enErr) : "recv timeout");
}
os_sleep_secs(1);
}
//* 結束ping測驗
ping_end(nPing);
return 0;
}
上述示例代碼呼叫了ping測驗工具提供的幾個api函式,ping_start()函式的呼叫非常簡單,其功能就是開啟ping測驗,結束ping測驗需要呼叫ping_end()函式,否則ping測驗會一直占用協議堆疊資源,這幾個函式的說明如下:
2. dns測驗
這個測驗需要為網卡設定好能夠訪問互聯網的網關、DNS服務器地址等配置資訊,當然如果采用dhcp動態地址申請的方式能夠得到這些資訊那就更省事了,dns查詢工具的頭檔案為"net_tools/dns.h",
……
#include "onps.h"
#include "net_tools/ping.h"
int main(void)
{
if(open_npstack_load(&enErr))
{
printf("The open source network protocol stack (ver %s) is loaded successfully. \r\n", ONPS_VER);
//* 協議堆疊加載成功,在這里初始化ethernet網卡或等待ppp鏈路就緒
#if 0
emac_init(); //* ethernet網卡初始化函式,并注冊網卡到協議堆疊
#else
while(!netif_is_ready("ppp0")) //* 等待ppp鏈路建立成功
os_sleep_secs(1);
#endif
}
else
{
printf("The open source network protocol stack failed to load, %s\r\n", onps_error(enErr));
return -1;
}
//* dns查詢測驗
in_addr_t unPrimaryDNS, unSecondaryDNS;
INT nDnsClient = dns_client_start(&unPrimaryDNS, &unSecondaryDNS, 3, &enErr);
if(nDnsClient < 0)
{
//* dns客戶端啟動失敗,輸出一條錯誤日志
printf("%s\r\n", onps_error(enErr));
}
else
{
//* 發送查詢請求并等待dns服務器的應答
in_addr_t unIp = dns_client_query(nDnsClient, unPrimaryDNS, unSecondaryDNS, "gitee.com", &enErr);
if(unIp) //* 查詢成功
{
CHAR szAddr[20];
printf("The ip addr: %s\r\n", inet_ntoa_safe_ext(unIp, szAddr));
}
else
printf("%s\r\n", onps_error(enErr)); //* 查詢失敗
//* 結束dns查詢,釋放占用的協議堆疊資源
dns_client_end(nDnsClient);
}
return 0;
}
與ping測驗工具相同,dns查詢工具同樣提供了一組簡單的api函式用于實作域名查詢,這一組函式包括dns_client_start()、dns_client_end()以及dns_client_query(),其使用說明如下:
3. sntp網路校時測驗
與dns的測驗要求一樣,要進行這個測驗依然要確保你的開發板在物理層能夠訪問互聯網,同時你的開發板支持rtc,并提供一組rtc操作函式,包括讀取、設定系統當前時間等api,這里假設你的測驗環境已經具備上述測驗條件,sntp網路校時工具的頭檔案為"net_tools/sntp.h",
……
#include "onps.h"
#include "net_tools/sntp.h"
int main(void)
{
if(open_npstack_load(&enErr))
{
printf("The open source network protocol stack (ver %s) is loaded successfully. \r\n", ONPS_VER);
//* 協議堆疊加載成功,在這里初始化ethernet網卡或等待ppp鏈路就緒
#if 0
emac_init(); //* ethernet網卡初始化函式,并注冊網卡到協議堆疊
#else
while(!netif_is_ready("ppp0")) //* 等待ppp鏈路建立成功
os_sleep_secs(1);
#endif
}
else
{
printf("The open source network protocol stack failed to load, %s\r\n", onps_error(enErr));
return -1;
}
//* 先設定個不合理的時間,以測驗網路校時功能是否正常,由rtc驅動提供,負責修改系統當前時間
//* RTC前綴的函式為目標系統應提供的rtc時鐘操作函式
RTCSetSysTime(22, 9, 5, 17, 42, 30);
//* 開啟網路校時,sntp_update_by_ip()與sntp_update_by_dns()均可使用
ST_DATETIME stDateTime;
#if 1
if(sntp_update_by_ip("52.231.114.183", NULL, RTCSetSystemUnixTimestamp, 8, &enErr)) //* ntp服務器地址直接校時
#else
if(sntp_update_by_dns("time.windows.com", Time, RTCSetSystemUnixTimestamp, 8, &enErr)) //* ntp服務器域名方式校時
#endif
{
//* 獲取系統時間,檢查校時結果
RTCGetSysTime(&stDateTime);
//* 控制臺輸出當前系統時間
printf("The time is %d-%02d-%02d %02d:%02d:%02d\r\n", stDateTime.usYear, stDateTime.ubMonth,
stDateTime.ubDay, stDateTime.ubHour, stDateTime.ubMin, stDateTime.ubSec);
}
else
{
printf("%s\r\n", onps_error(enErr));
return -1;
}
return 0;
}
測驗代碼首先把時間設定在了2022年9月5日17點42分30秒,目的是為了驗證目標系統時間是否會被成功校正,測驗代碼用到了目標系統應提供的一組rtc時鐘操作函式,其中RTCSetSysTime()用于設定系統時間,RTCSetSystemUnixTimestamp()函式同樣也是設定系統時間,只不過是通過unix時間戳進行設定,RTCGetSysTime()函式用于讀取當前系統時間,相較于ping及dns工具,sntp網路校時工具只提供了一個介面函式sntp_update_by_xx()即可完成校時,我們可以通過ntp服務器地址也可以通過ntp服務器域名進行校時,該函式的詳細使用說明如下:
sntp_update_by_dns()函式與sntp_update_by_ip()函式除了第一個入口引數變成了域名外,其它完全相同,不再贅述,
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/531781.html
標籤:嵌入式
上一篇:AIR32F103(五) FreeRTOSv202112核心庫的集成和示例代碼
下一篇:一文搞懂Path環境變數
