下载 FreeRTOS
 

出色的 RTOS & 嵌入式软件

最新资讯
简化任何设备的身份验证云连接。
利用 CoAP 设计节能型云连接 IoT 解决方案。
11.0.0 版 FreeRTOS 内核简介:
FreeRTOS 路线图和代码贡献流程。
使用 FreeRTOS 实现 OPC-UA over TSN。

FreeRTOS_SendPingRequest()

[FreeRTOS-Plus-TCP API 引用]

FreeRTOS_sockets.h
BaseType_t FreeRTOS_SendPingRequest( uint32_t ulIPAddress,
                                        size_t xNumberOfBytesToSend,
                                        TickType_t xBlockTimeTicks );
		

将 ping (ICMP 回显)请求发送到远程计算机。

ipconfigSUPPORT_OUTGOING_PINGS 必须在 FreeRTOSIPConfig.h 中设置为 1,使 FreeRTOS_SendPingRequest() 可用。

当接收到对传出的 ping 请求的响应时,TCP/IP 堆栈 vApplicationPingReplyHook() 会调用应用程序定义的钩子(或回调函数)。

参数:

ulIPAddress   ping 将发送到的 IP 地址。

IP 地址表示为按网络字节排序的 32 位数字。

xNumberOfBytesToSend   在 ping 请求中发送的数据字节数。

xBlockTimeTicks   调用任务准备等待缓冲区的最长时间RTOS 调用任务准备等待网络缓冲区的最长时间。

如果网络缓冲区不可用,则调用任务将RTOS 保持在阻塞状态(以便执行其他任务), 直至缓冲区变为可用并传输了 ping 请求,或直至 阻塞时间到期。

阻塞时间以 tick 为单位。 毫秒可以转换为 tick, 方法是将毫秒时间除以 portTICK_PERIOD_MS 转换为 tick。

返回:

如果 ping 请求发送成功,则返回在 ping 消息中发送的序列号, 以使应用程序编写者可以将传输的 ping 请求 与接收的 ping 回复进行匹配。 请参阅下面的示例。

如果无法发送 ping 请求,则返回 pdFAIL。

用法示例:

本示例定义了两个函数。 vSendPing() 会将 8 个字节传输到远程 IP 地址。 vApplicationPingReplyHook() 是标准的 FreeRTOS-Plus-TCP ping 回复回调函数。 vApplicationPingReplyHook() 接收 ping 回复,然后发送接收的序列号 到 vSendPing() 中,将它与 ping 请求的序列号进行比较。


/* FreeRTOS-Plus-TCP sockets include. */
#include "FreeRTOS_sockets.h"

/* This example code snippet assumes the queue has already been created! */
QueueHandle_t xPingReplyQueue;

/* If ipconfigSUPPORT_OUTGOING_PINGS is set to 1 in FreeRTOSIPConfig.h then
vApplicationPingReplyHook() is called by the TCP/IP stack when the stack receives a
ping reply.

void vApplicationPingReplyHook( ePingReplyStatus_t eStatus, uint16_t usIdentifier )
{
switch( eStatus )
{
case eSuccess :
/* A valid ping reply has been received. Post the sequence number
on the queue that is read by the vSendPing() function below. Do
not wait more than 10ms trying to send the message if it cannot be
sent immediately because this function is called from the TCP/IP
RTOS task - blocking in this function will block the TCP/IP RTOS task. */

xQueueSend( xPingReplyQueue, &usIdentifier, 10 / portTICK_PERIOD_MS );
break;

case eInvalidChecksum :
case eInvalidData :
/* A reply was received but it was not valid. */
break;
}
}


BaseType_t vSendPing( const int8_t *pcIPAddress )
{
uint16_t usRequestSequenceNumber, usReplySequenceNumber;
uint32_t ulIPAddress;

/* The pcIPAddress parameter holds the destination IP address as a string in
decimal dot notation (for example, "192.168.0.200"). Convert the string into
the required 32-bit format. */

ulIPAddress = FreeRTOS_inet_addr( pcIPAddress );

/* Send a ping containing 8 data bytes. Wait (in the Blocked state) a
maximum of 100ms for a network buffer into which the generated ping request
can be written and sent. */

usRequestSequenceNumber = FreeRTOS_SendPingRequest( ulIPAddress, 8, 100 / portTICK_PERIOD_MS );

if( usRequestSequenceNumber == pdFAIL )
{
/* The ping could not be sent because a network buffer could not be
obtained within 100ms of FreeRTOS_SendPingRequest() being called. */

}
else
{
/* The ping was sent. Wait 200ms for a reply. The sequence number from
each reply is sent from the vApplicationPingReplyHook() on the
xPingReplyQueue queue (this is not standard behaviour, but implemented in
the example function above). It is assumed the queue was created before
this function was called! */

if( xQueueReceive( xPingReplyQueue,
&usReplySequenceNumber,
200 / portTICK_PERIOD_MS ) == pdPASS )
{
/* A ping reply was received. Was it a reply to the ping just sent? */
if( usRequestSequenceNumber == usReplySequenceNumber )
{
/* This was a reply to the request just sent. */
}
}
}
}

Example use of the FreeRTOS_SendPingRequest() API function
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.