Skip to content

Conversation

@zs39
Copy link
Contributor

@zs39 zs39 commented Jan 7, 2026

Summary

Perform connectivity testing on the specified network interface card.

Impact

Adding new APIs will not affect existing functionality.

Testing

Use the following code to test.

/****************************************************************************
 * apps/examples/hello/hello_main.c
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.  The
 * ASF licenses this file to you 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.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#ifdef CONFIG_NETUTILS_PING
#include <netutils/netlib.h>
#endif

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * hello_main
 ****************************************************************************/

int main(int argc, FAR char *argv[])
{
  printf("Hello, World!!\n");

#ifdef CONFIG_NETUTILS_PING
  const char *ifname = "eth0";  /* Default network interface */
  int timeout = 5000;            /* 5 seconds (in milliseconds) */
  int retry = 3;                 /* 3 times */
  int replies;

  /* Optional: interface name, timeout and retry from command line */
  if (argc > 1)
    {
      ifname = argv[1];
    }

  if (argc > 2)
    {
      timeout = atoi(argv[2]);
    }

  if (argc > 3)
    {
      retry = atoi(argv[3]);
    }

  printf("\n=== Network Interface Connectivity Test ===\n");
  printf("Interface: %s\n", ifname);
  printf("Timeout: %d ms\n", timeout);
  printf("Retry: %d times\n", retry);
  printf("Checking connectivity by pinging gateway of %s...\n\n", ifname);

  replies = netlib_check_ifconnectivity(ifname, timeout, retry);

  if (replies > 0)
    {
      printf("SUCCESS: Interface %s is reachable!\n", ifname);
      printf("Received %d ping reply(ies) from gateway.\n", replies);
      return 0;
    }
  else if (replies == 0)
    {
      printf("FAILED: Interface %s is not reachable.\n", ifname);
      printf("No ping replies received from gateway.\n");
      return 1;
    }
  else
    {
      printf("ERROR: Failed to check connectivity for %s.\n", ifname);
      printf("Error code: %d (errno: %d)\n", replies, errno);
      return 1;
    }
#else
  printf("\nNote: Network connectivity check is disabled.\n");
  printf("Enable CONFIG_NETUTILS_PING to use this feature.\n");
  return 0;
#endif
}

The execution results are as follows,Test results show that the API works correctly.

nsh> hello
Hello, World!!

=== Network Interface Connectivity Test ===
Interface: eth0
Timeout: 5000 ms
Retry: 3 times
Checking connectivity by pinging gateway of eth0...

SUCCESS: Interface eth0 is reachable!
Received 3 ping reply(ies) from gateway.

Perform connectivity testing on the specified network interface card.

Signed-off-by: meijian <meijian@xiaomi.com>
@zs39
Copy link
Contributor Author

zs39 commented Jan 7, 2026

Documentation supplement here
apache/nuttx#17786

Copy link
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @zs39 :-)

@xiaoxiang781216 xiaoxiang781216 merged commit c61fb84 into apache:master Jan 9, 2026
69 of 77 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants