From 0a82741e86b7a6d5d2e7dd6c18890d4e3d6fd2a5 Mon Sep 17 00:00:00 2001 From: JakubBrachTieto Date: Sat, 2 Sep 2017 06:24:36 +0200 Subject: [PATCH] [commissioning] changing CPU clock for commissioning process (#2041) --- examples/apps/cli/Makefile.am | 14 ++ examples/apps/ncp/Makefile.am | 14 ++ examples/platforms/da15000/misc.c | 4 +- .../da15000/openthread-core-da15000-config.h | 10 +- examples/platforms/da15000/platform.c | 167 +++++++++++++++-- examples/platforms/da15000/uart.c | 2 +- include/openthread/joiner.h | 29 +++ include/openthread/types.h | 2 + src/core/api/commissioner_api.cpp | 174 +++++++++++++++--- src/core/api/joiner_api.cpp | 47 ++++- src/core/meshcop/commissioner.cpp | 3 + src/core/meshcop/joiner.cpp | 42 +++-- src/core/meshcop/joiner.hpp | 26 ++- 13 files changed, 457 insertions(+), 77 deletions(-) diff --git a/examples/apps/cli/Makefile.am b/examples/apps/cli/Makefile.am index b353d47f9..36319283b 100644 --- a/examples/apps/cli/Makefile.am +++ b/examples/apps/cli/Makefile.am @@ -43,6 +43,10 @@ LDADD_COMMON = \ LDFLAGS_COMMON = \ $(NULL) +LIBTOOLFLAGS_COMMON = \ + --preserve-dup-deps \ + $(NULL) + SOURCES_COMMON = \ main.c \ $(NULL) @@ -181,12 +185,17 @@ ot_cli_ftd_LDADD = \ $(top_builddir)/src/cli/libopenthread-cli-ftd.a \ $(top_builddir)/src/core/libopenthread-ftd.a \ $(LDADD_COMMON) \ + $(top_builddir)/src/core/libopenthread-ftd.a \ $(NULL) ot_cli_ftd_LDFLAGS = \ $(LDFLAGS_COMMON) \ $(NULL) +ot_cli_ftd_LIBTOOLFLAGS = \ + $(LIBTOOLFLAGS_COMMON) \ + $(NULL) + ot_cli_ftd_SOURCES = \ $(SOURCES_COMMON) \ $(NULL) @@ -205,12 +214,17 @@ ot_cli_mtd_LDADD = \ $(top_builddir)/src/cli/libopenthread-cli-mtd.a \ $(top_builddir)/src/core/libopenthread-mtd.a \ $(LDADD_COMMON) \ + $(top_builddir)/src/core/libopenthread-mtd.a \ $(NULL) ot_cli_mtd_LDFLAGS = \ $(LDFLAGS_COMMON) \ $(NULL) +ot_cli_mtd_LIBTOOLFLAGS = \ + $(LIBTOOLFLAGS_COMMON) \ + $(NULL) + ot_cli_mtd_SOURCES = \ $(SOURCES_COMMON) \ $(NULL) diff --git a/examples/apps/ncp/Makefile.am b/examples/apps/ncp/Makefile.am index 5908c458c..fbf2fbcea 100644 --- a/examples/apps/ncp/Makefile.am +++ b/examples/apps/ncp/Makefile.am @@ -43,6 +43,10 @@ LDADD_COMMON = \ LDFLAGS_COMMON = \ $(NULL) +LIBTOOLFLAGS_COMMON = \ + --preserve-dup-deps \ + $(NULL) + SOURCES_COMMON = \ main.c \ $(NULL) @@ -173,12 +177,17 @@ ot_ncp_ftd_LDADD = \ $(top_builddir)/src/ncp/libopenthread-ncp-ftd.a \ $(top_builddir)/src/core/libopenthread-ftd.a \ $(LDADD_COMMON) \ + $(top_builddir)/src/core/libopenthread-ftd.a \ $(NULL) ot_ncp_ftd_LDFLAGS = \ $(LDFLAGS_COMMON) \ $(NULL) +ot_ncp_ftd_LIBTOOLFLAGS = \ + $(LIBTOOLFLAGS_COMMON) \ + $(NULL) + ot_ncp_ftd_SOURCES = \ $(SOURCES_COMMON) \ $(NULL) @@ -197,12 +206,17 @@ ot_ncp_mtd_LDADD = \ $(top_builddir)/src/ncp/libopenthread-ncp-mtd.a \ $(top_builddir)/src/core/libopenthread-mtd.a \ $(LDADD_COMMON) \ + $(top_builddir)/src/core/libopenthread-mtd.a \ $(NULL) ot_ncp_mtd_LDFLAGS = \ $(LDFLAGS_COMMON) \ $(NULL) +ot_ncp_mtd_LIBTOOLFLAGS = \ + $(LIBTOOLFLAGS_COMMON) \ + $(NULL) + ot_ncp_mtd_SOURCES = \ $(SOURCES_COMMON) \ $(NULL) diff --git a/examples/platforms/da15000/misc.c b/examples/platforms/da15000/misc.c index c2a9a7027..15ab00abf 100644 --- a/examples/platforms/da15000/misc.c +++ b/examples/platforms/da15000/misc.c @@ -26,10 +26,12 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include +#include +#include #include +#include "hw_cpm.h" #include "sdk_defs.h" void otPlatReset(otInstance *aInstance) diff --git a/examples/platforms/da15000/openthread-core-da15000-config.h b/examples/platforms/da15000/openthread-core-da15000-config.h index 464eab9e9..02c4bfcab 100644 --- a/examples/platforms/da15000/openthread-core-da15000-config.h +++ b/examples/platforms/da15000/openthread-core-da15000-config.h @@ -79,7 +79,15 @@ * The DA15000 platform provides an otPlatLog() function. */ #ifndef OPENTHREAD_CONFIG_LOG_OUTPUT /* allow command line override */ -#define OPENTHREAD_CONFIG_LOG_OUTPUT OPENTHERAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED +#define OPENTHREAD_CONFIG_LOG_OUTPUT OPENTHREAD_CONFIG_LOG_OUTPUT_APP #endif +/** + * @def OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS + * + * The maximum number of state-changed callback handlers (set using `otSetStateChangedCallback()`). + * + */ +#define OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS 3 + #endif // OPENTHREAD_CORE_DA15000_CONFIG_H_ diff --git a/examples/platforms/da15000/platform.c b/examples/platforms/da15000/platform.c index f59d0be4a..87bd5745e 100644 --- a/examples/platforms/da15000/platform.c +++ b/examples/platforms/da15000/platform.c @@ -31,9 +31,10 @@ * This file includes the platform-specific initializers. */ -#include -#include +#include +#include +#include #include #include #include @@ -58,9 +59,7 @@ static int sMsCounter; #define ROUTER_BLINK_TIME (500) #define CHILD_BLINK_TIME (2000) -#define SET_CLOCK_TO_96MHZ - -otInstance *sInstance; +static otInstance *sInstance = NULL; void ClkInit(void) { @@ -80,19 +79,11 @@ void ClkInit(void) hw_watchdog_freeze(); // Stop watchdog hw_cpm_set_recharge_period((uint16_t)dg_configSET_RECHARGE_PERIOD); - hw_watchdog_unfreeze(); // Start watchdog - hw_cpm_pll_sys_on(); // Turn on PLL - hw_watchdog_freeze(); // Stop watchdog - - hw_qspi_set_div(HW_QSPI_DIV_2); // Set QSPI div by 2 - - hw_cpm_disable_pll_divider(); // Disable divider (div by 2) - hw_cpm_set_sysclk(SYS_CLK_IS_PLL); - hw_cpm_set_hclk_div(ahb_div2); + hw_cpm_set_sysclk(SYS_CLK_IS_XTAL16M); + hw_cpm_set_hclk_div(0); hw_cpm_set_pclk_div(0); - hw_otpc_init(); - hw_otpc_set_speed(HW_OTPC_SYS_CLK_FREQ_48); + hw_otpc_set_speed(HW_OTPC_SYS_CLK_FREQ_16); } /* @@ -163,7 +154,6 @@ void ExampleProcess(otInstance *aInstance) } } - void PlatformInit(int argc, char *argv[]) { // Initialize System Clock @@ -181,10 +171,151 @@ void PlatformInit(int argc, char *argv[]) (void)argv; } +static sys_clk_t ClkGet(void) +{ + sys_clk_t clk = sysclk_RC16; + uint32_t hw_clk = hw_cpm_get_sysclk(); + + switch (hw_clk) + { + case SYS_CLK_IS_RC16: + clk = sysclk_RC16; + break; + + case SYS_CLK_IS_XTAL16M: + if (dg_configEXT_CRYSTAL_FREQ == EXT_CRYSTAL_IS_16M) + { + clk = sysclk_XTAL16M; + } + else + { + clk = sysclk_XTAL32M; + } + + break; + + case SYS_CLK_IS_PLL: + if (hw_cpm_get_pll_divider_status() == 1) + { + clk = sysclk_PLL48; + } + else + { + clk = sysclk_PLL96; + } + + break; + + case SYS_CLK_IS_LP: + + // fall-through + + default: + ASSERT_WARNING(0); + break; + } + + return clk; +} + +static void ClkSet(sys_clk_t clock) +{ + switch (clock) + { + case sysclk_XTAL16M: + if (!hw_cpm_check_xtal16m_status()) // XTAL16M disabled + { + hw_cpm_enable_xtal16m(); // Enable XTAL16M + } + + hw_cpm_set_sysclk(SYS_CLK_IS_XTAL16M); // Set XTAL16 as sys_clk + hw_watchdog_unfreeze(); // Start watchdog + + while (!hw_cpm_is_xtal16m_started()); // Block until XTAL16M starts + + hw_qspi_set_div(HW_QSPI_DIV_1); + hw_watchdog_freeze(); // Stop watchdog + hw_cpm_set_hclk_div(0); + hw_cpm_set_pclk_div(0); + break; + + case sysclk_PLL48: + if (hw_cpm_is_pll_locked() == 0) + { + hw_watchdog_unfreeze(); // Start watchdog + hw_cpm_pll_sys_on(); // Turn on PLL + hw_watchdog_freeze(); // Stop watchdog + } + + hw_cpm_enable_pll_divider(); // Enable divider (div by 2) + hw_qspi_set_div(HW_QSPI_DIV_1); + hw_cpm_set_sysclk(SYS_CLK_IS_PLL); + hw_cpm_set_hclk_div(0); + hw_cpm_set_pclk_div(0); + break; + + case sysclk_PLL96: + if (hw_cpm_is_pll_locked() == 0) + { + hw_watchdog_unfreeze(); // Start watchdog + hw_cpm_pll_sys_on(); // Turn on PLL + hw_watchdog_freeze(); // Stop watchdog + } + + hw_cpm_disable_pll_divider(); // Disable divider (div by 1) + hw_qspi_set_div(HW_QSPI_DIV_2); + hw_cpm_set_sysclk(SYS_CLK_IS_PLL); + hw_cpm_set_hclk_div(0); + hw_cpm_set_pclk_div(0); + break; + + default: + break; + } +} + +static void ClkChange(sys_clk_t lastClock, sys_clk_t newClock) +{ + if (ClkGet() == lastClock) + { + ClkSet(newClock); + } +} + +void StateChangedCallback(uint32_t aFlags, void *aContext) +{ + if ((aFlags & OT_CHANGED_COMMISSIONER_STATE) != 0) + { + if (otCommissionerGetState(sInstance) == OT_COMMISSIONER_STATE_ACTIVE) + { + ClkChange(sysclk_XTAL16M, sysclk_PLL96); + } + else + { + ClkChange(sysclk_PLL96, sysclk_XTAL16M); + } + } + + if ((aFlags & OT_CHANGED_JOINER_STATE) != 0) + { + if (otJoinerGetState(sInstance) != OT_JOINER_STATE_IDLE) + { + ClkChange(sysclk_XTAL16M, sysclk_PLL96); + } + else + { + ClkChange(sysclk_PLL96, sysclk_XTAL16M); + } + } +} void PlatformProcessDrivers(otInstance *aInstance) { - sInstance = aInstance; + if (sInstance == NULL) + { + sInstance = aInstance; + otSetStateChangedCallback(aInstance, StateChangedCallback, 0); + } da15000UartProcess(); da15000RadioProcess(aInstance); diff --git a/examples/platforms/da15000/uart.c b/examples/platforms/da15000/uart.c index 23b9b8396..c62b9dda6 100644 --- a/examples/platforms/da15000/uart.c +++ b/examples/platforms/da15000/uart.c @@ -50,7 +50,7 @@ otError otPlatUartEnable(void) otError error = OT_ERROR_NONE; uart_config uart_init = { - .baud_rate = HW_UART_BAUDRATE_115200, + .baud_rate = HW_UART_BAUDRATE_57600, .data = HW_UART_DATABITS_8, .stop = HW_UART_STOPBITS_1, .parity = HW_UART_PARITY_NONE, diff --git a/include/openthread/joiner.h b/include/openthread/joiner.h index 456d0165f..3bfb3e7b7 100644 --- a/include/openthread/joiner.h +++ b/include/openthread/joiner.h @@ -52,6 +52,20 @@ extern "C" { * */ +/** + * This enumeration defines the Joiner State. + * + */ +typedef enum otJoinerState +{ + OT_JOINER_STATE_IDLE = 0, + OT_JOINER_STATE_DISCOVER = 1, + OT_JOINER_STATE_CONNECT = 2, + OT_JOINER_STATE_CONNECTED = 3, + OT_JOINER_STATE_ENTRUST = 4, + OT_JOINER_STATE_JOINED = 5, +} otJoinerState; + /** * This function pointer is called to notify the completion of a join operation. * @@ -94,6 +108,21 @@ OTAPI otError OTCALL otJoinerStart(otInstance *aInstance, const char *aPSKd, con */ OTAPI otError OTCALL otJoinerStop(otInstance *aInstance); +/** + * This function returns the Joiner State. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @retval OT_JOINER_STATE_IDLE + * @retval OT_JOINER_STATE_DISCOVER + * @retval OT_JOINER_STATE_CONNECT + * @retval OT_JOINER_STATE_CONNECTED + * @retval OT_JOINER_STATE_ENTRUST + * @retval OT_JOINER_STATE_JOINED + * + */ +OTAPI otJoinerState OTCALL otJoinerGetState(otInstance *aInstance); + /** * @} * diff --git a/include/openthread/types.h b/include/openthread/types.h index cf2abf669..0984ef5d4 100644 --- a/include/openthread/types.h +++ b/include/openthread/types.h @@ -685,6 +685,8 @@ enum OT_CHANGED_THREAD_CHILD_REMOVED = 1 << 11, ///< Child was removed OT_CHANGED_IP6_MULTICAST_SUBSRCRIBED = 1 << 12, ///< Subscribed to a IPv6 multicast address OT_CHANGED_IP6_MULTICAST_UNSUBSRCRIBED = 1 << 13, ///< Unsubscribed from a IPv6 multicast address + OT_CHANGED_COMMISSIONER_STATE = 1 << 14, ///< Commissioner state changed + OT_CHANGED_JOINER_STATE = 1 << 15, ///< Joiner state changed }; /** diff --git a/src/core/api/commissioner_api.cpp b/src/core/api/commissioner_api.cpp index 28836ee7e..6b996f601 100644 --- a/src/core/api/commissioner_api.cpp +++ b/src/core/api/commissioner_api.cpp @@ -39,84 +39,214 @@ using namespace ot; -#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER - otError otCommissionerStart(otInstance *aInstance) { - return aInstance->mThreadNetif.GetCommissioner().Start(); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().Start(); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; } otError otCommissionerStop(otInstance *aInstance) { - return aInstance->mThreadNetif.GetCommissioner().Stop(); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().Stop(); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; } otError otCommissionerAddJoiner(otInstance *aInstance, const otExtAddress *aExtAddress, const char *aPSKd, uint32_t aTimeout) { - return aInstance->mThreadNetif.GetCommissioner().AddJoiner(static_cast(aExtAddress), aPSKd, - aTimeout); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().AddJoiner(static_cast(aExtAddress), aPSKd, + aTimeout); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aExtAddress); + OT_UNUSED_VARIABLE(aPSKd); + OT_UNUSED_VARIABLE(aTimeout); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; } otError otCommissionerRemoveJoiner(otInstance *aInstance, const otExtAddress *aExtAddress) { - return aInstance->mThreadNetif.GetCommissioner().RemoveJoiner(static_cast(aExtAddress), 0); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().RemoveJoiner(static_cast(aExtAddress), 0); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aExtAddress); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; } otError otCommissionerSetProvisioningUrl(otInstance *aInstance, const char *aProvisioningUrl) { - return aInstance->mThreadNetif.GetCommissioner().SetProvisioningUrl(aProvisioningUrl); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().SetProvisioningUrl(aProvisioningUrl); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aProvisioningUrl); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; } otError otCommissionerAnnounceBegin(otInstance *aInstance, uint32_t aChannelMask, uint8_t aCount, uint16_t aPeriod, const otIp6Address *aAddress) { - return aInstance->mThreadNetif.GetCommissioner().mAnnounceBegin.SendRequest(aChannelMask, aCount, aPeriod, - *static_cast(aAddress)); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().mAnnounceBegin.SendRequest(aChannelMask, aCount, aPeriod, + *static_cast(aAddress)); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aChannelMask); + OT_UNUSED_VARIABLE(aCount); + OT_UNUSED_VARIABLE(aPeriod); + OT_UNUSED_VARIABLE(aAddress); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; } otError otCommissionerEnergyScan(otInstance *aInstance, uint32_t aChannelMask, uint8_t aCount, uint16_t aPeriod, uint16_t aScanDuration, const otIp6Address *aAddress, otCommissionerEnergyReportCallback aCallback, void *aContext) { - return aInstance->mThreadNetif.GetCommissioner().mEnergyScan.SendQuery(aChannelMask, aCount, aPeriod, aScanDuration, - *static_cast(aAddress), - aCallback, aContext); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().mEnergyScan.SendQuery(aChannelMask, aCount, aPeriod, aScanDuration, + *static_cast(aAddress), + aCallback, aContext); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aChannelMask); + OT_UNUSED_VARIABLE(aCount); + OT_UNUSED_VARIABLE(aPeriod); + OT_UNUSED_VARIABLE(aScanDuration); + OT_UNUSED_VARIABLE(aAddress); + OT_UNUSED_VARIABLE(aCallback); + OT_UNUSED_VARIABLE(aContext); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; } otError otCommissionerPanIdQuery(otInstance *aInstance, uint16_t aPanId, uint32_t aChannelMask, const otIp6Address *aAddress, otCommissionerPanIdConflictCallback aCallback, void *aContext) { - return aInstance->mThreadNetif.GetCommissioner().mPanIdQuery.SendQuery( - aPanId, aChannelMask, *static_cast(aAddress), aCallback, aContext); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().mPanIdQuery.SendQuery( + aPanId, aChannelMask, *static_cast(aAddress), aCallback, aContext); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aPanId); + OT_UNUSED_VARIABLE(aChannelMask); + OT_UNUSED_VARIABLE(aAddress); + OT_UNUSED_VARIABLE(aCallback); + OT_UNUSED_VARIABLE(aContext); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; } otError otCommissionerSendMgmtGet(otInstance *aInstance, const uint8_t *aTlvs, uint8_t aLength) { - return aInstance->mThreadNetif.GetCommissioner().SendMgmtCommissionerGetRequest(aTlvs, aLength); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().SendMgmtCommissionerGetRequest(aTlvs, aLength); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aTlvs); + OT_UNUSED_VARIABLE(aLength); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; } otError otCommissionerSendMgmtSet(otInstance *aInstance, const otCommissioningDataset *aDataset, const uint8_t *aTlvs, uint8_t aLength) { - return aInstance->mThreadNetif.GetCommissioner().SendMgmtCommissionerSetRequest(*aDataset, aTlvs, aLength); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().SendMgmtCommissionerSetRequest(*aDataset, aTlvs, aLength); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aDataset); + OT_UNUSED_VARIABLE(aTlvs); + OT_UNUSED_VARIABLE(aLength); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; } uint16_t otCommissionerGetSessionId(otInstance *aInstance) { - return aInstance->mThreadNetif.GetCommissioner().GetSessionId(); + uint16_t sessionId = 0; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + sessionId = aInstance->mThreadNetif.GetCommissioner().GetSessionId(); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return sessionId; } otCommissionerState otCommissionerGetState(otInstance *aInstance) { - return aInstance->mThreadNetif.GetCommissioner().GetState(); + otCommissionerState state = OT_COMMISSIONER_STATE_DISABLED; + +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + state = aInstance->mThreadNetif.GetCommissioner().GetState(); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return state; } otError otCommissionerGeneratePSKc(otInstance *aInstance, const char *aPassPhrase, const char *aNetworkName, const uint8_t *aExtPanId, uint8_t *aPSKc) { - return aInstance->mThreadNetif.GetCommissioner().GeneratePSKc(aPassPhrase, aNetworkName, aExtPanId, aPSKc); -} + otError error = OT_ERROR_DISABLED_FEATURE; -#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD +#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER + error = aInstance->mThreadNetif.GetCommissioner().GeneratePSKc(aPassPhrase, aNetworkName, aExtPanId, aPSKc); +#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aPassPhrase); + OT_UNUSED_VARIABLE(aNetworkName); + OT_UNUSED_VARIABLE(aExtPanId); + OT_UNUSED_VARIABLE(aPSKc); +#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD + + return error; +} diff --git a/src/core/api/joiner_api.cpp b/src/core/api/joiner_api.cpp index 88a27fe20..737a9f4f5 100644 --- a/src/core/api/joiner_api.cpp +++ b/src/core/api/joiner_api.cpp @@ -39,21 +39,54 @@ using namespace ot; -#if OPENTHREAD_ENABLE_JOINER - otError otJoinerStart(otInstance *aInstance, const char *aPSKd, const char *aProvisioningUrl, const char *aVendorName, const char *aVendorModel, const char *aVendorSwVersion, const char *aVendorData, otJoinerCallback aCallback, void *aContext) { - return aInstance->mThreadNetif.GetJoiner().Start(aPSKd, aProvisioningUrl, - aVendorName, aVendorModel, aVendorSwVersion, aVendorData, - aCallback, aContext); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_ENABLE_JOINER + error = aInstance->mThreadNetif.GetJoiner().Start(aPSKd, aProvisioningUrl, + aVendorName, aVendorModel, aVendorSwVersion, aVendorData, + aCallback, aContext); +#else // OPENTHREAD_ENABLE_JOINER + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aPSKd); + OT_UNUSED_VARIABLE(aProvisioningUrl); + OT_UNUSED_VARIABLE(aVendorName); + OT_UNUSED_VARIABLE(aVendorModel); + OT_UNUSED_VARIABLE(aVendorSwVersion); + OT_UNUSED_VARIABLE(aVendorData); + OT_UNUSED_VARIABLE(aCallback); + OT_UNUSED_VARIABLE(aContext); +#endif // OPENTHREAD_ENABLE_JOINER + + return error; } otError otJoinerStop(otInstance *aInstance) { - return aInstance->mThreadNetif.GetJoiner().Stop(); + otError error = OT_ERROR_DISABLED_FEATURE; + +#if OPENTHREAD_ENABLE_JOINER + error = aInstance->mThreadNetif.GetJoiner().Stop(); +#else // OPENTHREAD_ENABLE_JOINER + OT_UNUSED_VARIABLE(aInstance); +#endif // OPENTHREAD_ENABLE_JOINER + + return error; } -#endif // OPENTHREAD_ENABLE_JOINER +otJoinerState otJoinerGetState(otInstance *aInstance) +{ + otJoinerState state = OT_JOINER_STATE_IDLE; + +#if OPENTHREAD_ENABLE_JOINER + state = aInstance->mThreadNetif.GetJoiner().GetState(); +#else // OPENTHREAD_ENABLE_JOINER + OT_UNUSED_VARIABLE(aInstance); +#endif // OPENTHREAD_ENABLE_JOINER + + return state; +} diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 0ff73d4e2..b70058aca 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -128,6 +128,7 @@ otError Commissioner::Stop(void) GetNetif().GetCoapSecure().Stop(); mState = OT_COMMISSIONER_STATE_DISABLED; + GetNetif().SetStateChangedFlags(OT_CHANGED_COMMISSIONER_STATE); RemoveCoapResources(); mTransmitAttempts = 0; @@ -671,6 +672,8 @@ exit: } } + GetNetif().SetStateChangedFlags(OT_CHANGED_COMMISSIONER_STATE); + otLogFuncExit(); } diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index d3eb6ad26..ab46110ad 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -62,7 +62,7 @@ namespace MeshCoP { Joiner::Joiner(ThreadNetif &aNetif): ThreadNetifLocator(aNetif), - mState(kStateIdle), + mState(OT_JOINER_STATE_IDLE), mCallback(NULL), mContext(NULL), mCcitt(0), @@ -94,7 +94,9 @@ otError Joiner::Start(const char *aPSKd, const char *aProvisioningUrl, otLogFuncEntry(); - VerifyOrExit(mState == kStateIdle, error = OT_ERROR_BUSY); + VerifyOrExit(mState == OT_JOINER_STATE_IDLE, error = OT_ERROR_BUSY); + + GetNetif().SetStateChangedFlags(OT_CHANGED_JOINER_STATE); // use extended address based on factory-assigned IEEE EUI-64 netif.GetMac().GetHashMacAddress(&extAddress); @@ -132,7 +134,7 @@ otError Joiner::Start(const char *aPSKd, const char *aProvisioningUrl, mVendorData = aVendorData; mCallback = aCallback; mContext = aContext; - mState = kStateDiscover; + mState = OT_JOINER_STATE_DISCOVER; exit: otLogFuncExitErr(error); @@ -149,6 +151,11 @@ otError Joiner::Stop(void) return OT_ERROR_NONE; } +otJoinerState Joiner::GetState(void) const +{ + return mState; +} + void Joiner::Close(void) { ThreadNetif &netif = GetNetif(); @@ -162,7 +169,8 @@ void Joiner::Close(void) void Joiner::Complete(otError aError) { - mState = kStateIdle; + mState = OT_JOINER_STATE_IDLE; + GetNetif().SetStateChangedFlags(OT_CHANGED_JOINER_STATE); GetNetif().GetCoapSecure().Stop(); @@ -242,7 +250,7 @@ void Joiner::HandleDiscoverResult(otActiveScanResult *aResult) messageInfo.mInterfaceId = OT_NETIF_INTERFACE_ID_THREAD; netif.GetCoapSecure().Connect(messageInfo, Joiner::HandleSecureCoapClientConnect, this); - mState = kStateConnect; + mState = OT_JOINER_STATE_CONNECT; } else { @@ -263,10 +271,10 @@ void Joiner::HandleSecureCoapClientConnect(bool aConnected) { switch (mState) { - case kStateConnect: + case OT_JOINER_STATE_CONNECT: if (aConnected) { - mState = kStateConnected; + mState = OT_JOINER_STATE_CONNECTED; SendJoinerFinalize(); mTimer.Start(kTimeout); } @@ -377,7 +385,7 @@ void Joiner::HandleJoinerFinalizeResponse(Coap::Header *aHeader, Message *aMessa otLogFuncEntry(); - VerifyOrExit(mState == kStateConnected && + VerifyOrExit(mState == OT_JOINER_STATE_CONNECTED && aResult == OT_ERROR_NONE && aHeader->GetType() == OT_COAP_TYPE_ACKNOWLEDGMENT && aHeader->GetCode() == OT_COAP_CODE_CHANGED); @@ -385,7 +393,7 @@ void Joiner::HandleJoinerFinalizeResponse(Coap::Header *aHeader, Message *aMessa SuccessOrExit(Tlv::GetTlv(*aMessage, Tlv::kState, sizeof(state), state)); VerifyOrExit(state.IsValid()); - mState = kStateEntrust; + mState = OT_JOINER_STATE_ENTRUST; mTimer.Start(kTimeout); otLogInfoMeshCoP(GetInstance(), "received joiner finalize response %d", static_cast(state.GetState())); @@ -423,7 +431,7 @@ void Joiner::HandleJoinerEntrust(Coap::Header &aHeader, Message &aMessage, const otLogFuncEntry(); - VerifyOrExit(mState == kStateEntrust && + VerifyOrExit(mState == OT_JOINER_STATE_ENTRUST && aHeader.GetType() == OT_COAP_TYPE_CONFIRMABLE && aHeader.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_DROP); @@ -498,7 +506,7 @@ void Joiner::SendJoinerEntrustResponse(const Coap::Header &aRequestHeader, memset(&responseInfo.mSockAddr, 0, sizeof(responseInfo.mSockAddr)); SuccessOrExit(error = netif.GetCoap().SendMessage(*message, responseInfo)); - mState = kStateJoined; + mState = OT_JOINER_STATE_JOINED; otLogInfoArp(GetInstance(), "Sent Joiner Entrust response"); @@ -527,18 +535,18 @@ void Joiner::HandleTimer(void) switch (mState) { - case kStateIdle: - case kStateDiscover: - case kStateConnect: + case OT_JOINER_STATE_IDLE: + case OT_JOINER_STATE_DISCOVER: + case OT_JOINER_STATE_CONNECT: assert(false); break; - case kStateConnected: - case kStateEntrust: + case OT_JOINER_STATE_CONNECTED: + case OT_JOINER_STATE_ENTRUST: error = OT_ERROR_RESPONSE_TIMEOUT; break; - case kStateJoined: + case OT_JOINER_STATE_JOINED: Mac::ExtAddress extAddress; netif.GetMac().GenerateExtAddress(&extAddress); diff --git a/src/core/meshcop/joiner.hpp b/src/core/meshcop/joiner.hpp index 6a3478504..5bddfcbe2 100644 --- a/src/core/meshcop/joiner.hpp +++ b/src/core/meshcop/joiner.hpp @@ -91,6 +91,21 @@ public: */ otError Stop(void); + /** + * This function returns the Joiner State. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @retval OT_JOINER_STATE_IDLE + * @retval OT_JOINER_STATE_DISCOVER + * @retval OT_JOINER_STATE_CONNECT + * @retval OT_JOINER_STATE_CONNECTED + * @retval OT_JOINER_STATE_ENTRUST + * @retval OT_JOINER_STATE_JOINED + * + */ + otJoinerState GetState(void) const; + private: enum { @@ -123,16 +138,7 @@ private: static Joiner &GetOwner(const Context &aContext); - enum State - { - kStateIdle = 0, - kStateDiscover = 1, - kStateConnect = 2, - kStateConnected = 3, - kStateEntrust = 4, - kStateJoined = 5, - }; - State mState; + otJoinerState mState; otJoinerCallback mCallback; void *mContext;