[instance] define ot::Instance class (#2307)

This commit makes the following changes: It defines the public
`otInstance` as an empty opaque structure which is used by all public
C OpenThread APIs. It defines a new class `ot::Instance` (inheriting
from `otInstance) which is then used in core source files. The
functionality related to the instance is also moved/added into the
newly added `Instance` class (as class/static or member methods).
This commit is contained in:
Abtin Keshavarzian
2017-11-13 08:27:57 -08:00
committed by Jonathan Hui
parent a394155372
commit 02c876ef62
174 changed files with 1903 additions and 1137 deletions
+127 -62
View File
@@ -38,7 +38,7 @@
#include <openthread/thread.h>
#include <openthread/platform/settings.h>
#include "openthread-instance.h"
#include "common/instance.hpp"
#include "common/logging.hpp"
#include "common/settings.hpp"
@@ -46,37 +46,44 @@ using namespace ot;
uint32_t otThreadGetChildTimeout(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMle().GetTimeout();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().GetTimeout();
}
void otThreadSetChildTimeout(otInstance *aInstance, uint32_t aTimeout)
{
aInstance->mThreadNetif.GetMle().SetTimeout(aTimeout);
Instance &instance = *static_cast<Instance *>(aInstance);
instance.GetThreadNetif().GetMle().SetTimeout(aTimeout);
}
const uint8_t *otThreadGetExtendedPanId(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMac().GetExtendedPanId();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMac().GetExtendedPanId();
}
otError otThreadSetExtendedPanId(otInstance *aInstance, const uint8_t *aExtendedPanId)
{
otError error = OT_ERROR_NONE;
Instance &instance = *static_cast<Instance *>(aInstance);
uint8_t mlPrefix[8];
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().SetExtendedPanId(aExtendedPanId);
instance.GetThreadNetif().GetMac().SetExtendedPanId(aExtendedPanId);
mlPrefix[0] = 0xfd;
memcpy(mlPrefix + 1, aExtendedPanId, 5);
mlPrefix[6] = 0x00;
mlPrefix[7] = 0x00;
aInstance->mThreadNetif.GetMle().SetMeshLocalPrefix(mlPrefix);
instance.GetThreadNetif().GetMle().SetMeshLocalPrefix(mlPrefix);
aInstance->mThreadNetif.GetActiveDataset().Clear();
aInstance->mThreadNetif.GetPendingDataset().Clear();
instance.GetThreadNetif().GetActiveDataset().Clear();
instance.GetThreadNetif().GetPendingDataset().Clear();
exit:
return error;
@@ -85,10 +92,11 @@ exit:
otError otThreadGetLeaderRloc(otInstance *aInstance, otIp6Address *aAddress)
{
otError error;
Instance &instance = *static_cast<Instance *>(aInstance);
VerifyOrExit(aAddress != NULL, error = OT_ERROR_INVALID_ARGS);
error = aInstance->mThreadNetif.GetMle().GetLeaderAddress(*static_cast<Ip6::Address *>(aAddress));
error = instance.GetThreadNetif().GetMle().GetLeaderAddress(*static_cast<Ip6::Address *>(aAddress));
exit:
return error;
@@ -97,7 +105,8 @@ exit:
otLinkModeConfig otThreadGetLinkMode(otInstance *aInstance)
{
otLinkModeConfig config;
uint8_t mode = aInstance->mThreadNetif.GetMle().GetDeviceMode();
Instance &instance = *static_cast<Instance *>(aInstance);
uint8_t mode = instance.GetThreadNetif().GetMle().GetDeviceMode();
memset(&config, 0, sizeof(otLinkModeConfig));
@@ -127,6 +136,7 @@ otLinkModeConfig otThreadGetLinkMode(otInstance *aInstance)
otError otThreadSetLinkMode(otInstance *aInstance, otLinkModeConfig aConfig)
{
uint8_t mode = 0;
Instance &instance = *static_cast<Instance *>(aInstance);
if (aConfig.mRxOnWhenIdle)
{
@@ -148,25 +158,28 @@ otError otThreadSetLinkMode(otInstance *aInstance, otLinkModeConfig aConfig)
mode |= Mle::ModeTlv::kModeFullNetworkData;
}
return aInstance->mThreadNetif.GetMle().SetDeviceMode(mode);
return instance.GetThreadNetif().GetMle().SetDeviceMode(mode);
}
const otMasterKey *otThreadGetMasterKey(otInstance *aInstance)
{
return &aInstance->mThreadNetif.GetKeyManager().GetMasterKey();
Instance &instance = *static_cast<Instance *>(aInstance);
return &instance.GetThreadNetif().GetKeyManager().GetMasterKey();
}
otError otThreadSetMasterKey(otInstance *aInstance, const otMasterKey *aKey)
{
otError error = OT_ERROR_NONE;
Instance &instance = *static_cast<Instance *>(aInstance);
VerifyOrExit(aKey != 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);
error = aInstance->mThreadNetif.GetKeyManager().SetMasterKey(*aKey);
aInstance->mThreadNetif.GetActiveDataset().Clear();
aInstance->mThreadNetif.GetPendingDataset().Clear();
error = instance.GetThreadNetif().GetKeyManager().SetMasterKey(*aKey);
instance.GetThreadNetif().GetActiveDataset().Clear();
instance.GetThreadNetif().GetPendingDataset().Clear();
exit:
return error;
@@ -174,24 +187,29 @@ exit:
const otIp6Address *otThreadGetMeshLocalEid(otInstance *aInstance)
{
return &aInstance->mThreadNetif.GetMle().GetMeshLocal64();
Instance &instance = *static_cast<Instance *>(aInstance);
return &instance.GetThreadNetif().GetMle().GetMeshLocal64();
}
const uint8_t *otThreadGetMeshLocalPrefix(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMle().GetMeshLocalPrefix();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().GetMeshLocalPrefix();
}
otError otThreadSetMeshLocalPrefix(otInstance *aInstance, const uint8_t *aMeshLocalPrefix)
{
otError error = OT_ERROR_NONE;
Instance &instance = *static_cast<Instance *>(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.GetMle().SetMeshLocalPrefix(aMeshLocalPrefix);
aInstance->mThreadNetif.GetActiveDataset().Clear();
aInstance->mThreadNetif.GetPendingDataset().Clear();
error = instance.GetThreadNetif().GetMle().SetMeshLocalPrefix(aMeshLocalPrefix);
instance.GetThreadNetif().GetActiveDataset().Clear();
instance.GetThreadNetif().GetPendingDataset().Clear();
exit:
return error;
@@ -199,24 +217,29 @@ exit:
const otIp6Address *otThreadGetLinkLocalIp6Address(otInstance *aInstance)
{
return &aInstance->mThreadNetif.GetMle().GetLinkLocalAddress();
Instance &instance = *static_cast<Instance *>(aInstance);
return &instance.GetThreadNetif().GetMle().GetLinkLocalAddress();
}
const char *otThreadGetNetworkName(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMac().GetNetworkName();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMac().GetNetworkName();
}
otError otThreadSetNetworkName(otInstance *aInstance, const char *aNetworkName)
{
otError error = OT_ERROR_NONE;
Instance &instance = *static_cast<Instance *>(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().SetNetworkName(aNetworkName);
aInstance->mThreadNetif.GetActiveDataset().Clear();
aInstance->mThreadNetif.GetPendingDataset().Clear();
error = instance.GetThreadNetif().GetMac().SetNetworkName(aNetworkName);
instance.GetThreadNetif().GetActiveDataset().Clear();
instance.GetThreadNetif().GetPendingDataset().Clear();
exit:
return error;
@@ -224,41 +247,54 @@ exit:
uint32_t otThreadGetKeySequenceCounter(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetKeyManager().GetCurrentKeySequence();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetKeyManager().GetCurrentKeySequence();
}
void otThreadSetKeySequenceCounter(otInstance *aInstance, uint32_t aKeySequenceCounter)
{
aInstance->mThreadNetif.GetKeyManager().SetCurrentKeySequence(aKeySequenceCounter);
Instance &instance = *static_cast<Instance *>(aInstance);
instance.GetThreadNetif().GetKeyManager().SetCurrentKeySequence(aKeySequenceCounter);
}
uint32_t otThreadGetKeySwitchGuardTime(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetKeyManager().GetKeySwitchGuardTime();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetKeyManager().GetKeySwitchGuardTime();
}
void otThreadSetKeySwitchGuardTime(otInstance *aInstance, uint32_t aKeySwitchGuardTime)
{
aInstance->mThreadNetif.GetKeyManager().SetKeySwitchGuardTime(aKeySwitchGuardTime);
Instance &instance = *static_cast<Instance *>(aInstance);
instance.GetThreadNetif().GetKeyManager().SetKeySwitchGuardTime(aKeySwitchGuardTime);
}
otError otThreadBecomeDetached(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMle().BecomeDetached();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().BecomeDetached();
}
otError otThreadBecomeChild(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMle().BecomeChild(Mle::kAttachAny);
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().BecomeChild(Mle::kAttachAny);
}
otError otThreadGetNextNeighborInfo(otInstance *aInstance, otNeighborInfoIterator *aIterator, otNeighborInfo *aInfo)
{
otError error = OT_ERROR_NONE;
Instance &instance = *static_cast<Instance *>(aInstance);
VerifyOrExit((aInfo != NULL) && (aIterator != NULL), error = OT_ERROR_INVALID_ARGS);
error = aInstance->mThreadNetif.GetMle().GetNextNeighborInfo(*aIterator, *aInfo);
error = instance.GetThreadNetif().GetMle().GetNextNeighborInfo(*aIterator, *aInfo);
exit:
return error;
@@ -266,16 +302,19 @@ exit:
otDeviceRole otThreadGetDeviceRole(otInstance *aInstance)
{
return static_cast<otDeviceRole>(aInstance->mThreadNetif.GetMle().GetRole());
Instance &instance = *static_cast<Instance *>(aInstance);
return static_cast<otDeviceRole>(instance.GetThreadNetif().GetMle().GetRole());
}
otError otThreadGetLeaderData(otInstance *aInstance, otLeaderData *aLeaderData)
{
otError error;
Instance &instance = *static_cast<Instance *>(aInstance);
VerifyOrExit(aLeaderData != NULL, error = OT_ERROR_INVALID_ARGS);
error = aInstance->mThreadNetif.GetMle().GetLeaderData(*aLeaderData);
error = instance.GetThreadNetif().GetMle().GetLeaderData(*aLeaderData);
exit:
return error;
@@ -283,32 +322,41 @@ exit:
uint8_t otThreadGetLeaderRouterId(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMle().GetLeaderDataTlv().GetLeaderRouterId();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().GetLeaderDataTlv().GetLeaderRouterId();
}
uint8_t otThreadGetLeaderWeight(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMle().GetLeaderDataTlv().GetWeighting();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().GetLeaderDataTlv().GetWeighting();
}
uint32_t otThreadGetPartitionId(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMle().GetLeaderDataTlv().GetPartitionId();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().GetLeaderDataTlv().GetPartitionId();
}
uint16_t otThreadGetRloc16(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMle().GetRloc16();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().GetRloc16();
}
otError otThreadGetParentInfo(otInstance *aInstance, otRouterInfo *aParentInfo)
{
otError error = OT_ERROR_NONE;
Instance &instance = *static_cast<Instance *>(aInstance);
Router *parent;
VerifyOrExit(aParentInfo != NULL, error = OT_ERROR_INVALID_ARGS);
parent = aInstance->mThreadNetif.GetMle().GetParent();
parent = instance.GetThreadNetif().GetMle().GetParent();
memcpy(aParentInfo->mExtAddress.m8, &parent->GetExtAddress(), sizeof(aParentInfo->mExtAddress));
aParentInfo->mRloc16 = parent->GetRloc16();
@@ -329,11 +377,12 @@ exit:
otError otThreadGetParentAverageRssi(otInstance *aInstance, int8_t *aParentRssi)
{
otError error = OT_ERROR_NONE;
Instance &instance = *static_cast<Instance *>(aInstance);
Router *parent;
VerifyOrExit(aParentRssi != NULL, error = OT_ERROR_INVALID_ARGS);
parent = aInstance->mThreadNetif.GetMle().GetParent();
parent = instance.GetThreadNetif().GetMle().GetParent();
*aParentRssi = parent->GetLinkInfo().GetAverageRss();
VerifyOrExit(*aParentRssi != OT_RADIO_RSSI_INVALID, error = OT_ERROR_FAILED);
@@ -345,11 +394,12 @@ exit:
otError otThreadGetParentLastRssi(otInstance *aInstance, int8_t *aLastRssi)
{
otError error = OT_ERROR_NONE;
Instance &instance = *static_cast<Instance *>(aInstance);
Router *parent;
VerifyOrExit(aLastRssi != NULL, error = OT_ERROR_INVALID_ARGS);
parent = aInstance->mThreadNetif.GetMle().GetParent();
parent = instance.GetThreadNetif().GetMle().GetParent();
*aLastRssi = parent->GetLinkInfo().GetLastRss();
VerifyOrExit(*aLastRssi != OT_RADIO_RSSI_INVALID, error = OT_ERROR_FAILED);
@@ -398,43 +448,50 @@ const char *otGetVersionString(void)
void otThreadSetReceiveDiagnosticGetCallback(otInstance *aInstance, otReceiveDiagnosticGetCallback aCallback,
void *aCallbackContext)
{
aInstance->mThreadNetif.GetNetworkDiagnostic().SetReceiveDiagnosticGetCallback(aCallback, aCallbackContext);
Instance &instance = *static_cast<Instance *>(aInstance);
instance.GetThreadNetif().GetNetworkDiagnostic().SetReceiveDiagnosticGetCallback(aCallback, aCallbackContext);
}
otError otThreadSendDiagnosticGet(otInstance *aInstance, const otIp6Address *aDestination,
const uint8_t aTlvTypes[], uint8_t aCount)
{
return aInstance->mThreadNetif.GetNetworkDiagnostic().SendDiagnosticGet(*static_cast<const Ip6::Address *>
(aDestination),
aTlvTypes,
aCount);
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetNetworkDiagnostic().SendDiagnosticGet(*static_cast<const Ip6::Address *>
(aDestination),
aTlvTypes,
aCount);
}
otError otThreadSendDiagnosticReset(otInstance *aInstance, const otIp6Address *aDestination,
const uint8_t aTlvTypes[], uint8_t aCount)
{
return aInstance->mThreadNetif.GetNetworkDiagnostic().SendDiagnosticReset(*static_cast<const Ip6::Address *>
(aDestination),
aTlvTypes,
aCount);
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetNetworkDiagnostic().SendDiagnosticReset(*static_cast<const Ip6::Address *>
(aDestination),
aTlvTypes,
aCount);
}
#endif // OPENTHREAD_FTD || OPENTHREAD_ENABLE_MTD_NETWORK_DIAGNOSTIC
otError otThreadSetEnabled(otInstance *aInstance, bool aEnabled)
{
otError error = OT_ERROR_NONE;
Instance &instance = *static_cast<Instance *>(aInstance);
otLogFuncEntry();
if (aEnabled)
{
VerifyOrExit(aInstance->mThreadNetif.GetMac().GetPanId() != Mac::kPanIdBroadcast,
VerifyOrExit(instance.GetThreadNetif().GetMac().GetPanId() != Mac::kPanIdBroadcast,
error = OT_ERROR_INVALID_STATE);
error = aInstance->mThreadNetif.GetMle().Start(true, false);
error = instance.GetThreadNetif().GetMle().Start(true, false);
}
else
{
error = aInstance->mThreadNetif.GetMle().Stop(true);
error = instance.GetThreadNetif().GetMle().Stop(true);
}
exit:
@@ -475,22 +532,30 @@ otError otThreadSetAutoStart(otInstance *aInstance, bool aStartAutomatically)
bool otThreadIsSingleton(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMle().IsSingleton();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().IsSingleton();
}
otError otThreadDiscover(otInstance *aInstance, uint32_t aScanChannels, uint16_t aPanId, bool aJoiner,
bool aEnableEui64Filtering, otHandleActiveScanResult aCallback, void *aCallbackContext)
{
return aInstance->mThreadNetif.GetMle().Discover(aScanChannels, aPanId, aJoiner, aEnableEui64Filtering, aCallback,
aCallbackContext);
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().Discover(aScanChannels, aPanId, aJoiner, aEnableEui64Filtering, aCallback,
aCallbackContext);
}
bool otThreadIsDiscoverInProgress(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMle().IsDiscoverInProgress();
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetMle().IsDiscoverInProgress();
}
const otIpCounters *otThreadGetIp6Counters(otInstance *aInstance)
{
return &aInstance->mThreadNetif.GetMeshForwarder().GetCounters();
Instance &instance = *static_cast<Instance *>(aInstance);
return &instance.GetThreadNetif().GetMeshForwarder().GetCounters();
}