Refactor OpenThread Global Variables into a Single Context Structure (#357)

* Refactor OpenThread to contain all global/static variables in a single context structure.
This commit is contained in:
Nick Banks
2016-10-04 15:23:25 -07:00
committed by Jonathan Hui
parent 671964c2d4
commit 79d99e7a91
27 changed files with 619 additions and 460 deletions
-1
View File
@@ -89,4 +89,3 @@ draft-spinel-protocol-bis.xml: \
spinel-tech-thread.md \
spinel-test-vectors.md \
$(NULL)
+20
View File
@@ -44,6 +44,26 @@
extern "C" {
#endif
#ifdef _WIN32
#ifdef WINDOWS_KERNEL
#include <ntdef.h>
#else
#include <windows.h>
#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.
*/
+3
View File
@@ -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
+5 -6
View File
@@ -42,6 +42,7 @@
#include <string.h>
#include <openthread.h>
#include <openthread-instance.h>
#include <openthread-diag.h>
#include <commissioning/commissioner.h>
#include <commissioning/joiner.h>
@@ -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(&timestamp, sizeof(timestamp)));
SuccessOrExit(error = message->SetLength(sLength));
SuccessOrExit(error = sIp6->mIcmp.SendEchoRequest(*message, sMessageInfo));
SuccessOrExit(error = mInstance->mIp6.mIcmp.SendEchoRequest(*message, sMessageInfo));
sCount--;
exit:
+1
View File
@@ -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 \
+4
View File
@@ -36,6 +36,10 @@
#include <stdbool.h>
// 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))]
+1
View File
@@ -35,6 +35,7 @@
#include <common/code_utils.hpp>
#include <common/debug.hpp>
#include <common/logging.hpp>
#include <common/message.hpp>
#include <common/logging.hpp>
#include <net/ip6.hpp>
+7 -1
View File
@@ -35,6 +35,7 @@
#include <common/code_utils.hpp>
#include <common/debug.hpp>
#include <common/tasklet.hpp>
#include <net/ip6.hpp>
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
+10
View File
@@ -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;
+13 -9
View File
@@ -33,18 +33,17 @@
#include <common/code_utils.hpp>
#include <common/timer.hpp>
#include <common/debug.hpp>
#include <common/logging.hpp>
#include <net/ip6.hpp>
#include <platform/alarm.h>
#include <openthread-instance.h>
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();
+10
View File
@@ -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);
+32 -34
View File
@@ -51,6 +51,7 @@
#include <platform/random.h>
#include <thread/mle_router.hpp>
#include <thread/thread_netif.hpp>
#include <openthread-instance.h>
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<Frame *>(otPlatRadioGetTransmitBuffer(NULL)));
Frame &sendFrame(*static_cast<Frame *>(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<Frame *>(otPlatRadioGetTransmitBuffer(NULL)));
Frame &sendFrame(*static_cast<Frame *>(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<Frame *>(aFrame), aError);
aInstance->mThreadNetif.GetMac().ReceiveDoneTask(static_cast<Frame *>(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");
}
+6
View File
@@ -43,6 +43,7 @@
#include <net/netif.hpp>
#include <net/udp6.hpp>
#include <thread/mle.hpp>
#include <openthread-instance.h>
namespace Thread {
namespace Ip6 {
@@ -831,5 +832,10 @@ exit:
return rval;
}
otInstance *Ip6::GetInstance()
{
return otInstanceFromIp6(this);
}
} // namespace Ip6
} // namespace Thread
+18
View File
@@ -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);
}
/**
* @}
*
-3
View File
@@ -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.
+92
View File
@@ -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 <stdint.h>
#include <stdbool.h>
#include <openthread-types.h>
#include <crypto/mbedtls.hpp>
#include <net/ip6.hpp>
#include <thread/thread_netif.hpp>
/**
* 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_
+349 -370
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -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);
+3 -1
View File
@@ -37,6 +37,8 @@
#include <stdio.h>
#include <openthread-types.h>
#include <common/code_utils.hpp>
#include <common/debug.hpp>
#include <coap/coap_header.hpp>
#include <common/debug.hpp>
#include <common/code_utils.hpp>
@@ -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)
{
}
+1 -1
View File
@@ -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;
+6
View File
@@ -41,6 +41,7 @@
#include <thread/thread_netif.hpp>
#include <thread/thread_tlvs.hpp>
#include <thread/thread_uris.hpp>
#include <openthread-instance.h>
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
+8
View File
@@ -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;
+2 -2
View File
@@ -38,6 +38,7 @@
#include <net/ip6.hpp>
#include <openthread.h>
#include <openthread-diag.h>
#include <openthread-instance.h>
#include <stdarg.h>
#include <platform/radio.h>
#include <platform/misc.h>
@@ -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;
+3 -4
View File
@@ -37,6 +37,7 @@
#include <ncp/ncp_spi.hpp>
#include <platform/spi-slave.h>
#include <core/openthread-core-config.h>
#include <openthread-instance.h>
#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);
+2 -3
View File
@@ -46,14 +46,13 @@
#include <platform/logging.h>
#include <platform/uart.h>
#include <core/openthread-core-config.h>
#include <openthread-instance.h>
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;
-5
View File
@@ -65,11 +65,6 @@ extern "C" {
{
}
bool otAreTaskletsPending(otInstance *)
{
return false;
}
//
// Alarm
//
+21 -18
View File
@@ -32,6 +32,7 @@
#include <common/timer.hpp>
#include <platform/alarm.h>
#include <string.h>
#include <openthread-instance.h>
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);