diff --git a/etc/visual-studio/libopenthread.vcxproj b/etc/visual-studio/libopenthread.vcxproj index ecc07ab63..003dbd4f3 100644 --- a/etc/visual-studio/libopenthread.vcxproj +++ b/etc/visual-studio/libopenthread.vcxproj @@ -59,6 +59,7 @@ + diff --git a/etc/visual-studio/libopenthread.vcxproj.filters b/etc/visual-studio/libopenthread.vcxproj.filters index d9059f6db..27f01fbf5 100644 --- a/etc/visual-studio/libopenthread.vcxproj.filters +++ b/etc/visual-studio/libopenthread.vcxproj.filters @@ -72,6 +72,9 @@ Source Files\api + + Source Files\api + Source Files\api diff --git a/etc/visual-studio/libopenthread_k.vcxproj b/etc/visual-studio/libopenthread_k.vcxproj index e2ecc321e..ad6f8bb73 100644 --- a/etc/visual-studio/libopenthread_k.vcxproj +++ b/etc/visual-studio/libopenthread_k.vcxproj @@ -68,6 +68,7 @@ + diff --git a/etc/visual-studio/libopenthread_k.vcxproj.filters b/etc/visual-studio/libopenthread_k.vcxproj.filters index 39466d959..7fbbc86af 100644 --- a/etc/visual-studio/libopenthread_k.vcxproj.filters +++ b/etc/visual-studio/libopenthread_k.vcxproj.filters @@ -72,6 +72,9 @@ Source Files\api + + Source Files\api + Source Files\api diff --git a/examples/drivers/windows/otApi/otApi.cpp b/examples/drivers/windows/otApi/otApi.cpp index 20e12924e..241189445 100644 --- a/examples/drivers/windows/otApi/otApi.cpp +++ b/examples/drivers/windows/otApi/otApi.cpp @@ -2907,7 +2907,7 @@ otLinkSetAssignLinkQuality( OTAPI void OTCALL -otPlatformReset( +otInstanceReset( _In_ otInstance *aInstance ) { @@ -2917,7 +2917,7 @@ otPlatformReset( OTAPI void OTCALL -otFactoryReset( +otInstanceFactoryReset( _In_ otInstance *aInstance ) { diff --git a/examples/drivers/windows/otLwf/iocontrol.c b/examples/drivers/windows/otLwf/iocontrol.c index e25f83835..93bc2894b 100644 --- a/examples/drivers/windows/otLwf/iocontrol.c +++ b/examples/drivers/windows/otLwf/iocontrol.c @@ -4864,7 +4864,7 @@ otLwfIoCtl_otPlatformReset( UNREFERENCED_PARAMETER(OutBuffer); *OutBufferLength = 0; - otPlatformReset(pFilter->otCtx); + otInstanceReset(pFilter->otCtx); return status; } @@ -4888,7 +4888,7 @@ otLwfIoCtl_otFactoryReset( UNREFERENCED_PARAMETER(OutBuffer); *OutBufferLength = 0; - otFactoryReset(pFilter->otCtx); + otInstanceFactoryReset(pFilter->otCtx); return status; } diff --git a/examples/drivers/windows/otNodeApi/otNodeApi.cpp b/examples/drivers/windows/otNodeApi/otNodeApi.cpp index 00f515827..25287448c 100644 --- a/examples/drivers/windows/otNodeApi/otNodeApi.cpp +++ b/examples/drivers/windows/otNodeApi/otNodeApi.cpp @@ -844,7 +844,7 @@ OTNODEAPI otNode* OTCALL otNodeInit(uint32_t id) InitializeCriticalSection(&node->mCS); // Reset any previously saved settings - otFactoryReset(instance); + otInstanceFactoryReset(instance); otSetStateChangedCallback(instance, otNodeStateChangedCallback, node); diff --git a/include/openthread-types.h b/include/openthread-types.h index 377f96693..c389a4e7c 100644 --- a/include/openthread-types.h +++ b/include/openthread-types.h @@ -1093,15 +1093,6 @@ typedef void (OTCALL *otDeviceAvailabilityChangedCallback)(bool aAdded, const GU #endif // OTDLL -/** - * This function pointer is called to notify certain configuration or state changes within OpenThread. - * - * @param[in] aFlags A bit-field indicating specific state that has changed. - * @param[in] aContext A pointer to application-specific context. - * - */ -typedef void (OTCALL *otStateChangedCallback)(uint32_t aFlags, void *aContext); - /** * This function pointer is called when Network Diagnostic Get response is received. * diff --git a/include/openthread.h b/include/openthread.h index 5eb4a64e2..366f6c29e 100644 --- a/include/openthread.h +++ b/include/openthread.h @@ -39,6 +39,7 @@ #include "openthread/crypto.h" #include "openthread/dataset.h" +#include "openthread/instance.h" #include "openthread/ip6.h" #include "openthread/link.h" #include "openthread/message.h" @@ -126,143 +127,6 @@ extern "C" { */ OTAPI const char *OTCALL otGetVersionString(void); -#ifdef OTDLL - -/** - * This function initializes a new instance of the OpenThread library. - * - * @retval otApiInstance* The new OpenThread context structure. - * - * @sa otApiFinalize - * - */ -OTAPI otApiInstance *OTCALL otApiInit(void); - -/** - * This function uninitializes the OpenThread library. - * - * Call this function when OpenThread is no longer in use. - * - * @param[in] aApiInstance The OpenThread api instance. - * - */ -OTAPI void OTCALL otApiFinalize(otApiInstance *aApiInstance); - -/** - * This function frees any memory returned/allocated by the library. - * - * @param[in] aMem The memory to free. - * - */ -OTAPI void OTCALL otFreeMemory(const void *aMem); - -/** - * This function registers a callback to indicate OpenThread devices come and go. - * - * @param[in] aApiInstance The OpenThread api instance. - * @param[in] aCallback A pointer to a function that is called with the state changes. - * @param[in] aContextContext A pointer to application-specific context. - * - */ -OTAPI void OTCALL otSetDeviceAvailabilityChangedCallback(otApiInstance *aApiInstance, - otDeviceAvailabilityChangedCallback aCallback, void *aCallbackContext); - -/** - * This function querys the list of OpenThread device contexts on the system. - * - * @param[in] aApiInstance The OpenThread api instance. - * - * @sa otFreeMemory - */ -OTAPI otDeviceList *OTCALL otEnumerateDevices(otApiInstance *aApiInstance); - -/** - * This function initializes an OpenThread context for a device. - * - * @param[in] aApiInstance The OpenThread api instance. - * @param[in] aDeviceGuid The device guid to create an OpenThread context for. - * - * @retval otInstance* The new OpenThread device instance structure for the device. - * - * @sa otFreeMemory - * - */ -OTAPI otInstance *OTCALL otInstanceInit(otApiInstance *aApiInstance, const GUID *aDeviceGuid); - -/** - * This queries the Windows device/interface GUID for the otContext. - * - * @param[in] aContext The OpenThread context structure. - * - * @retval GUID The device GUID. - * - */ -OTAPI GUID OTCALL otGetDeviceGuid(otInstance *aInstance); - -/** - * This queries the Windows device/interface IfIndex for the otContext. - * - * @param[in] aContext The OpenThread context structure. - * - * @retval uint32_t The device IfIndex. - * - */ -OTAPI uint32_t OTCALL otGetDeviceIfIndex(otInstance *aInstance); - -/** - * This queries the Windows Compartment ID for the otContext. - * - * @param[in] aContext The OpenThread context structure. - * - * @retval uint32_t The compartment ID. - * - */ -OTAPI uint32_t OTCALL otGetCompartmentId(otInstance *aInstance); - -#else - -#ifdef OPENTHREAD_MULTIPLE_INSTANCE -/** - * This function initializes the OpenThread library. - * - * This function initializes OpenThread and prepares it for subsequent OpenThread API calls. This function must be - * called before any other calls to OpenThread. By default, OpenThread is initialized in the 'enabled' state. - * - * @param[in] aInstanceBuffer The buffer for OpenThread to use for allocating the otInstance structure. - * @param[inout] aInstanceBufferSize On input, the size of aInstanceBuffer. On output, if not enough space for otInstance, - the number of bytes required for otInstance. - * - * @retval otInstance* The new OpenThread instance structure. - * - * @sa otContextFinalize - * - */ -otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize); -#else -/** - * This function initializes the static instance of the OpenThread library. - * - * This function initializes OpenThread and prepares it for subsequent OpenThread API calls. This function must be - * called before any other calls to OpenThread. By default, OpenThread is initialized in the 'enabled' state. - * - * @retval otInstance* The new OpenThread instance structure. - * - */ -otInstance *otInstanceInit(void); -#endif - -/** - * This function disables the OpenThread library. - * - * Call this function when OpenThread is no longer in use. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * - */ -void otInstanceFinalize(otInstance *aInstance); - -#endif - /** * This function starts Thread protocol operation. * @@ -575,31 +439,6 @@ OTAPI bool OTCALL otIsRouterRoleEnabled(otInstance *aInstance); */ OTAPI void OTCALL otSetRouterRoleEnabled(otInstance *aInstance, bool aEnabled); -/** - * This function registers a callback to indicate when certain configuration or state changes within OpenThread. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aCallback A pointer to a function that is called with certain configuration or state changes. - * @param[in] aContext A pointer to application-specific context. - * - * @retval kThreadError_None Added the callback to the list of callbacks. - * @retval kThreadError_NoBufs Could not add the callback due to resource constraints. - * - */ -OTAPI ThreadError OTCALL otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, - void *aContext); - -/** - * This function removes a callback to indicate when certain configuration or state changes within OpenThread. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aCallback A pointer to a function that is called with certain configuration or state changes. - * @param[in] aContext A pointer to application-specific context. - * - */ -OTAPI void OTCALL otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback aCallback, - void *aCallbackContext); - /** * Set the preferred Router Id. * @@ -865,35 +704,6 @@ OTAPI ThreadError OTCALL otBecomeRouter(otInstance *aInstance); */ OTAPI ThreadError OTCALL otBecomeLeader(otInstance *aInstance); -/** - * This method triggers a platform reset. - * - * The reset process ensures that all the OpenThread state/info (stored in volatile memory) is erased. Note that the - * `otPlatformReset` does not erase any persistent state/info saved in non-volatile memory. - * - * @param[in] aInstance A pointer to an OpenThread instance. - */ -OTAPI void OTCALL otPlatformReset(otInstance *aInstance); - -/** - * This method deletes all the settings stored on non-volatile memory, and then triggers platform reset. - * - * @param[in] aInstance A pointer to an OpenThread instance. - */ -OTAPI void OTCALL otFactoryReset(otInstance *aInstance); - -/** - * This function erases all the OpenThread persistent info (network settings) stored on non-volatile memory. - * Erase is successful only if the device is in `disabled` state/role. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * - * @retval kThreadError_None All persistent info/state was erased successfully. - * @retval kThreadError_InvalidState Device is not in `disabled` state/role. - * - */ -ThreadError otPersistentInfoErase(otInstance *aInstance); - /** * Get the ROUTER_DOWNGRADE_THRESHOLD parameter used in the Router role. * diff --git a/include/openthread/Makefile.am b/include/openthread/Makefile.am index 7c908ee6d..cabe9c088 100644 --- a/include/openthread/Makefile.am +++ b/include/openthread/Makefile.am @@ -35,6 +35,7 @@ openthread_headers = \ dataset.h \ dhcp6_client.h \ dhcp6_server.h \ + instance.h \ ip6.h \ jam_detection.h \ joiner.h \ diff --git a/include/openthread/instance.h b/include/openthread/instance.h new file mode 100644 index 000000000..f7a1cac70 --- /dev/null +++ b/include/openthread/instance.h @@ -0,0 +1,273 @@ +/* + * Copyright (c) 2016, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * @brief + * This file defines the OpenThread Instance API. + */ + +#ifndef OPENTHREAD_INSTANCE_H_ +#define OPENTHREAD_INSTANCE_H_ + +#include "openthread-types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @addtogroup instance Instance + * + * @brief + * This module includes functions that control the OpenThread Instance. + * + * @{ + * + */ + +#ifdef OTDLL + +/** + * This function initializes a new instance of the OpenThread library. + * + * @retval otApiInstance* The new OpenThread context structure. + * + * @sa otApiFinalize + * + */ +OTAPI otApiInstance *OTCALL otApiInit(void); + +/** + * This function uninitializes the OpenThread library. + * + * Call this function when OpenThread is no longer in use. + * + * @param[in] aApiInstance The OpenThread api instance. + * + */ +OTAPI void OTCALL otApiFinalize(otApiInstance *aApiInstance); + +/** + * This function frees any memory returned/allocated by the library. + * + * @param[in] aMem The memory to free. + * + */ +OTAPI void OTCALL otFreeMemory(const void *aMem); + +/** + * This function pointer is called to notify addition and removal of OpenThread devices. + * + * @param[in] aAdded A flag indicating if the device was added or removed. + * @param[in] aDeviceGuid A GUID indicating which device state changed. + * @param[in] aContext A pointer to application-specific context. + * + */ +typedef void (OTCALL *otDeviceAvailabilityChangedCallback)(bool aAdded, const GUID *aDeviceGuid, void *aContext); + +/** + * This function registers a callback to indicate OpenThread devices come and go. + * + * @param[in] aApiInstance The OpenThread api instance. + * @param[in] aCallback A pointer to a function that is called with the state changes. + * @param[in] aContextContext A pointer to application-specific context. + * + */ +OTAPI void OTCALL otSetDeviceAvailabilityChangedCallback(otApiInstance *aApiInstance, + otDeviceAvailabilityChangedCallback aCallback, void *aCallbackContext); + +/** + * This function querys the list of OpenThread device contexts on the system. + * + * @param[in] aApiInstance The OpenThread api instance. + * + * @sa otFreeMemory + */ +OTAPI otDeviceList *OTCALL otEnumerateDevices(otApiInstance *aApiInstance); + +/** + * This function initializes an OpenThread context for a device. + * + * @param[in] aApiInstance The OpenThread api instance. + * @param[in] aDeviceGuid The device guid to create an OpenThread context for. + * + * @retval otInstance* The new OpenThread device instance structure for the device. + * + * @sa otFreeMemory + * + */ +OTAPI otInstance *OTCALL otInstanceInit(otApiInstance *aApiInstance, const GUID *aDeviceGuid); + +/** + * This queries the Windows device/interface GUID for the otContext. + * + * @param[in] aContext The OpenThread context structure. + * + * @retval GUID The device GUID. + * + */ +OTAPI GUID OTCALL otGetDeviceGuid(otInstance *aInstance); + +/** + * This queries the Windows device/interface IfIndex for the otContext. + * + * @param[in] aContext The OpenThread context structure. + * + * @retval uint32_t The device IfIndex. + * + */ +OTAPI uint32_t OTCALL otGetDeviceIfIndex(otInstance *aInstance); + +/** + * This queries the Windows Compartment ID for the otContext. + * + * @param[in] aContext The OpenThread context structure. + * + * @retval uint32_t The compartment ID. + * + */ +OTAPI uint32_t OTCALL otGetCompartmentId(otInstance *aInstance); + +#else + +#ifdef OPENTHREAD_MULTIPLE_INSTANCE +/** + * This function initializes the OpenThread library. + * + * This function initializes OpenThread and prepares it for subsequent OpenThread API calls. This function must be + * called before any other calls to OpenThread. By default, OpenThread is initialized in the 'enabled' state. + * + * @param[in] aInstanceBuffer The buffer for OpenThread to use for allocating the otInstance structure. + * @param[inout] aInstanceBufferSize On input, the size of aInstanceBuffer. On output, if not enough space for otInstance, + the number of bytes required for otInstance. + * + * @retval otInstance* The new OpenThread instance structure. + * + * @sa otContextFinalize + * + */ +otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize); +#else +/** + * This function initializes the static instance of the OpenThread library. + * + * This function initializes OpenThread and prepares it for subsequent OpenThread API calls. This function must be + * called before any other calls to OpenThread. By default, OpenThread is initialized in the 'enabled' state. + * + * @retval otInstance* The new OpenThread instance structure. + * + */ +otInstance *otInstanceInit(void); +#endif + +/** + * This function disables the OpenThread library. + * + * Call this function when OpenThread is no longer in use. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + */ +void otInstanceFinalize(otInstance *aInstance); + +#endif + +/** + * This function pointer is called to notify certain configuration or state changes within OpenThread. + * + * @param[in] aFlags A bit-field indicating specific state that has changed. + * @param[in] aContext A pointer to application-specific context. + * + */ +typedef void (OTCALL *otStateChangedCallback)(uint32_t aFlags, void *aContext); + +/** + * This function registers a callback to indicate when certain configuration or state changes within OpenThread. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aCallback A pointer to a function that is called with certain configuration or state changes. + * @param[in] aContext A pointer to application-specific context. + * + * @retval kThreadError_None Added the callback to the list of callbacks. + * @retval kThreadError_NoBufs Could not add the callback due to resource constraints. + * + */ +OTAPI ThreadError OTCALL otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, + void *aContext); + +/** + * This function removes a callback to indicate when certain configuration or state changes within OpenThread. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aCallback A pointer to a function that is called with certain configuration or state changes. + * @param[in] aContext A pointer to application-specific context. + * + */ +OTAPI void OTCALL otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback aCallback, + void *aCallbackContext); + +/** + * This method triggers a platform reset. + * + * The reset process ensures that all the OpenThread state/info (stored in volatile memory) is erased. Note that the + * `otPlatformReset` does not erase any persistent state/info saved in non-volatile memory. + * + * @param[in] aInstance A pointer to an OpenThread instance. + */ +OTAPI void OTCALL otInstanceReset(otInstance *aInstance); + +/** + * This method deletes all the settings stored on non-volatile memory, and then triggers platform reset. + * + * @param[in] aInstance A pointer to an OpenThread instance. + */ +OTAPI void OTCALL otInstanceFactoryReset(otInstance *aInstance); + +/** + * This function erases all the OpenThread persistent info (network settings) stored on non-volatile memory. + * Erase is successful only if the device is in `disabled` state/role. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @retval kThreadError_None All persistent info/state was erased successfully. + * @retval kThreadError_InvalidState Device is not in `disabled` state/role. + * + */ +ThreadError otInstanceErasePersistentInfo(otInstance *aInstance); + +/** + * @} + * + */ + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // OPENTHREAD_INSTANCE_H_ diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 9115885dd..a070290a8 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -812,7 +812,7 @@ exit: void Interpreter::ProcessFactoryReset(int argc, char *argv[]) { - otFactoryReset(mInstance); + otInstanceFactoryReset(mInstance); (void)argc; (void)argv; } @@ -1821,7 +1821,7 @@ exit: void Interpreter::ProcessReset(int argc, char *argv[]) { - otPlatformReset(mInstance); + otInstanceReset(mInstance); (void)argc; (void)argv; } diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 75bfa4a97..0648b6154 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -42,6 +42,7 @@ SOURCES_COMMON = \ openthread.cpp \ api/crypto_api.cpp \ api/dataset_api.cpp \ + api/instance_api.cpp \ api/ip6_api.cpp \ api/link_api.cpp \ api/message_api.cpp \ diff --git a/src/core/api/instance_api.cpp b/src/core/api/instance_api.cpp new file mode 100644 index 000000000..7e446ba68 --- /dev/null +++ b/src/core/api/instance_api.cpp @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2016, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file implements the OpenThread Instance API. + */ + +#define WPP_NAME "instance_api.tmh" + +#include "openthread/instance.h" +#include "platform/misc.h" +#include "platform/settings.h" + +#include "openthread-instance.h" + +#include "common/logging.hpp" +#include "common/new.hpp" + +#ifndef OPENTHREAD_MULTIPLE_INSTANCE +static otDEFINE_ALIGNED_VAR(sInstanceRaw, sizeof(otInstance), uint64_t); +otInstance *sInstance = NULL; +#endif + +otInstance::otInstance(void) : + mReceiveIp6DatagramCallback(NULL), + mReceiveIp6DatagramCallbackContext(NULL), + mActiveScanCallback(NULL), + mActiveScanCallbackContext(NULL), + mEnergyScanCallback(NULL), + mEnergyScanCallbackContext(NULL), + mThreadNetif(mIp6) +#if OPENTHREAD_ENABLE_RAW_LINK_API + , mLinkRaw(*this) +#endif // OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_ENABLE_APPLICATION_COAP + , mApplicationCoapServer(mIp6.mUdp, OT_DEFAULT_COAP_PORT) +#endif // OPENTHREAD_ENABLE_APPLICATION_COAP +{ +} + +using namespace Thread; + +#ifdef __cplusplus +extern "C" { +#endif + +void otInstancePostConstructor(otInstance *aInstance) +{ + // restore datasets and network information + otPlatSettingsInit(aInstance); + aInstance->mThreadNetif.GetMle().Restore(); + +#if OPENTHREAD_CONFIG_ENABLE_AUTO_START_SUPPORT + + // If auto start is configured, do that now + if (otThreadGetAutoStart(aInstance)) + { + if (otIp6SetEnabled(aInstance, true) == kThreadError_None) + { + // Only try to start Thread if we could bring up the interface + if (otThreadStart(aInstance) != kThreadError_None) + { + // Bring the interface down if Thread failed to start + otIp6SetEnabled(aInstance, false); + } + } + } + +#endif +} + +#ifdef OPENTHREAD_MULTIPLE_INSTANCE + +otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize) +{ + otInstance *aInstance = NULL; + + otLogFuncEntry(); + otLogInfoApi("otInstanceInit"); + + VerifyOrExit(aInstanceBufferSize != NULL, ;); + + // Make sure the input buffer is big enough + VerifyOrExit(sizeof(otInstance) <= *aInstanceBufferSize, *aInstanceBufferSize = sizeof(otInstance)); + + VerifyOrExit(aInstanceBuffer != NULL, ;); + + // Construct the context + aInstance = new(aInstanceBuffer)otInstance(); + + // Execute post constructor operations + otInstancePostConstructor(aInstance); + +exit: + + otLogFuncExit(); + return aInstance; +} + +#else + +otInstance *otInstanceInit() +{ + otLogFuncEntry(); + + otLogInfoApi("otInstanceInit"); + + VerifyOrExit(sInstance == NULL, ;); + + // Construct the context + sInstance = new(&sInstanceRaw)otInstance(); + + // Execute post constructor operations + otInstancePostConstructor(sInstance); + +exit: + + otLogFuncExit(); + return sInstance; +} + +#endif + +void otInstanceFinalize(otInstance *aInstance) +{ + otLogFuncEntry(); + + // Ensure we are disabled + (void)otThreadStop(aInstance); + (void)otIp6SetEnabled(aInstance, false); + +#ifndef OPENTHREAD_MULTIPLE_INSTANCE + sInstance = NULL; +#endif + otLogFuncExit(); +} + +ThreadError otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aCallbackContext) +{ + ThreadError error = kThreadError_NoBufs; + + for (size_t i = 0; i < OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS; i++) + { + if (aInstance->mNetifCallback[i].IsFree()) + { + aInstance->mNetifCallback[i].Set(aCallback, aCallbackContext); + error = aInstance->mThreadNetif.RegisterCallback(aInstance->mNetifCallback[i]); + break; + } + } + + return error; +} + +void otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aCallbackContext) +{ + for (size_t i = 0; i < OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS; i++) + { + if (aInstance->mNetifCallback[i].IsServing(aCallback, aCallbackContext)) + { + aInstance->mThreadNetif.RemoveCallback(aInstance->mNetifCallback[i]); + aInstance->mNetifCallback[i].Free(); + break; + } + } +} + +void otInstanceReset(otInstance *aInstance) +{ + otPlatReset(aInstance); +} + +void otInstanceFactoryReset(otInstance *aInstance) +{ + otPlatSettingsWipe(aInstance); + otPlatReset(aInstance); +} + +ThreadError otInstanceErasePersistentInfo(otInstance *aInstance) +{ + ThreadError error = kThreadError_None; + + VerifyOrExit(otGetDeviceRole(aInstance) == kDeviceRoleDisabled, error = kThreadError_InvalidState); + otPlatSettingsWipe(aInstance); + +exit: + return error; +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index a35b0407c..171bf0fcf 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -62,28 +62,6 @@ using namespace Thread; -#ifndef OPENTHREAD_MULTIPLE_INSTANCE -static otDEFINE_ALIGNED_VAR(sInstanceRaw, sizeof(otInstance), uint64_t); -otInstance *sInstance = NULL; -#endif - -otInstance::otInstance(void) : - mReceiveIp6DatagramCallback(NULL), - mReceiveIp6DatagramCallbackContext(NULL), - mActiveScanCallback(NULL), - mActiveScanCallbackContext(NULL), - mEnergyScanCallback(NULL), - mEnergyScanCallbackContext(NULL), - mThreadNetif(mIp6) -#if OPENTHREAD_ENABLE_RAW_LINK_API - , mLinkRaw(*this) -#endif // OPENTHREAD_ENABLE_RAW_LINK_API -#if OPENTHREAD_ENABLE_APPLICATION_COAP - , mApplicationCoapServer(mIp6.mUdp, OT_DEFAULT_COAP_PORT) -#endif // OPENTHREAD_ENABLE_APPLICATION_COAP -{ -} - #ifdef __cplusplus extern "C" { #endif @@ -367,28 +345,6 @@ ThreadError otBecomeLeader(otInstance *aInstance) return aInstance->mThreadNetif.GetMle().BecomeLeader(); } -void otPlatformReset(otInstance *aInstance) -{ - otPlatReset(aInstance); -} - -void otFactoryReset(otInstance *aInstance) -{ - otPlatSettingsWipe(aInstance); - otPlatReset(aInstance); -} - -ThreadError otPersistentInfoErase(otInstance *aInstance) -{ - ThreadError error = kThreadError_None; - - VerifyOrExit(otGetDeviceRole(aInstance) == kDeviceRoleDisabled, error = kThreadError_InvalidState); - otPlatSettingsWipe(aInstance); - -exit: - return error; -} - uint8_t otGetRouterDowngradeThreshold(otInstance *aInstance) { return aInstance->mThreadNetif.GetMle().GetRouterDowngradeThreshold(); @@ -575,36 +531,6 @@ exit: return error; } -ThreadError otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aCallbackContext) -{ - ThreadError error = kThreadError_NoBufs; - - for (size_t i = 0; i < OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS; i++) - { - if (aInstance->mNetifCallback[i].IsFree()) - { - aInstance->mNetifCallback[i].Set(aCallback, aCallbackContext); - error = aInstance->mThreadNetif.RegisterCallback(aInstance->mNetifCallback[i]); - break; - } - } - - return error; -} - -void otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aCallbackContext) -{ - for (size_t i = 0; i < OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS; i++) - { - if (aInstance->mNetifCallback[i].IsServing(aCallback, aCallbackContext)) - { - aInstance->mThreadNetif.RemoveCallback(aInstance->mNetifCallback[i]); - aInstance->mNetifCallback[i].Free(); - break; - } - } -} - const char *otGetVersionString(void) { /** @@ -649,84 +575,6 @@ ThreadError otSetPreferredRouterId(otInstance *aInstance, uint8_t aRouterId) return aInstance->mThreadNetif.GetMle().SetPreferredRouterId(aRouterId); } -void otInstancePostConstructor(otInstance *aInstance) -{ - // restore datasets and network information - otPlatSettingsInit(aInstance); - aInstance->mThreadNetif.GetMle().Restore(); - -#if OPENTHREAD_CONFIG_ENABLE_AUTO_START_SUPPORT - - // If auto start is configured, do that now - if (otThreadGetAutoStart(aInstance)) - { - if (otIp6SetEnabled(aInstance, true) == kThreadError_None) - { - // Only try to start Thread if we could bring up the interface - if (otThreadStart(aInstance) != kThreadError_None) - { - // Bring the interface down if Thread failed to start - otIp6SetEnabled(aInstance, false); - } - } - } - -#endif -} - -#ifdef OPENTHREAD_MULTIPLE_INSTANCE - -otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize) -{ - otInstance *aInstance = NULL; - - otLogFuncEntry(); - otLogInfoApi("otInstanceInit"); - - VerifyOrExit(aInstanceBufferSize != NULL, ;); - - // Make sure the input buffer is big enough - VerifyOrExit(sizeof(otInstance) <= *aInstanceBufferSize, *aInstanceBufferSize = sizeof(otInstance)); - - VerifyOrExit(aInstanceBuffer != NULL, ;); - - // Construct the context - aInstance = new(aInstanceBuffer)otInstance(); - - // Execute post constructor operations - otInstancePostConstructor(aInstance); - -exit: - - otLogFuncExit(); - return aInstance; -} - -#else - -otInstance *otInstanceInit() -{ - otLogFuncEntry(); - - otLogInfoApi("otInstanceInit"); - - VerifyOrExit(sInstance == NULL, ;); - - // Construct the context - sInstance = new(&sInstanceRaw)otInstance(); - - // Execute post constructor operations - otInstancePostConstructor(sInstance); - -exit: - - otLogFuncExit(); - return sInstance; -} - -#endif - - void otSetReceiveDiagnosticGetCallback(otInstance *aInstance, otReceiveDiagnosticGetCallback aCallback, void *aCallbackContext) { @@ -751,20 +599,6 @@ ThreadError otSendDiagnosticReset(otInstance *aInstance, const otIp6Address *aDe aCount); } -void otInstanceFinalize(otInstance *aInstance) -{ - otLogFuncEntry(); - - // Ensure we are disabled - (void)otThreadStop(aInstance); - (void)otIp6SetEnabled(aInstance, false); - -#ifndef OPENTHREAD_MULTIPLE_INSTANCE - sInstance = NULL; -#endif - otLogFuncExit(); -} - ThreadError otThreadStart(otInstance *aInstance) { ThreadError error = kThreadError_None; diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index a5da509d0..95b809270 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -1518,7 +1518,7 @@ ThreadError NcpBase::CommandHandler_RESET(uint8_t header, unsigned int command, // Signal a platform reset. If implemented, this function // shouldn't return. - otPlatformReset(mInstance); + otInstanceReset(mInstance); // We only get to this point if the // platform doesn't support resetting. @@ -1656,7 +1656,7 @@ ThreadError NcpBase::CommandHandler_NET_CLEAR(uint8_t header, unsigned int comma (void)arg_ptr; (void)arg_len; - return SendLastStatus(header, ThreadErrorToSpinelStatus(otPersistentInfoErase(mInstance))); + return SendLastStatus(header, ThreadErrorToSpinelStatus(otInstanceErasePersistentInfo(mInstance))); } ThreadError NcpBase::CommandHandler_NET_RECALL(uint8_t header, unsigned int command, const uint8_t *arg_ptr,