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