From 79d99e7a91505aeba0c37b1c5e4115b891011d5c Mon Sep 17 00:00:00 2001 From: Nick Banks Date: Tue, 4 Oct 2016 15:23:25 -0700 Subject: [PATCH] Refactor OpenThread Global Variables into a Single Context Structure (#357) * Refactor OpenThread to contain all global/static variables in a single context structure. --- doc/spinel-protocol-src/Makefile | 1 - include/openthread-types.h | 20 + include/openthread.h | 3 + src/cli/cli.cpp | 11 +- src/core/Makefile.am | 1 + src/core/common/code_utils.hpp | 4 + src/core/common/message.cpp | 1 + src/core/common/tasklet.cpp | 8 +- src/core/common/tasklet.hpp | 10 + src/core/common/timer.cpp | 22 +- src/core/common/timer.hpp | 10 + src/core/mac/mac.cpp | 66 +- src/core/net/ip6.cpp | 6 + src/core/net/ip6.hpp | 18 + src/core/net/netif.hpp | 3 - src/core/openthread-instance.h | 92 +++ src/core/openthread.cpp | 719 ++++++++++---------- src/core/thread/address_resolver.cpp | 4 +- src/core/thread/meshcop_dataset_manager.cpp | 4 +- src/core/thread/mle.cpp | 2 +- src/core/thread/thread_netif.cpp | 6 + src/core/thread/thread_netif.hpp | 8 + src/ncp/ncp_base.cpp | 4 +- src/ncp/ncp_spi.cpp | 7 +- src/ncp/ncp_uart.cpp | 5 +- tests/unit/test_platform.cpp | 5 - tests/unit/test_timer.cpp | 39 +- 27 files changed, 619 insertions(+), 460 deletions(-) create mode 100644 src/core/openthread-instance.h diff --git a/doc/spinel-protocol-src/Makefile b/doc/spinel-protocol-src/Makefile index 6c5c4174a..59c89a01e 100644 --- a/doc/spinel-protocol-src/Makefile +++ b/doc/spinel-protocol-src/Makefile @@ -89,4 +89,3 @@ draft-spinel-protocol-bis.xml: \ spinel-tech-thread.md \ spinel-test-vectors.md \ $(NULL) - diff --git a/include/openthread-types.h b/include/openthread-types.h index 4595a0eac..a2733da6b 100644 --- a/include/openthread-types.h +++ b/include/openthread-types.h @@ -44,6 +44,26 @@ extern "C" { #endif +#ifdef _WIN32 +#ifdef WINDOWS_KERNEL +#include +#else +#include +#endif +#else +#ifndef CONTAINING_RECORD +/*#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Winvalid-offsetof" +#define CONTAINING_RECORD(address, type, field) \ + ((type *)((uint8_t*)(address) - offsetof(type, field))) +#pragma GCC diagnostic pop*/ +#define BASE 0x1 +#define myoffsetof(s,m) (((size_t)&(((s*)BASE)->m))-BASE) +#define CONTAINING_RECORD(address, type, field) \ + ((type *)((uint8_t*)(address) - myoffsetof(type, field))) +#endif +#endif + /** * This type represents the OpenThread instance structure. */ diff --git a/include/openthread.h b/include/openthread.h index 23e41cfec..4b5328cda 100644 --- a/include/openthread.h +++ b/include/openthread.h @@ -1621,6 +1621,8 @@ void otPlatformReset(otInstance *aInstance); /** * Get the ROUTER_DOWNGRADE_THRESHOLD parameter used in the Router role. * + * @param[in] aInstance A pointer to an OpenThread instance. + * * @returns The ROUTER_DOWNGRADE_THRESHOLD value. * * @sa otSetRouterDowngradeThreshold @@ -1630,6 +1632,7 @@ uint8_t otGetRouterDowngradeThreshold(otInstance *aInstance); /** * Set the ROUTER_DOWNGRADE_THRESHOLD parameter used in the Leader role. * + * @param[in] aInstance A pointer to an OpenThread instance. * @param[in] aThreshold The ROUTER_DOWNGRADE_THRESHOLD value. * * @sa otGetRouterDowngradeThreshold diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 0f9d964e3..1d87b951e 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -42,6 +42,7 @@ #include #include +#include #include #include #include @@ -59,8 +60,6 @@ using Thread::Encoding::BigEndian::HostSwap32; namespace Thread { -extern Ip6::Ip6 *sIp6; - namespace Cli { const struct Command Interpreter::sCommands[] = @@ -133,10 +132,10 @@ Interpreter::Interpreter(otInstance *aInstance): sLength(8), sCount(1), sInterval(1000), - sPingTimer(sIp6->mTimerScheduler, &Interpreter::s_HandlePingTimer, this), + sPingTimer(aInstance->mIp6.mTimerScheduler, &Interpreter::s_HandlePingTimer, this), mInstance(aInstance) { - sIp6->mIcmp.SetEchoReplyHandler(&s_HandleEchoResponse, this); + mInstance->mIp6.mIcmp.SetEchoReplyHandler(&s_HandleEchoResponse, this); otSetStateChangedCallback(mInstance, &Interpreter::s_HandleNetifStateChanged, this); } @@ -1142,11 +1141,11 @@ void Interpreter::HandlePingTimer() uint32_t timestamp = HostSwap32(Timer::GetNow()); Message *message; - VerifyOrExit((message = sIp6->mIcmp.NewMessage(0)) != NULL, error = kThreadError_NoBufs); + VerifyOrExit((message = mInstance->mIp6.mIcmp.NewMessage(0)) != NULL, error = kThreadError_NoBufs); SuccessOrExit(error = message->Append(×tamp, sizeof(timestamp))); SuccessOrExit(error = message->SetLength(sLength)); - SuccessOrExit(error = sIp6->mIcmp.SendEchoRequest(*message, sMessageInfo)); + SuccessOrExit(error = mInstance->mIp6.mIcmp.SendEchoRequest(*message, sMessageInfo)); sCount--; exit: diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 655a61c8d..196a61b37 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -111,6 +111,7 @@ endif # OPENTHREAD_ENABLE_DTLS noinst_HEADERS = \ openthread-core-config.h \ openthread-core-default-config.h \ + openthread-instance.h \ coap/coap_header.hpp \ coap/coap_server.hpp \ common/code_utils.hpp \ diff --git a/src/core/common/code_utils.hpp b/src/core/common/code_utils.hpp index 0cf3f7b11..a4f182d97 100644 --- a/src/core/common/code_utils.hpp +++ b/src/core/common/code_utils.hpp @@ -36,6 +36,10 @@ #include +// Calculates the aligned variable size. +#define otALIGNED_VAR_SIZE(size, align_type) \ + (((size) + (sizeof (align_type) - 1)) / sizeof (align_type)) + // Allocate the structure using "raw" storage. #define otDEFINE_ALIGNED_VAR(name, size, align_type) \ align_type name[(((size) + (sizeof (align_type) - 1)) / sizeof (align_type))] diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index 02b8f0caf..167cbd896 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -35,6 +35,7 @@ #include #include +#include #include #include #include diff --git a/src/core/common/tasklet.cpp b/src/core/common/tasklet.cpp index dbcdf6314..6eb2efaa1 100644 --- a/src/core/common/tasklet.cpp +++ b/src/core/common/tasklet.cpp @@ -35,6 +35,7 @@ #include #include #include +#include namespace Thread { @@ -67,7 +68,7 @@ ThreadError TaskletScheduler::Post(Tasklet &aTasklet) { mHead = &aTasklet; mTail = &aTasklet; - otSignalTaskletPending(NULL); + otSignalTaskletPending(aTasklet.mScheduler.GetIp6()->GetInstance()); } else { @@ -120,4 +121,9 @@ void TaskletScheduler::ProcessQueuedTasklets(void) } } +Ip6::Ip6 *TaskletScheduler::GetIp6() +{ + return Ip6::Ip6FromTaskletScheduler(this); +} + } // namespace Thread diff --git a/src/core/common/tasklet.hpp b/src/core/common/tasklet.hpp index c5cd44412..65bfbc266 100644 --- a/src/core/common/tasklet.hpp +++ b/src/core/common/tasklet.hpp @@ -38,6 +38,8 @@ namespace Thread { +namespace Ip6 { class Ip6; } + class TaskletScheduler; /** @@ -130,6 +132,14 @@ public: */ void ProcessQueuedTasklets(void); + /** + * This method returns the pointer to the parent Ip6 structure. + * + * @returns The pointer to the parent Ip6 structure. + * + */ + Ip6::Ip6 *GetIp6(); + private: Tasklet *PopTasklet(void); Tasklet *mHead; diff --git a/src/core/common/timer.cpp b/src/core/common/timer.cpp index 22b9f8463..3b4bee039 100644 --- a/src/core/common/timer.cpp +++ b/src/core/common/timer.cpp @@ -33,18 +33,17 @@ #include #include +#include +#include #include #include +#include namespace Thread { -// FIXME: the otPlatAlarm callback should provide the context -static TimerScheduler *sTimerScheduler; - TimerScheduler::TimerScheduler(void): mHead(NULL) { - sTimerScheduler = this; } void TimerScheduler::Add(Timer &aTimer) @@ -136,23 +135,23 @@ void TimerScheduler::SetAlarm(void) if (mHead == NULL) { - otPlatAlarmStop(NULL); + otPlatAlarmStop(GetIp6()->GetInstance()); } else { elapsed = now - mHead->mT0; remaining = (mHead->mDt > elapsed) ? mHead->mDt - elapsed : 0; - otPlatAlarmStartAt(NULL, now, remaining); + otPlatAlarmStartAt(GetIp6()->GetInstance(), now, remaining); } } -extern "C" void otPlatAlarmFired(otInstance *) +extern "C" void otPlatAlarmFired(otInstance *aInstance) { - sTimerScheduler->FireTimers(); + aInstance->mIp6.mTimerScheduler.FireTimers(); } -void TimerScheduler::FireTimers(void) +void TimerScheduler::FireTimers() { uint32_t now = otPlatAlarmGetNow(); uint32_t elapsed; @@ -178,6 +177,11 @@ void TimerScheduler::FireTimers(void) } } +Ip6::Ip6 *TimerScheduler::GetIp6() +{ + return Ip6::Ip6FromTimerScheduler(this); +} + bool TimerScheduler::TimerCompare(const Timer &aTimerA, const Timer &aTimerB) { uint32_t now = otPlatAlarmGetNow(); diff --git a/src/core/common/timer.hpp b/src/core/common/timer.hpp index 0c19f1831..ddf1e0658 100644 --- a/src/core/common/timer.hpp +++ b/src/core/common/timer.hpp @@ -43,6 +43,8 @@ namespace Thread { +namespace Ip6 { class Ip6; } + class Timer; /** @@ -103,6 +105,14 @@ public: */ void FireTimers(void); + /** + * This method returns the pointer to the parent Ip6 structure. + * + * @returns The pointer to the parent Ip6 structure. + * + */ + Ip6::Ip6 *GetIp6(); + private: void SetAlarm(void); diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 6aae0751a..ad5482c39 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -51,6 +51,7 @@ #include #include #include +#include namespace Thread { namespace Mac { @@ -67,7 +68,6 @@ static const otExtAddress sMode2ExtAddress = static const uint8_t sExtendedPanidInit[] = {0xde, 0xad, 0x00, 0xbe, 0xef, 0x00, 0xca, 0xfe}; static const char sNetworkNameInit[] = "OpenThread"; -static Mac *sMac; void Mac::StartCsmaBackoff(void) { @@ -96,8 +96,6 @@ Mac::Mac(ThreadNetif &aThreadNetif): mWhitelist(), mBlacklist() { - sMac = this; - mState = kStateIdle; mRxOnWhenIdle = false; @@ -145,7 +143,7 @@ Mac::Mac(ThreadNetif &aThreadNetif): mPcapCallback = NULL; mPcapCallbackContext = NULL; - otPlatRadioEnable(NULL); + otPlatRadioEnable(mNetif.GetInstance()); } ThreadError Mac::ActiveScan(uint32_t aScanChannels, uint16_t aScanDuration, ActiveScanHandler aHandler, void *aContext) @@ -241,7 +239,7 @@ void Mac::HandleEnergyScanSampleRssi(void) VerifyOrExit(mState == kStateEnergyScan, ;); - rssi = otPlatRadioGetRssi(NULL); + rssi = otPlatRadioGetRssi(mNetif.GetInstance()); if (rssi != kInvalidRssiValue) { @@ -307,7 +305,7 @@ ThreadError Mac::SetExtAddress(const ExtAddress &aExtAddress) buf[i] = aExtAddress.m8[7 - i]; } - SuccessOrExit(error = otPlatRadioSetExtendedAddress(NULL, buf)); + SuccessOrExit(error = otPlatRadioSetExtendedAddress(mNetif.GetInstance(), buf)); mExtAddress = aExtAddress; exit: @@ -319,7 +317,7 @@ void Mac::GetHashMacAddress(ExtAddress *aHashMacAddress) Crypto::Sha256 sha256; uint8_t buf[Crypto::Sha256::kHashSize]; - otPlatRadioGetIeeeEui64(NULL, buf); + otPlatRadioGetIeeeEui64(mNetif.GetInstance(), buf); sha256.Start(); sha256.Update(buf, OT_EXT_ADDRESS_SIZE); sha256.Finish(buf); @@ -336,7 +334,7 @@ ShortAddress Mac::GetShortAddress(void) const ThreadError Mac::SetShortAddress(ShortAddress aShortAddress) { mShortAddress = aShortAddress; - return otPlatRadioSetShortAddress(NULL, aShortAddress); + return otPlatRadioSetShortAddress(mNetif.GetInstance(), aShortAddress); } uint8_t Mac::GetChannel(void) const @@ -392,7 +390,7 @@ PanId Mac::GetPanId(void) const ThreadError Mac::SetPanId(PanId aPanId) { mPanId = aPanId; - return otPlatRadioSetPanId(NULL, mPanId); + return otPlatRadioSetPanId(mNetif.GetInstance(), mPanId); } const uint8_t *Mac::GetExtendedPanId(void) const @@ -439,17 +437,17 @@ void Mac::NextOperation(void) { case kStateActiveScan: case kStateEnergyScan: - otPlatRadioReceive(NULL, mScanChannel); + otPlatRadioReceive(mNetif.GetInstance(), mScanChannel); break; default: - if (mRxOnWhenIdle || mReceiveTimer.IsRunning() || otPlatRadioGetPromiscuous(NULL)) + if (mRxOnWhenIdle || mReceiveTimer.IsRunning() || otPlatRadioGetPromiscuous(mNetif.GetInstance())) { - otPlatRadioReceive(NULL, mChannel); + otPlatRadioReceive(mNetif.GetInstance(), mChannel); } else { - otPlatRadioSleep(NULL); + otPlatRadioSleep(mNetif.GetInstance()); } break; @@ -628,7 +626,7 @@ exit: void Mac::HandleBeginTransmit(void) { - Frame &sendFrame(*static_cast(otPlatRadioGetTransmitBuffer(NULL))); + Frame &sendFrame(*static_cast(otPlatRadioGetTransmitBuffer(mNetif.GetInstance()))); ThreadError error = kThreadError_None; sendFrame.SetPower(mMaxTransmitPower); @@ -636,7 +634,7 @@ void Mac::HandleBeginTransmit(void) switch (mState) { case kStateActiveScan: - otPlatRadioSetPanId(NULL, kPanIdBroadcast); + otPlatRadioSetPanId(mNetif.GetInstance(), kPanIdBroadcast); sendFrame.SetChannel(mScanChannel); SendBeaconRequest(sendFrame); sendFrame.SetSequence(0); @@ -667,12 +665,12 @@ void Mac::HandleBeginTransmit(void) sendFrame.SetPower(mMaxTransmitPower); } - error = otPlatRadioReceive(NULL, sendFrame.GetChannel()); + error = otPlatRadioReceive(mNetif.GetInstance(), sendFrame.GetChannel()); assert(error == kThreadError_None); - error = otPlatRadioTransmit(NULL); + error = otPlatRadioTransmit(mNetif.GetInstance()); assert(error == kThreadError_None); - if (sendFrame.GetAckRequest() && !(otPlatRadioGetCaps(NULL) & kRadioCapsAckTimeout)) + if (sendFrame.GetAckRequest() && !(otPlatRadioGetCaps(mNetif.GetInstance()) & kRadioCapsAckTimeout)) { mMacTimer.Start(kAckTimeout); otLogDebgMac("ack timer start\n"); @@ -692,9 +690,9 @@ exit: } } -extern "C" void otPlatRadioTransmitDone(otInstance *, bool aRxPending, ThreadError aError) +extern "C" void otPlatRadioTransmitDone(otInstance *aInstance, bool aRxPending, ThreadError aError) { - sMac->TransmitDoneTask(aRxPending, aError); + aInstance->mThreadNetif.GetMac().TransmitDoneTask(aRxPending, aError); } void Mac::TransmitDoneTask(bool aRxPending, ThreadError aError) @@ -747,7 +745,7 @@ void Mac::HandleMacTimer(void *aContext) void Mac::HandleMacTimer(void) { - otPlatRadioReceive(NULL, mChannel); + otPlatRadioReceive(mNetif.GetInstance(), mChannel); switch (mState) { @@ -759,7 +757,7 @@ void Mac::HandleMacTimer(void) if (mScanChannels == 0 || mScanChannel > kPhyMaxChannel) { - otPlatRadioSetPanId(NULL, mPanId); + otPlatRadioSetPanId(mNetif.GetInstance(), mPanId); mActiveScanHandler(mScanContext, NULL); ScheduleNextTransmission(); ExitNow(); @@ -830,7 +828,7 @@ void Mac::HandleReceiveTimer(void) void Mac::SentFrame(ThreadError aError) { - Frame &sendFrame(*static_cast(otPlatRadioGetTransmitBuffer(NULL))); + Frame &sendFrame(*static_cast(otPlatRadioGetTransmitBuffer(mNetif.GetInstance()))); Sender *sender; switch (aError) @@ -1048,9 +1046,9 @@ exit: return error; } -extern "C" void otPlatRadioReceiveDone(otInstance *, RadioPacket *aFrame, ThreadError aError) +extern "C" void otPlatRadioReceiveDone(otInstance *aInstance, RadioPacket *aFrame, ThreadError aError) { - sMac->ReceiveDoneTask(static_cast(aFrame), aError); + aInstance->mThreadNetif.GetMac().ReceiveDoneTask(static_cast(aFrame), aError); } void Mac::ReceiveDoneTask(Frame *aFrame, ThreadError aError) @@ -1298,12 +1296,12 @@ void Mac::SetPcapCallback(otLinkPcapCallback aPcapCallback, void *aCallbackConte bool Mac::IsPromiscuous(void) { - return otPlatRadioGetPromiscuous(NULL); + return otPlatRadioGetPromiscuous(mNetif.GetInstance()); } void Mac::SetPromiscuous(bool aPromiscuous) { - otPlatRadioSetPromiscuous(NULL, aPromiscuous); + otPlatRadioSetPromiscuous(mNetif.GetInstance(), aPromiscuous); if (mState == kStateIdle) { @@ -1328,7 +1326,7 @@ otMacCounters &Mac::GetCounters(void) void Mac::EnableSrcMatch(bool aEnable) { - otPlatRadioEnableSrcMatch(NULL, aEnable); + otPlatRadioEnableSrcMatch(mNetif.GetInstance(), aEnable); otLogDebgMac("Enable SrcMatch -- %d(0:Dis, 1:En)\n", aEnable); } @@ -1338,7 +1336,7 @@ ThreadError Mac::AddSrcMatchEntry(Address &aAddr) if (aAddr.mLength == 2) { - error = otPlatRadioAddSrcMatchShortEntry(NULL, aAddr.mShortAddress); + error = otPlatRadioAddSrcMatchShortEntry(mNetif.GetInstance(), aAddr.mShortAddress); otLogDebgMac("Adding short address: 0x%x -- %d (0:Ok, 3:NoBufs)\n", aAddr.mShortAddress, error); } else @@ -1350,7 +1348,7 @@ ThreadError Mac::AddSrcMatchEntry(Address &aAddr) buf[i] = aAddr.mExtAddress.m8[7 - i]; } - error = otPlatRadioAddSrcMatchExtEntry(NULL, buf); + error = otPlatRadioAddSrcMatchExtEntry(mNetif.GetInstance(), buf); otLogDebgMac("Adding extended address: 0x%02x%02x%02x%02x%02x%02x%02x%02x -- %d (0:OK, 3:NoBufs)\n", buf[7], buf[6], buf[5], buf[4], buf[3], buf[2], buf[1], buf[0], error); } @@ -1364,7 +1362,7 @@ ThreadError Mac::ClearSrcMatchEntry(Address &aAddr) if (aAddr.mLength == 2) { - error = otPlatRadioClearSrcMatchShortEntry(NULL, aAddr.mShortAddress); + error = otPlatRadioClearSrcMatchShortEntry(mNetif.GetInstance(), aAddr.mShortAddress); otLogDebgMac("Clearing short address: 0x%x -- %d (0:OK, 10:NoAddress)\n", aAddr.mShortAddress, error); } else @@ -1376,7 +1374,7 @@ ThreadError Mac::ClearSrcMatchEntry(Address &aAddr) buf[i] = aAddr.mExtAddress.m8[7 - i]; } - error = otPlatRadioClearSrcMatchExtEntry(NULL, buf); + error = otPlatRadioClearSrcMatchExtEntry(mNetif.GetInstance(), buf); otLogDebgMac("Clearing extended address: 0x%02x%02x%02x%02x%02x%02x%02x%02x -- %d (0:OK, 10:NoAddress)\n", buf[7], buf[6], buf[5], buf[4], buf[3], buf[2], buf[1], buf[0], error); } @@ -1386,8 +1384,8 @@ ThreadError Mac::ClearSrcMatchEntry(Address &aAddr) void Mac::ClearSrcMatchEntries() { - otPlatRadioClearSrcMatchShortEntries(NULL); - otPlatRadioClearSrcMatchExtEntries(NULL); + otPlatRadioClearSrcMatchShortEntries(mNetif.GetInstance()); + otPlatRadioClearSrcMatchExtEntries(mNetif.GetInstance()); otLogDebgMac("Clearing source match table"); } diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 87cb61f7b..ecc1c4d54 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -43,6 +43,7 @@ #include #include #include +#include namespace Thread { namespace Ip6 { @@ -831,5 +832,10 @@ exit: return rval; } +otInstance *Ip6::GetInstance() +{ + return otInstanceFromIp6(this); +} + } // namespace Ip6 } // namespace Thread diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index d99f1612a..d7433442d 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -323,6 +323,14 @@ public: */ int8_t GetOnLinkNetif(const Address &aAddress); + /** + * This method returns the pointer to the parent otInstance structure. + * + * @returns The pointer to the parent otInstance structure. + * + */ + otInstance *GetInstance(); + Routes mRoutes; Icmp mIcmp; Udp mUdp; @@ -357,6 +365,16 @@ private: int8_t mNextInterfaceId; }; +static inline Ip6 *Ip6FromTaskletScheduler(TaskletScheduler *aTaskletScheduler) +{ + return (Ip6 *)CONTAINING_RECORD(aTaskletScheduler, Ip6, mTaskletScheduler); +} + +static inline Ip6 *Ip6FromTimerScheduler(TimerScheduler *aTimerScheduler) +{ + return (Ip6 *)CONTAINING_RECORD(aTimerScheduler, Ip6, mTimerScheduler); +} + /** * @} * diff --git a/src/core/net/netif.hpp b/src/core/net/netif.hpp index 1624ff2ca..468cfcbed 100644 --- a/src/core/net/netif.hpp +++ b/src/core/net/netif.hpp @@ -427,9 +427,6 @@ private: NetifUnicastAddress mExtUnicastAddresses[OPENTHREAD_CONFIG_MAX_EXT_IP_ADDRS]; uint8_t mMaskExtUnicastAddresses; // Must have enough bits to hold OPENTHREAD_CONFIG_MAX_EXT_IP_ADDRS - static Netif *sNetifListHead; - static int8_t sNextInterfaceId; - /** * This method determines if an address is one of the external unicast addresses, and if so returns * the index in the mExtUnicastAddresses array. diff --git a/src/core/openthread-instance.h b/src/core/openthread-instance.h new file mode 100644 index 000000000..abc0e9972 --- /dev/null +++ b/src/core/openthread-instance.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2016, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * @brief + * This file defines the structure of the variables required for all instances of OpenThread API. + */ + +#ifndef OPENTHREADINSTANCE_H_ +#define OPENTHREADINSTANCE_H_ + +#include +#include + +#include +#include +#include +#include + +/** + * This type represents all the static / global variables used by OpenThread allocated in one place. + */ +typedef struct otInstance +{ + // + // Callbacks + // + + Thread::Ip6::NetifCallback mNetifCallback; + + otReceiveIp6DatagramCallback mReceiveIp6DatagramCallback; + void *mReceiveIp6DatagramCallbackContext; + + otHandleActiveScanResult mActiveScanCallback; + void *mActiveScanCallbackContext; + + otHandleEnergyScanResult mEnergyScanCallback; + void *mEnergyScanCallbackContext; + + otHandleActiveScanResult mDiscoverCallback; + void *mDiscoverCallbackContext; + + // + // State + // + + Thread::Crypto::MbedTls mMbedTls; + Thread::Ip6::Ip6 mIp6; + Thread::ThreadNetif mThreadNetif; + + // Constructor + otInstance(void); + +} otInstance; + +static inline otInstance *otInstanceFromIp6(Thread::Ip6::Ip6 *aIp6) +{ + return (otInstance *)CONTAINING_RECORD(aIp6, otInstance, mIp6); +} + +static inline otInstance *otInstanceFromThreadNetif(Thread::ThreadNetif *aThreadNetif) +{ + return (otInstance *)CONTAINING_RECORD(aThreadNetif, otInstance, mThreadNetif); +} + +#endif // OPENTHREADINSTANCE_H_ diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index 9dc05f5e3..998216c54 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -56,128 +56,115 @@ #include #include #include - -// Temporary definition -typedef struct otInstance -{ -} otInstance; - -namespace Thread { - -// This needs to not be static until the NCP -// the OpenThread API is capable enough for -// of of the features in the NCP. -ThreadNetif *sThreadNetif; +#include #ifndef OPENTHREAD_MULTIPLE_INSTANCE static otDEFINE_ALIGNED_VAR(sInstanceRaw, sizeof(otInstance), uint64_t); otInstance *sInstance = NULL; #endif -static Ip6::NetifCallback sNetifCallback; +otInstance::otInstance(void) : + mReceiveIp6DatagramCallback(NULL), + mReceiveIp6DatagramCallbackContext(NULL), + mActiveScanCallback(NULL), + mActiveScanCallbackContext(NULL), + mDiscoverCallback(NULL), + mDiscoverCallbackContext(NULL), + mMbedTls(), + mIp6(), + mThreadNetif(mIp6) +{ +} -static otDEFINE_ALIGNED_VAR(sMbedTlsRaw, sizeof(Crypto::MbedTls), uint64_t); - -static otDEFINE_ALIGNED_VAR(sIp6Raw, sizeof(Ip6::Ip6), uint64_t); -Ip6::Ip6 *sIp6; +namespace Thread { #ifdef __cplusplus extern "C" { #endif -static otDEFINE_ALIGNED_VAR(sThreadNetifRaw, sizeof(ThreadNetif), uint64_t); - static void HandleActiveScanResult(void *aContext, Mac::Frame *aFrame); static void HandleEnergyScanResult(void *aContext, otEnergyScanResult *aResult); static void HandleMleDiscover(otActiveScanResult *aResult, void *aContext); -static otHandleActiveScanResult sActiveScanCallback = NULL; -static void *sActiveScanCallbackContext = NULL; - -static otHandleEnergyScanResult sEnergyScanCallback = NULL; -static void *sEnergyScanCallbackContext = NULL; - -static otHandleActiveScanResult sDiscoverCallback = NULL; -static void *sDiscoverCallbackContext = NULL; - -void otProcessQueuedTasklets(otInstance *) +void otProcessQueuedTasklets(otInstance *aInstance) { - sIp6->mTaskletScheduler.ProcessQueuedTasklets(); + aInstance->mIp6.mTaskletScheduler.ProcessQueuedTasklets(); } -bool otAreTaskletsPending(otInstance *) +bool otAreTaskletsPending(otInstance *aInstance) { - return sIp6->mTaskletScheduler.AreTaskletsPending(); + return aInstance->mIp6.mTaskletScheduler.AreTaskletsPending(); } -uint8_t otGetChannel(otInstance *) +uint8_t otGetChannel(otInstance *aInstance) { - return sThreadNetif->GetMac().GetChannel(); + return aInstance->mThreadNetif.GetMac().GetChannel(); } -ThreadError otSetChannel(otInstance *, uint8_t aChannel) +ThreadError otSetChannel(otInstance *aInstance, uint8_t aChannel) { - return sThreadNetif->GetMac().SetChannel(aChannel); + return aInstance->mThreadNetif.GetMac().SetChannel(aChannel); } -uint8_t otGetMaxAllowedChildren(otInstance *) +uint8_t otGetMaxAllowedChildren(otInstance *aInstance) { uint8_t aNumChildren; - (void)sThreadNetif->GetMle().GetChildren(&aNumChildren); + (void)aInstance->mThreadNetif.GetMle().GetChildren(&aNumChildren); return aNumChildren; } -ThreadError otSetMaxAllowedChildren(otInstance *, uint8_t aMaxChildren) +ThreadError otSetMaxAllowedChildren(otInstance *aInstance, uint8_t aMaxChildren) { - return sThreadNetif->GetMle().SetMaxAllowedChildren(aMaxChildren); + return aInstance->mThreadNetif.GetMle().SetMaxAllowedChildren(aMaxChildren); } -uint32_t otGetChildTimeout(otInstance *) +uint32_t otGetChildTimeout(otInstance *aInstance) { - return sThreadNetif->GetMle().GetTimeout(); + return aInstance->mThreadNetif.GetMle().GetTimeout(); } -void otSetChildTimeout(otInstance *, uint32_t aTimeout) +void otSetChildTimeout(otInstance *aInstance, uint32_t aTimeout) { - sThreadNetif->GetMle().SetTimeout(aTimeout); + aInstance->mThreadNetif.GetMle().SetTimeout(aTimeout); } -const uint8_t *otGetExtendedAddress(otInstance *) +const uint8_t *otGetExtendedAddress(otInstance *aInstance) { - return reinterpret_cast(sThreadNetif->GetMac().GetExtAddress()); + return reinterpret_cast(aInstance->mThreadNetif.GetMac().GetExtAddress()); } -ThreadError otSetExtendedAddress(otInstance *, const otExtAddress *aExtAddress) +ThreadError otSetExtendedAddress(otInstance *aInstance, const otExtAddress *aExtAddress) { ThreadError error = kThreadError_None; VerifyOrExit(aExtAddress != NULL, error = kThreadError_InvalidArgs); - SuccessOrExit(error = sThreadNetif->GetMac().SetExtAddress(*static_cast(aExtAddress))); - SuccessOrExit(error = sThreadNetif->GetMle().UpdateLinkLocalAddress()); + SuccessOrExit(error = aInstance->mThreadNetif.GetMac().SetExtAddress(*static_cast + (aExtAddress))); + SuccessOrExit(error = aInstance->mThreadNetif.GetMle().UpdateLinkLocalAddress()); exit: return error; } -const uint8_t *otGetExtendedPanId(otInstance *) +const uint8_t *otGetExtendedPanId(otInstance *aInstance) { - return sThreadNetif->GetMac().GetExtendedPanId(); + return aInstance->mThreadNetif.GetMac().GetExtendedPanId(); } -void otSetExtendedPanId(otInstance *, const uint8_t *aExtendedPanId) +void otSetExtendedPanId(otInstance *aInstance, const uint8_t *aExtendedPanId) { uint8_t mlPrefix[8]; - sThreadNetif->GetMac().SetExtendedPanId(aExtendedPanId); + aInstance->mThreadNetif.GetMac().SetExtendedPanId(aExtendedPanId); mlPrefix[0] = 0xfd; memcpy(mlPrefix + 1, aExtendedPanId, 5); mlPrefix[6] = 0x00; mlPrefix[7] = 0x00; - sThreadNetif->GetMle().SetMeshLocalPrefix(mlPrefix); + aInstance->mThreadNetif.GetMle().SetMeshLocalPrefix(mlPrefix); } void otGetFactoryAssignedIeeeEui64(otInstance *aInstance, otExtAddress *aEui64) @@ -185,27 +172,27 @@ void otGetFactoryAssignedIeeeEui64(otInstance *aInstance, otExtAddress *aEui64) otPlatRadioGetIeeeEui64(aInstance, aEui64->m8); } -void otGetHashMacAddress(otInstance *, otExtAddress *aHashMacAddress) +void otGetHashMacAddress(otInstance *aInstance, otExtAddress *aHashMacAddress) { - sThreadNetif->GetMac().GetHashMacAddress(static_cast(aHashMacAddress)); + aInstance->mThreadNetif.GetMac().GetHashMacAddress(static_cast(aHashMacAddress)); } -ThreadError otGetLeaderRloc(otInstance *, otIp6Address *aAddress) +ThreadError otGetLeaderRloc(otInstance *aInstance, otIp6Address *aAddress) { ThreadError error; VerifyOrExit(aAddress != NULL, error = kThreadError_InvalidArgs); - error = sThreadNetif->GetMle().GetLeaderAddress(*static_cast(aAddress)); + error = aInstance->mThreadNetif.GetMle().GetLeaderAddress(*static_cast(aAddress)); exit: return error; } -otLinkModeConfig otGetLinkMode(otInstance *) +otLinkModeConfig otGetLinkMode(otInstance *aInstance) { otLinkModeConfig config; - uint8_t mode = sThreadNetif->GetMle().GetDeviceMode(); + uint8_t mode = aInstance->mThreadNetif.GetMle().GetDeviceMode(); memset(&config, 0, sizeof(otLinkModeConfig)); @@ -232,7 +219,7 @@ otLinkModeConfig otGetLinkMode(otInstance *) return config; } -ThreadError otSetLinkMode(otInstance *, otLinkModeConfig aConfig) +ThreadError otSetLinkMode(otInstance *aInstance, otLinkModeConfig aConfig) { uint8_t mode = 0; @@ -256,144 +243,144 @@ ThreadError otSetLinkMode(otInstance *, otLinkModeConfig aConfig) mode |= Mle::ModeTlv::kModeFullNetworkData; } - return sThreadNetif->GetMle().SetDeviceMode(mode); + return aInstance->mThreadNetif.GetMle().SetDeviceMode(mode); } -const uint8_t *otGetMasterKey(otInstance *, uint8_t *aKeyLength) +const uint8_t *otGetMasterKey(otInstance *aInstance, uint8_t *aKeyLength) { - return sThreadNetif->GetKeyManager().GetMasterKey(aKeyLength); + return aInstance->mThreadNetif.GetKeyManager().GetMasterKey(aKeyLength); } -ThreadError otSetMasterKey(otInstance *, const uint8_t *aKey, uint8_t aKeyLength) +ThreadError otSetMasterKey(otInstance *aInstance, const uint8_t *aKey, uint8_t aKeyLength) { - return sThreadNetif->GetKeyManager().SetMasterKey(aKey, aKeyLength); + return aInstance->mThreadNetif.GetKeyManager().SetMasterKey(aKey, aKeyLength); } -int8_t otGetMaxTransmitPower(otInstance *) +int8_t otGetMaxTransmitPower(otInstance *aInstance) { - return sThreadNetif->GetMac().GetMaxTransmitPower(); + return aInstance->mThreadNetif.GetMac().GetMaxTransmitPower(); } -void otSetMaxTransmitPower(otInstance *, int8_t aPower) +void otSetMaxTransmitPower(otInstance *aInstance, int8_t aPower) { - sThreadNetif->GetMac().SetMaxTransmitPower(aPower); + aInstance->mThreadNetif.GetMac().SetMaxTransmitPower(aPower); } -const otIp6Address *otGetMeshLocalEid(otInstance *) +const otIp6Address *otGetMeshLocalEid(otInstance *aInstance) { - return sThreadNetif->GetMle().GetMeshLocal64(); + return aInstance->mThreadNetif.GetMle().GetMeshLocal64(); } -const uint8_t *otGetMeshLocalPrefix(otInstance *) +const uint8_t *otGetMeshLocalPrefix(otInstance *aInstance) { - return sThreadNetif->GetMle().GetMeshLocalPrefix(); + return aInstance->mThreadNetif.GetMle().GetMeshLocalPrefix(); } -ThreadError otSetMeshLocalPrefix(otInstance *, const uint8_t *aMeshLocalPrefix) +ThreadError otSetMeshLocalPrefix(otInstance *aInstance, const uint8_t *aMeshLocalPrefix) { - return sThreadNetif->GetMle().SetMeshLocalPrefix(aMeshLocalPrefix); + return aInstance->mThreadNetif.GetMle().SetMeshLocalPrefix(aMeshLocalPrefix); } -ThreadError otGetNetworkDataLeader(otInstance *, bool aStable, uint8_t *aData, uint8_t *aDataLength) +ThreadError otGetNetworkDataLeader(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength) { ThreadError error = kThreadError_None; VerifyOrExit(aData != NULL && aDataLength != NULL, error = kThreadError_InvalidArgs); - sThreadNetif->GetNetworkDataLeader().GetNetworkData(aStable, aData, *aDataLength); + aInstance->mThreadNetif.GetNetworkDataLeader().GetNetworkData(aStable, aData, *aDataLength); exit: return error; } -ThreadError otGetNetworkDataLocal(otInstance *, bool aStable, uint8_t *aData, uint8_t *aDataLength) +ThreadError otGetNetworkDataLocal(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength) { ThreadError error = kThreadError_None; VerifyOrExit(aData != NULL && aDataLength != NULL, error = kThreadError_InvalidArgs); - sThreadNetif->GetNetworkDataLocal().GetNetworkData(aStable, aData, *aDataLength); + aInstance->mThreadNetif.GetNetworkDataLocal().GetNetworkData(aStable, aData, *aDataLength); exit: return error; } -const char *otGetNetworkName(otInstance *) +const char *otGetNetworkName(otInstance *aInstance) { - return sThreadNetif->GetMac().GetNetworkName(); + return aInstance->mThreadNetif.GetMac().GetNetworkName(); } -ThreadError otSetNetworkName(otInstance *, const char *aNetworkName) +ThreadError otSetNetworkName(otInstance *aInstance, const char *aNetworkName) { - return sThreadNetif->GetMac().SetNetworkName(aNetworkName); + return aInstance->mThreadNetif.GetMac().SetNetworkName(aNetworkName); } -otPanId otGetPanId(otInstance *) +otPanId otGetPanId(otInstance *aInstance) { - return sThreadNetif->GetMac().GetPanId(); + return aInstance->mThreadNetif.GetMac().GetPanId(); } -ThreadError otSetPanId(otInstance *, otPanId aPanId) +ThreadError otSetPanId(otInstance *aInstance, otPanId aPanId) { ThreadError error = kThreadError_None; // do not allow setting PAN ID to broadcast if Thread is running VerifyOrExit(aPanId != Mac::kPanIdBroadcast || - sThreadNetif->GetMle().GetDeviceState() != Mle::kDeviceStateDisabled, + aInstance->mThreadNetif.GetMle().GetDeviceState() != Mle::kDeviceStateDisabled, error = kThreadError_InvalidState); - error = sThreadNetif->GetMac().SetPanId(aPanId); + error = aInstance->mThreadNetif.GetMac().SetPanId(aPanId); exit: return error; } -bool otIsRouterRoleEnabled(otInstance *) +bool otIsRouterRoleEnabled(otInstance *aInstance) { - return sThreadNetif->GetMle().IsRouterRoleEnabled(); + return aInstance->mThreadNetif.GetMle().IsRouterRoleEnabled(); } -void otSetRouterRoleEnabled(otInstance *, bool aEnabled) +void otSetRouterRoleEnabled(otInstance *aInstance, bool aEnabled) { - sThreadNetif->GetMle().SetRouterRoleEnabled(aEnabled); + aInstance->mThreadNetif.GetMle().SetRouterRoleEnabled(aEnabled); } -otShortAddress otGetShortAddress(otInstance *) +otShortAddress otGetShortAddress(otInstance *aInstance) { - return sThreadNetif->GetMac().GetShortAddress(); + return aInstance->mThreadNetif.GetMac().GetShortAddress(); } -uint8_t otGetLocalLeaderWeight(otInstance *) +uint8_t otGetLocalLeaderWeight(otInstance *aInstance) { - return sThreadNetif->GetMle().GetLeaderWeight(); + return aInstance->mThreadNetif.GetMle().GetLeaderWeight(); } -void otSetLocalLeaderWeight(otInstance *, uint8_t aWeight) +void otSetLocalLeaderWeight(otInstance *aInstance, uint8_t aWeight) { - sThreadNetif->GetMle().SetLeaderWeight(aWeight); + aInstance->mThreadNetif.GetMle().SetLeaderWeight(aWeight); } -uint32_t otGetLocalLeaderPartitionId(otInstance *) +uint32_t otGetLocalLeaderPartitionId(otInstance *aInstance) { - return sThreadNetif->GetMle().GetLeaderPartitionId(); + return aInstance->mThreadNetif.GetMle().GetLeaderPartitionId(); } -void otSetLocalLeaderPartitionId(otInstance *, uint32_t aPartitionId) +void otSetLocalLeaderPartitionId(otInstance *aInstance, uint32_t aPartitionId) { - return sThreadNetif->GetMle().SetLeaderPartitionId(aPartitionId); + return aInstance->mThreadNetif.GetMle().SetLeaderPartitionId(aPartitionId); } -uint16_t otGetJoinerUdpPort(otInstance *) +uint16_t otGetJoinerUdpPort(otInstance *aInstance) { - return sThreadNetif->GetJoinerRouter().GetJoinerUdpPort(); + return aInstance->mThreadNetif.GetJoinerRouter().GetJoinerUdpPort(); } -ThreadError otSetJoinerUdpPort(otInstance *, uint16_t aJoinerUdpPort) +ThreadError otSetJoinerUdpPort(otInstance *aInstance, uint16_t aJoinerUdpPort) { - return sThreadNetif->GetJoinerRouter().SetJoinerUdpPort(aJoinerUdpPort); + return aInstance->mThreadNetif.GetJoinerRouter().SetJoinerUdpPort(aJoinerUdpPort); } -ThreadError otAddBorderRouter(otInstance *, const otBorderRouterConfig *aConfig) +ThreadError otAddBorderRouter(otInstance *aInstance, const otBorderRouterConfig *aConfig) { uint8_t flags = 0; @@ -427,17 +414,17 @@ ThreadError otAddBorderRouter(otInstance *, const otBorderRouterConfig *aConfig) flags |= NetworkData::BorderRouterEntry::kOnMeshFlag; } - return sThreadNetif->GetNetworkDataLocal().AddOnMeshPrefix(aConfig->mPrefix.mPrefix.mFields.m8, - aConfig->mPrefix.mLength, - aConfig->mPreference, flags, aConfig->mStable); + return aInstance->mThreadNetif.GetNetworkDataLocal().AddOnMeshPrefix(aConfig->mPrefix.mPrefix.mFields.m8, + aConfig->mPrefix.mLength, + aConfig->mPreference, flags, aConfig->mStable); } -ThreadError otRemoveBorderRouter(otInstance *, const otIp6Prefix *aPrefix) +ThreadError otRemoveBorderRouter(otInstance *aInstance, const otIp6Prefix *aPrefix) { - return sThreadNetif->GetNetworkDataLocal().RemoveOnMeshPrefix(aPrefix->mPrefix.mFields.m8, aPrefix->mLength); + return aInstance->mThreadNetif.GetNetworkDataLocal().RemoveOnMeshPrefix(aPrefix->mPrefix.mFields.m8, aPrefix->mLength); } -ThreadError otGetNextOnMeshPrefix(otInstance *, bool aLocal, otNetworkDataIterator *aIterator, +ThreadError otGetNextOnMeshPrefix(otInstance *aInstance, bool aLocal, otNetworkDataIterator *aIterator, otBorderRouterConfig *aConfig) { ThreadError error = kThreadError_None; @@ -446,99 +433,100 @@ ThreadError otGetNextOnMeshPrefix(otInstance *, bool aLocal, otNetworkDataIterat if (aLocal) { - error = sThreadNetif->GetNetworkDataLocal().GetNextOnMeshPrefix(aIterator, aConfig); + error = aInstance->mThreadNetif.GetNetworkDataLocal().GetNextOnMeshPrefix(aIterator, aConfig); } else { - error = sThreadNetif->GetNetworkDataLeader().GetNextOnMeshPrefix(aIterator, aConfig); + error = aInstance->mThreadNetif.GetNetworkDataLeader().GetNextOnMeshPrefix(aIterator, aConfig); } exit: return error; } -ThreadError otAddExternalRoute(otInstance *, const otExternalRouteConfig *aConfig) +ThreadError otAddExternalRoute(otInstance *aInstance, const otExternalRouteConfig *aConfig) { - return sThreadNetif->GetNetworkDataLocal().AddHasRoutePrefix(aConfig->mPrefix.mPrefix.mFields.m8, - aConfig->mPrefix.mLength, - aConfig->mPreference, aConfig->mStable); + return aInstance->mThreadNetif.GetNetworkDataLocal().AddHasRoutePrefix(aConfig->mPrefix.mPrefix.mFields.m8, + aConfig->mPrefix.mLength, + aConfig->mPreference, aConfig->mStable); } -ThreadError otRemoveExternalRoute(otInstance *, const otIp6Prefix *aPrefix) +ThreadError otRemoveExternalRoute(otInstance *aInstance, const otIp6Prefix *aPrefix) { - return sThreadNetif->GetNetworkDataLocal().RemoveHasRoutePrefix(aPrefix->mPrefix.mFields.m8, aPrefix->mLength); + return aInstance->mThreadNetif.GetNetworkDataLocal().RemoveHasRoutePrefix(aPrefix->mPrefix.mFields.m8, + aPrefix->mLength); } -ThreadError otSendServerData(otInstance *) +ThreadError otSendServerData(otInstance *aInstance) { - return sThreadNetif->GetNetworkDataLocal().SendServerDataNotification(); + return aInstance->mThreadNetif.GetNetworkDataLocal().SendServerDataNotification(); } -ThreadError otAddUnsecurePort(otInstance *, uint16_t aPort) +ThreadError otAddUnsecurePort(otInstance *aInstance, uint16_t aPort) { - return sThreadNetif->GetIp6Filter().AddUnsecurePort(aPort); + return aInstance->mThreadNetif.GetIp6Filter().AddUnsecurePort(aPort); } -ThreadError otRemoveUnsecurePort(otInstance *, uint16_t aPort) +ThreadError otRemoveUnsecurePort(otInstance *aInstance, uint16_t aPort) { - return sThreadNetif->GetIp6Filter().RemoveUnsecurePort(aPort); + return aInstance->mThreadNetif.GetIp6Filter().RemoveUnsecurePort(aPort); } -const uint16_t *otGetUnsecurePorts(otInstance *, uint8_t *aNumEntries) +const uint16_t *otGetUnsecurePorts(otInstance *aInstance, uint8_t *aNumEntries) { - return sThreadNetif->GetIp6Filter().GetUnsecurePorts(*aNumEntries); + return aInstance->mThreadNetif.GetIp6Filter().GetUnsecurePorts(*aNumEntries); } -uint32_t otGetContextIdReuseDelay(otInstance *) +uint32_t otGetContextIdReuseDelay(otInstance *aInstance) { - return sThreadNetif->GetNetworkDataLeader().GetContextIdReuseDelay(); + return aInstance->mThreadNetif.GetNetworkDataLeader().GetContextIdReuseDelay(); } -void otSetContextIdReuseDelay(otInstance *, uint32_t aDelay) +void otSetContextIdReuseDelay(otInstance *aInstance, uint32_t aDelay) { - sThreadNetif->GetNetworkDataLeader().SetContextIdReuseDelay(aDelay); + aInstance->mThreadNetif.GetNetworkDataLeader().SetContextIdReuseDelay(aDelay); } -uint32_t otGetKeySequenceCounter(otInstance *) +uint32_t otGetKeySequenceCounter(otInstance *aInstance) { - return sThreadNetif->GetKeyManager().GetCurrentKeySequence(); + return aInstance->mThreadNetif.GetKeyManager().GetCurrentKeySequence(); } -void otSetKeySequenceCounter(otInstance *, uint32_t aKeySequenceCounter) +void otSetKeySequenceCounter(otInstance *aInstance, uint32_t aKeySequenceCounter) { - sThreadNetif->GetKeyManager().SetCurrentKeySequence(aKeySequenceCounter); + aInstance->mThreadNetif.GetKeyManager().SetCurrentKeySequence(aKeySequenceCounter); } -uint8_t otGetNetworkIdTimeout(otInstance *) +uint8_t otGetNetworkIdTimeout(otInstance *aInstance) { - return sThreadNetif->GetMle().GetNetworkIdTimeout(); + return aInstance->mThreadNetif.GetMle().GetNetworkIdTimeout(); } -void otSetNetworkIdTimeout(otInstance *, uint8_t aTimeout) +void otSetNetworkIdTimeout(otInstance *aInstance, uint8_t aTimeout) { - sThreadNetif->GetMle().SetNetworkIdTimeout(aTimeout); + aInstance->mThreadNetif.GetMle().SetNetworkIdTimeout((uint8_t)aTimeout); } -uint8_t otGetRouterUpgradeThreshold(otInstance *) +uint8_t otGetRouterUpgradeThreshold(otInstance *aInstance) { - return sThreadNetif->GetMle().GetRouterUpgradeThreshold(); + return aInstance->mThreadNetif.GetMle().GetRouterUpgradeThreshold(); } -void otSetRouterUpgradeThreshold(otInstance *, uint8_t aThreshold) +void otSetRouterUpgradeThreshold(otInstance *aInstance, uint8_t aThreshold) { - sThreadNetif->GetMle().SetRouterUpgradeThreshold(aThreshold); + aInstance->mThreadNetif.GetMle().SetRouterUpgradeThreshold(aThreshold); } -ThreadError otReleaseRouterId(otInstance *, uint8_t aRouterId) +ThreadError otReleaseRouterId(otInstance *aInstance, uint8_t aRouterId) { - return sThreadNetif->GetMle().ReleaseRouterId(aRouterId); + return aInstance->mThreadNetif.GetMle().ReleaseRouterId(aRouterId); } -ThreadError otAddMacWhitelist(otInstance *, const uint8_t *aExtAddr) +ThreadError otAddMacWhitelist(otInstance *aInstance, const uint8_t *aExtAddr) { ThreadError error = kThreadError_None; - if (sThreadNetif->GetMac().GetWhitelist().Add(*reinterpret_cast(aExtAddr)) == NULL) + if (aInstance->mThreadNetif.GetMac().GetWhitelist().Add(*reinterpret_cast(aExtAddr)) == NULL) { error = kThreadError_NoBufs; } @@ -546,80 +534,80 @@ ThreadError otAddMacWhitelist(otInstance *, const uint8_t *aExtAddr) return error; } -ThreadError otAddMacWhitelistRssi(otInstance *, const uint8_t *aExtAddr, int8_t aRssi) +ThreadError otAddMacWhitelistRssi(otInstance *aInstance, const uint8_t *aExtAddr, int8_t aRssi) { ThreadError error = kThreadError_None; otMacWhitelistEntry *entry; - entry = sThreadNetif->GetMac().GetWhitelist().Add(*reinterpret_cast(aExtAddr)); + entry = aInstance->mThreadNetif.GetMac().GetWhitelist().Add(*reinterpret_cast(aExtAddr)); VerifyOrExit(entry != NULL, error = kThreadError_NoBufs); - sThreadNetif->GetMac().GetWhitelist().SetFixedRssi(*entry, aRssi); + aInstance->mThreadNetif.GetMac().GetWhitelist().SetFixedRssi(*entry, aRssi); exit: return error; } -void otRemoveMacWhitelist(otInstance *, const uint8_t *aExtAddr) +void otRemoveMacWhitelist(otInstance *aInstance, const uint8_t *aExtAddr) { - sThreadNetif->GetMac().GetWhitelist().Remove(*reinterpret_cast(aExtAddr)); + aInstance->mThreadNetif.GetMac().GetWhitelist().Remove(*reinterpret_cast(aExtAddr)); } -void otClearMacWhitelist(otInstance *) +void otClearMacWhitelist(otInstance *aInstance) { - sThreadNetif->GetMac().GetWhitelist().Clear(); + aInstance->mThreadNetif.GetMac().GetWhitelist().Clear(); } -ThreadError otGetMacWhitelistEntry(otInstance *, uint8_t aIndex, otMacWhitelistEntry *aEntry) +ThreadError otGetMacWhitelistEntry(otInstance *aInstance, uint8_t aIndex, otMacWhitelistEntry *aEntry) { ThreadError error = kThreadError_None; VerifyOrExit(aEntry != NULL, error = kThreadError_InvalidArgs); - error = sThreadNetif->GetMac().GetWhitelist().GetEntry(aIndex, *aEntry); + error = aInstance->mThreadNetif.GetMac().GetWhitelist().GetEntry(aIndex, *aEntry); exit: return error; } -void otDisableMacWhitelist(otInstance *) +void otDisableMacWhitelist(otInstance *aInstance) { - sThreadNetif->GetMac().GetWhitelist().Disable(); + aInstance->mThreadNetif.GetMac().GetWhitelist().Disable(); } -void otEnableMacWhitelist(otInstance *) +void otEnableMacWhitelist(otInstance *aInstance) { - sThreadNetif->GetMac().GetWhitelist().Enable(); + aInstance->mThreadNetif.GetMac().GetWhitelist().Enable(); } -bool otIsMacWhitelistEnabled(otInstance *) +bool otIsMacWhitelistEnabled(otInstance *aInstance) { - return sThreadNetif->GetMac().GetWhitelist().IsEnabled(); + return aInstance->mThreadNetif.GetMac().GetWhitelist().IsEnabled(); } -ThreadError otBecomeDetached(otInstance *) +ThreadError otBecomeDetached(otInstance *aInstance) { - return sThreadNetif->GetMle().BecomeDetached(); + return aInstance->mThreadNetif.GetMle().BecomeDetached(); } -ThreadError otBecomeChild(otInstance *, otMleAttachFilter aFilter) +ThreadError otBecomeChild(otInstance *aInstance, otMleAttachFilter aFilter) { - return sThreadNetif->GetMle().BecomeChild(aFilter); + return aInstance->mThreadNetif.GetMle().BecomeChild(aFilter); } -ThreadError otBecomeRouter(otInstance *) +ThreadError otBecomeRouter(otInstance *aInstance) { - return sThreadNetif->GetMle().BecomeRouter(ThreadStatusTlv::kTooFewRouters); + return aInstance->mThreadNetif.GetMle().BecomeRouter(ThreadStatusTlv::kTooFewRouters); } -ThreadError otBecomeLeader(otInstance *) +ThreadError otBecomeLeader(otInstance *aInstance) { - return sThreadNetif->GetMle().BecomeLeader(); + return aInstance->mThreadNetif.GetMle().BecomeLeader(); } -ThreadError otAddMacBlacklist(otInstance *, const uint8_t *aExtAddr) +ThreadError otAddMacBlacklist(otInstance *aInstance, const uint8_t *aExtAddr) { ThreadError error = kThreadError_None; - if (sThreadNetif->GetMac().GetBlacklist().Add(*reinterpret_cast(aExtAddr)) == NULL) + if (aInstance->mThreadNetif.GetMac().GetBlacklist().Add(*reinterpret_cast(aExtAddr)) == NULL) { error = kThreadError_NoBufs; } @@ -627,60 +615,60 @@ ThreadError otAddMacBlacklist(otInstance *, const uint8_t *aExtAddr) return error; } -void otRemoveMacBlacklist(otInstance *, const uint8_t *aExtAddr) +void otRemoveMacBlacklist(otInstance *aInstance, const uint8_t *aExtAddr) { - sThreadNetif->GetMac().GetBlacklist().Remove(*reinterpret_cast(aExtAddr)); + aInstance->mThreadNetif.GetMac().GetBlacklist().Remove(*reinterpret_cast(aExtAddr)); } -void otClearMacBlacklist(otInstance *) +void otClearMacBlacklist(otInstance *aInstance) { - sThreadNetif->GetMac().GetBlacklist().Clear(); + aInstance->mThreadNetif.GetMac().GetBlacklist().Clear(); } -ThreadError otGetMacBlacklistEntry(otInstance *, uint8_t aIndex, otMacBlacklistEntry *aEntry) +ThreadError otGetMacBlacklistEntry(otInstance *aInstance, uint8_t aIndex, otMacBlacklistEntry *aEntry) { ThreadError error = kThreadError_None; VerifyOrExit(aEntry != NULL, error = kThreadError_InvalidArgs); - error = sThreadNetif->GetMac().GetBlacklist().GetEntry(aIndex, *aEntry); + error = aInstance->mThreadNetif.GetMac().GetBlacklist().GetEntry(aIndex, *aEntry); exit: return error; } -void otDisableMacBlacklist(otInstance *) +void otDisableMacBlacklist(otInstance *aInstance) { - sThreadNetif->GetMac().GetBlacklist().Disable(); + aInstance->mThreadNetif.GetMac().GetBlacklist().Disable(); } -void otEnableMacBlacklist(otInstance *) +void otEnableMacBlacklist(otInstance *aInstance) { - sThreadNetif->GetMac().GetBlacklist().Enable(); + aInstance->mThreadNetif.GetMac().GetBlacklist().Enable(); } -bool otIsMacBlacklistEnabled(otInstance *) +bool otIsMacBlacklistEnabled(otInstance *aInstance) { - return sThreadNetif->GetMac().GetBlacklist().IsEnabled(); + return aInstance->mThreadNetif.GetMac().GetBlacklist().IsEnabled(); } -ThreadError otGetAssignLinkQuality(otInstance *, const uint8_t *aExtAddr, uint8_t *aLinkQuality) +ThreadError otGetAssignLinkQuality(otInstance *aInstance, const uint8_t *aExtAddr, uint8_t *aLinkQuality) { Mac::ExtAddress extAddress; memset(&extAddress, 0, sizeof(extAddress)); memcpy(extAddress.m8, aExtAddr, OT_EXT_ADDRESS_SIZE); - return sThreadNetif->GetMle().GetAssignLinkQuality(extAddress, *aLinkQuality); + return aInstance->mThreadNetif.GetMle().GetAssignLinkQuality(extAddress, *aLinkQuality); } -void otSetAssignLinkQuality(otInstance *, const uint8_t *aExtAddr, uint8_t aLinkQuality) +void otSetAssignLinkQuality(otInstance *aInstance, const uint8_t *aExtAddr, uint8_t aLinkQuality) { Mac::ExtAddress extAddress; memset(&extAddress, 0, sizeof(extAddress)); memcpy(extAddress.m8, aExtAddr, OT_EXT_ADDRESS_SIZE); - sThreadNetif->GetMle().SetAssignLinkQuality(extAddress, aLinkQuality); + aInstance->mThreadNetif.GetMle().SetAssignLinkQuality(extAddress, aLinkQuality); } void otPlatformReset(otInstance *aInstance) @@ -688,67 +676,67 @@ void otPlatformReset(otInstance *aInstance) otPlatReset(aInstance); } -uint8_t otGetRouterDowngradeThreshold(otInstance *) +uint8_t otGetRouterDowngradeThreshold(otInstance *aInstance) { - return sThreadNetif->GetMle().GetRouterDowngradeThreshold(); + return aInstance->mThreadNetif.GetMle().GetRouterDowngradeThreshold(); } -void otSetRouterDowngradeThreshold(otInstance *, uint8_t aThreshold) +void otSetRouterDowngradeThreshold(otInstance *aInstance, uint8_t aThreshold) { - sThreadNetif->GetMle().SetRouterDowngradeThreshold(aThreshold); + aInstance->mThreadNetif.GetMle().SetRouterDowngradeThreshold(aThreshold); } -uint8_t otGetRouterSelectionJitter(otInstance *) +uint8_t otGetRouterSelectionJitter(otInstance *aInstance) { - return sThreadNetif->GetMle().GetRouterSelectionJitter(); + return aInstance->mThreadNetif.GetMle().GetRouterSelectionJitter(); } -void otSetRouterSelectionJitter(otInstance *, uint8_t aRouterJitter) +void otSetRouterSelectionJitter(otInstance *aInstance, uint8_t aRouterJitter) { - sThreadNetif->GetMle().SetRouterSelectionJitter(aRouterJitter); + aInstance->mThreadNetif.GetMle().SetRouterSelectionJitter(aRouterJitter); } -ThreadError otGetChildInfoById(otInstance *, uint16_t aChildId, otChildInfo *aChildInfo) +ThreadError otGetChildInfoById(otInstance *aInstance, uint16_t aChildId, otChildInfo *aChildInfo) { ThreadError error = kThreadError_None; VerifyOrExit(aChildInfo != NULL, error = kThreadError_InvalidArgs); - error = sThreadNetif->GetMle().GetChildInfoById(aChildId, *aChildInfo); + error = aInstance->mThreadNetif.GetMle().GetChildInfoById(aChildId, *aChildInfo); exit: return error; } -ThreadError otGetChildInfoByIndex(otInstance *, uint8_t aChildIndex, otChildInfo *aChildInfo) +ThreadError otGetChildInfoByIndex(otInstance *aInstance, uint8_t aChildIndex, otChildInfo *aChildInfo) { ThreadError error = kThreadError_None; VerifyOrExit(aChildInfo != NULL, error = kThreadError_InvalidArgs); - error = sThreadNetif->GetMle().GetChildInfoByIndex(aChildIndex, *aChildInfo); + error = aInstance->mThreadNetif.GetMle().GetChildInfoByIndex(aChildIndex, *aChildInfo); exit: return error; } -ThreadError otGetNextNeighborInfo(otInstance *, otNeighborInfoIterator *aIterator, otNeighborInfo *aInfo) +ThreadError otGetNextNeighborInfo(otInstance *aInstance, otNeighborInfoIterator *aIterator, otNeighborInfo *aInfo) { ThreadError error = kThreadError_None; VerifyOrExit((aInfo != NULL) && (aIterator != NULL), error = kThreadError_InvalidArgs); - error = sThreadNetif->GetMle().GetNextNeighborInfo(*aIterator, *aInfo); + error = aInstance->mThreadNetif.GetMle().GetNextNeighborInfo(*aIterator, *aInfo); exit: return error; } -otDeviceRole otGetDeviceRole(otInstance *) +otDeviceRole otGetDeviceRole(otInstance *aInstance) { otDeviceRole rval = kDeviceRoleDisabled; - switch (sThreadNetif->GetMle().GetDeviceState()) + switch (aInstance->mThreadNetif.GetMle().GetDeviceState()) { case Mle::kDeviceStateDisabled: rval = kDeviceRoleDisabled; @@ -774,79 +762,79 @@ otDeviceRole otGetDeviceRole(otInstance *) return rval; } -ThreadError otGetEidCacheEntry(otInstance *, uint8_t aIndex, otEidCacheEntry *aEntry) +ThreadError otGetEidCacheEntry(otInstance *aInstance, uint8_t aIndex, otEidCacheEntry *aEntry) { ThreadError error; VerifyOrExit(aEntry != NULL, error = kThreadError_InvalidArgs); - error = sThreadNetif->GetAddressResolver().GetEntry(aIndex, *aEntry); + error = aInstance->mThreadNetif.GetAddressResolver().GetEntry(aIndex, *aEntry); exit: return error; } -ThreadError otGetLeaderData(otInstance *, otLeaderData *aLeaderData) +ThreadError otGetLeaderData(otInstance *aInstance, otLeaderData *aLeaderData) { ThreadError error; VerifyOrExit(aLeaderData != NULL, error = kThreadError_InvalidArgs); - error = sThreadNetif->GetMle().GetLeaderData(*aLeaderData); + error = aInstance->mThreadNetif.GetMle().GetLeaderData(*aLeaderData); exit: return error; } -uint8_t otGetLeaderRouterId(otInstance *) +uint8_t otGetLeaderRouterId(otInstance *aInstance) { - return sThreadNetif->GetMle().GetLeaderDataTlv().GetLeaderRouterId(); + return aInstance->mThreadNetif.GetMle().GetLeaderDataTlv().GetLeaderRouterId(); } -uint8_t otGetLeaderWeight(otInstance *) +uint8_t otGetLeaderWeight(otInstance *aInstance) { - return sThreadNetif->GetMle().GetLeaderDataTlv().GetWeighting(); + return aInstance->mThreadNetif.GetMle().GetLeaderDataTlv().GetWeighting(); } -uint8_t otGetNetworkDataVersion(otInstance *) +uint8_t otGetNetworkDataVersion(otInstance *aInstance) { - return sThreadNetif->GetMle().GetLeaderDataTlv().GetDataVersion(); + return aInstance->mThreadNetif.GetMle().GetLeaderDataTlv().GetDataVersion(); } -uint32_t otGetPartitionId(otInstance *) +uint32_t otGetPartitionId(otInstance *aInstance) { - return sThreadNetif->GetMle().GetLeaderDataTlv().GetPartitionId(); + return aInstance->mThreadNetif.GetMle().GetLeaderDataTlv().GetPartitionId(); } -uint16_t otGetRloc16(otInstance *) +uint16_t otGetRloc16(otInstance *aInstance) { - return sThreadNetif->GetMle().GetRloc16(); + return aInstance->mThreadNetif.GetMle().GetRloc16(); } -uint8_t otGetRouterIdSequence(otInstance *) +uint8_t otGetRouterIdSequence(otInstance *aInstance) { - return sThreadNetif->GetMle().GetRouterIdSequence(); + return aInstance->mThreadNetif.GetMle().GetRouterIdSequence(); } -ThreadError otGetRouterInfo(otInstance *, uint16_t aRouterId, otRouterInfo *aRouterInfo) +ThreadError otGetRouterInfo(otInstance *aInstance, uint16_t aRouterId, otRouterInfo *aRouterInfo) { ThreadError error = kThreadError_None; VerifyOrExit(aRouterInfo != NULL, error = kThreadError_InvalidArgs); - error = sThreadNetif->GetMle().GetRouterInfo(aRouterId, *aRouterInfo); + error = aInstance->mThreadNetif.GetMle().GetRouterInfo(aRouterId, *aRouterInfo); exit: return error; } -ThreadError otGetParentInfo(otInstance *, otRouterInfo *aParentInfo) +ThreadError otGetParentInfo(otInstance *aInstance, otRouterInfo *aParentInfo) { ThreadError error = kThreadError_None; Router *parent; VerifyOrExit(aParentInfo != NULL, error = kThreadError_InvalidArgs); - parent = sThreadNetif->GetMle().GetParent(); + parent = aInstance->mThreadNetif.GetMle().GetParent(); memcpy(aParentInfo->mExtAddress.m8, parent->mMacAddr.m8, OT_EXT_ADDRESS_SIZE); aParentInfo->mRloc16 = parent->mValid.mRloc16; @@ -854,37 +842,37 @@ exit: return error; } -uint8_t otGetStableNetworkDataVersion(otInstance *) +uint8_t otGetStableNetworkDataVersion(otInstance *aInstance) { - return sThreadNetif->GetMle().GetLeaderDataTlv().GetStableDataVersion(); + return aInstance->mThreadNetif.GetMle().GetLeaderDataTlv().GetStableDataVersion(); } -void otSetLinkPcapCallback(otInstance *, otLinkPcapCallback aPcapCallback, void *aCallbackContext) +void otSetLinkPcapCallback(otInstance *aInstance, otLinkPcapCallback aPcapCallback, void *aCallbackContext) { - sThreadNetif->GetMac().SetPcapCallback(aPcapCallback, aCallbackContext); + aInstance->mThreadNetif.GetMac().SetPcapCallback(aPcapCallback, aCallbackContext); } -bool otIsLinkPromiscuous(otInstance *) +bool otIsLinkPromiscuous(otInstance *aInstance) { - return sThreadNetif->GetMac().IsPromiscuous(); + return aInstance->mThreadNetif.GetMac().IsPromiscuous(); } -ThreadError otSetLinkPromiscuous(otInstance *, bool aPromiscuous) +ThreadError otSetLinkPromiscuous(otInstance *aInstance, bool aPromiscuous) { ThreadError error = kThreadError_None; // cannot enable IEEE 802.15.4 promiscuous mode if the Thread interface is enabled - VerifyOrExit(sThreadNetif->IsUp() == false, error = kThreadError_Busy); + VerifyOrExit(aInstance->mThreadNetif.IsUp() == false, error = kThreadError_Busy); - sThreadNetif->GetMac().SetPromiscuous(aPromiscuous); + aInstance->mThreadNetif.GetMac().SetPromiscuous(aPromiscuous); exit: return error; } -const otMacCounters *otGetMacCounters(otInstance *) +const otMacCounters *otGetMacCounters(otInstance *aInstance) { - return &sThreadNetif->GetMac().GetCounters(); + return &aInstance->mThreadNetif.GetMac().GetCounters(); } bool otIsIp6AddressEqual(const otIp6Address *a, const otIp6Address *b) @@ -897,19 +885,19 @@ ThreadError otIp6AddressFromString(const char *str, otIp6Address *address) return static_cast(address)->FromString(str); } -const otNetifAddress *otGetUnicastAddresses(otInstance *) +const otNetifAddress *otGetUnicastAddresses(otInstance *aInstance) { - return sThreadNetif->GetUnicastAddresses(); + return aInstance->mThreadNetif.GetUnicastAddresses(); } -ThreadError otAddUnicastAddress(otInstance *, const otNetifAddress *address) +ThreadError otAddUnicastAddress(otInstance *aInstance, const otNetifAddress *address) { - return sThreadNetif->AddExternalUnicastAddress(*static_cast(address)); + return aInstance->mThreadNetif.AddExternalUnicastAddress(*static_cast(address)); } -ThreadError otRemoveUnicastAddress(otInstance *, const otIp6Address *address) +ThreadError otRemoveUnicastAddress(otInstance *aInstance, const otIp6Address *address) { - return sThreadNetif->RemoveExternalUnicastAddress(*static_cast(address)); + return aInstance->mThreadNetif.RemoveExternalUnicastAddress(*static_cast(address)); } void otSlaacUpdate(otInstance *aInstance, otNetifAddress *aAddresses, uint32_t aNumAddresses, @@ -923,10 +911,10 @@ ThreadError otCreateRandomIid(otInstance *aInstance, otNetifAddress *aAddress, v return Utils::Slaac::CreateRandomIid(aInstance, aAddress, aContext); } -ThreadError otCreateMacIid(otInstance *, otNetifAddress *aAddress, void *) +ThreadError otCreateMacIid(otInstance *aInstance, otNetifAddress *aAddress, void *) { memcpy(&aAddress->mAddress.mFields.m8[OT_IP6_ADDRESS_SIZE - OT_IP6_IID_SIZE], - sThreadNetif->GetMac().GetExtAddress(), OT_IP6_IID_SIZE); + aInstance->mThreadNetif.GetMac().GetExtAddress(), OT_IP6_IID_SIZE); aAddress->mAddress.mFields.m8[OT_IP6_ADDRESS_SIZE - OT_IP6_IID_SIZE] ^= 0x02; return kThreadError_None; @@ -937,10 +925,10 @@ ThreadError otCreateSemanticallyOpaqueIid(otInstance *aInstance, otNetifAddress return static_cast(aContext)->CreateIid(aInstance, aAddress); } -void otSetStateChangedCallback(otInstance *, otStateChangedCallback aCallback, void *aCallbackContext) +void otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aCallbackContext) { - sNetifCallback.Set(aCallback, aCallbackContext); - sThreadNetif->RegisterCallback(sNetifCallback); + aInstance->mNetifCallback.Set(aCallback, aCallbackContext); + aInstance->mThreadNetif.RegisterCallback(aInstance->mNetifCallback); } const char *otGetVersionString(void) @@ -959,19 +947,19 @@ const char *otGetVersionString(void) return sVersion; } -uint32_t otGetPollPeriod(otInstance *) +uint32_t otGetPollPeriod(otInstance *aInstance) { - return sThreadNetif->GetMeshForwarder().GetAssignPollPeriod(); + return aInstance->mThreadNetif.GetMeshForwarder().GetAssignPollPeriod(); } -void otSetPollPeriod(otInstance *, uint32_t aPollPeriod) +void otSetPollPeriod(otInstance *aInstance, uint32_t aPollPeriod) { - sThreadNetif->GetMeshForwarder().SetAssignPollPeriod(aPollPeriod); + aInstance->mThreadNetif.GetMeshForwarder().SetAssignPollPeriod(aPollPeriod); } -ThreadError otSetPreferredRouterId(otInstance *, uint8_t aRouterId) +ThreadError otSetPreferredRouterId(otInstance *aInstance, uint8_t aRouterId) { - return sThreadNetif->GetMle().SetPreferredRouterId(aRouterId); + return aInstance->mThreadNetif.GetMle().SetPreferredRouterId(aRouterId); } #ifdef OPENTHREAD_MULTIPLE_INSTANCE @@ -992,10 +980,6 @@ otInstance *otInstanceInit(void *aInstanceBuffer, uint64_t *aInstanceBufferSize) // Construct the context aInstance = new(aInstanceBuffer)otInstance(); - new(&sMbedTlsRaw) Crypto::MbedTls; - sIp6 = new(&sIp6Raw) Ip6::Ip6; - sThreadNetif = new(&sThreadNetifRaw) ThreadNetif(*sIp6); - exit: return aInstance; @@ -1012,10 +996,6 @@ otInstance *otInstanceInit() // Construct the context sInstance = new(&sInstanceRaw)otInstance(); - new(&sMbedTlsRaw) Crypto::MbedTls; - sIp6 = new(&sIp6Raw) Ip6::Ip6; - sThreadNetif = new(&sThreadNetifRaw) ThreadNetif(*sIp6); - exit: return sInstance; @@ -1025,17 +1005,17 @@ exit: ThreadError otSendDiagnosticGet(otInstance *aInstance, otIp6Address *aDestination, uint8_t aTlvTypes[], uint8_t aCount) { - (void)aInstance; - return sThreadNetif->GetNetworkDiagnostic().SendDiagnosticGet(*static_cast(aDestination), aTlvTypes, - aCount); + return aInstance->mThreadNetif.GetNetworkDiagnostic().SendDiagnosticGet(*static_cast(aDestination), + aTlvTypes, + aCount); } ThreadError otSendDiagnosticReset(otInstance *aInstance, otIp6Address *aDestination, uint8_t aTlvTypes[], uint8_t aCount) { - (void)aInstance; - return sThreadNetif->GetNetworkDiagnostic().SendDiagnosticReset(*static_cast(aDestination), aTlvTypes, - aCount); + return aInstance->mThreadNetif.GetNetworkDiagnostic().SendDiagnosticReset(*static_cast(aDestination), + aTlvTypes, + aCount); } void otInstanceFinalize(otInstance *aInstance) @@ -1045,69 +1025,72 @@ void otInstanceFinalize(otInstance *aInstance) (void)otInterfaceDown(aInstance); // Nothing to actually free, since the caller supplied the buffer - sThreadNetif = NULL; + +#ifndef OPENTHREAD_MULTIPLE_INSTANCE + sInstance = NULL; +#endif } -ThreadError otInterfaceUp(otInstance *) +ThreadError otInterfaceUp(otInstance *aInstance) { ThreadError error = kThreadError_None; - error = sThreadNetif->Up(); + error = aInstance->mThreadNetif.Up(); return error; } -ThreadError otInterfaceDown(otInstance *) +ThreadError otInterfaceDown(otInstance *aInstance) { ThreadError error = kThreadError_None; - error = sThreadNetif->Down(); + error = aInstance->mThreadNetif.Down(); return error; } -bool otIsInterfaceUp(otInstance *) +bool otIsInterfaceUp(otInstance *aInstance) { - return sThreadNetif->IsUp(); + return aInstance->mThreadNetif.IsUp(); } -ThreadError otThreadStart(otInstance *) +ThreadError otThreadStart(otInstance *aInstance) { ThreadError error = kThreadError_None; - VerifyOrExit(sThreadNetif->GetMac().GetPanId() != Mac::kPanIdBroadcast, error = kThreadError_InvalidState); + VerifyOrExit(aInstance->mThreadNetif.GetMac().GetPanId() != Mac::kPanIdBroadcast, error = kThreadError_InvalidState); - error = sThreadNetif->GetMle().Start(); + error = aInstance->mThreadNetif.GetMle().Start(); exit: return error; } -ThreadError otThreadStop(otInstance *) +ThreadError otThreadStop(otInstance *aInstance) { ThreadError error = kThreadError_None; - error = sThreadNetif->GetMle().Stop(); + error = aInstance->mThreadNetif.GetMle().Stop(); return error; } -bool otIsSingleton(otInstance *) +bool otIsSingleton(otInstance *aInstance) { - return sThreadNetif->GetMle().IsSingleton(); + return aInstance->mThreadNetif.GetMle().IsSingleton(); } ThreadError otActiveScan(otInstance *aInstance, uint32_t aScanChannels, uint16_t aScanDuration, otHandleActiveScanResult aCallback, void *aCallbackContext) { - sActiveScanCallback = aCallback; - sActiveScanCallbackContext = aCallbackContext; - return sThreadNetif->GetMac().ActiveScan(aScanChannels, aScanDuration, &HandleActiveScanResult, aInstance); + aInstance->mActiveScanCallback = aCallback; + aInstance->mActiveScanCallbackContext = aCallbackContext; + return aInstance->mThreadNetif.GetMac().ActiveScan(aScanChannels, aScanDuration, &HandleActiveScanResult, aInstance); } -bool otIsActiveScanInProgress(otInstance *) +bool otIsActiveScanInProgress(otInstance *aInstance) { - return sThreadNetif->GetMac().IsActiveScanInProgress(); + return aInstance->mThreadNetif.GetMac().IsActiveScanInProgress(); } void HandleActiveScanResult(void *aContext, Mac::Frame *aFrame) @@ -1122,7 +1105,7 @@ void HandleActiveScanResult(void *aContext, Mac::Frame *aFrame) if (aFrame == NULL) { - sActiveScanCallback(NULL, sActiveScanCallbackContext); + aInstance->mActiveScanCallback(NULL, aInstance->mActiveScanCallbackContext); ExitNow(); } @@ -1147,86 +1130,81 @@ void HandleActiveScanResult(void *aContext, Mac::Frame *aFrame) memcpy(&result.mExtendedPanId, beacon->GetExtendedPanId(), sizeof(result.mExtendedPanId)); } - sActiveScanCallback(&result, sActiveScanCallbackContext); + aInstance->mActiveScanCallback(&result, aInstance->mActiveScanCallbackContext); exit: - (void)aInstance; return; } ThreadError otEnergyScan(otInstance *aInstance, uint32_t aScanChannels, uint16_t aScanDuration, otHandleEnergyScanResult aCallback, void *aCallbackContext) { - sEnergyScanCallback = aCallback; - sEnergyScanCallbackContext = aCallbackContext; - return sThreadNetif->GetMac().EnergyScan(aScanChannels, aScanDuration, &HandleEnergyScanResult, aInstance); + aInstance->mEnergyScanCallback = aCallback; + aInstance->mEnergyScanCallbackContext = aCallbackContext; + return aInstance->mThreadNetif.GetMac().EnergyScan(aScanChannels, aScanDuration, &HandleEnergyScanResult, aInstance); } void HandleEnergyScanResult(void *aContext, otEnergyScanResult *aResult) { otInstance *aInstance = static_cast(aContext); - sEnergyScanCallback(aResult, sEnergyScanCallbackContext); - - (void)aInstance; + aInstance->mEnergyScanCallback(aResult, aInstance->mEnergyScanCallbackContext); } - bool otIsEnergyScanInProgress(otInstance *aInstance) { - (void)aInstance; - return sThreadNetif->GetMac().IsEnergyScanInProgress(); + return aInstance->mThreadNetif.GetMac().IsEnergyScanInProgress(); } ThreadError otDiscover(otInstance *aInstance, uint32_t aScanChannels, uint16_t aScanDuration, uint16_t aPanId, otHandleActiveScanResult aCallback, void *aCallbackContext) { - sDiscoverCallback = aCallback; - sDiscoverCallbackContext = aCallbackContext; - return sThreadNetif->GetMle().Discover(aScanChannels, aScanDuration, aPanId, &HandleMleDiscover, aInstance); + aInstance->mDiscoverCallback = aCallback; + aInstance->mDiscoverCallbackContext = aCallbackContext; + return aInstance->mThreadNetif.GetMle().Discover(aScanChannels, aScanDuration, aPanId, &HandleMleDiscover, aInstance); } -bool otIsDiscoverInProgress(otInstance *) +bool otIsDiscoverInProgress(otInstance *aInstance) { - return sThreadNetif->GetMle().IsDiscoverInProgress(); + return aInstance->mThreadNetif.GetMle().IsDiscoverInProgress(); } void HandleMleDiscover(otActiveScanResult *aResult, void *aContext) { otInstance *aInstance = static_cast(aContext); - (void)aInstance; - sDiscoverCallback(aResult, sDiscoverCallbackContext); + aInstance->mDiscoverCallback(aResult, aInstance->mDiscoverCallbackContext); } -void otSetReceiveIp6DatagramCallback(otInstance *, otReceiveIp6DatagramCallback aCallback, +void otSetReceiveIp6DatagramCallback(otInstance *aInstance, otReceiveIp6DatagramCallback aCallback, void *aCallbackContext) { - sIp6->SetReceiveDatagramCallback(aCallback, aCallbackContext); + aInstance->mIp6.SetReceiveDatagramCallback(aCallback, aCallbackContext); } -bool otIsReceiveIp6DatagramFilterEnabled(otInstance *) +bool otIsReceiveIp6DatagramFilterEnabled(otInstance *aInstance) { - return sIp6->IsReceiveIp6FilterEnabled(); + return aInstance->mIp6.IsReceiveIp6FilterEnabled(); } -void otSetReceiveIp6DatagramFilterEnabled(otInstance *, bool aEnabled) +void otSetReceiveIp6DatagramFilterEnabled(otInstance *aInstance, bool aEnabled) { - sIp6->SetReceiveIp6FilterEnabled(aEnabled); + aInstance->mIp6.SetReceiveIp6FilterEnabled(aEnabled); } -ThreadError otSendIp6Datagram(otInstance *, otMessage aMessage) +ThreadError otSendIp6Datagram(otInstance *aInstance, otMessage aMessage) { - return sIp6->HandleDatagram(*static_cast(aMessage), NULL, sThreadNetif->GetInterfaceId(), NULL, true); + return aInstance->mIp6.HandleDatagram(*static_cast(aMessage), NULL, aInstance->mThreadNetif.GetInterfaceId(), + NULL, true); } -otMessage otNewUdpMessage(otInstance *) +otMessage otNewUdpMessage(otInstance *aInstance) { - return sIp6->mUdp.NewMessage(0); + return aInstance->mIp6.mUdp.NewMessage(0); } -otMessage otNewIp6Message(otInstance *, bool aLinkSecurityEnabled) +otMessage otNewIp6Message(otInstance *aInstance, bool aLinkSecurityEnabled) { - Message *message = sIp6->mMessagePool.New(Message::kTypeIp6, 0); + Message *message = aInstance->mIp6.mMessagePool.New(Message::kTypeIp6, 0); if (message) { @@ -1283,14 +1261,14 @@ int otWriteMessage(otMessage aMessage, uint16_t aOffset, const void *aBuf, uint1 return message->Write(aOffset, aLength, aBuf); } -ThreadError otOpenUdpSocket(otInstance *, otUdpSocket *aSocket, otUdpReceive aCallback, void *aCallbackContext) +ThreadError otOpenUdpSocket(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aCallbackContext) { ThreadError error = kThreadError_Busy; Ip6::UdpSocket *socket = static_cast(aSocket); if (socket->mTransport == NULL) { - socket->mTransport = &sIp6->mUdp; + socket->mTransport = &aInstance->mIp6.mUdp; error = socket->Open(aCallback, aCallbackContext); } @@ -1328,14 +1306,14 @@ ThreadError otSendUdp(otUdpSocket *aSocket, otMessage aMessage, const otMessageI *static_cast(aMessageInfo)); } -bool otIsIcmpEchoEnabled(otInstance *) +bool otIsIcmpEchoEnabled(otInstance *aInstance) { - return sIp6->mIcmp.IsEchoEnabled(); + return aInstance->mIp6.mIcmp.IsEchoEnabled(); } -void otSetIcmpEchoEnabled(otInstance *, bool aEnabled) +void otSetIcmpEchoEnabled(otInstance *aInstance, bool aEnabled) { - sIp6->mIcmp.SetEchoEnabled(aEnabled); + aInstance->mIp6.mIcmp.SetEchoEnabled(aEnabled); } uint8_t otIp6PrefixMatch(const otIp6Address *aFirst, const otIp6Address *aSecond) @@ -1350,147 +1328,148 @@ exit: return rval; } -ThreadError otGetActiveDataset(otInstance *, otOperationalDataset *aDataset) +ThreadError otGetActiveDataset(otInstance *aInstance, otOperationalDataset *aDataset) { ThreadError error = kThreadError_None; VerifyOrExit(aDataset != NULL, error = kThreadError_InvalidArgs); - sThreadNetif->GetActiveDataset().GetLocal().Get(*aDataset); + aInstance->mThreadNetif.GetActiveDataset().GetLocal().Get(*aDataset); exit: return error; } -ThreadError otSetActiveDataset(otInstance *, otOperationalDataset *aDataset) +ThreadError otSetActiveDataset(otInstance *aInstance, otOperationalDataset *aDataset) { ThreadError error; VerifyOrExit(aDataset != NULL, error = kThreadError_InvalidArgs); - error = sThreadNetif->GetActiveDataset().Set(*aDataset); + error = aInstance->mThreadNetif.GetActiveDataset().Set(*aDataset); exit: return error; } -ThreadError otGetPendingDataset(otInstance *, otOperationalDataset *aDataset) +ThreadError otGetPendingDataset(otInstance *aInstance, otOperationalDataset *aDataset) { ThreadError error = kThreadError_None; VerifyOrExit(aDataset != NULL, error = kThreadError_InvalidArgs); - sThreadNetif->GetPendingDataset().GetLocal().Get(*aDataset); + aInstance->mThreadNetif.GetPendingDataset().GetLocal().Get(*aDataset); exit: return error; } -ThreadError otSetPendingDataset(otInstance *, otOperationalDataset *aDataset) +ThreadError otSetPendingDataset(otInstance *aInstance, otOperationalDataset *aDataset) { ThreadError error; VerifyOrExit(aDataset != NULL, error = kThreadError_InvalidArgs); - error = sThreadNetif->GetPendingDataset().Set(*aDataset); + error = aInstance->mThreadNetif.GetPendingDataset().Set(*aDataset); exit: return error; } -ThreadError otSendActiveGet(otInstance *, const uint8_t *aTlvTypes, uint8_t aLength) +ThreadError otSendActiveGet(otInstance *aInstance, const uint8_t *aTlvTypes, uint8_t aLength) { - return sThreadNetif->GetActiveDataset().SendGetRequest(aTlvTypes, aLength); + return aInstance->mThreadNetif.GetActiveDataset().SendGetRequest(aTlvTypes, aLength); } -ThreadError otSendActiveSet(otInstance *, const otOperationalDataset *aDataset, const uint8_t *aTlvs, uint8_t aLength) +ThreadError otSendActiveSet(otInstance *aInstance, const otOperationalDataset *aDataset, const uint8_t *aTlvs, + uint8_t aLength) { - return sThreadNetif->GetActiveDataset().SendSetRequest(*aDataset, aTlvs, aLength); + return aInstance->mThreadNetif.GetActiveDataset().SendSetRequest(*aDataset, aTlvs, aLength); } -ThreadError otSendPendingGet(otInstance *, const uint8_t *aTlvTypes, uint8_t aLength) +ThreadError otSendPendingGet(otInstance *aInstance, const uint8_t *aTlvTypes, uint8_t aLength) { - return sThreadNetif->GetPendingDataset().SendGetRequest(aTlvTypes, aLength); + return aInstance->mThreadNetif.GetPendingDataset().SendGetRequest(aTlvTypes, aLength); } -ThreadError otSendPendingSet(otInstance *, const otOperationalDataset *aDataset, const uint8_t *aTlvs, uint8_t aLength) +ThreadError otSendPendingSet(otInstance *aInstance, const otOperationalDataset *aDataset, const uint8_t *aTlvs, + uint8_t aLength) { - return sThreadNetif->GetPendingDataset().SendSetRequest(*aDataset, aTlvs, aLength); + return aInstance->mThreadNetif.GetPendingDataset().SendSetRequest(*aDataset, aTlvs, aLength); } #if OPENTHREAD_ENABLE_COMMISSIONER #include -ThreadError otCommissionerStart(otInstance *) +ThreadError otCommissionerStart(otInstance *aInstance) { - return sThreadNetif->GetCommissioner().Start(); + return aInstance->mThreadNetif.GetCommissioner().Start(); } -ThreadError otCommissionerStop(otInstance *) +ThreadError otCommissionerStop(otInstance *aInstance) { - return sThreadNetif->GetCommissioner().Stop(); + return aInstance->mThreadNetif.GetCommissioner().Stop(); } -ThreadError otCommissionerAddJoiner(otInstance *, const otExtAddress *aExtAddress, const char *aPSKd) +ThreadError otCommissionerAddJoiner(otInstance *aInstance, const otExtAddress *aExtAddress, const char *aPSKd) { - return sThreadNetif->GetCommissioner().AddJoiner(static_cast(aExtAddress), aPSKd); + return aInstance->mThreadNetif.GetCommissioner().AddJoiner(static_cast(aExtAddress), aPSKd); } -ThreadError otCommissionerRemoveJoiner(otInstance *, const otExtAddress *aExtAddress) +ThreadError otCommissionerRemoveJoiner(otInstance *aInstance, const otExtAddress *aExtAddress) { - return sThreadNetif->GetCommissioner().RemoveJoiner(static_cast(aExtAddress)); + return aInstance->mThreadNetif.GetCommissioner().RemoveJoiner(static_cast(aExtAddress)); } -ThreadError otCommissionerSetProvisioningUrl(otInstance *, const char *aProvisioningUrl) +ThreadError otCommissionerSetProvisioningUrl(otInstance *aInstance, const char *aProvisioningUrl) { - return sThreadNetif->GetCommissioner().SetProvisioningUrl(aProvisioningUrl); + return aInstance->mThreadNetif.GetCommissioner().SetProvisioningUrl(aProvisioningUrl); } -ThreadError otCommissionerAnnounceBegin(otInstance *, uint32_t aChannelMask, uint8_t aCount, uint16_t aPeriod, +ThreadError otCommissionerAnnounceBegin(otInstance *aInstance, uint32_t aChannelMask, uint8_t aCount, uint16_t aPeriod, const otIp6Address *aAddress) { - return sThreadNetif->GetCommissioner().mAnnounceBegin.SendRequest(aChannelMask, aCount, aPeriod, - *static_cast(aAddress)); + return aInstance->mThreadNetif.GetCommissioner().mAnnounceBegin.SendRequest(aChannelMask, aCount, aPeriod, + *static_cast(aAddress)); } -ThreadError otCommissionerEnergyScan(otInstance *, uint32_t aChannelMask, uint8_t aCount, uint16_t aPeriod, +ThreadError otCommissionerEnergyScan(otInstance *aInstance, uint32_t aChannelMask, uint8_t aCount, uint16_t aPeriod, uint16_t aScanDuration, const otIp6Address *aAddress, otCommissionerEnergyReportCallback aCallback, void *aContext) { - return sThreadNetif->GetCommissioner().mEnergyScan.SendQuery(aChannelMask, aCount, aPeriod, aScanDuration, - *static_cast(aAddress), - aCallback, aContext); + return aInstance->mThreadNetif.GetCommissioner().mEnergyScan.SendQuery(aChannelMask, aCount, aPeriod, aScanDuration, + *static_cast(aAddress), + aCallback, aContext); } -ThreadError otCommissionerPanIdQuery(otInstance *, uint16_t aPanId, uint32_t aChannelMask, +ThreadError otCommissionerPanIdQuery(otInstance *aInstance, uint16_t aPanId, uint32_t aChannelMask, const otIp6Address *aAddress, otCommissionerPanIdConflictCallback aCallback, void *aContext) { - return sThreadNetif->GetCommissioner().mPanIdQuery.SendQuery(aPanId, aChannelMask, - *static_cast(aAddress), - aCallback, aContext); + return aInstance->mThreadNetif.GetCommissioner().mPanIdQuery.SendQuery( + aPanId, aChannelMask, *static_cast(aAddress), aCallback, aContext); } -ThreadError otSendMgmtCommissionerGet(otInstance *, const uint8_t *aTlvs, uint8_t aLength) +ThreadError otSendMgmtCommissionerGet(otInstance *aInstance, const uint8_t *aTlvs, uint8_t aLength) { - return sThreadNetif->GetCommissioner().SendMgmtCommissionerGetRequest(aTlvs, aLength); + return aInstance->mThreadNetif.GetCommissioner().SendMgmtCommissionerGetRequest(aTlvs, aLength); } -ThreadError otSendMgmtCommissionerSet(otInstance *, const otCommissioningDataset *aDataset, +ThreadError otSendMgmtCommissionerSet(otInstance *aInstance, const otCommissioningDataset *aDataset, const uint8_t *aTlvs, uint8_t aLength) { - return sThreadNetif->GetCommissioner().SendMgmtCommissionerSetRequest(*aDataset, aTlvs, aLength); + return aInstance->mThreadNetif.GetCommissioner().SendMgmtCommissionerSetRequest(*aDataset, aTlvs, aLength); } #endif // OPENTHREAD_ENABLE_COMMISSIONER #if OPENTHREAD_ENABLE_JOINER -ThreadError otJoinerStart(otInstance *, const char *aPSKd, const char *aProvisioningUrl) +ThreadError otJoinerStart(otInstance *aInstance, const char *aPSKd, const char *aProvisioningUrl) { - return sThreadNetif->GetJoiner().Start(aPSKd, aProvisioningUrl); + return aInstance->mThreadNetif.GetJoiner().Start(aPSKd, aProvisioningUrl); } -ThreadError otJoinerStop(otInstance *) +ThreadError otJoinerStop(otInstance *aInstance) { - return sThreadNetif->GetJoiner().Stop(); + return aInstance->mThreadNetif.GetJoiner().Stop(); } #endif // OPENTHREAD_ENABLE_JOINER diff --git a/src/core/thread/address_resolver.cpp b/src/core/thread/address_resolver.cpp index 30bd580c7..5da300a8c 100644 --- a/src/core/thread/address_resolver.cpp +++ b/src/core/thread/address_resolver.cpp @@ -175,7 +175,7 @@ ThreadError AddressResolver::SendAddressQuery(const Ip6::Address &aEid) Ip6::MessageInfo messageInfo; sockaddr.mPort = kCoapUdpPort; - mSocket.Open(&HandleUdpReceive, this); + mSocket.Open(&AddressResolver::HandleUdpReceive, this); mSocket.Bind(sockaddr); VerifyOrExit((message = mSocket.NewMessage(0)) != NULL, error = kThreadError_NoBufs); @@ -350,7 +350,7 @@ ThreadError AddressResolver::SendAddressError(const ThreadTargetTlv &aTarget, co Ip6::SockAddr sockaddr; sockaddr.mPort = kCoapUdpPort; - mSocket.Open(&HandleUdpReceive, this); + mSocket.Open(&AddressResolver::HandleUdpReceive, this); mSocket.Bind(sockaddr); VerifyOrExit((message = mSocket.NewMessage(0)) != NULL, error = kThreadError_NoBufs); diff --git a/src/core/thread/meshcop_dataset_manager.cpp b/src/core/thread/meshcop_dataset_manager.cpp index 30d59e675..a53fdf054 100644 --- a/src/core/thread/meshcop_dataset_manager.cpp +++ b/src/core/thread/meshcop_dataset_manager.cpp @@ -37,6 +37,8 @@ #include #include +#include +#include #include #include #include @@ -753,7 +755,7 @@ ThreadError ActiveDataset::ApplyConfiguration(void) PendingDataset::PendingDataset(ThreadNetif &aThreadNetif): DatasetManager(aThreadNetif, Tlv::kPendingTimestamp, OPENTHREAD_URI_PENDING_SET, OPENTHREAD_URI_PENDING_GET), - mTimer(aThreadNetif.GetIp6().mTimerScheduler, HandleTimer, this) + mTimer(aThreadNetif.GetIp6().mTimerScheduler, PendingDataset::HandleTimer, this) { } diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 78587015f..1e62fb0f4 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -195,7 +195,7 @@ ThreadError Mle::Start(void) ThreadError error = kThreadError_None; // cannot bring up the interface if IEEE 802.15.4 promiscuous mode is enabled - VerifyOrExit(otPlatRadioGetPromiscuous(NULL) == false, error = kThreadError_Busy); + VerifyOrExit(otPlatRadioGetPromiscuous(mNetif.GetInstance()) == false, error = kThreadError_Busy); VerifyOrExit(mNetif.IsUp(), error = kThreadError_InvalidState); mDeviceState = kDeviceStateDetached; diff --git a/src/core/thread/thread_netif.cpp b/src/core/thread/thread_netif.cpp index 01b114340..37a31c5d8 100644 --- a/src/core/thread/thread_netif.cpp +++ b/src/core/thread/thread_netif.cpp @@ -41,6 +41,7 @@ #include #include #include +#include using Thread::Encoding::BigEndian::HostSwap16; @@ -151,4 +152,9 @@ ThreadError ThreadNetif::SendMessage(Message &message) return mMeshForwarder.SendMessage(message); } +otInstance *ThreadNetif::GetInstance() +{ + return otInstanceFromThreadNetif(this); +} + } // namespace Thread diff --git a/src/core/thread/thread_netif.hpp b/src/core/thread/thread_netif.hpp index 419d695b6..d3800c85c 100644 --- a/src/core/thread/thread_netif.hpp +++ b/src/core/thread/thread_netif.hpp @@ -261,6 +261,14 @@ public: MeshCoP::Joiner &GetJoiner(void) { return mJoiner; } #endif // OPENTHREAD_ENABLE_JOINER + /** + * This method returns the pointer to the parent otInstance structure. + * + * @returns The pointer to the parent otInstance structure. + * + */ + otInstance *GetInstance(); + private: Coap::Server mCoapServer; AddressResolver mAddressResolver; diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index 7c99b7329..0c31f20d3 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -45,7 +46,6 @@ namespace Thread { -extern Ip6::Ip6 *sIp6; static NcpBase *sNcpContext = NULL; #define NCP_PLAT_RESET_REASON (1U<<31) @@ -415,7 +415,7 @@ static uint8_t BorderRouterConfigToFlagByte(const otBorderRouterConfig &config) NcpBase::NcpBase(otInstance *aInstance): mInstance(aInstance), - mUpdateChangedPropsTask(sIp6->mTaskletScheduler, &NcpBase::UpdateChangedProps, this) + mUpdateChangedPropsTask(aInstance->mIp6.mTaskletScheduler, &NcpBase::UpdateChangedProps, this) { assert(mInstance != NULL); mSupportedChannelMask = kPhySupportedChannelMask; diff --git a/src/ncp/ncp_spi.cpp b/src/ncp/ncp_spi.cpp index 3ee63f986..e3f26abaa 100644 --- a/src/ncp/ncp_spi.cpp +++ b/src/ncp/ncp_spi.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #define SPI_RESET_FLAG 0x80 #define SPI_CRC_FLAG 0x40 @@ -48,8 +49,6 @@ namespace Thread { static otDEFINE_ALIGNED_VAR(sNcpRaw, sizeof(NcpSpi), uint64_t); static NcpSpi *sNcpSpi; -extern Ip6::Ip6 *sIp6; - extern "C" void otNcpInit(otInstance *aInstance) { sNcpSpi = new(&sNcpRaw) NcpSpi(aInstance); @@ -89,8 +88,8 @@ static uint16_t spi_header_get_data_len(const uint8_t *header) NcpSpi::NcpSpi(otInstance *aInstance): NcpBase(aInstance), - mHandleRxFrameTask(sIp6->mTaskletScheduler, &NcpSpi::HandleRxFrame, this), - mPrepareTxFrameTask(sIp6->mTaskletScheduler, &NcpSpi::PrepareTxFrame, this), + mHandleRxFrameTask(aInstance->mIp6.mTaskletScheduler, &NcpSpi::HandleRxFrame, this), + mPrepareTxFrameTask(aInstance->mIp6.mTaskletScheduler, &NcpSpi::PrepareTxFrame, this), mTxFrameBuffer(mTxBuffer, sizeof(mTxBuffer)) { memset(mEmptySendFrame, 0, kSpiHeaderLength); diff --git a/src/ncp/ncp_uart.cpp b/src/ncp/ncp_uart.cpp index f81042eb6..3100e9ce7 100644 --- a/src/ncp/ncp_uart.cpp +++ b/src/ncp/ncp_uart.cpp @@ -46,14 +46,13 @@ #include #include #include +#include namespace Thread { static otDEFINE_ALIGNED_VAR(sNcpRaw, sizeof(NcpUart), uint64_t); static NcpUart *sNcpUart; -extern Ip6::Ip6 *sIp6; - extern "C" void otNcpInit(otInstance *aInstance) { sNcpUart = new(&sNcpRaw) NcpUart(aInstance); @@ -91,7 +90,7 @@ NcpUart::NcpUart(otInstance *aInstance): mFrameDecoder(mRxBuffer, sizeof(mRxBuffer), &NcpUart::HandleFrame, &NcpUart::HandleError, this), mUartBuffer(), mTxFrameBuffer(mTxBuffer, sizeof(mTxBuffer)), - mUartSendTask(sIp6->mTaskletScheduler, EncodeAndSendToUart, this) + mUartSendTask(aInstance->mIp6.mTaskletScheduler, EncodeAndSendToUart, this) { mState = kStartingFrame; diff --git a/tests/unit/test_platform.cpp b/tests/unit/test_platform.cpp index bee6d129c..85d554b7b 100644 --- a/tests/unit/test_platform.cpp +++ b/tests/unit/test_platform.cpp @@ -65,11 +65,6 @@ extern "C" { { } - bool otAreTaskletsPending(otInstance *) - { - return false; - } - // // Alarm // diff --git a/tests/unit/test_timer.cpp b/tests/unit/test_timer.cpp index 5c89c49dd..232522a53 100644 --- a/tests/unit/test_timer.cpp +++ b/tests/unit/test_timer.cpp @@ -32,6 +32,7 @@ #include #include #include +#include enum { @@ -42,7 +43,6 @@ enum kCallCountIndexMax }; -static Thread::TimerScheduler sTimerScheduler; extern uint32_t sNow; extern uint32_t sPlatT0; extern uint32_t sPlatDt; @@ -71,7 +71,8 @@ int TestOneTimer(void) { const uint32_t kTimeT0 = 1000; const uint32_t kTimerInterval = 10; - Thread::Timer timer(sTimerScheduler, TestTimerHandler, NULL); + otInstance aInstance; + Thread::Timer timer(aInstance.mIp6.mTimerScheduler, TestTimerHandler, NULL); // Test one Timer basic operation. @@ -89,7 +90,7 @@ int TestOneTimer(void) sNow += kTimerInterval; - otPlatAlarmFired(NULL); + otPlatAlarmFired(&aInstance); VerifyOrQuit(sCallCount[kCallCountIndexAlarmStart] == 1, "TestOneTimer: Start CallCount Failed.\n"); VerifyOrQuit(sCallCount[kCallCountIndexAlarmStop] == 1, "TestOneTimer: Stop CallCount Failed.\n"); @@ -113,7 +114,7 @@ int TestOneTimer(void) sNow += kTimerInterval; - otPlatAlarmFired(NULL); + otPlatAlarmFired(&aInstance); VerifyOrQuit(sCallCount[kCallCountIndexAlarmStart] == 1, "TestOneTimer: Start CallCount Failed.\n"); VerifyOrQuit(sCallCount[kCallCountIndexAlarmStop] == 1, "TestOneTimer: Stop CallCount Failed.\n"); @@ -137,7 +138,7 @@ int TestOneTimer(void) sNow += kTimerInterval + 5; - otPlatAlarmFired(NULL); + otPlatAlarmFired(&aInstance); VerifyOrQuit(sCallCount[kCallCountIndexAlarmStart] == 1, "TestOneTimer: Start CallCount Failed.\n"); VerifyOrQuit(sCallCount[kCallCountIndexAlarmStop] == 1, "TestOneTimer: Stop CallCount Failed.\n"); @@ -161,7 +162,7 @@ int TestOneTimer(void) sNow += kTimerInterval - 2; - otPlatAlarmFired(NULL); + otPlatAlarmFired(&aInstance); VerifyOrQuit(sCallCount[kCallCountIndexAlarmStart] == 2, "TestOneTimer: Start CallCount Failed.\n"); VerifyOrQuit(sCallCount[kCallCountIndexAlarmStop] == 0, "TestOneTimer: Stop CallCount Failed.\n"); @@ -171,7 +172,7 @@ int TestOneTimer(void) sNow += kTimerInterval; - otPlatAlarmFired(NULL); + otPlatAlarmFired(&aInstance); VerifyOrQuit(sCallCount[kCallCountIndexAlarmStart] == 2, "TestOneTimer: Start CallCount Failed.\n"); VerifyOrQuit(sCallCount[kCallCountIndexAlarmStop] == 1, "TestOneTimer: Stop CallCount Failed.\n"); @@ -302,17 +303,19 @@ int TestTenTimers(void) 11 }; + otInstance aInstance; + uint32_t timerContextHandleCounter[kNumTimers] = {0}; - Thread::Timer timer0(sTimerScheduler, TestTimerHandler, &timerContextHandleCounter[0]); - Thread::Timer timer1(sTimerScheduler, TestTimerHandler, &timerContextHandleCounter[1]); - Thread::Timer timer2(sTimerScheduler, TestTimerHandler, &timerContextHandleCounter[2]); - Thread::Timer timer3(sTimerScheduler, TestTimerHandler, &timerContextHandleCounter[3]); - Thread::Timer timer4(sTimerScheduler, TestTimerHandler, &timerContextHandleCounter[4]); - Thread::Timer timer5(sTimerScheduler, TestTimerHandler, &timerContextHandleCounter[5]); - Thread::Timer timer6(sTimerScheduler, TestTimerHandler, &timerContextHandleCounter[6]); - Thread::Timer timer7(sTimerScheduler, TestTimerHandler, &timerContextHandleCounter[7]); - Thread::Timer timer8(sTimerScheduler, TestTimerHandler, &timerContextHandleCounter[8]); - Thread::Timer timer9(sTimerScheduler, TestTimerHandler, &timerContextHandleCounter[9]); + Thread::Timer timer0(aInstance.mIp6.mTimerScheduler, TestTimerHandler, &timerContextHandleCounter[0]); + Thread::Timer timer1(aInstance.mIp6.mTimerScheduler, TestTimerHandler, &timerContextHandleCounter[1]); + Thread::Timer timer2(aInstance.mIp6.mTimerScheduler, TestTimerHandler, &timerContextHandleCounter[2]); + Thread::Timer timer3(aInstance.mIp6.mTimerScheduler, TestTimerHandler, &timerContextHandleCounter[3]); + Thread::Timer timer4(aInstance.mIp6.mTimerScheduler, TestTimerHandler, &timerContextHandleCounter[4]); + Thread::Timer timer5(aInstance.mIp6.mTimerScheduler, TestTimerHandler, &timerContextHandleCounter[5]); + Thread::Timer timer6(aInstance.mIp6.mTimerScheduler, TestTimerHandler, &timerContextHandleCounter[6]); + Thread::Timer timer7(aInstance.mIp6.mTimerScheduler, TestTimerHandler, &timerContextHandleCounter[7]); + Thread::Timer timer8(aInstance.mIp6.mTimerScheduler, TestTimerHandler, &timerContextHandleCounter[8]); + Thread::Timer timer9(aInstance.mIp6.mTimerScheduler, TestTimerHandler, &timerContextHandleCounter[9]); Thread::Timer *timers[kNumTimers] = {&timer0, &timer1, &timer2, &timer3, &timer4, &timer5, &timer6, &timer7, &timer8, &timer9}; size_t i; @@ -353,7 +356,7 @@ int TestTenTimers(void) // timer is ready to be triggered by examining the aDt arg passed into otPlatAlarmStartAt(). If // that value is 0, then otPlatAlarmFired should be fired immediately. This loop calls otPlatAlarmFired() // the requisite number of times based on the aDt argument. - otPlatAlarmFired(NULL); + otPlatAlarmFired(&aInstance); } while (sPlatDt == 0);