[mac-filter] hide the mFiltered in public filter APIs (#3722)

This commit improves the `Mac::Filter` implementation. It mainly
removes the internally used `mFiltered` variable from the public
`otMacFilterEntry` definition and moves it inside `Mac::Filter`
private definitions.
This commit is contained in:
Abtin Keshavarzian
2019-04-02 10:20:22 -07:00
committed by Jonathan Hui
parent b6a539c8d5
commit c862afc6b3
3 changed files with 116 additions and 113 deletions
-1
View File
@@ -99,7 +99,6 @@ typedef struct otMacFilterEntry
{ {
otExtAddress mExtAddress; ///< IEEE 802.15.4 Extended Address otExtAddress mExtAddress; ///< IEEE 802.15.4 Extended Address
int8_t mRssIn; ///< Received signal strength int8_t mRssIn; ///< Received signal strength
bool mFiltered; ///< Indicates whether or not this entry is filtered.
} otMacFilterEntry; } otMacFilterEntry;
/** /**
+80 -90
View File
@@ -44,46 +44,44 @@ namespace Mac {
Filter::Filter(void) Filter::Filter(void)
: mAddressMode(OT_MAC_FILTER_ADDRESS_MODE_DISABLED) : mAddressMode(OT_MAC_FILTER_ADDRESS_MODE_DISABLED)
, mRssIn(OT_MAC_FILTER_FIXED_RSS_DISABLED) , mDefaultRssIn(kFixedRssDisabled)
{ {
for (int i = 0; i < GetMaxEntries(); i++) for (FilterEntry *entry = &mFilterEntries[0]; entry < OT_ARRAY_END(mFilterEntries); entry++)
{ {
memset(&mEntries[i], 0, sizeof(Entry)); entry->mFiltered = false;
entry->mRssIn = kFixedRssDisabled;
mEntries[i].mFiltered = false;
mEntries[i].mRssIn = OT_MAC_FILTER_FIXED_RSS_DISABLED;
} }
} }
Filter::Entry *Filter::FindEntry(const ExtAddress &aExtAddress) Filter::FilterEntry *Filter::FindEntry(const ExtAddress &aExtAddress)
{ {
Entry *entry = NULL; FilterEntry *entry;
for (uint8_t i = 0; i < GetMaxEntries(); i++) for (entry = &mFilterEntries[0]; entry < OT_ARRAY_END(mFilterEntries); entry++)
{ {
if ((mEntries[i].mFiltered || mEntries[i].mRssIn != OT_MAC_FILTER_FIXED_RSS_DISABLED) && if (entry->IsInUse() && (aExtAddress == entry->mExtAddress))
(aExtAddress == static_cast<const ExtAddress &>(mEntries[i].mExtAddress)))
{ {
ExitNow(entry = &mEntries[i]); ExitNow();
} }
} }
entry = NULL;
exit: exit:
return entry; return entry;
} }
Filter::Entry *Filter::FindAvailEntry(void) Filter::FilterEntry *Filter::FindAvailableEntry(void)
{ {
Entry *entry = NULL; FilterEntry *entry;
for (uint8_t i = 0; i < GetMaxEntries(); i++) for (entry = &mFilterEntries[0]; entry < OT_ARRAY_END(mFilterEntries); entry++)
{ {
if (!mEntries[i].mFiltered && mEntries[i].mRssIn == OT_MAC_FILTER_FIXED_RSS_DISABLED) VerifyOrExit(entry->IsInUse());
{
ExitNow(entry = &mEntries[i]);
}
} }
entry = NULL;
exit: exit:
return entry; return entry;
} }
@@ -104,20 +102,16 @@ exit:
otError Filter::AddAddress(const ExtAddress &aExtAddress) otError Filter::AddAddress(const ExtAddress &aExtAddress)
{ {
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
Entry * entry = FindEntry(aExtAddress); FilterEntry *entry = FindEntry(aExtAddress);
if (entry == NULL) if (entry == NULL)
{ {
VerifyOrExit((entry = FindAvailEntry()) != NULL, error = OT_ERROR_NO_BUFS); VerifyOrExit((entry = FindAvailableEntry()) != NULL, error = OT_ERROR_NO_BUFS);
entry->mExtAddress = aExtAddress; entry->mExtAddress = aExtAddress;
} }
if (entry->mFiltered) VerifyOrExit(!entry->mFiltered, error = OT_ERROR_ALREADY);
{
ExitNow(error = OT_ERROR_ALREADY);
}
entry->mFiltered = true; entry->mFiltered = true;
exit: exit:
@@ -126,8 +120,8 @@ exit:
otError Filter::RemoveAddress(const ExtAddress &aExtAddress) otError Filter::RemoveAddress(const ExtAddress &aExtAddress)
{ {
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
Entry * entry = FindEntry(aExtAddress); FilterEntry *entry = FindEntry(aExtAddress);
if (entry == NULL || !entry->mFiltered) if (entry == NULL || !entry->mFiltered)
{ {
@@ -142,72 +136,68 @@ exit:
void Filter::ClearAddresses(void) void Filter::ClearAddresses(void)
{ {
for (uint8_t i = 0; i < GetMaxEntries(); i++) for (FilterEntry *entry = &mFilterEntries[0]; entry < OT_ARRAY_END(mFilterEntries); entry++)
{ {
mEntries[i].mFiltered = false; entry->mFiltered = false;
} }
} }
otError Filter::GetNextAddress(otMacFilterIterator &aIterator, Entry &aEntry) otError Filter::GetNextAddress(Iterator &aIterator, Entry &aEntry)
{ {
otError error = OT_ERROR_NOT_FOUND; otError error = OT_ERROR_NOT_FOUND;
uint8_t i = *reinterpret_cast<uint8_t *>(&aIterator);
for (; i < GetMaxEntries(); i++) for (; aIterator < OT_ARRAY_LENGTH(mFilterEntries); aIterator++)
{ {
if (mEntries[i].mFiltered) FilterEntry &entry = mFilterEntries[aIterator];
if (entry.mFiltered)
{ {
aEntry = mEntries[i]; aEntry.mExtAddress = entry.mExtAddress;
aIterator = *reinterpret_cast<otMacFilterIterator *>(&(++i)); aEntry.mRssIn = entry.mRssIn;
ExitNow(error = OT_ERROR_NONE); error = OT_ERROR_NONE;
aIterator++;
break;
} }
} }
exit:
return error; return error;
} }
otError Filter::AddRssIn(const ExtAddress *aExtAddress, int8_t aRss) otError Filter::AddRssIn(const ExtAddress *aExtAddress, int8_t aRss)
{ {
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
FilterEntry *entry;
// set the default RssIn for all received messages. // Set the default RssIn when aExtAddress is not given (NULL)
if (aExtAddress == NULL) VerifyOrExit(aExtAddress != NULL, mDefaultRssIn = aRss);
entry = FindEntry(*aExtAddress);
if (entry == NULL)
{ {
mRssIn = aRss; entry = FindAvailableEntry();
ExitNow(); VerifyOrExit(entry != NULL, error = OT_ERROR_NO_BUFS);
}
else
{
Entry *entry = FindEntry(*aExtAddress);
if (entry == NULL) entry->mExtAddress = *aExtAddress;
{
VerifyOrExit((entry = FindAvailEntry()) != NULL, error = OT_ERROR_NO_BUFS);
entry->mExtAddress = static_cast<const otExtAddress &>(*aExtAddress);
}
entry->mRssIn = aRss;
} }
entry->mRssIn = aRss;
exit: exit:
return error; return error;
} }
otError Filter::RemoveRssIn(const ExtAddress *aExtAddress) otError Filter::RemoveRssIn(const ExtAddress *aExtAddress)
{ {
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
FilterEntry *entry;
if (aExtAddress == NULL) // If no aExtAddress is given, remove default RssIn
{ VerifyOrExit(aExtAddress != NULL, mDefaultRssIn = kFixedRssDisabled);
mRssIn = OT_MAC_FILTER_FIXED_RSS_DISABLED;
} entry = FindEntry(*aExtAddress);
else VerifyOrExit(entry != NULL, error = OT_ERROR_NOT_FOUND);
{ entry->mRssIn = kFixedRssDisabled;
Entry *entry = FindEntry(*aExtAddress);
VerifyOrExit(entry != NULL, error = OT_ERROR_NOT_FOUND);
entry->mRssIn = OT_MAC_FILTER_FIXED_RSS_DISABLED;
}
exit: exit:
return error; return error;
@@ -215,36 +205,39 @@ exit:
void Filter::ClearRssIn(void) void Filter::ClearRssIn(void)
{ {
mRssIn = OT_MAC_FILTER_FIXED_RSS_DISABLED; for (FilterEntry *entry = &mFilterEntries[0]; entry < OT_ARRAY_END(mFilterEntries); entry++)
for (uint8_t i = 0; i < GetMaxEntries(); i++)
{ {
mEntries[i].mRssIn = OT_MAC_FILTER_FIXED_RSS_DISABLED; entry->mRssIn = kFixedRssDisabled;
} }
mDefaultRssIn = kFixedRssDisabled;
} }
otError Filter::GetNextRssIn(otMacFilterIterator &aIterator, Entry &aEntry) otError Filter::GetNextRssIn(Iterator &aIterator, Entry &aEntry)
{ {
otError error = OT_ERROR_NOT_FOUND; otError error = OT_ERROR_NOT_FOUND;
uint8_t i = *reinterpret_cast<uint8_t *>(&aIterator);
for (; i < GetMaxEntries(); i++) for (; aIterator < OT_ARRAY_LENGTH(mFilterEntries); aIterator++)
{ {
if (mEntries[i].mRssIn != OT_MAC_FILTER_FIXED_RSS_DISABLED) FilterEntry &entry = mFilterEntries[aIterator];
if (entry.mRssIn != kFixedRssDisabled)
{ {
aEntry = mEntries[i]; aEntry.mExtAddress = entry.mExtAddress;
aIterator = *reinterpret_cast<otMacFilterIterator *>(&(++i)); aEntry.mRssIn = entry.mRssIn;
ExitNow(error = OT_ERROR_NONE); error = OT_ERROR_NONE;
aIterator++;
ExitNow();
} }
} }
// return default rssin setting if no more rssin filter entry. // Return the default RssIn at the end of list
if (i == GetMaxEntries() && mRssIn != OT_MAC_FILTER_FIXED_RSS_DISABLED) if ((aIterator == OT_ARRAY_LENGTH(mFilterEntries)) && (mDefaultRssIn != kFixedRssDisabled))
{ {
memset(&aEntry.mExtAddress, 0xff, OT_EXT_ADDRESS_SIZE); memset(&aEntry.mExtAddress, 0xff, OT_EXT_ADDRESS_SIZE);
aEntry.mRssIn = mRssIn; aEntry.mRssIn = mDefaultRssIn;
aIterator = *reinterpret_cast<otMacFilterIterator *>(&(++i)); error = OT_ERROR_NONE;
ExitNow(error = OT_ERROR_NONE); aIterator++;
} }
exit: exit:
@@ -253,14 +246,12 @@ exit:
otError Filter::Apply(const ExtAddress &aExtAddress, int8_t &aRss) otError Filter::Apply(const ExtAddress &aExtAddress, int8_t &aRss)
{ {
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
FilterEntry *entry = FindEntry(aExtAddress);
otMacFilterEntry *entry = FindEntry(aExtAddress); // Use the default RssIn setting for all receiving messages first.
aRss = mDefaultRssIn;
// assign the default RssIn setting for all receiving messages first.
aRss = mRssIn;
// check AddressFilter.
if (mAddressMode == OT_MAC_FILTER_ADDRESS_MODE_WHITELIST) if (mAddressMode == OT_MAC_FILTER_ADDRESS_MODE_WHITELIST)
{ {
VerifyOrExit(entry != NULL && entry->mFiltered, error = OT_ERROR_ADDRESS_FILTERED); VerifyOrExit(entry != NULL && entry->mFiltered, error = OT_ERROR_ADDRESS_FILTERED);
@@ -270,8 +261,7 @@ otError Filter::Apply(const ExtAddress &aExtAddress, int8_t &aRss)
VerifyOrExit(entry == NULL || !entry->mFiltered, error = OT_ERROR_ADDRESS_FILTERED); VerifyOrExit(entry == NULL || !entry->mFiltered, error = OT_ERROR_ADDRESS_FILTERED);
} }
// not override the default RssIn setting if no specific RssIn on the Extended Address. if ((entry != NULL) && (entry->mRssIn != kFixedRssDisabled))
if (entry != NULL && entry->mRssIn != OT_MAC_FILTER_FIXED_RSS_DISABLED)
{ {
aRss = entry->mRssIn; aRss = entry->mRssIn;
} }
+36 -22
View File
@@ -59,11 +59,24 @@ namespace Mac {
class Filter class Filter
{ {
public: public:
/**
* This structure represents a Mac Filter entry (used during iteration).
*
*/
typedef otMacFilterEntry Entry; typedef otMacFilterEntry Entry;
/**
* This type represents an iterator used to iterate through filter entries.
*
* See `GetNextAddress()` and `GetNextRssIn()`.
*
*/
typedef otMacFilterIterator Iterator;
enum enum
{ {
kMaxEntries = OPENTHREAD_CONFIG_MAC_FILTER_SIZE, kMaxEntries = OPENTHREAD_CONFIG_MAC_FILTER_SIZE, ///< The maximum number of filter entries.
kFixedRssDisabled = OT_MAC_FILTER_FIXED_RSS_DISABLED, ///< Value to indicate no fixed RSS is set.
}; };
/** /**
@@ -72,14 +85,6 @@ public:
*/ */
Filter(void); Filter(void);
/**
* This method returns the maximum number of filter entries.
*
* @returns The maximum number of filter entries.
*
*/
uint8_t GetMaxEntries(void) const { return kMaxEntries; }
/** /**
* This function gets the address mode of the filter. * This function gets the address mode of the filter.
* *
@@ -129,17 +134,17 @@ public:
void ClearAddresses(void); void ClearAddresses(void);
/** /**
* This method gets an in-use address filter entry. * This method iterates through filter entries.
* *
* @param[inout] aIterator A reference to the MAC filter iterator context. To get the first in-use address filter * @param[inout] aIterator A reference to the MAC filter iterator context.
* entry, it should be set to OT_MAC_FILTER_ITERATOR_INIT. * To get the first in-use address filter, set it to OT_MAC_FILTER_ITERATOR_INIT.
* @param[out] aEntry A reference to where the information is placed. * @param[out] aEntry A reference to where the information is placed.
* *
* @retval OT_ERROR_NONE Successfully retrieved an in-use address filter entry. * @retval OT_ERROR_NONE Successfully retrieved the next address filter entry.
* @retval OT_ERROR_NOT_FOUND No subsequent entry exists. * @retval OT_ERROR_NOT_FOUND No subsequent entry exists.
* *
*/ */
otError GetNextAddress(otMacFilterIterator &aIterator, Entry &aEntry); otError GetNextAddress(Iterator &aIterator, Entry &aEntry);
/** /**
* This method sets the received signal strength for the messages from the Extended Address. * This method sets the received signal strength for the messages from the Extended Address.
@@ -176,7 +181,7 @@ public:
void ClearRssIn(void); void ClearRssIn(void);
/** /**
* This method gets an in-use RssIn filter entry. * This method iterates through RssIn filter entry.
* *
* @param[inout] aIterator A reference to the MAC filter iterator context. To get the first in-use RssIn * @param[inout] aIterator A reference to the MAC filter iterator context. To get the first in-use RssIn
* filter entry, it should be set to OT_MAC_FILTER_ITERATOR_INIT. * filter entry, it should be set to OT_MAC_FILTER_ITERATOR_INIT.
@@ -184,14 +189,14 @@ public:
* Extended Address as all 0xff to indicate the default received signal strength * Extended Address as all 0xff to indicate the default received signal strength
* if it was set. * if it was set.
* *
* @retval OT_ERROR_NONE Successfully retrieved the in-use RssIn filter entry. * @retval OT_ERROR_NONE Successfully retrieved the next RssIn filter entry.
* @retval OT_ERROR_NOT_FOUND No subsequent entry exists. * @retval OT_ERROR_NOT_FOUND No subsequent entry exists.
* *
*/ */
otError GetNextRssIn(otMacFilterIterator &aIterator, Entry &aEntry); otError GetNextRssIn(Iterator &aIterator, Entry &aEntry);
/** /**
* This method applies the filter rules on the Extended Address. * This method applies the filter rules on a given Extended Address.
* *
* @param[in] aExtAddress A reference to the Extended Address. * @param[in] aExtAddress A reference to the Extended Address.
* @param[out] aRss A reference to where the received signal strength to be placed. * @param[out] aRss A reference to where the received signal strength to be placed.
@@ -204,12 +209,21 @@ public:
otError Apply(const ExtAddress &aExtAddress, int8_t &aRss); otError Apply(const ExtAddress &aExtAddress, int8_t &aRss);
private: private:
Entry *FindAvailEntry(void); struct FilterEntry
Entry *FindEntry(const ExtAddress &aExtAddress); {
bool mFiltered; // Indicates whether or not this entry is filtered (whitelist/blacklist modes).
int8_t mRssIn; // The RssIn value for this entry or `kFixedRssDisabled`.
ExtAddress mExtAddress; // IEEE 802.15.4 Extended Address.
bool IsInUse(void) const { return mFiltered || (mRssIn != kFixedRssDisabled); }
};
FilterEntry *FindAvailableEntry(void);
FilterEntry *FindEntry(const ExtAddress &aExtAddress);
Entry mEntries[kMaxEntries];
otMacFilterAddressMode mAddressMode; otMacFilterAddressMode mAddressMode;
int8_t mRssIn; int8_t mDefaultRssIn;
FilterEntry mFilterEntries[kMaxEntries];
}; };
/** /**