zs39 opened a new pull request, #3300:
URL: https://github.com/apache/nuttx-apps/pull/3300

   ## 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.
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to