目标:STM32端作为客户端,然后发送ping命令,检查客户端到服务端的网络通断问题。
内容实质:客服端发送ICMP数据包,等待服务端的响应。有响应:网络正常,无响应:网络断开。
参考资料:1、《嵌入式网络那些事》--五木 2、http://blog.csdn.net/jtttl/article/details/50583462
关键代码:
/** ping identifier - must fit on a u16_t */
#ifndef PING_ID
#define PING_ID 0xAFAF
#endif
/** ping additional data size to include in the packet */
#ifndef PING_DATA_SIZE
#define PING_DATA_SIZE 32
#endif
/* ping variables */
static u16_t ping_seq_num;
static struct raw_pcb *ping_pcb = NULL;
static ip_addr_t ping_dst;
//组装报文
static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
{
size_t i;
size_t data_len = len - sizeof(struct icmp_echo_hdr);
ICMPH_TYPE_SET(iecho, ICMP_ECHO);
ICMPH_CODE_SET(iecho, 0);
iecho->chksum = 0;
iecho->id = PING_ID;
iecho->seqno = htons(++ping_seq_num);
/* fill the additional data buffer with some data */
for(i = 0; i < data_len; i++) {
((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
}
iecho->chksum = inet_chksum(iecho, len);
}
static void ping_send(struct raw_pcb *raw, ip_addr_t *addr)
{
struct pbuf *p;
struct icmp_echo_hdr *iecho;
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
if (!p) {
return;
}
if ((p->len == p->tot_len) && (p->next == NULL)) {
iecho = (struct icmp_echo_hdr *)p->payload;
ping_prepare_echo(iecho, (u16_t)ping_size);
//发送
raw_sendto(raw, p, addr);
}
pbuf_free(p);
}
//发送ping包
void pingCmdSend(void)
{
ping_send(ping_pcb, &ping_dst);
}
//初始化
void ping_init(void)
{
ping_pcb = raw_new(IP_PROTO_ICMP);
IP4_ADDR(&ping_dst, 192,168,1,100);//服务端地址
}
//freertos 任务
void tcpClient(void *arg)
{
/* init code for LWIP */
MX_LWIP_Init();
//ping命令初始化
ping_init();
for(;;){
pingCmdSend();
vTaskDelay(5000);//每5sping一次服务端
}
}