diff --git a/etc/visual-studio/libopenthread-cli-windows.vcxproj b/etc/visual-studio/libopenthread-cli-windows.vcxproj
index 76859c9d7..047e6f8b6 100644
--- a/etc/visual-studio/libopenthread-cli-windows.vcxproj
+++ b/etc/visual-studio/libopenthread-cli-windows.vcxproj
@@ -49,6 +49,8 @@
..\..\include;
..\..\src;
..\..\src\core;
+ ..\..\third_party\mbedtls;
+ ..\..\third_party\mbedtls\repo.patched\include;
Level3
true
diff --git a/etc/visual-studio/libopenthread.vcxproj b/etc/visual-studio/libopenthread.vcxproj
index 2f92ae1b1..eb0f75ee2 100644
--- a/etc/visual-studio/libopenthread.vcxproj
+++ b/etc/visual-studio/libopenthread.vcxproj
@@ -77,6 +77,7 @@
+
@@ -155,6 +156,7 @@
+
diff --git a/etc/visual-studio/libopenthread.vcxproj.filters b/etc/visual-studio/libopenthread.vcxproj.filters
index 2f64f57f1..1b6526cd5 100644
--- a/etc/visual-studio/libopenthread.vcxproj.filters
+++ b/etc/visual-studio/libopenthread.vcxproj.filters
@@ -129,6 +129,9 @@
Source Files\coap
+
+ Source Files\common
+
Source Files\common
@@ -353,6 +356,9 @@
Header Files\common
+
+ Header Files\common
+
Header Files\common
diff --git a/etc/visual-studio/libopenthread_k.vcxproj b/etc/visual-studio/libopenthread_k.vcxproj
index 66af508ae..f687a92cf 100644
--- a/etc/visual-studio/libopenthread_k.vcxproj
+++ b/etc/visual-studio/libopenthread_k.vcxproj
@@ -86,6 +86,7 @@
+
@@ -187,6 +188,7 @@
+
@@ -232,7 +234,6 @@
-
diff --git a/etc/visual-studio/libopenthread_k.vcxproj.filters b/etc/visual-studio/libopenthread_k.vcxproj.filters
index 536e40f2b..816a4bfbe 100644
--- a/etc/visual-studio/libopenthread_k.vcxproj.filters
+++ b/etc/visual-studio/libopenthread_k.vcxproj.filters
@@ -129,6 +129,9 @@
Source Files\coap
+
+ Source Files\common
+
Source Files\common
@@ -353,6 +356,9 @@
Header Files\common
+
+ Header Files\common
+
Header Files\common
@@ -569,9 +575,6 @@
Header Files
-
- Header Files
-
Header Files\openthread
diff --git a/examples/platforms/emsk/platform-emsk.h b/examples/platforms/emsk/platform-emsk.h
index 038f72e48..c0f2d1f33 100755
--- a/examples/platforms/emsk/platform-emsk.h
+++ b/examples/platforms/emsk/platform-emsk.h
@@ -43,9 +43,6 @@
#include "board/board.h"
-// Global OpenThread instance structure
-extern otInstance *sInstance;
-
/**
* This function initializes the alarm service used by OpenThread.
*
diff --git a/examples/platforms/emsk/platform.c b/examples/platforms/emsk/platform.c
index ac79e400d..acc73fc2a 100755
--- a/examples/platforms/emsk/platform.c
+++ b/examples/platforms/emsk/platform.c
@@ -55,8 +55,6 @@ void PlatformInit(int argc, char *argv[])
void PlatformProcessDrivers(otInstance *aInstance)
{
- // sInstance = aInstance;
-
emskUartProcess();
emskRadioProcess(aInstance);
emskAlarmProcess(aInstance);
diff --git a/examples/platforms/emsk/random.c b/examples/platforms/emsk/random.c
index e609166e3..c7ffdd788 100755
--- a/examples/platforms/emsk/random.c
+++ b/examples/platforms/emsk/random.c
@@ -68,15 +68,24 @@ otError otPlatRandomGetTrue(uint8_t *aOutput, uint16_t aOutputLength)
{
otError error = OT_ERROR_NONE;
uint8_t channel = 0;
+ otInstance *aInstance = NULL;
otEXPECT_ACTION(aOutput && aOutputLength, error = OT_ERROR_INVALID_ARGS);
+ // TODO: `otPlatRandomGet()` and `otPlatRandomGetTrue()` should include a pointer to the
+ // owning OpenThread Instance as an input argument (similar to other platform APIs).
+ //
+ // The platform implementation requires to know the OpenThread instance to be able to
+ // use other radio platform APIs. However, the emsk platform implementation of radio API
+ // does not actually use the passed-in instance argument. Till the above TODO is done, as
+ // a workaround, a NULL `aInstance` is used instead.
+
/* disable radio*/
- if (otPlatRadioIsEnabled(sInstance))
+ if (otPlatRadioIsEnabled(aInstance))
{
channel = (mrf24j40_read_long_ctrl_reg(MRF24J40_RFCON0) >> 4) + 11;
- otPlatRadioSleep(sInstance);
- otPlatRadioDisable(sInstance);
+ otPlatRadioSleep(aInstance);
+ otPlatRadioDisable(aInstance);
}
/*
@@ -97,8 +106,8 @@ otError otPlatRandomGetTrue(uint8_t *aOutput, uint16_t aOutputLength)
if (channel)
{
emskRadioInit();
- otPlatRadioEnable(sInstance);
- otPlatRadioReceive(sInstance, channel);
+ otPlatRadioEnable(aInstance);
+ otPlatRadioReceive(aInstance, channel);
}
exit:
diff --git a/include/openthread/instance.h b/include/openthread/instance.h
index bcb830d7a..ac9de8feb 100644
--- a/include/openthread/instance.h
+++ b/include/openthread/instance.h
@@ -120,7 +120,7 @@ OTAPI otDeviceList *OTCALL otEnumerateDevices(otApiInstance *aApiInstance);
* @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.
+ * @returns The new OpenThread device instance structure for the device.
*
* @sa otFreeMemory
*
@@ -132,7 +132,7 @@ OTAPI otInstance *OTCALL otInstanceInit(otApiInstance *aApiInstance, const GUID
*
* @param[in] aContext The OpenThread context structure.
*
- * @retval GUID The device GUID.
+ * @returns The device GUID.
*
*/
OTAPI GUID OTCALL otGetDeviceGuid(otInstance *aInstance);
@@ -142,7 +142,7 @@ OTAPI GUID OTCALL otGetDeviceGuid(otInstance *aInstance);
*
* @param[in] aContext The OpenThread context structure.
*
- * @retval uint32_t The device IfIndex.
+ * @returns The device IfIndex.
*
*/
OTAPI uint32_t OTCALL otGetDeviceIfIndex(otInstance *aInstance);
@@ -152,7 +152,7 @@ OTAPI uint32_t OTCALL otGetDeviceIfIndex(otInstance *aInstance);
*
* @param[in] aContext The OpenThread context structure.
*
- * @retval uint32_t The compartment ID.
+ * @returns The compartment ID.
*
*/
OTAPI uint32_t OTCALL otGetCompartmentId(otInstance *aInstance);
@@ -162,9 +162,8 @@ OTAPI uint32_t OTCALL otGetCompartmentId(otInstance *aInstance);
/**
* 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.
+ * called before any other calls to OpenThread.
*
* This function is available and can only be used when support for multiple OpenThread instances is enabled.
*
@@ -172,9 +171,9 @@ OTAPI uint32_t OTCALL otGetCompartmentId(otInstance *aInstance);
* @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.
+ * @returns A pointer to the new OpenThread instance.
*
- * @sa otContextFinalize
+ * @sa otInstanceFinalize
*
*/
otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize);
@@ -183,11 +182,11 @@ otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize);
* This function initializes the static single 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.
+ * called before any other calls to OpenThread.
*
* This function is available and can only be used when support for multiple OpenThread instances is disabled.
*
- * @retval A pointer to the single OpenThread instance structure.
+ * @returns A pointer to the single OpenThread instance.
*
*/
otInstance *otInstanceInitSingle(void);
@@ -195,11 +194,9 @@ otInstance *otInstanceInitSingle(void);
/**
* This function indicates whether or not the instance is valid/initialized.
*
- * For single-instance case, the instance is considered valid if it is acquired and initialized using
- * `otInstanceInitSingle()`. A subsequent call to `otInstanceFinalize()` causes the instance to be considered as
- * invalid (not initialized).
- *
- * For multi-instance case, any non-NULL instance pointer is considered as valid/initialized.
+ * The instance is considered valid if it is acquired and initialized using either `otInstanceInitSingle()` (in single
+ * instance case) or `otInstanceInit()` (in multi instance case). A subsequent call to `otInstanceFinalize()` causes
+ * the instance to be considered as uninitialized.
*
* @param[in] aInstance A pointer to an OpenThread instance.
*
diff --git a/src/cli/Makefile.am b/src/cli/Makefile.am
index 80b36eb69..ee2136bb5 100644
--- a/src/cli/Makefile.am
+++ b/src/cli/Makefile.am
@@ -31,14 +31,14 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am
#----------------------------------------
#
# This library on the face of it, appears to be identical
-# for both the MTD and FTD varients, however ...
+# for both the MTD and FTD variants, however ...
#
# The source code here includes numerous OpenThread internal headers.
# Due to the "domino-effect" other internal headers are included.
#
# For example:
-# cli.cpp includes:
-# src/core/openthread-instance.h
+# cli.cpp includes:
+# src/core/common/instance.hpp
# Which Includes:
# src/core/therad/thread_netif.hpp
# Which Includes:
@@ -51,8 +51,8 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am
# The FTD flavor has many private components (class variables).
# The MTD flavor has no private components.
#
-# Bottom line: The Class(structs) are thus different in the downstream
-# libs. At this level (in the CLI, and likewise in the NCP) they are
+# Bottom line: The Class(structs) are thus different in the downstream
+# libs. At this level (in the CLI, and likewise in the NCP) they are
# functionally identical.
#
# ORIGINALLY (historical note about how things are/where built):
diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp
index 69f5f8af0..2e6027551 100644
--- a/src/cli/cli.cpp
+++ b/src/cli/cli.cpp
@@ -66,7 +66,6 @@
#include
#include
-#include "openthread-instance.h"
#include "common/new.hpp"
#include "net/ip6.hpp"
#endif
@@ -252,7 +251,7 @@ typedef otPtr otNetifAddressPtr;
typedef otPtr otBufferPtr;
typedef otPtr otStringPtr;
-Interpreter::Interpreter(otInstance *aInstance):
+Interpreter::Interpreter(Instance *aInstance):
#if OPENTHREAD_ENABLE_APPLICATION_COAP
mCoap(*this),
#endif
diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp
index af38238be..ad300de80 100644
--- a/src/cli/cli.hpp
+++ b/src/cli/cli.hpp
@@ -53,6 +53,7 @@
#include "common/code_utils.hpp"
#include "common/context.hpp"
+#include "common/instance.hpp"
#ifndef OTDLL
#include
@@ -104,7 +105,7 @@ public:
*
* @param[in] aInstance The OpenThread instance structure.
*/
- Interpreter(otInstance *aInstance);
+ Interpreter(Instance *aInstance);
/**
* This method interprets a CLI command.
@@ -396,7 +397,7 @@ private:
struct otCliContext
{
Interpreter *mInterpreter;
- otInstance *mInstance;
+ Instance *mInstance;
};
otCliContext mInstances[MAX_CLI_OT_INSTANCES];
uint8_t mInstancesLength;
@@ -426,7 +427,7 @@ private:
#endif
- otInstance *mInstance;
+ Instance *mInstance;
};
} // namespace Cli
diff --git a/src/cli/cli_console.cpp b/src/cli/cli_console.cpp
index c2f208b74..126f9f769 100644
--- a/src/cli/cli_console.cpp
+++ b/src/cli/cli_console.cpp
@@ -39,6 +39,7 @@
#include "utils/wrap_string.h"
#include "cli/cli.hpp"
+#include "common/instance.hpp"
#include "common/new.hpp"
namespace ot {
@@ -50,7 +51,9 @@ static otDEFINE_ALIGNED_VAR(sCliConsoleRaw, sizeof(Console), uint64_t);
extern "C" void otCliConsoleInit(otInstance *aInstance, otCliConsoleOutputCallback aCallback, void *aContext)
{
- sServer = new(&sCliConsoleRaw) Console(aInstance);
+ Instance *instance = static_cast(aInstance);
+
+ sServer = new(&sCliConsoleRaw) Console(instance);
sServer->SetOutputCallback(aCallback);
sServer->SetContext(aContext);
}
@@ -60,7 +63,7 @@ extern "C" void otCliConsoleInputLine(char *aBuf, uint16_t aBufLength)
sServer->ReceiveTask(aBuf, aBufLength);
}
-Console::Console(otInstance *aInstance):
+Console::Console(Instance *aInstance):
mCallback(NULL),
mContext(NULL),
mInterpreter(aInstance)
diff --git a/src/cli/cli_console.hpp b/src/cli/cli_console.hpp
index 5b81e6e77..51808ed55 100644
--- a/src/cli/cli_console.hpp
+++ b/src/cli/cli_console.hpp
@@ -57,7 +57,7 @@ public:
* @param[in] aInstance The OpenThread instance structure.
*
*/
- Console(otInstance *aInstance);
+ Console(Instance *aInstance);
/**
* This method delivers raw characters to the client.
diff --git a/src/cli/cli_instance.cpp b/src/cli/cli_instance.cpp
index 81162389f..23b037332 100644
--- a/src/cli/cli_instance.cpp
+++ b/src/cli/cli_instance.cpp
@@ -61,7 +61,7 @@ void Interpreter::CacheInstances()
for (uint8_t i = 0; i < mInstancesLength; i++)
{
mInstances[i].mInterpreter = this;
- mInstances[i].mInstance = otInstanceInit(mApiInstance, &aDeviceList->aDevices[i]);
+ mInstances[i].mInstance = static_cast(otInstanceInit(mApiInstance, &aDeviceList->aDevices[i]));
assert(mInstances[i].mInstance);
otSetStateChangedCallback(mInstances[i].mInstance, &Interpreter::s_HandleNetifStateChanged, &mInstances[i]);
}
diff --git a/src/cli/cli_uart.cpp b/src/cli/cli_uart.cpp
index a5af59515..df51752dc 100644
--- a/src/cli/cli_uart.cpp
+++ b/src/cli/cli_uart.cpp
@@ -64,7 +64,9 @@ static otDEFINE_ALIGNED_VAR(sCliUartRaw, sizeof(Uart), uint64_t);
extern "C" void otCliUartInit(otInstance *aInstance)
{
- Uart::sUartServer = new(&sCliUartRaw) Uart(aInstance);
+ Instance *instance = static_cast(aInstance);
+
+ Uart::sUartServer = new(&sCliUartRaw) Uart(instance);
}
extern "C" void otCliUartSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength)
@@ -90,7 +92,7 @@ extern "C" void otCliUartAppendResult(otError aError)
Uart::sUartServer->GetInterpreter().AppendResult(aError);
}
-Uart::Uart(otInstance *aInstance):
+Uart::Uart(Instance *aInstance):
mInterpreter(aInstance)
{
mRxLength = 0;
@@ -193,13 +195,13 @@ otError Uart::ProcessCommand(void)
*
* Yes, while rare it is a race condition that is hard to debug.
*
- * Thus this is here to afirmatively LOG exactly when the CLI
+ * Thus this is here to affirmatively LOG exactly when the CLI
* command is being executed.
*/
#if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
/* TODO: how exactly do we get the instance here? */
#else
- otLogInfoCli(otGetInstance(), "execute command: %s", mRxBuffer);
+ otLogInfoCli(&Instance::Get(), "execute command: %s", mRxBuffer);
#endif
#endif
mInterpreter.ProcessLine(mRxBuffer, mRxLength, *this);
diff --git a/src/cli/cli_uart.hpp b/src/cli/cli_uart.hpp
index 3ba1988cd..c7e06c919 100644
--- a/src/cli/cli_uart.hpp
+++ b/src/cli/cli_uart.hpp
@@ -40,8 +40,10 @@
#include "cli/cli.hpp"
#include "cli/cli_server.hpp"
+#include "common/instance.hpp"
#include "common/tasklet.hpp"
+
namespace ot {
namespace Cli {
@@ -58,7 +60,7 @@ public:
* @param[in] aInstance The OpenThread instance structure.
*
*/
- Uart(otInstance *aInstance);
+ Uart(Instance *aInstance);
/**
* This method delivers raw characters to the client.
diff --git a/src/core/Makefile.am b/src/core/Makefile.am
index de60614e1..661896814 100644
--- a/src/core/Makefile.am
+++ b/src/core/Makefile.am
@@ -116,6 +116,7 @@ SOURCES_COMMON = \
coap/coap_header.cpp \
coap/coap_secure.cpp \
common/crc16.cpp \
+ common/instance.cpp \
common/logging.cpp \
common/locator.cpp \
common/message.cpp \
@@ -200,8 +201,6 @@ libopenthread_ftd_a_SOURCES = \
HEADERS_COMMON = \
openthread-core-config.h \
openthread-core-default-config.h \
- openthread-instance.h \
- openthread-single-instance.h \
api/link_raw.hpp \
coap/coap.hpp \
coap/coap_header.hpp \
@@ -211,6 +210,7 @@ HEADERS_COMMON = \
common/crc16.hpp \
common/debug.hpp \
common/encoding.hpp \
+ common/instance.hpp \
common/locator.hpp \
common/logging.hpp \
common/message.hpp \
diff --git a/src/core/api/border_router_api.cpp b/src/core/api/border_router_api.cpp
index 5a3022734..b1c7da3ea 100644
--- a/src/core/api/border_router_api.cpp
+++ b/src/core/api/border_router_api.cpp
@@ -37,17 +37,18 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
otError otBorderRouterGetNetData(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aData != NULL && aDataLength != NULL, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetNetworkDataLocal().GetNetworkData(aStable, aData, *aDataLength);
+ error = instance.GetThreadNetif().GetNetworkDataLocal().GetNetworkData(aStable, aData, *aDataLength);
exit:
return error;
@@ -56,6 +57,7 @@ exit:
otError otBorderRouterAddOnMeshPrefix(otInstance *aInstance, const otBorderRouterConfig *aConfig)
{
uint8_t flags = 0;
+ Instance &instance = *static_cast(aInstance);
if (aConfig->mPreferred)
{
@@ -87,24 +89,28 @@ otError otBorderRouterAddOnMeshPrefix(otInstance *aInstance, const otBorderRoute
flags |= NetworkData::BorderRouterEntry::kOnMeshFlag;
}
- return aInstance->mThreadNetif.GetNetworkDataLocal().AddOnMeshPrefix(aConfig->mPrefix.mPrefix.mFields.m8,
- aConfig->mPrefix.mLength,
- aConfig->mPreference, flags, aConfig->mStable);
+ return instance.GetThreadNetif().GetNetworkDataLocal().AddOnMeshPrefix(aConfig->mPrefix.mPrefix.mFields.m8,
+ aConfig->mPrefix.mLength,
+ aConfig->mPreference, flags, aConfig->mStable);
}
otError otBorderRouterRemoveOnMeshPrefix(otInstance *aInstance, const otIp6Prefix *aPrefix)
{
- return aInstance->mThreadNetif.GetNetworkDataLocal().RemoveOnMeshPrefix(aPrefix->mPrefix.mFields.m8, aPrefix->mLength);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetNetworkDataLocal().RemoveOnMeshPrefix(aPrefix->mPrefix.mFields.m8,
+ aPrefix->mLength);
}
otError otBorderRouterGetNextOnMeshPrefix(otInstance *aInstance, otNetworkDataIterator *aIterator,
otBorderRouterConfig *aConfig)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aIterator && aConfig, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetNetworkDataLocal().GetNextOnMeshPrefix(aIterator, aConfig);
+ error = instance.GetThreadNetif().GetNetworkDataLocal().GetNextOnMeshPrefix(aIterator, aConfig);
exit:
return error;
@@ -112,25 +118,30 @@ exit:
otError otBorderRouterAddRoute(otInstance *aInstance, const otExternalRouteConfig *aConfig)
{
- return aInstance->mThreadNetif.GetNetworkDataLocal().AddHasRoutePrefix(aConfig->mPrefix.mPrefix.mFields.m8,
- aConfig->mPrefix.mLength,
- aConfig->mPreference, aConfig->mStable);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetNetworkDataLocal().AddHasRoutePrefix(aConfig->mPrefix.mPrefix.mFields.m8,
+ aConfig->mPrefix.mLength,
+ aConfig->mPreference, aConfig->mStable);
}
otError otBorderRouterRemoveRoute(otInstance *aInstance, const otIp6Prefix *aPrefix)
{
- return aInstance->mThreadNetif.GetNetworkDataLocal().RemoveHasRoutePrefix(aPrefix->mPrefix.mFields.m8,
- aPrefix->mLength);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetNetworkDataLocal().RemoveHasRoutePrefix(aPrefix->mPrefix.mFields.m8,
+ aPrefix->mLength);
}
otError otBorderRouterGetNextRoute(otInstance *aInstance, otNetworkDataIterator *aIterator,
otExternalRouteConfig *aConfig)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aIterator && aConfig, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetNetworkDataLocal().GetNextExternalRoute(aIterator, aConfig);
+ error = instance.GetThreadNetif().GetNetworkDataLocal().GetNextExternalRoute(aIterator, aConfig);
exit:
return error;
@@ -138,7 +149,9 @@ exit:
otError otBorderRouterRegister(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetNetworkDataLocal().SendServerDataNotification();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetNetworkDataLocal().SendServerDataNotification();
}
#endif // OPENTHREAD_ENABLE_BORDER_ROUTER
diff --git a/src/core/api/child_supervision_api.cpp b/src/core/api/child_supervision_api.cpp
index c7733be12..f667f5326 100644
--- a/src/core/api/child_supervision_api.cpp
+++ b/src/core/api/child_supervision_api.cpp
@@ -34,7 +34,7 @@
#include "openthread-core-config.h"
#include "openthread/child_supervision.h"
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
@@ -42,22 +42,30 @@ using namespace ot;
uint16_t otChildSupervisionGetInterval(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetChildSupervisor().GetSupervisionInterval();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetChildSupervisor().GetSupervisionInterval();
}
void otChildSupervisionSetInterval(otInstance *aInstance, uint16_t aInterval)
{
- aInstance->mThreadNetif.GetChildSupervisor().SetSupervisionInterval(aInterval);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetThreadNetif().GetChildSupervisor().SetSupervisionInterval(aInterval);
}
uint16_t otChildSupervisionGetCheckTimeout(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetSupervisionListener().GetTimeout();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetSupervisionListener().GetTimeout();
}
void otChildSupervisionSetCheckTimeout(otInstance *aInstance, uint16_t aTimeout)
{
- aInstance->mThreadNetif.GetSupervisionListener().SetTimeout(aTimeout);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetThreadNetif().GetSupervisionListener().SetTimeout(aTimeout);
}
#endif // OPENTHREAD_ENABLE_CHILD_SUPERVISION
diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp
index ed0585bea..4b6209a00 100644
--- a/src/core/api/coap_api.cpp
+++ b/src/core/api/coap_api.cpp
@@ -35,7 +35,7 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
#include "coap/coap_header.hpp"
#if OPENTHREAD_ENABLE_APPLICATION_COAP
@@ -141,8 +141,10 @@ const otCoapOption *otCoapHeaderGetNextOption(otCoapHeader *aHeader)
otMessage *otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader)
{
Message *message;
+ Instance &instance = *static_cast(aInstance);
+
VerifyOrExit(aHeader != NULL, message = NULL);
- message = aInstance->mApplicationCoap.NewMessage(*(static_cast(aHeader)));
+ message = instance.GetApplicationCoap().NewMessage(*(static_cast(aHeader)));
exit:
return message;
}
@@ -150,7 +152,9 @@ exit:
otError otCoapSendRequest(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo,
otCoapResponseHandler aHandler, void *aContext)
{
- return aInstance->mApplicationCoap.SendMessage(
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetApplicationCoap().SendMessage(
*static_cast(aMessage),
*static_cast(aMessageInfo),
aHandler, aContext);
@@ -158,32 +162,44 @@ otError otCoapSendRequest(otInstance *aInstance, otMessage *aMessage, const otMe
otError otCoapStart(otInstance *aInstance, uint16_t aPort)
{
- return aInstance->mApplicationCoap.Start(aPort);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetApplicationCoap().Start(aPort);
}
otError otCoapStop(otInstance *aInstance)
{
- return aInstance->mApplicationCoap.Stop();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetApplicationCoap().Stop();
}
otError otCoapAddResource(otInstance *aInstance, otCoapResource *aResource)
{
- return aInstance->mApplicationCoap.AddResource(*static_cast(aResource));
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetApplicationCoap().AddResource(*static_cast(aResource));
}
void otCoapRemoveResource(otInstance *aInstance, otCoapResource *aResource)
{
- aInstance->mApplicationCoap.RemoveResource(*static_cast(aResource));
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetApplicationCoap().RemoveResource(*static_cast(aResource));
}
void otCoapSetDefaultHandler(otInstance *aInstance, otCoapRequestHandler aHandler, void *aContext)
{
- aInstance->mApplicationCoap.SetDefaultHandler(aHandler, aContext);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetApplicationCoap().SetDefaultHandler(aHandler, aContext);
}
otError otCoapSendResponse(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo)
{
- return aInstance->mApplicationCoap.SendMessage(
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetApplicationCoap().SendMessage(
*static_cast(aMessage), *static_cast(aMessageInfo));
}
diff --git a/src/core/api/commissioner_api.cpp b/src/core/api/commissioner_api.cpp
index 81df3ae20..467a49f97 100644
--- a/src/core/api/commissioner_api.cpp
+++ b/src/core/api/commissioner_api.cpp
@@ -35,7 +35,7 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
@@ -44,10 +44,12 @@ otError otCommissionerStart(otInstance *aInstance)
otError error = OT_ERROR_DISABLED_FEATURE;
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- error = aInstance->mThreadNetif.GetCommissioner().Start();
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().Start();
+#else
OT_UNUSED_VARIABLE(aInstance);
-#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#endif
return error;
}
@@ -57,10 +59,12 @@ otError otCommissionerStop(otInstance *aInstance)
otError error = OT_ERROR_DISABLED_FEATURE;
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- error = aInstance->mThreadNetif.GetCommissioner().Stop();
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().Stop();
+#else
OT_UNUSED_VARIABLE(aInstance);
-#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#endif
return error;
}
@@ -70,15 +74,18 @@ otError otCommissionerAddJoiner(otInstance *aInstance, const otExtAddress *aEui6
{
otError error = OT_ERROR_DISABLED_FEATURE;
+
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- error = aInstance->mThreadNetif.GetCommissioner().AddJoiner(static_cast(aEui64), aPSKd,
- aTimeout);
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().AddJoiner(static_cast(aEui64), aPSKd,
+ aTimeout);
+#else
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aEui64);
OT_UNUSED_VARIABLE(aPSKd);
OT_UNUSED_VARIABLE(aTimeout);
-#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#endif
return error;
}
@@ -88,11 +95,13 @@ otError otCommissionerRemoveJoiner(otInstance *aInstance, const otExtAddress *aE
otError error = OT_ERROR_DISABLED_FEATURE;
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- error = aInstance->mThreadNetif.GetCommissioner().RemoveJoiner(static_cast(aEui64), 0);
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().RemoveJoiner(static_cast(aEui64), 0);
+#else
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aEui64);
-#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#endif
return error;
}
@@ -102,11 +111,13 @@ otError otCommissionerSetProvisioningUrl(otInstance *aInstance, const char *aPro
otError error = OT_ERROR_DISABLED_FEATURE;
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- error = aInstance->mThreadNetif.GetCommissioner().SetProvisioningUrl(aProvisioningUrl);
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().SetProvisioningUrl(aProvisioningUrl);
+#else
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aProvisioningUrl);
-#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#endif
return error;
}
@@ -117,15 +128,17 @@ otError otCommissionerAnnounceBegin(otInstance *aInstance, uint32_t aChannelMask
otError error = OT_ERROR_DISABLED_FEATURE;
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- error = aInstance->mThreadNetif.GetCommissioner().GetAnnounceBeginClient().SendRequest(
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().GetAnnounceBeginClient().SendRequest(
aChannelMask, aCount, aPeriod, *static_cast(aAddress));
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#else
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
+#endif
return error;
}
@@ -137,10 +150,12 @@ otError otCommissionerEnergyScan(otInstance *aInstance, uint32_t aChannelMask, u
otError error = OT_ERROR_DISABLED_FEATURE;
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- error = aInstance->mThreadNetif.GetCommissioner().GetEnergyScanClient().SendQuery(
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().GetEnergyScanClient().SendQuery(
aChannelMask, aCount, aPeriod, aScanDuration, *static_cast(aAddress),
aCallback, aContext);
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#else
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aChannelMask);
OT_UNUSED_VARIABLE(aCount);
@@ -149,7 +164,7 @@ otError otCommissionerEnergyScan(otInstance *aInstance, uint32_t aChannelMask, u
OT_UNUSED_VARIABLE(aAddress);
OT_UNUSED_VARIABLE(aCallback);
OT_UNUSED_VARIABLE(aContext);
-#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#endif
return error;
}
@@ -161,16 +176,18 @@ otError otCommissionerPanIdQuery(otInstance *aInstance, uint16_t aPanId, uint32_
otError error = OT_ERROR_DISABLED_FEATURE;
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- error = aInstance->mThreadNetif.GetCommissioner().GetPanIdQueryClient().SendQuery(
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().GetPanIdQueryClient().SendQuery(
aPanId, aChannelMask, *static_cast(aAddress), aCallback, aContext);
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#else
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
+#endif
return error;
}
@@ -180,12 +197,14 @@ otError otCommissionerSendMgmtGet(otInstance *aInstance, const uint8_t *aTlvs, u
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
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().SendMgmtCommissionerGetRequest(aTlvs, aLength);
+#else
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aTlvs);
OT_UNUSED_VARIABLE(aLength);
-#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#endif
return error;
}
@@ -196,13 +215,15 @@ otError otCommissionerSendMgmtSet(otInstance *aInstance, const otCommissioningDa
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
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().SendMgmtCommissionerSetRequest(*aDataset, aTlvs, aLength);
+#else
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aDataset);
OT_UNUSED_VARIABLE(aTlvs);
OT_UNUSED_VARIABLE(aLength);
-#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#endif
return error;
}
@@ -212,10 +233,12 @@ uint16_t otCommissionerGetSessionId(otInstance *aInstance)
uint16_t sessionId = 0;
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- sessionId = aInstance->mThreadNetif.GetCommissioner().GetSessionId();
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+ Instance &instance = *static_cast(aInstance);
+
+ sessionId = instance.GetThreadNetif().GetCommissioner().GetSessionId();
+#else
OT_UNUSED_VARIABLE(aInstance);
-#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#endif
return sessionId;
}
@@ -225,10 +248,12 @@ otCommissionerState otCommissionerGetState(otInstance *aInstance)
otCommissionerState state = OT_COMMISSIONER_STATE_DISABLED;
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- state = aInstance->mThreadNetif.GetCommissioner().GetState();
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+ Instance &instance = *static_cast(aInstance);
+
+ state = instance.GetThreadNetif().GetCommissioner().GetState();
+#else
OT_UNUSED_VARIABLE(aInstance);
-#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+#endif
return state;
}
@@ -239,14 +264,16 @@ otError otCommissionerGeneratePSKc(otInstance *aInstance, const char *aPassPhras
otError error = OT_ERROR_DISABLED_FEATURE;
#if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER
- error = aInstance->mThreadNetif.GetCommissioner().GeneratePSKc(aPassPhrase, aNetworkName, aExtPanId, aPSKc);
-#else // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetCommissioner().GeneratePSKc(aPassPhrase, aNetworkName, aExtPanId, aPSKc);
+#else
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
+#endif
return error;
}
diff --git a/src/core/api/dataset_api.cpp b/src/core/api/dataset_api.cpp
index a84e3a2d3..dc4f22022 100644
--- a/src/core/api/dataset_api.cpp
+++ b/src/core/api/dataset_api.cpp
@@ -35,7 +35,7 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
@@ -57,10 +57,11 @@ bool otDatasetIsCommissioned(otInstance *aInstance)
otError otDatasetGetActive(otInstance *aInstance, otOperationalDataset *aDataset)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aDataset != NULL, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetActiveDataset().GetLocal().Get(*aDataset);
+ error = instance.GetThreadNetif().GetActiveDataset().GetLocal().Get(*aDataset);
exit:
return error;
@@ -69,10 +70,11 @@ exit:
otError otDatasetGetPending(otInstance *aInstance, otOperationalDataset *aDataset)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aDataset != NULL, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetPendingDataset().GetLocal().Get(*aDataset);
+ error = instance.GetThreadNetif().GetPendingDataset().GetLocal().Get(*aDataset);
exit:
return error;
diff --git a/src/core/api/dataset_ftd_api.cpp b/src/core/api/dataset_ftd_api.cpp
index c231966c5..5cbed5df8 100644
--- a/src/core/api/dataset_ftd_api.cpp
+++ b/src/core/api/dataset_ftd_api.cpp
@@ -37,17 +37,18 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
otError otDatasetSetActive(otInstance *aInstance, const otOperationalDataset *aDataset)
{
otError error;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aDataset != NULL, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetActiveDataset().Set(*aDataset);
+ error = instance.GetThreadNetif().GetActiveDataset().Set(*aDataset);
exit:
return error;
@@ -56,10 +57,11 @@ exit:
otError otDatasetSetPending(otInstance *aInstance, const otOperationalDataset *aDataset)
{
otError error;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aDataset != NULL, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetPendingDataset().Set(*aDataset);
+ error = instance.GetThreadNetif().GetPendingDataset().Set(*aDataset);
exit:
return error;
@@ -68,35 +70,47 @@ exit:
otError otDatasetSendMgmtActiveGet(otInstance *aInstance, const uint8_t *aTlvTypes, uint8_t aLength,
const otIp6Address *aAddress)
{
- return aInstance->mThreadNetif.GetActiveDataset().SendGetRequest(aTlvTypes, aLength, aAddress);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetActiveDataset().SendGetRequest(aTlvTypes, aLength, aAddress);
}
otError otDatasetSendMgmtActiveSet(otInstance *aInstance, const otOperationalDataset *aDataset,
const uint8_t *aTlvs, uint8_t aLength)
{
- return aInstance->mThreadNetif.GetActiveDataset().SendSetRequest(*aDataset, aTlvs, aLength);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetActiveDataset().SendSetRequest(*aDataset, aTlvs, aLength);
}
otError otDatasetSendMgmtPendingGet(otInstance *aInstance, const uint8_t *aTlvTypes, uint8_t aLength,
const otIp6Address *aAddress)
{
- return aInstance->mThreadNetif.GetPendingDataset().SendGetRequest(aTlvTypes, aLength, aAddress);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetPendingDataset().SendGetRequest(aTlvTypes, aLength, aAddress);
}
otError otDatasetSendMgmtPendingSet(otInstance *aInstance, const otOperationalDataset *aDataset,
const uint8_t *aTlvs, uint8_t aLength)
{
- return aInstance->mThreadNetif.GetPendingDataset().SendSetRequest(*aDataset, aTlvs, aLength);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetPendingDataset().SendSetRequest(*aDataset, aTlvs, aLength);
}
uint32_t otDatasetGetDelayTimerMinimal(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetLeader().GetDelayTimerMinimal();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetLeader().GetDelayTimerMinimal();
}
otError otDatasetSetDelayTimerMinimal(otInstance *aInstance, uint32_t aDelayTimerMinimal)
{
- return aInstance->mThreadNetif.GetLeader().SetDelayTimerMinimal(aDelayTimerMinimal);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetLeader().SetDelayTimerMinimal(aDelayTimerMinimal);
}
#endif // OPENTHREAD_FTD
diff --git a/src/core/api/dhcp6_api.cpp b/src/core/api/dhcp6_api.cpp
index ef307e481..9bb664e76 100644
--- a/src/core/api/dhcp6_api.cpp
+++ b/src/core/api/dhcp6_api.cpp
@@ -36,20 +36,24 @@
#include
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
#if OPENTHREAD_ENABLE_DHCP6_CLIENT
void otDhcp6ClientUpdate(otInstance *aInstance, otDhcpAddress *aAddresses, uint32_t aNumAddresses, void *aContext)
{
- aInstance->mThreadNetif.GetDhcp6Client().UpdateAddresses(aInstance, aAddresses, aNumAddresses, aContext);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetThreadNetif().GetDhcp6Client().UpdateAddresses(aInstance, aAddresses, aNumAddresses, aContext);
}
-#endif // OPENTHREAD_ENABLE_DHCP6_CLIENT
+#endif
#if OPENTHREAD_ENABLE_DHCP6_SERVER
void otDhcp6ServerUpdate(otInstance *aInstance)
{
- aInstance->mThreadNetif.GetDhcp6Server().UpdateService();
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetThreadNetif().GetDhcp6Server().UpdateService();
}
-#endif // OPENTHREAD_ENABLE_DHCP6_SERVER
+#endif
diff --git a/src/core/api/dns_api.cpp b/src/core/api/dns_api.cpp
index 14294f99a..54e5be3c1 100644
--- a/src/core/api/dns_api.cpp
+++ b/src/core/api/dns_api.cpp
@@ -35,7 +35,7 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
@@ -43,6 +43,8 @@ using namespace ot;
otError otDnsClientQuery(otInstance *aInstance, const otDnsQuery *aQuery, otDnsResponseHandler aHandler,
void *aContext)
{
- return aInstance->mThreadNetif.GetDnsClient().Query(aQuery, aHandler, aContext);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetDnsClient().Query(aQuery, aHandler, aContext);
}
-#endif // OPENTHREAD_ENABLE_DNS_CLIENT
+#endif
diff --git a/src/core/api/icmp6_api.cpp b/src/core/api/icmp6_api.cpp
index 7d5aed4f7..719600598 100644
--- a/src/core/api/icmp6_api.cpp
+++ b/src/core/api/icmp6_api.cpp
@@ -35,29 +35,37 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
bool otIcmp6IsEchoEnabled(otInstance *aInstance)
{
- return aInstance->mIp6.GetIcmp().IsEchoEnabled();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetIp6().GetIcmp().IsEchoEnabled();
}
void otIcmp6SetEchoEnabled(otInstance *aInstance, bool aEnabled)
{
- aInstance->mIp6.GetIcmp().SetEchoEnabled(aEnabled);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetIp6().GetIcmp().SetEchoEnabled(aEnabled);
}
otError otIcmp6RegisterHandler(otInstance *aInstance, otIcmp6Handler *aHandler)
{
- return aInstance->mIp6.GetIcmp().RegisterHandler(*static_cast(aHandler));
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetIp6().GetIcmp().RegisterHandler(*static_cast(aHandler));
}
otError otIcmp6SendEchoRequest(otInstance *aInstance, otMessage *aMessage,
const otMessageInfo *aMessageInfo, uint16_t aIdentifier)
{
- return aInstance->mIp6.GetIcmp().SendEchoRequest(*static_cast(aMessage),
- *static_cast(aMessageInfo),
- aIdentifier);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetIp6().GetIcmp().SendEchoRequest(*static_cast(aMessage),
+ *static_cast(aMessageInfo),
+ aIdentifier);
}
diff --git a/src/core/api/instance_api.cpp b/src/core/api/instance_api.cpp
index 53c41404a..b2f3bf31b 100644
--- a/src/core/api/instance_api.cpp
+++ b/src/core/api/instance_api.cpp
@@ -39,227 +39,81 @@
#include
#include
-#include "openthread-instance.h"
-#include "openthread-single-instance.h"
+#include "common/instance.hpp"
#include "common/logging.hpp"
#include "common/new.hpp"
-#if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
-
-static otDEFINE_ALIGNED_VAR(sInstanceRaw, sizeof(otInstance), uint64_t);
-otInstance *sInstance = NULL;
-
-otInstance *otGetInstance(void)
-{
- return sInstance;
-}
-
-ot::ThreadNetif &otGetThreadNetif(void)
-{
- return sInstance->mThreadNetif;
-}
-
-ot::MeshForwarder &otGetMeshForwarder(void)
-{
- return sInstance->mThreadNetif.GetMeshForwarder();
-}
-
-ot::TaskletScheduler &otGetTaskletScheduler(void)
-{
- return sInstance->mTaskletScheduler;
-}
-
-ot::Ip6::Ip6 &otGetIp6(void)
-{
- return sInstance->mIp6;
-}
-#endif // #if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
-
-otInstance::otInstance(void) :
- mReceiveIp6DatagramCallback(NULL),
- mReceiveIp6DatagramCallbackContext(NULL),
- mActiveScanCallback(NULL),
- mActiveScanCallbackContext(NULL),
- mEnergyScanCallback(NULL),
- mEnergyScanCallbackContext(NULL),
- mTimerMilliScheduler(*this),
-#if OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER
- mTimerMicroScheduler(*this),
-#endif
- mIp6(*this),
- mThreadNetif(*this),
-#if OPENTHREAD_ENABLE_RAW_LINK_API
- mLinkRaw(*this),
-#endif // OPENTHREAD_ENABLE_RAW_LINK_API
-#if OPENTHREAD_ENABLE_APPLICATION_COAP
- mApplicationCoap(*this),
-#endif // OPENTHREAD_ENABLE_APPLICATION_COAP
-#if OPENTHREAD_CONFIG_ENABLE_DYNAMIC_LOG_LEVEL
- mLogLevel(static_cast(OPENTHREAD_CONFIG_LOG_LEVEL)),
-#endif // OPENTHREAD_CONFIG_ENABLE_DYNAMIC_LOG_LEVEL
- mMessagePool(*this)
-{
-}
-
using namespace ot;
-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) == OT_ERROR_NONE)
- {
- // Only try to start Thread if we could bring up the interface
- if (otThreadSetEnabled(aInstance, true) != OT_ERROR_NONE)
- {
- // Bring the interface down if Thread failed to start
- otIp6SetEnabled(aInstance, false);
- }
- }
- }
-
-#endif
-}
-
#if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
-
otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize)
{
- otInstance *instance = NULL;
+ Instance *instance;
otLogFuncEntry();
-
- VerifyOrExit(aInstanceBufferSize != NULL);
-
- // Make sure the input buffer is big enough
- VerifyOrExit(sizeof(otInstance) <= *aInstanceBufferSize, *aInstanceBufferSize = sizeof(otInstance));
-
- VerifyOrExit(aInstanceBuffer != NULL);
-
- // Construct the context
- instance = new(aInstanceBuffer)otInstance();
-
- // Execute post constructor operations
- otInstancePostConstructor(instance);
-
+ instance = Instance::Init(aInstanceBuffer, aInstanceBufferSize);
otLogInfoApi(*instance, "otInstance Initialized");
-
-exit:
-
otLogFuncExit();
+
return instance;
}
-
-bool otInstanceIsInitialized(otInstance *aInstance)
-{
- return (aInstance != NULL);
-}
-
-#else // #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
-
+#else
otInstance *otInstanceInitSingle(void)
{
- otLogFuncEntry();
-
- VerifyOrExit(sInstance == NULL);
-
- // We need to ensure `sInstance` pointer is correctly set
- // before any object constructor is called.
- sInstance = reinterpret_cast(&sInstanceRaw);
-
- // Construct the context
- sInstance = new(&sInstanceRaw)otInstance();
-
- // Execute post constructor operations
- otInstancePostConstructor(sInstance);
-
- otLogInfoApi(*sInstance, "otInstance Initialized");
-
-exit:
-
- otLogFuncExit();
- return sInstance;
+ return &Instance::InitSingle();
}
+#endif // #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
+
bool otInstanceIsInitialized(otInstance *aInstance)
{
- return (aInstance != NULL) && (aInstance == sInstance);
-}
+ Instance &instance = *static_cast(aInstance);
-#endif // #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
+ return instance.IsInitialized();
+}
void otInstanceFinalize(otInstance *aInstance)
{
+ Instance &instance = *static_cast(aInstance);
+
otLogFuncEntry();
-
- // Ensure we are disabled
- (void)otThreadSetEnabled(aInstance, false);
- (void)otIp6SetEnabled(aInstance, false);
-
-#if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
- sInstance = NULL;
-#endif
-
+ instance.Finalize();
otLogFuncExit();
}
otError otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aCallbackContext)
{
- otError error = OT_ERROR_NO_BUFS;
+ Instance &instance = *static_cast(aInstance);
- 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;
+ return instance.RegisterStateChangedCallback(aCallback, aCallbackContext);
}
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;
- }
- }
+ Instance &instance = *static_cast(aInstance);
+
+ instance.RemoveStateChangedCallback(aCallback, aCallbackContext);
}
void otInstanceReset(otInstance *aInstance)
{
- otPlatReset(aInstance);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.Reset();
}
void otInstanceFactoryReset(otInstance *aInstance)
{
- otPlatSettingsWipe(aInstance);
- otPlatReset(aInstance);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.FactoryReset();
}
otError otInstanceErasePersistentInfo(otInstance *aInstance)
{
- otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(otThreadGetDeviceRole(aInstance) == OT_DEVICE_ROLE_DISABLED, error = OT_ERROR_INVALID_STATE);
- otPlatSettingsWipe(aInstance);
-
-exit:
- return error;
+ return instance.ErasePersistentInfo();
}
otLogLevel otGetDynamicLogLevel(otInstance *aInstance)
@@ -267,7 +121,9 @@ otLogLevel otGetDynamicLogLevel(otInstance *aInstance)
otLogLevel logLevel;
#if OPENTHREAD_CONFIG_ENABLE_DYNAMIC_LOG_LEVEL
- logLevel = aInstance->mLogLevel;
+ Instance &instance = *static_cast(aInstance);
+
+ logLevel = instance.GetDynamicLogLevel();
#else
logLevel = static_cast(OPENTHREAD_CONFIG_LOG_LEVEL);
OT_UNUSED_VARIABLE(aInstance);
@@ -281,7 +137,9 @@ otError otSetDynamicLogLevel(otInstance *aInstance, otLogLevel aLogLevel)
otError error = OT_ERROR_NONE;
#if OPENTHREAD_CONFIG_ENABLE_DYNAMIC_LOG_LEVEL
- aInstance->mLogLevel = aLogLevel;
+ Instance &instance = *static_cast(aInstance);
+
+ instance.SetDynamicLogLevel(aLogLevel);
#else
error = OT_ERROR_DISABLED_FEATURE;
OT_UNUSED_VARIABLE(aInstance);
diff --git a/src/core/api/ip6_api.cpp b/src/core/api/ip6_api.cpp
index a811db494..ebf3a96c5 100644
--- a/src/core/api/ip6_api.cpp
+++ b/src/core/api/ip6_api.cpp
@@ -37,7 +37,7 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
#include "common/logging.hpp"
#include "utils/slaac_address.hpp"
@@ -46,22 +46,23 @@ using namespace ot;
otError otIp6SetEnabled(otInstance *aInstance, bool aEnabled)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
otLogFuncEntry();
if (aEnabled)
{
#if OPENTHREAD_ENABLE_RAW_LINK_API
- VerifyOrExit(!aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(!instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
#endif // OPENTHREAD_ENABLE_RAW_LINK_API
- error = aInstance->mThreadNetif.Up();
+ error = instance.GetThreadNetif().Up();
}
else
{
#if OPENTHREAD_ENABLE_RAW_LINK_API
- VerifyOrExit(!aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(!instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
#endif // OPENTHREAD_ENABLE_RAW_LINK_API
- error = aInstance->mThreadNetif.Down();
+ error = instance.GetThreadNetif().Down();
}
#if OPENTHREAD_ENABLE_RAW_LINK_API
@@ -73,47 +74,65 @@ exit:
bool otIp6IsEnabled(otInstance *aInstance)
{
- return aInstance->mThreadNetif.IsUp();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().IsUp();
}
const otNetifAddress *otIp6GetUnicastAddresses(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetUnicastAddresses();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetUnicastAddresses();
}
otError otIp6AddUnicastAddress(otInstance *aInstance, const otNetifAddress *address)
{
- return aInstance->mThreadNetif.AddExternalUnicastAddress(*static_cast(address));
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().AddExternalUnicastAddress(*static_cast(address));
}
otError otIp6RemoveUnicastAddress(otInstance *aInstance, const otIp6Address *address)
{
- return aInstance->mThreadNetif.RemoveExternalUnicastAddress(*static_cast(address));
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().RemoveExternalUnicastAddress(*static_cast(address));
}
const otNetifMulticastAddress *otIp6GetMulticastAddresses(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMulticastAddresses();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMulticastAddresses();
}
otError otIp6SubscribeMulticastAddress(otInstance *aInstance, const otIp6Address *aAddress)
{
- return aInstance->mThreadNetif.SubscribeExternalMulticast(*static_cast(aAddress));
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().SubscribeExternalMulticast(*static_cast(aAddress));
}
otError otIp6UnsubscribeMulticastAddress(otInstance *aInstance, const otIp6Address *aAddress)
{
- return aInstance->mThreadNetif.UnsubscribeExternalMulticast(*static_cast(aAddress));
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().UnsubscribeExternalMulticast(*static_cast(aAddress));
}
bool otIp6IsMulticastPromiscuousEnabled(otInstance *aInstance)
{
- return aInstance->mThreadNetif.IsMulticastPromiscuousEnabled();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().IsMulticastPromiscuousEnabled();
}
void otIp6SetMulticastPromiscuousEnabled(otInstance *aInstance, bool aEnabled)
{
- aInstance->mThreadNetif.SetMulticastPromiscuous(aEnabled);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetThreadNetif().SetMulticastPromiscuous(aEnabled);
}
void otIp6SlaacUpdate(otInstance *aInstance, otNetifAddress *aAddresses, uint32_t aNumAddresses,
@@ -129,8 +148,10 @@ otError otIp6CreateRandomIid(otInstance *aInstance, otNetifAddress *aAddress, vo
otError otIp6CreateMacIid(otInstance *aInstance, otNetifAddress *aAddress, void *)
{
+ Instance &instance = *static_cast(aInstance);
+
memcpy(&aAddress->mAddress.mFields.m8[OT_IP6_ADDRESS_SIZE - OT_IP6_IID_SIZE],
- aInstance->mThreadNetif.GetMac().GetExtAddress(), OT_IP6_IID_SIZE);
+ instance.GetThreadNetif().GetMac().GetExtAddress(), OT_IP6_IID_SIZE);
aAddress->mAddress.mFields.m8[OT_IP6_ADDRESS_SIZE - OT_IP6_IID_SIZE] ^= 0x02;
return OT_ERROR_NONE;
@@ -143,27 +164,34 @@ otError otIp6CreateSemanticallyOpaqueIid(otInstance *aInstance, otNetifAddress *
void otIp6SetReceiveCallback(otInstance *aInstance, otIp6ReceiveCallback aCallback, void *aCallbackContext)
{
- aInstance->mIp6.SetReceiveDatagramCallback(aCallback, aCallbackContext);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetIp6().SetReceiveDatagramCallback(aCallback, aCallbackContext);
}
bool otIp6IsReceiveFilterEnabled(otInstance *aInstance)
{
- return aInstance->mIp6.IsReceiveIp6FilterEnabled();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetIp6().IsReceiveIp6FilterEnabled();
}
void otIp6SetReceiveFilterEnabled(otInstance *aInstance, bool aEnabled)
{
- aInstance->mIp6.SetReceiveIp6FilterEnabled(aEnabled);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetIp6().SetReceiveIp6FilterEnabled(aEnabled);
}
otError otIp6Send(otInstance *aInstance, otMessage *aMessage)
{
otError error;
+ Instance &instance = *static_cast(aInstance);
otLogFuncEntry();
- error = aInstance->mIp6.SendRaw(*static_cast(aMessage),
- aInstance->mThreadNetif.GetInterfaceId());
+ error = instance.GetIp6().SendRaw(*static_cast(aMessage),
+ instance.GetThreadNetif().GetInterfaceId());
otLogFuncExitErr(error);
@@ -172,7 +200,9 @@ otError otIp6Send(otInstance *aInstance, otMessage *aMessage)
otMessage *otIp6NewMessage(otInstance *aInstance, bool aLinkSecurityEnabled)
{
- Message *message = aInstance->mMessagePool.New(Message::kTypeIp6, 0);
+ Instance &instance = *static_cast(aInstance);
+ Message *message = instance.GetMessagePool().New(Message::kTypeIp6, 0);
+
if (message)
{
@@ -184,17 +214,23 @@ otMessage *otIp6NewMessage(otInstance *aInstance, bool aLinkSecurityEnabled)
otError otIp6AddUnsecurePort(otInstance *aInstance, uint16_t aPort)
{
- return aInstance->mThreadNetif.GetIp6Filter().AddUnsecurePort(aPort);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetIp6Filter().AddUnsecurePort(aPort);
}
otError otIp6RemoveUnsecurePort(otInstance *aInstance, uint16_t aPort)
{
- return aInstance->mThreadNetif.GetIp6Filter().RemoveUnsecurePort(aPort);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetIp6Filter().RemoveUnsecurePort(aPort);
}
const uint16_t *otIp6GetUnsecurePorts(otInstance *aInstance, uint8_t *aNumEntries)
{
- return aInstance->mThreadNetif.GetIp6Filter().GetUnsecurePorts(*aNumEntries);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetIp6Filter().GetUnsecurePorts(*aNumEntries);
}
bool otIp6IsAddressEqual(const otIp6Address *a, const otIp6Address *b)
diff --git a/src/core/api/jam_detection_api.cpp b/src/core/api/jam_detection_api.cpp
index a77b7cc36..57893c08d 100644
--- a/src/core/api/jam_detection_api.cpp
+++ b/src/core/api/jam_detection_api.cpp
@@ -35,7 +35,7 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
@@ -43,57 +43,79 @@ using namespace ot;
otError otJamDetectionSetRssiThreshold(otInstance *aInstance, int8_t aRssiThreshold)
{
- return aInstance->mThreadNetif.GetJamDetector().SetRssiThreshold(aRssiThreshold);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().SetRssiThreshold(aRssiThreshold);
}
int8_t otJamDetectionGetRssiThreshold(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetJamDetector().GetRssiThreshold();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().GetRssiThreshold();
}
otError otJamDetectionSetWindow(otInstance *aInstance, uint8_t aWindow)
{
- return aInstance->mThreadNetif.GetJamDetector().SetWindow(aWindow);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().SetWindow(aWindow);
}
uint8_t otJamDetectionGetWindow(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetJamDetector().GetWindow();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().GetWindow();
}
otError otJamDetectionSetBusyPeriod(otInstance *aInstance, uint8_t aBusyPeriod)
{
- return aInstance->mThreadNetif.GetJamDetector().SetBusyPeriod(aBusyPeriod);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().SetBusyPeriod(aBusyPeriod);
}
uint8_t otJamDetectionGetBusyPeriod(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetJamDetector().GetBusyPeriod();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().GetBusyPeriod();
}
otError otJamDetectionStart(otInstance *aInstance, otJamDetectionCallback aCallback, void *aContext)
{
- return aInstance->mThreadNetif.GetJamDetector().Start(aCallback, aContext);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().Start(aCallback, aContext);
}
otError otJamDetectionStop(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetJamDetector().Stop();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().Stop();
}
bool otJamDetectionIsEnabled(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetJamDetector().IsEnabled();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().IsEnabled();
}
bool otJamDetectionGetState(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetJamDetector().GetState();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().GetState();
}
uint64_t otJamDetectionGetHistoryBitmap(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetJamDetector().GetHistoryBitmap();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetJamDetector().GetHistoryBitmap();
}
#endif // OPENTHREAD_ENABLE_JAM_DETECTION
diff --git a/src/core/api/joiner_api.cpp b/src/core/api/joiner_api.cpp
index 8680efc33..df3fba69a 100644
--- a/src/core/api/joiner_api.cpp
+++ b/src/core/api/joiner_api.cpp
@@ -35,7 +35,7 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
@@ -45,11 +45,11 @@ otError otJoinerStart(otInstance *aInstance, const char *aPSKd, const char *aPro
otJoinerCallback aCallback, void *aContext)
{
otError error = OT_ERROR_DISABLED_FEATURE;
-
#if OPENTHREAD_ENABLE_JOINER
- error = aInstance->mThreadNetif.GetJoiner().Start(aPSKd, aProvisioningUrl,
- aVendorName, aVendorModel, aVendorSwVersion, aVendorData,
- aCallback, aContext);
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetJoiner().Start(aPSKd, aProvisioningUrl, aVendorName, aVendorModel,
+ aVendorSwVersion, aVendorData, aCallback, aContext);
#else
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aPSKd);
@@ -70,7 +70,9 @@ otError otJoinerStop(otInstance *aInstance)
otError error = OT_ERROR_DISABLED_FEATURE;
#if OPENTHREAD_ENABLE_JOINER
- error = aInstance->mThreadNetif.GetJoiner().Stop();
+ Instance &instance = *static_cast(aInstance);
+
+ error = instance.GetThreadNetif().GetJoiner().Stop();
#else
OT_UNUSED_VARIABLE(aInstance);
#endif
@@ -83,7 +85,9 @@ otJoinerState otJoinerGetState(otInstance *aInstance)
otJoinerState state = OT_JOINER_STATE_IDLE;
#if OPENTHREAD_ENABLE_JOINER
- state = aInstance->mThreadNetif.GetJoiner().GetState();
+ Instance &instance = *static_cast(aInstance);
+
+ state = instance.GetThreadNetif().GetJoiner().GetState();
#else
OT_UNUSED_VARIABLE(aInstance);
#endif
@@ -96,7 +100,9 @@ otError otJoinerGetId(otInstance *aInstance, otExtAddress *aJoinerId)
otError error = OT_ERROR_DISABLED_FEATURE;
#if OPENTHREAD_ENABLE_JOINER
- aInstance->mThreadNetif.GetJoiner().GetJoinerId(*static_cast(aJoinerId));
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetThreadNetif().GetJoiner().GetJoinerId(*static_cast(aJoinerId));
error = OT_ERROR_NONE;
#else
OT_UNUSED_VARIABLE(aInstance);
diff --git a/src/core/api/link_api.cpp b/src/core/api/link_api.cpp
index c9217a302..4d7482a66 100644
--- a/src/core/api/link_api.cpp
+++ b/src/core/api/link_api.cpp
@@ -35,7 +35,7 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
@@ -44,19 +44,22 @@ static void HandleEnergyScanResult(void *aContext, otEnergyScanResult *aResult);
uint8_t otLinkGetChannel(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMac().GetChannel();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().GetChannel();
}
otError otLinkSetChannel(otInstance *aInstance, uint8_t aChannel)
{
otError error;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mThreadNetif.GetMle().GetRole() == OT_DEVICE_ROLE_DISABLED,
+ VerifyOrExit(instance.GetThreadNetif().GetMle().GetRole() == OT_DEVICE_ROLE_DISABLED,
error = OT_ERROR_INVALID_STATE);
- SuccessOrExit(error = aInstance->mThreadNetif.GetMac().SetChannel(aChannel));
- aInstance->mThreadNetif.GetActiveDataset().Clear();
- aInstance->mThreadNetif.GetPendingDataset().Clear();
+ SuccessOrExit(error = instance.GetThreadNetif().GetMac().SetChannel(aChannel));
+ instance.GetThreadNetif().GetActiveDataset().Clear();
+ instance.GetThreadNetif().GetPendingDataset().Clear();
exit:
return error;
@@ -64,20 +67,23 @@ exit:
const otExtAddress *otLinkGetExtendedAddress(otInstance *aInstance)
{
- return reinterpret_cast(aInstance->mThreadNetif.GetMac().GetExtAddress());
+ Instance &instance = *static_cast(aInstance);
+
+ return reinterpret_cast(instance.GetThreadNetif().GetMac().GetExtAddress());
}
otError otLinkSetExtendedAddress(otInstance *aInstance, const otExtAddress *aExtAddress)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aExtAddress != NULL, error = OT_ERROR_INVALID_ARGS);
- VerifyOrExit(aInstance->mThreadNetif.GetMle().GetRole() == OT_DEVICE_ROLE_DISABLED,
+ VerifyOrExit(instance.GetThreadNetif().GetMle().GetRole() == OT_DEVICE_ROLE_DISABLED,
error = OT_ERROR_INVALID_STATE);
- aInstance->mThreadNetif.GetMac().SetExtAddress(*static_cast(aExtAddress));
+ instance.GetThreadNetif().GetMac().SetExtAddress(*static_cast(aExtAddress));
- SuccessOrExit(error = aInstance->mThreadNetif.GetMle().UpdateLinkLocalAddress());
+ SuccessOrExit(error = instance.GetThreadNetif().GetMle().UpdateLinkLocalAddress());
exit:
return error;
@@ -90,30 +96,37 @@ void otLinkGetFactoryAssignedIeeeEui64(otInstance *aInstance, otExtAddress *aEui
int8_t otLinkGetMaxTransmitPower(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMac().GetMaxTransmitPower();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().GetMaxTransmitPower();
}
void otLinkSetMaxTransmitPower(otInstance *aInstance, int8_t aPower)
{
- aInstance->mThreadNetif.GetMac().SetMaxTransmitPower(aPower);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetThreadNetif().GetMac().SetMaxTransmitPower(aPower);
otPlatRadioSetDefaultTxPower(aInstance, aPower);
}
otPanId otLinkGetPanId(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMac().GetPanId();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().GetPanId();
}
otError otLinkSetPanId(otInstance *aInstance, otPanId aPanId)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mThreadNetif.GetMle().GetRole() == OT_DEVICE_ROLE_DISABLED,
+ VerifyOrExit(instance.GetThreadNetif().GetMle().GetRole() == OT_DEVICE_ROLE_DISABLED,
error = OT_ERROR_INVALID_STATE);
- error = aInstance->mThreadNetif.GetMac().SetPanId(aPanId);
- aInstance->mThreadNetif.GetActiveDataset().Clear();
- aInstance->mThreadNetif.GetPendingDataset().Clear();
+ error = instance.GetThreadNetif().GetMac().SetPanId(aPanId);
+ instance.GetThreadNetif().GetActiveDataset().Clear();
+ instance.GetThreadNetif().GetPendingDataset().Clear();
exit:
return error;
@@ -121,43 +134,56 @@ exit:
uint32_t otLinkGetPollPeriod(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMeshForwarder().GetDataPollManager().GetKeepAlivePollPeriod();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMeshForwarder().GetDataPollManager().GetKeepAlivePollPeriod();
}
void otLinkSetPollPeriod(otInstance *aInstance, uint32_t aPollPeriod)
{
- aInstance->mThreadNetif.GetMeshForwarder().GetDataPollManager().SetExternalPollPeriod(aPollPeriod);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetThreadNetif().GetMeshForwarder().GetDataPollManager().SetExternalPollPeriod(aPollPeriod);
}
otError otLinkSendDataRequest(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMeshForwarder().GetDataPollManager().SendDataPoll();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMeshForwarder().GetDataPollManager().SendDataPoll();
}
otShortAddress otLinkGetShortAddress(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMac().GetShortAddress();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().GetShortAddress();
}
#if OPENTHREAD_ENABLE_MAC_FILTER
otMacFilterAddressMode otLinkFilterGetAddressMode(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMac().GetFilter().GetAddressMode();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().GetFilter().GetAddressMode();
}
otError otLinkFilterSetAddressMode(otInstance *aInstance, otMacFilterAddressMode aMode)
{
- return aInstance->mThreadNetif.GetMac().GetFilter().SetAddressMode(aMode);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().GetFilter().SetAddressMode(aMode);
}
otError otLinkFilterAddAddress(otInstance *aInstance, const otExtAddress *aExtAddress)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aExtAddress != NULL, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetMac().GetFilter().AddAddress(*static_cast(aExtAddress));
+ error = instance.GetThreadNetif().GetMac().GetFilter().AddAddress(*static_cast(aExtAddress));
exit:
return error;
@@ -166,10 +192,11 @@ exit:
otError otLinkFilterRemoveAddress(otInstance *aInstance, const otExtAddress *aExtAddress)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aExtAddress != NULL, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetMac().GetFilter().RemoveAddress(
+ error = instance.GetThreadNetif().GetMac().GetFilter().RemoveAddress(
*static_cast(aExtAddress));
exit:
@@ -178,17 +205,19 @@ exit:
void otLinkFilterClearAddresses(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMac().GetFilter().ClearAddresses();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().GetFilter().ClearAddresses();
}
otError otLinkFilterGetNextAddress(otInstance *aInstance, otMacFilterIterator *aIterator, otMacFilterEntry *aEntry)
{
-
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aIterator != NULL && aEntry != NULL, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetMac().GetFilter().GetNextAddress(*aIterator, *aEntry);
+ error = instance.GetThreadNetif().GetMac().GetFilter().GetNextAddress(*aIterator, *aEntry);
exit:
return error;
@@ -196,27 +225,34 @@ exit:
otError otLinkFilterAddRssIn(otInstance *aInstance, const otExtAddress *aExtAddress, int8_t aRss)
{
- return aInstance->mThreadNetif.GetMac().GetFilter().AddRssIn(
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().GetFilter().AddRssIn(
static_cast(aExtAddress), aRss);
}
otError otLinkFilterRemoveRssIn(otInstance *aInstance, const otExtAddress *aExtAddress)
{
- return aInstance->mThreadNetif.GetMac().GetFilter().RemoveRssIn(static_cast(aExtAddress));
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().GetFilter().RemoveRssIn(static_cast(aExtAddress));
}
void otLinkFilterClearRssIn(otInstance *aInstance)
{
- aInstance->mThreadNetif.GetMac().GetFilter().ClearRssIn();
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetThreadNetif().GetMac().GetFilter().ClearRssIn();
}
otError otLinkFilterGetNextRssIn(otInstance *aInstance, otMacFilterIterator *aIterator, otMacFilterEntry *aEntry)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
VerifyOrExit(aIterator != NULL && aEntry != NULL, error = OT_ERROR_INVALID_ARGS);
- error = aInstance->mThreadNetif.GetMac().GetFilter().GetNextRssIn(*aIterator, *aEntry);
+ error = instance.GetThreadNetif().GetMac().GetFilter().GetNextRssIn(*aIterator, *aEntry);
exit:
return error;
@@ -224,34 +260,43 @@ exit:
uint8_t otLinkConvertRssToLinkQuality(otInstance *aInstance, int8_t aRss)
{
- return LinkQualityInfo::ConvertRssToLinkQuality(aInstance->mThreadNetif.GetMac().GetNoiseFloor(), aRss);
+ Instance &instance = *static_cast(aInstance);
+
+ return LinkQualityInfo::ConvertRssToLinkQuality(instance.GetThreadNetif().GetMac().GetNoiseFloor(), aRss);
}
int8_t otLinkConvertLinkQualityToRss(otInstance *aInstance, uint8_t aLinkQuality)
{
- return LinkQualityInfo::ConvertLinkQualityToRss(aInstance->mThreadNetif.GetMac().GetNoiseFloor(), aLinkQuality);
+ Instance &instance = *static_cast(aInstance);
+
+ return LinkQualityInfo::ConvertLinkQualityToRss(instance.GetThreadNetif().GetMac().GetNoiseFloor(), aLinkQuality);
}
#endif // OPENTHREAD_ENABLE_MAC_FILTER
void otLinkSetPcapCallback(otInstance *aInstance, otLinkPcapCallback aPcapCallback, void *aCallbackContext)
{
- aInstance->mThreadNetif.GetMac().SetPcapCallback(aPcapCallback, aCallbackContext);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.GetThreadNetif().GetMac().SetPcapCallback(aPcapCallback, aCallbackContext);
}
bool otLinkIsPromiscuous(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMac().IsPromiscuous();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().IsPromiscuous();
}
otError otLinkSetPromiscuous(otInstance *aInstance, bool aPromiscuous)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
// cannot enable IEEE 802.15.4 promiscuous mode if the Thread interface is enabled
- VerifyOrExit(aInstance->mThreadNetif.IsUp() == false, error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetThreadNetif().IsUp() == false, error = OT_ERROR_INVALID_STATE);
- aInstance->mThreadNetif.GetMac().SetPromiscuous(aPromiscuous);
+ instance.GetThreadNetif().GetMac().SetPromiscuous(aPromiscuous);
exit:
return error;
@@ -259,58 +304,72 @@ exit:
const otMacCounters *otLinkGetCounters(otInstance *aInstance)
{
- return &aInstance->mThreadNetif.GetMac().GetCounters();
+ Instance &instance = *static_cast(aInstance);
+
+ return &instance.GetThreadNetif().GetMac().GetCounters();
}
otError otLinkActiveScan(otInstance *aInstance, uint32_t aScanChannels, uint16_t aScanDuration,
otHandleActiveScanResult aCallback, void *aCallbackContext)
{
- aInstance->mActiveScanCallback = aCallback;
- aInstance->mActiveScanCallbackContext = aCallbackContext;
- return aInstance->mThreadNetif.GetMac().ActiveScan(aScanChannels, aScanDuration,
- &HandleActiveScanResult, aInstance);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.RegisterActiveScanCallback(aCallback, aCallbackContext);
+ return instance.GetThreadNetif().GetMac().ActiveScan(aScanChannels, aScanDuration,
+ &HandleActiveScanResult, aInstance);
}
bool otLinkIsActiveScanInProgress(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMac().IsActiveScanInProgress();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().IsActiveScanInProgress();
}
void HandleActiveScanResult(void *aContext, Mac::Frame *aFrame)
{
- otInstance *aInstance = static_cast(aContext);
- otActiveScanResult result;
+ Instance &instance = *static_cast(aContext);
- VerifyOrExit(aFrame != NULL, aInstance->mActiveScanCallback(NULL, aInstance->mActiveScanCallbackContext));
- aInstance->mThreadNetif.GetMac().ConvertBeaconToActiveScanResult(aFrame, result);
- aInstance->mActiveScanCallback(&result, aInstance->mActiveScanCallbackContext);
+ if (aFrame == NULL)
+ {
+ instance.InvokeActiveScanCallback(NULL);
+ }
+ else
+ {
+ otActiveScanResult result;
-exit:
- return;
+ instance.GetThreadNetif().GetMac().ConvertBeaconToActiveScanResult(aFrame, result);
+ instance.InvokeActiveScanCallback(&result);
+ }
}
otError otLinkEnergyScan(otInstance *aInstance, uint32_t aScanChannels, uint16_t aScanDuration,
otHandleEnergyScanResult aCallback, void *aCallbackContext)
{
- aInstance->mEnergyScanCallback = aCallback;
- aInstance->mEnergyScanCallbackContext = aCallbackContext;
- return aInstance->mThreadNetif.GetMac().EnergyScan(aScanChannels, aScanDuration,
- &HandleEnergyScanResult, aInstance);
+ Instance &instance = *static_cast(aInstance);
+
+ instance.RegisterEnergyScanCallback(aCallback, aCallbackContext);
+ return instance.GetThreadNetif().GetMac().EnergyScan(aScanChannels, aScanDuration,
+ &HandleEnergyScanResult, aInstance);
}
void HandleEnergyScanResult(void *aContext, otEnergyScanResult *aResult)
{
- otInstance *aInstance = static_cast(aContext);
+ Instance &instance = *static_cast(aContext);
- aInstance->mEnergyScanCallback(aResult, aInstance->mEnergyScanCallbackContext);
+ instance.InvokeEnergyScanCallback(aResult);
}
bool otLinkIsEnergyScanInProgress(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMac().IsEnergyScanInProgress();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().IsEnergyScanInProgress();
}
bool otLinkIsInTransmitState(otInstance *aInstance)
{
- return aInstance->mThreadNetif.GetMac().IsInTransmitState();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetThreadNetif().GetMac().IsInTransmitState();
}
diff --git a/src/core/api/link_raw.hpp b/src/core/api/link_raw.hpp
index dbc116b15..53046718a 100644
--- a/src/core/api/link_raw.hpp
+++ b/src/core/api/link_raw.hpp
@@ -55,7 +55,7 @@ public:
* This constructor initializes the object.
*
*/
- LinkRaw(otInstance &aInstance);
+ LinkRaw(Instance &aInstance);
/**
* This method returns true if the raw link-layer is enabled.
diff --git a/src/core/api/link_raw_api.cpp b/src/core/api/link_raw_api.cpp
index 3b5134fda..ee320561c 100644
--- a/src/core/api/link_raw_api.cpp
+++ b/src/core/api/link_raw_api.cpp
@@ -35,21 +35,24 @@
#include
-#include "openthread-instance.h"
#include "common/debug.hpp"
+#include "common/instance.hpp"
#include "common/logging.hpp"
+using namespace ot;
+
#if OPENTHREAD_ENABLE_RAW_LINK_API
otError otLinkRawSetEnable(otInstance *aInstance, bool aEnabled)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(!aInstance->mThreadNetif.IsUp(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(!instance.GetThreadNetif().IsUp(), error = OT_ERROR_INVALID_STATE);
otLogInfoPlat(aInstance, "LinkRaw Enabled=%d", aEnabled ? 1 : 0);
- aInstance->mLinkRaw.SetEnabled(aEnabled);
+ instance.GetLinkRaw().SetEnabled(aEnabled);
exit:
return error;
@@ -57,14 +60,17 @@ exit:
bool otLinkRawIsEnabled(otInstance *aInstance)
{
- return aInstance->mLinkRaw.IsEnabled();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetLinkRaw().IsEnabled();
}
otError otLinkRawSetPanId(otInstance *aInstance, uint16_t aPanId)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
otPlatRadioSetPanId(aInstance, aPanId);
@@ -75,9 +81,10 @@ exit:
otError otLinkRawSetExtendedAddress(otInstance *aInstance, const otExtAddress *aExtAddress)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
otExtAddress address;
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
for (size_t i = 0; i < sizeof(address); i++)
{
@@ -93,8 +100,9 @@ exit:
otError otLinkRawSetShortAddress(otInstance *aInstance, uint16_t aShortAddress)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
otPlatRadioSetShortAddress(aInstance, aShortAddress);
@@ -110,8 +118,9 @@ bool otLinkRawGetPromiscuous(otInstance *aInstance)
otError otLinkRawSetPromiscuous(otInstance *aInstance, bool aEnable)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
otLogInfoPlat(aInstance, "LinkRaw Promiscuous=%d", aEnable ? 1 : 0);
@@ -124,8 +133,9 @@ exit:
otError otLinkRawSleep(otInstance *aInstance)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
otLogInfoPlat(aInstance, "LinkRaw Sleep");
@@ -137,15 +147,18 @@ exit:
otError otLinkRawReceive(otInstance *aInstance, uint8_t aChannel, otLinkRawReceiveDone aCallback)
{
+ Instance &instance = *static_cast(aInstance);
+
otLogInfoPlat(aInstance, "LinkRaw Recv (Channel %d)", aChannel);
- return aInstance->mLinkRaw.Receive(aChannel, aCallback);
+ return instance.GetLinkRaw().Receive(aChannel, aCallback);
}
otRadioFrame *otLinkRawGetTransmitBuffer(otInstance *aInstance)
{
otRadioFrame *buffer = NULL;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled());
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled());
buffer = otPlatRadioGetTransmitBuffer(aInstance);
@@ -155,8 +168,10 @@ exit:
otError otLinkRawTransmit(otInstance *aInstance, otRadioFrame *aFrame, otLinkRawTransmitDone aCallback)
{
+ Instance &instance = *static_cast(aInstance);
+
otLogInfoPlat(aInstance, "LinkRaw Transmit (%d bytes on channel %d)", aFrame->mLength, aFrame->mChannel);
- return aInstance->mLinkRaw.Transmit(aFrame, aCallback);
+ return instance.GetLinkRaw().Transmit(aFrame, aCallback);
}
int8_t otLinkRawGetRssi(otInstance *aInstance)
@@ -166,20 +181,25 @@ int8_t otLinkRawGetRssi(otInstance *aInstance)
otRadioCaps otLinkRawGetCaps(otInstance *aInstance)
{
- return aInstance->mLinkRaw.GetCaps();
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetLinkRaw().GetCaps();
}
otError otLinkRawEnergyScan(otInstance *aInstance, uint8_t aScanChannel, uint16_t aScanDuration,
otLinkRawEnergyScanDone aCallback)
{
- return aInstance->mLinkRaw.EnergyScan(aScanChannel, aScanDuration, aCallback);
+ Instance &instance = *static_cast(aInstance);
+
+ return instance.GetLinkRaw().EnergyScan(aScanChannel, aScanDuration, aCallback);
}
otError otLinkRawSrcMatchEnable(otInstance *aInstance, bool aEnable)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
otPlatRadioEnableSrcMatch(aInstance, aEnable);
@@ -190,8 +210,9 @@ exit:
otError otLinkRawSrcMatchAddShortEntry(otInstance *aInstance, const uint16_t aShortAddress)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
error = otPlatRadioAddSrcMatchShortEntry(aInstance, aShortAddress);
@@ -202,8 +223,9 @@ exit:
otError otLinkRawSrcMatchAddExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
error = otPlatRadioAddSrcMatchExtEntry(aInstance, aExtAddress);
@@ -214,8 +236,9 @@ exit:
otError otLinkRawSrcMatchClearShortEntry(otInstance *aInstance, const uint16_t aShortAddress)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
error = otPlatRadioClearSrcMatchShortEntry(aInstance, aShortAddress);
@@ -226,8 +249,9 @@ exit:
otError otLinkRawSrcMatchClearExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
error = otPlatRadioClearSrcMatchExtEntry(aInstance, aExtAddress);
@@ -238,8 +262,9 @@ exit:
otError otLinkRawSrcMatchClearShortEntries(otInstance *aInstance)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
otPlatRadioClearSrcMatchShortEntries(aInstance);
@@ -250,8 +275,9 @@ exit:
otError otLinkRawSrcMatchClearExtEntries(otInstance *aInstance)
{
otError error = OT_ERROR_NONE;
+ Instance &instance = *static_cast(aInstance);
- VerifyOrExit(aInstance->mLinkRaw.IsEnabled(), error = OT_ERROR_INVALID_STATE);
+ VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE);
otPlatRadioClearSrcMatchExtEntries(aInstance);
@@ -261,7 +287,7 @@ exit:
namespace ot {
-LinkRaw::LinkRaw(otInstance &aInstance):
+LinkRaw::LinkRaw(Instance &aInstance):
mInstance(aInstance),
mEnabled(false),
mReceiveChannel(OPENTHREAD_CONFIG_DEFAULT_CHANNEL),
@@ -607,7 +633,7 @@ LinkRaw &LinkRaw::GetOwner(const Context &aContext)
#if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
LinkRaw &link = *static_cast(aContext.GetContext());
#else
- LinkRaw &link = otGetInstance()->mLinkRaw;
+ LinkRaw &link = Instance::Get().GetLinkRaw();
OT_UNUSED_VARIABLE(aContext);
#endif
return link;
diff --git a/src/core/api/message_api.cpp b/src/core/api/message_api.cpp
index dda8d7ed0..258546a1d 100644
--- a/src/core/api/message_api.cpp
+++ b/src/core/api/message_api.cpp
@@ -35,7 +35,7 @@
#include
-#include "openthread-instance.h"
+#include "common/instance.hpp"
using namespace ot;
@@ -46,70 +46,70 @@ void otMessageFree(otMessage *aMessage)
uint16_t otMessageGetLength(otMessage *aMessage)
{
- Message *message = static_cast(aMessage);
- return message->GetLength();
+ Message &message = *static_cast(aMessage);
+ return message.GetLength();
}
otError otMessageSetLength(otMessage *aMessage, uint16_t aLength)
{
- Message *message = static_cast(aMessage);
- return message->SetLength(aLength);
+ Message &message = *static_cast(aMessage);
+ return message.SetLength(aLength);
}
uint16_t otMessageGetOffset(otMessage *aMessage)
{
- Message *message = static_cast(aMessage);
- return message->GetOffset();
+ Message &message = *static_cast(aMessage);
+ return message.GetOffset();
}
otError otMessageSetOffset(otMessage *aMessage, uint16_t aOffset)
{
- Message *message = static_cast(aMessage);
- return message->SetOffset(aOffset);
+ Message &message = *static_cast(aMessage);
+ return message.SetOffset(aOffset);
}
bool otMessageIsLinkSecurityEnabled(otMessage *aMessage)
{
- Message *message = static_cast(aMessage);
- return message->IsLinkSecurityEnabled();
+ Message &message = *static_cast(aMessage);
+ return message.IsLinkSecurityEnabled();
}
void otMessageSetDirectTransmission(otMessage *aMessage, bool aEnabled)
{
- Message *message = static_cast(aMessage);
+ Message &message = *static_cast(aMessage);
if (aEnabled)
{
- message->SetDirectTransmission();
+ message.SetDirectTransmission();
}
else
{
- message->ClearDirectTransmission();
+ message.ClearDirectTransmission();
}
}
int8_t otMessageGetRss(otMessage *aMessage)
{
- Message *message = static_cast(aMessage);
- return message->GetAverageRss();
+ Message &message = *static_cast(aMessage);
+ return message.GetAverageRss();
}
otError otMessageAppend(otMessage *aMessage, const void *aBuf, uint16_t aLength)
{
- Message *message = static_cast(aMessage);
- return message->Append(aBuf, aLength);
+ Message &message = *static_cast(aMessage);
+ return message.Append(aBuf, aLength);
}
int otMessageRead(otMessage *aMessage, uint16_t aOffset, void *aBuf, uint16_t aLength)
{
- Message *message = static_cast(aMessage);
- return message->Read(aOffset, aLength, aBuf);
+ Message &message = *static_cast(aMessage);
+ return message.Read(aOffset, aLength, aBuf);
}
int otMessageWrite(otMessage *aMessage, uint16_t aOffset, const void *aBuf, uint16_t aLength)
{
- Message *message = static_cast(aMessage);
- return message->Write(aOffset, aLength, aBuf);
+ Message &message = *static_cast(aMessage);
+ return message.Write(aOffset, aLength, aBuf);
}
void otMessageQueueInit(otMessageQueue *aQueue)
@@ -119,40 +119,44 @@ void otMessageQueueInit(otMessageQueue *aQueue)
otError otMessageQueueEnqueue(otMessageQueue *aQueue, otMessage *aMessage)
{
- Message *message = static_cast(aMessage);
- MessageQueue *queue = static_cast(aQueue);
- return queue->Enqueue(*message);
+ Message &message = *static_cast(aMessage);
+ MessageQueue &queue = *static_cast(aQueue);
+ return queue.Enqueue(message);
}
otError otMessageQueueEnqueueAtHead(otMessageQueue *aQueue, otMessage *aMessage)
{
- Message *message = static_cast(aMessage);
- MessageQueue *queue = static_cast(aQueue);
- return queue->Enqueue(*message, MessageQueue::kQueuePositionHead);
+ Message &message = *static_cast(aMessage);
+ MessageQueue &queue = *static_cast(aQueue);
+ return queue.Enqueue(message, MessageQueue::kQueuePositionHead);
}
otError otMessageQueueDequeue(otMessageQueue *aQueue, otMessage *aMessage)
{
- Message *message = static_cast(aMessage);
- MessageQueue *queue = static_cast(aQueue);
- return queue->Dequeue(*message);
+ Message &message = *static_cast