From 657b4cb29116bde6b3d0ad3a67fedcd22a7d6725 Mon Sep 17 00:00:00 2001 From: rob-the-dude <43481893+rob-the-dude@users.noreply.github.com> Date: Thu, 21 May 2020 21:58:29 -0700 Subject: [PATCH] [cli] add command for getting the network interface name and index (#4985) * add a CLI command for getting the network interface name and index On some platform (mostly BSDs), we do not have the option to specify the tunnel name, although most of those platforms do have a way to dynamically obtain a unique interface name if desired. Add a CLI command -- only enabled on platforms that have PLATFORM_NETIF enabled -- that will return the name of the tunnel interface, as well as it's index. Both are useful in doing automated tests on non-Linux systems. * remove OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE from the "check" scripts This option really only applies to the POSIX "management" applications (ot-daemon, ot-ctl), and not to the core radio or simulation component builds (ot-cli, etc). My other change adds a command ("netif") that is only useful when the netif code is enabled, and that only happens when the netif code is linked, which is only in those "management" utilities. Turning this setting off allows the tests to pass, with no impact to the functionality previously tested. But if this setting remains on, then my new code creates a dependency that cannot be resolved outside the management applications. --- include/openthread/platform/misc.h | 15 +++++++++++++++ script/check-scan-build | 1 - script/check-simulation-build-autotools | 1 - src/cli/cli.cpp | 25 +++++++++++++++++++++++++ src/cli/cli.hpp | 3 +++ src/posix/platform/netif.cpp | 17 +++++++++++++++++ 6 files changed, 60 insertions(+), 2 deletions(-) diff --git a/include/openthread/platform/misc.h b/include/openthread/platform/misc.h index 99c3ed3f5..a5d5e4479 100644 --- a/include/openthread/platform/misc.h +++ b/include/openthread/platform/misc.h @@ -198,6 +198,21 @@ otPlatMcuPowerState otPlatGetMcuPowerState(otInstance *aInstance); * */ +#if defined(OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE) && OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE +/** + * This function gets the name and index of the platform's network interface (if it exists). + * + * @param[in] aInstance A pointer to OpenThread instance. + * @param[out] outNetIfName A pointer for the returned network interface name. + * @param[out] outNetIfIndex A pointer for the returned network interface index (i.e., if_nametoindex). + * + * @retval OT_ERROR_NONE Successfully returned the network interface and index. + * @retval OT_ERROR_FAILED The network interface is not enabled or is unknown. + * + */ +otError otPlatGetNetif(otInstance *aInstance, const char **outNetIfName, unsigned int *outNetIfIndex); +#endif + #ifdef __cplusplus } // extern "C" #endif diff --git a/script/check-scan-build b/script/check-scan-build index dc38bd726..9d4292fce 100755 --- a/script/check-scan-build +++ b/script/check-scan-build @@ -68,7 +68,6 @@ do_scan_build() "-DOPENTHREAD_CONFIG_MLE_STEERING_DATA_SET_OOB_ENABLE=1" "-DOPENTHREAD_CONFIG_MPL_DYNAMIC_INTERVAL_ENABLE" "-DOPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE=1" - "-DOPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE=1" "-DOPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE=1" "-DOPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE=1" "-DOPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE=1" diff --git a/script/check-simulation-build-autotools b/script/check-simulation-build-autotools index c1a50415b..7d54d7537 100755 --- a/script/check-simulation-build-autotools +++ b/script/check-simulation-build-autotools @@ -74,7 +74,6 @@ build_all_features() "-DOPENTHREAD_CONFIG_MPL_DYNAMIC_INTERVAL_ENABLE" "-DOPENTHREAD_CONFIG_NCP_UART_ENABLE=1" "-DOPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE=1" - "-DOPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE=1" "-DOPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE=1" "-DOPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE=1" "-DOPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE=1" diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 880e05b71..5d116efac 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -63,6 +63,9 @@ #include #include #include +#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE +#include +#endif #include "common/new.hpp" #include "net/ip6.hpp" @@ -184,6 +187,9 @@ const struct Command Interpreter::sCommands[] = { {"netdataregister", &Interpreter::ProcessNetworkDataRegister}, #endif {"netdatashow", &Interpreter::ProcessNetworkDataShow}, +#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE + {"netif", &Interpreter::ProcessNetif}, +#endif #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE {"networkdiagnostic", &Interpreter::ProcessNetworkDiagnostic}, #endif // OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE @@ -1985,6 +1991,25 @@ exit: AppendResult(error); } +#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE +void Interpreter::ProcessNetif(uint8_t aArgsLength, char *aArgs[]) +{ + OT_UNUSED_VARIABLE(aArgsLength); + OT_UNUSED_VARIABLE(aArgs); + + otError error = OT_ERROR_NONE; + const char * netif = NULL; + unsigned int netifidx = 0; + + SuccessOrExit(error = otPlatGetNetif(mInstance, &netif, &netifidx)); + + mServer->OutputFormat("%s:%u\r\n", netif ? netif : "", netifidx); + +exit: + AppendResult(error); +} +#endif + #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE void Interpreter::ProcessService(uint8_t aArgsLength, char *aArgs[]) { diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 3eaaa17f6..1fdf25ac1 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -285,6 +285,9 @@ private: void ProcessNetworkDataRegister(uint8_t aArgsLength, char *aArgs[]); #endif void ProcessNetworkDataShow(uint8_t aArgsLength, char *aArgs[]); +#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE + void ProcessNetif(uint8_t aArgsLength, char *aArgs[]); +#endif #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE void ProcessService(uint8_t aArgsLength, char *aArgs[]); #endif diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index 8bc6c9bb3..a5605ed03 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -141,6 +141,7 @@ extern int #include #include #include +#include #include "common/code_utils.hpp" #include "common/logging.hpp" @@ -1461,4 +1462,20 @@ exit: return; } +otError otPlatGetNetif(otInstance *aInstance, const char **outNetIfName, unsigned int *outNetIfIndex) +{ + OT_UNUSED_VARIABLE(aInstance); + + otError error; + + VerifyOrExit(sTunIndex != 0, error = OT_ERROR_FAILED); + + *outNetIfName = sTunName; + *outNetIfIndex = sTunIndex; + error = OT_ERROR_NONE; + +exit: + + return error; +} #endif // OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE