diff --git a/.travis/script.sh b/.travis/script.sh index b4f608980..09c2829bf 100755 --- a/.travis/script.sh +++ b/.travis/script.sh @@ -61,7 +61,7 @@ set -x --enable-jam-detection \ --enable-joiner \ --enable-legacy \ - --enable-mac-whitelist \ + --enable-mac-filter \ --enable-mtd-network-diagnostic \ --enable-raw-link-api \ --enable-tmf-proxy || die diff --git a/Makefile.am b/Makefile.am index 81861e62b..ccf46bfa9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -44,6 +44,7 @@ AM_DISTCHECK_CONFIGURE_FLAGS = \ --enable-dns-client \ --enable-application-coap \ --enable-border-router \ + --enable-mac-filter \ $(NULL) SUBDIRS = \ diff --git a/configure.ac b/configure.ac index a1713a1f3..50271ea2c 100644 --- a/configure.ac +++ b/configure.ac @@ -742,36 +742,36 @@ AM_CONDITIONAL([OPENTHREAD_ENABLE_JAM_DETECTION], [test "${enable_jam_detection} AC_DEFINE_UNQUOTED([OPENTHREAD_ENABLE_JAM_DETECTION],[${OPENTHREAD_ENABLE_JAM_DETECTION}],[Define to 1 if you want to use jam detection feature]) # -# MAC Whitelist and Blacklist +# MAC Filter (include AddressFilter and RssInFilter) # -AC_ARG_ENABLE(mac_whitelist, - [AS_HELP_STRING([--enable-mac-whitelist],[Enable MAC whitelist/blacklist support @<:@default=yes@:>@.])], +AC_ARG_ENABLE(mac_filter, + [AS_HELP_STRING([--enable-mac-filter],[Enable MAC filter support @<:@default=yes@:>@.])], [ case "${enableval}" in no|yes) - enable_mac_whitelist=${enableval} + enable_mac_filter=${enableval} ;; *) - AC_MSG_ERROR([Invalid value ${enable_mac_whitelist} for --enable-mac-whitelist]) + AC_MSG_ERROR([Invalid value ${enable_mac_filter} for --enable-mac-filter]) ;; esac ], - [enable_mac_whitelist=yes]) + [enable_mac_filter=no]) -if test "$enable_mac_whitelist" = "yes"; then - OPENTHREAD_ENABLE_MAC_WHITELIST=1 +if test "$enable_mac_filter" = "yes"; then + OPENTHREAD_ENABLE_MAC_FILTER=1 else - OPENTHREAD_ENABLE_MAC_WHITELIST=0 + OPENTHREAD_ENABLE_MAC_FILTER=0 fi -AC_MSG_CHECKING([whether to enable mac whitelist]) -AC_MSG_RESULT(${enable_mac_whitelist}) -AC_SUBST(OPENTHREAD_ENABLE_MAC_WHITELIST) -AM_CONDITIONAL([OPENTHREAD_ENABLE_MAC_WHITELIST], [test "${enable_mac_whitelist}" = "yes"]) -AC_DEFINE_UNQUOTED([OPENTHREAD_ENABLE_MAC_WHITELIST],[${OPENTHREAD_ENABLE_MAC_WHITELIST}],[Define to 1 if you want to use MAC whitelist/blacklist feature]) +AC_MSG_CHECKING([whether to enable mac filter]) +AC_MSG_RESULT(${enable_mac_filter}) +AC_SUBST(OPENTHREAD_ENABLE_MAC_FILTER) +AM_CONDITIONAL([OPENTHREAD_ENABLE_MAC_FILTER], [test "${enable_mac_filter}" = "yes"]) +AC_DEFINE_UNQUOTED([OPENTHREAD_ENABLE_MAC_FILTER],[${OPENTHREAD_ENABLE_MAC_FILTER}],[Define to 1 if you want to use MAC filter feature]) # # Diagnostics Library @@ -1429,7 +1429,7 @@ AC_MSG_NOTICE([ OpenThread Joiner support : ${enable_joiner} OpenThread DTLS support : ${enable_dtls} OpenThread Jam Detection support : ${enable_jam_detection} - OpenThread MAC Whitelist support : ${enable_mac_whitelist} + OpenThread MAC Filter support : ${enable_mac_filter} OpenThread Diagnostics support : ${enable_diag} OpenThread Child Supervision support : ${enable_child_supervision} OpenThread Legacy network support : ${enable_legacy} diff --git a/etc/visual-studio/libopenthread.vcxproj b/etc/visual-studio/libopenthread.vcxproj index e1e702a6d..f2257092b 100644 --- a/etc/visual-studio/libopenthread.vcxproj +++ b/etc/visual-studio/libopenthread.vcxproj @@ -90,9 +90,8 @@ - + - @@ -168,13 +167,8 @@ - - - + - - - diff --git a/etc/visual-studio/libopenthread.vcxproj.filters b/etc/visual-studio/libopenthread.vcxproj.filters index 7a7335c39..c7a15d55f 100644 --- a/etc/visual-studio/libopenthread.vcxproj.filters +++ b/etc/visual-studio/libopenthread.vcxproj.filters @@ -153,10 +153,10 @@ Source Files\mac - + Source Files\mac - + Source Files\mac @@ -240,9 +240,6 @@ Source Files\net - - Source Files\mac - Source Files\meshcop @@ -380,18 +377,12 @@ Header Files\mac + + Header Files\mac + Header Files\mac - - Header Files\mac - - - Header Files\mac - - - Header Files\mac - Header Files\net @@ -494,15 +485,6 @@ Header Files\thread - - Header Files\mac - - - Header Files\mac - - - Header Files\mac - Header Files\meshcop diff --git a/etc/visual-studio/libopenthread_k.vcxproj b/etc/visual-studio/libopenthread_k.vcxproj index 78cb04e86..ea389c490 100644 --- a/etc/visual-studio/libopenthread_k.vcxproj +++ b/etc/visual-studio/libopenthread_k.vcxproj @@ -98,9 +98,8 @@ - + - @@ -200,13 +199,8 @@ - - - + - - - diff --git a/etc/visual-studio/libopenthread_k.vcxproj.filters b/etc/visual-studio/libopenthread_k.vcxproj.filters index d3a6d1cfc..981bf68aa 100644 --- a/etc/visual-studio/libopenthread_k.vcxproj.filters +++ b/etc/visual-studio/libopenthread_k.vcxproj.filters @@ -153,10 +153,10 @@ Source Files\mac - + Source Files\mac - + Source Files\mac @@ -243,9 +243,6 @@ Source Files\net - - Source Files\mac - Source Files\meshcop @@ -380,18 +377,12 @@ Header Files\mac + + Header Files\mac + Header Files\mac - - Header Files\mac - - - Header Files\mac - - - Header Files\mac - Header Files\net @@ -497,15 +488,6 @@ Header Files\thread - - Header Files\mac - - - Header Files\mac - - - Header Files\mac - Header Files\meshcop diff --git a/examples/Makefile-posix b/examples/Makefile-posix index ecc3e01cc..efeb6cc82 100644 --- a/examples/Makefile-posix +++ b/examples/Makefile-posix @@ -107,7 +107,7 @@ configure_OPTIONS = \ --enable-jam-detection \ --enable-joiner \ --enable-legacy \ - --enable-mac-whitelist \ + --enable-mac-filter \ --enable-mtd-network-diagnostic \ --enable-raw-link-api \ --enable-tmf-proxy \ diff --git a/examples/common-switches.mk b/examples/common-switches.mk index c43d684fe..da411c518 100644 --- a/examples/common-switches.mk +++ b/examples/common-switches.mk @@ -82,8 +82,8 @@ ifeq ($(LEGACY),1) configure_OPTIONS += --enable-legacy endif -ifeq ($(MAC_WHITELIST),1) -configure_OPTIONS += --enable-mac-whitelist +ifeq ($(MAC_FILTER),1) +configure_OPTIONS += --enable-mac-filter endif ifeq ($(MTD_NETDIAG),1) diff --git a/examples/drivers/windows/include/otLwfIoctl.h b/examples/drivers/windows/include/otLwfIoctl.h index 5dbdf6dab..4a139a45d 100644 --- a/examples/drivers/windows/include/otLwfIoctl.h +++ b/examples/drivers/windows/include/otLwfIoctl.h @@ -350,11 +350,12 @@ typedef enum _OTLWF_NOTIF_TYPE // GUID - InterfaceGuid // otExtAddress - aExtAddr -#define IOCTL_OTLWF_OT_MAC_WHITELIST_ENTRY \ +#define IOCTL_OTLWF_OT_NEXT_MAC_WHITELIST \ OTLWF_CTL_CODE(137, METHOD_BUFFERED, FILE_READ_DATA) // GUID - InterfaceGuid - // uint8_t - aIndex (input) - // otMacWhitelistEntry - aEntry (output) + // uint8_t - aIterator (input) + // uint8_t - aNewIterator (output) + // otMacFilterEntry - aEntry (output) #define IOCTL_OTLWF_OT_CLEAR_MAC_WHITELIST \ OTLWF_CTL_CODE(138, METHOD_BUFFERED, FILE_WRITE_DATA) @@ -445,11 +446,12 @@ typedef enum _OTLWF_NOTIF_TYPE // GUID - InterfaceGuid // otExtAddress - aExtAddr -#define IOCTL_OTLWF_OT_MAC_BLACKLIST_ENTRY \ +#define IOCTL_OTLWF_OT_NEXT_MAC_BLACKLIST \ OTLWF_CTL_CODE(155, METHOD_BUFFERED, FILE_READ_DATA) // GUID - InterfaceGuid - // uint8_t - aIndex (input) - // otMacBlacklistEntry - aEntry (output) + // uint8_t - aIterator (input) + // uint8_t - aNewIterator (output) + // otMacFilterEntry - aEntry (output) #define IOCTL_OTLWF_OT_CLEAR_MAC_BLACKLIST \ OTLWF_CTL_CODE(156, METHOD_BUFFERED, FILE_WRITE_DATA) @@ -478,12 +480,6 @@ typedef enum _OTLWF_NOTIF_TYPE // GUID - InterfaceGuid // uint32_t - aPartitionId -#define IOCTL_OTLWF_OT_ASSIGN_LINK_QUALITY \ - OTLWF_CTL_CODE(161, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA) - // GUID - InterfaceGuid - // otExtAddress - aExtAddr (input) - // uint8_t - aLinkQuality (input or output) - #define IOCTL_OTLWF_OT_PLATFORM_RESET \ OTLWF_CTL_CODE(162, METHOD_BUFFERED, FILE_WRITE_DATA) // GUID - InterfaceGuid @@ -699,8 +695,30 @@ typedef struct otCommissionConfig // GUID - InterfaceGuid // int8_t - aParentPriority +#define IOCTL_OTLWF_OT_ADD_MAC_FIXED_RSS \ + OTLWF_CTL_CODE(197, METHOD_BUFFERED, FILE_WRITE_DATA) + // GUID - InterfaceGuid + // otExtAddress - aExtAddr (optional) + // int8_t - aRssi + +#define IOCTL_OTLWF_OT_REMOVE_MAC_FIXED_RSS \ + OTLWF_CTL_CODE(198, METHOD_BUFFERED, FILE_WRITE_DATA) + // GUID - InterfaceGuid + // otExtAddress - aExtAddr (optional) + +#define IOCTL_OTLWF_OT_NEXT_MAC_FIXED_RSS \ + OTLWF_CTL_CODE(199, METHOD_BUFFERED, FILE_READ_DATA) + // GUID - InterfaceGuid + // uint8_t - aIterator (input) + // uint8_t - aNewIterator (output) + // otMacFilterEntry - aEntry (output) + +#define IOCTL_OTLWF_OT_CLEAR_MAC_FIXED_RSS \ + OTLWF_CTL_CODE(200, METHOD_BUFFERED, FILE_WRITE_DATA) + // GUID - InterfaceGuid + // OpenThread function IOCTL codes #define MIN_OTLWF_IOCTL_FUNC_CODE 100 -#define MAX_OTLWF_IOCTL_FUNC_CODE 196 +#define MAX_OTLWF_IOCTL_FUNC_CODE 200 #endif //__OTLWFIOCTL_H__ diff --git a/examples/drivers/windows/otApi/otApi.cpp b/examples/drivers/windows/otApi/otApi.cpp index f3bdc0e9d..5aabd2529 100644 --- a/examples/drivers/windows/otApi/otApi.cpp +++ b/examples/drivers/windows/otApi/otApi.cpp @@ -2731,90 +2731,6 @@ otThreadReleaseRouterId( return DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_RELEASE_ROUTER_ID, aRouterId)); } -OTAPI -otError -OTCALL -otLinkAddWhitelist( - _In_ otInstance *aInstance, - const uint8_t *aExtAddr - ) -{ - if (aInstance == nullptr) return OT_ERROR_INVALID_ARGS; - return DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_ADD_MAC_WHITELIST, (const otExtAddress*)aExtAddr)); -} - -OTAPI -otError -OTCALL -otLinkAddWhitelistRssi( - _In_ otInstance *aInstance, - const uint8_t *aExtAddr, - int8_t aRssi - ) -{ - if (aInstance == nullptr) return OT_ERROR_INVALID_ARGS; - - PackedBuffer3 Buffer(aInstance->InterfaceGuid, *(otExtAddress*)aExtAddr, aRssi); - return DwordToThreadError(SendIOCTL(aInstance->ApiHandle, IOCTL_OTLWF_OT_ADD_MAC_WHITELIST, &Buffer, sizeof(Buffer), nullptr, 0)); -} - -OTAPI -void -OTCALL -otLinkRemoveWhitelist( - _In_ otInstance *aInstance, - const uint8_t *aExtAddr - ) -{ - if (aInstance) (void)SetIOCTL(aInstance, IOCTL_OTLWF_OT_REMOVE_MAC_WHITELIST, (const otExtAddress*)aExtAddr); -} - -OTAPI -otError -OTCALL -otLinkGetWhitelistEntry( - _In_ otInstance *aInstance, - uint8_t aIndex, - _Out_ otMacWhitelistEntry *aEntry - ) -{ - if (aInstance == nullptr) return OT_ERROR_INVALID_ARGS; - return DwordToThreadError(QueryIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_WHITELIST_ENTRY, &aIndex, aEntry)); -} - -OTAPI -void -OTCALL -otLinkClearWhitelist( - _In_ otInstance *aInstance - ) -{ - if (aInstance) (void)SetIOCTL(aInstance, IOCTL_OTLWF_OT_CLEAR_MAC_WHITELIST); -} - -OTAPI -void -OTCALL -otLinkSetWhitelistEnabled( - _In_ otInstance *aInstance, - bool aEnabled - ) -{ - if (aInstance) (void)SetIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_WHITELIST_ENABLED, (BOOLEAN)aEnabled); -} - -OTAPI -bool -OTCALL -otLinkIsWhitelistEnabled( - _In_ otInstance *aInstance - ) -{ - BOOLEAN Result = 0; - if (aInstance) (void)QueryIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_WHITELIST_ENABLED, &Result); - return Result != FALSE; -} - OTAPI otError OTCALL @@ -2860,99 +2776,262 @@ otThreadBecomeLeader( } OTAPI -otError +otMacFilterAddressMode OTCALL -otLinkAddBlacklist( - _In_ otInstance *aInstance, - const uint8_t *aExtAddr +otLinkFilterGetAddressMode( + _In_ otInstance *aInstance ) { - if (aInstance == nullptr) return OT_ERROR_INVALID_ARGS; - return DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_ADD_MAC_BLACKLIST, (const otExtAddress*)aExtAddr)); -} + otMacFilterAddressMode mode = OT_MAC_FILTER_ADDRESS_MODE_DISABLED; + BOOLEAN enabled = 0; -OTAPI -void -OTCALL -otLinkRemoveBlacklist( - _In_ otInstance *aInstance, - const uint8_t *aExtAddr - ) -{ - if (aInstance) (void)SetIOCTL(aInstance, IOCTL_OTLWF_OT_REMOVE_MAC_BLACKLIST, (const otExtAddress*)aExtAddr); + if (aInstance) + { + (void)QueryIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_WHITELIST_ENABLED, &enabled); + + if (enabled) + { + mode = OT_MAC_FILTER_ADDRESS_MODE_WHITELIST; + } + else + { + (void)QueryIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_BLACKLIST_ENABLED, &enabled); + + if (enabled) + { + mode = OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST; + } + } + } + + return mode; } OTAPI otError OTCALL -otLinkGetBlacklistEntry( - _In_ otInstance *aInstance, - uint8_t aIndex, - _Out_ otMacBlacklistEntry *aEntry +otLinkFilterSetAddressMode( + _In_ otInstance *aInstance, + otMacFilterAddressMode aMode ) { - if (aInstance == nullptr) return OT_ERROR_INVALID_ARGS; - return DwordToThreadError(QueryIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_BLACKLIST_ENTRY, &aIndex, aEntry)); + otError error = OT_ERROR_NONE; + + if (aInstance == nullptr) + { + error = OT_ERROR_INVALID_ARGS; + } + else + { + bool enabled = false; + + if (aMode == OT_MAC_FILTER_ADDRESS_MODE_WHITELIST) + { + enabled = true; + error = DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_WHITELIST_ENABLED, (BOOLEAN)enabled)); + } + else if (aMode == OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST) + { + enabled = true; + error = DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_BLACKLIST_ENABLED, (BOOLEAN)enabled)); + } + else + { + error = DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_WHITELIST_ENABLED, (BOOLEAN)enabled)); + } + } + return error; } OTAPI -void +otError OTCALL -otLinkClearBlacklist( +otLinkFilterAddAddress( + _In_ otInstance *aInstance, + const otExtAddress *aExtAddr + ) +{ + otError error = OT_ERROR_NONE; + + if (aInstance == nullptr || aExtAddr == nullptr) + { + error = OT_ERROR_INVALID_ARGS; + } + else + { + // same effect to add an address to the address filter with IOCTL_OTLWF_OT_ADD_MAC_WHITELIST or + // IOCTL_OTLWF_OT_ADD_MAC_BLACKLIST. + error = DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_ADD_MAC_WHITELIST, aExtAddr)); + } + + return error; +} + + +OTAPI +otError +OTCALL +otLinkFilterRemoveAddress( + _In_ otInstance *aInstance, + const otExtAddress *aExtAddr + ) +{ + otError error = OT_ERROR_NONE; + + if (aInstance == nullptr || aExtAddr == nullptr) + { + error = OT_ERROR_INVALID_ARGS; + } + else + { + // same effect to remove an address from the address filter with IOCTL_OTLWF_OT_REMOVE_MAC_WHITELIST or + // IOCTL_OTLWF_OT_REMOVE_MAC_BLACKLIST. + error = DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_REMOVE_MAC_WHITELIST, aExtAddr)); + } + + return error; +} + +OTAPI +void +OTCALL +otLinkFilterClearAddresses( _In_ otInstance *aInstance ) { - if (aInstance) (void)SetIOCTL(aInstance, IOCTL_OTLWF_OT_CLEAR_MAC_BLACKLIST); + if (aInstance) + { + // same effect to clear the addresses from the address filter with IOCTL_OTLWF_OT_CLEAR_MAC_WHITELIST or + // IOCTL_OTLWF_OT_CLEAR_MAC_BLACKLIST. + (void)SetIOCTL(aInstance, IOCTL_OTLWF_OT_CLEAR_MAC_WHITELIST); + } } OTAPI -void +otError OTCALL -otLinkSetBlacklistEnabled( +otLinkFilterGetNextAddress( _In_ otInstance *aInstance, - bool aEnabled + _Inout_ otMacFilterIterator *aIterator, + _Out_ otMacFilterEntry *aEntry ) { - if (aInstance) (void)SetIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_BLACKLIST_ENABLED, (BOOLEAN)aEnabled); + otError error = OT_ERROR_NONE; + + if (aInstance == nullptr || aIterator == nullptr|| aEntry == nullptr) + { + error = OT_ERROR_INVALID_ARGS; + } + else + { + // same effect to get an in-use address filter entry with IOCTL_OTLWF_OT_NEXT_MAC_WHITELIST or + // IOCTL_OTLWF_OT_NEXT_MAC_BLACKLIST. + return DwordToThreadError(QueryIOCTL(aInstance, IOCTL_OTLWF_OT_NEXT_MAC_WHITELIST, aIterator, aEntry)); + } + return OT_ERROR_NOT_FOUND; +} + + +OTAPI +otError +OTCALL +otLinkFilterAddRssIn( + _In_ otInstance *aInstance, + const otExtAddress *aExtAddr, + int8_t aRss + ) +{ + otError error = OT_ERROR_NONE; + + if (aInstance == nullptr) + { + error = OT_ERROR_INVALID_ARGS; + } + else + { + if (aExtAddr == nullptr) + { + PackedBuffer2 Buffer(aInstance->InterfaceGuid, aRss); + + error = DwordToThreadError(SendIOCTL(aInstance->ApiHandle, IOCTL_OTLWF_OT_ADD_MAC_FIXED_RSS, + &Buffer, sizeof(Buffer), nullptr, 0)); + } + else + { + PackedBuffer3 Buffer(aInstance->InterfaceGuid, *aExtAddr, aRss); + + error = DwordToThreadError(SendIOCTL(aInstance->ApiHandle, IOCTL_OTLWF_OT_ADD_MAC_FIXED_RSS, + &Buffer, sizeof(Buffer), nullptr, 0)); + } + + } + return error; } OTAPI -bool +otError OTCALL -otLinkIsBlacklistEnabled( +otLinkFilterRemoveRssIn( + _In_ otInstance *aInstance, + const otExtAddress *aExtAddr + ) +{ + otError error = OT_ERROR_NONE; + + if (aInstance == nullptr) + { + error = OT_ERROR_INVALID_ARGS; + } + else + { + if (aExtAddr == nullptr) + { + error = DwordToThreadError(SendIOCTL(aInstance->ApiHandle, IOCTL_OTLWF_OT_REMOVE_MAC_FIXED_RSS, + nullptr, 0, nullptr, 0)); + } + else + { + PackedBuffer2 Buffer(aInstance->InterfaceGuid, *aExtAddr); + error = DwordToThreadError(SendIOCTL(aInstance->ApiHandle, IOCTL_OTLWF_OT_REMOVE_MAC_FIXED_RSS, + &Buffer, sizeof(Buffer), nullptr, 0)); + } + } + return error; +} + +OTAPI +void +OTCALL +otLinkFilterClearRssIn( _In_ otInstance *aInstance ) { - BOOLEAN Result = 0; - if (aInstance) (void)QueryIOCTL(aInstance, IOCTL_OTLWF_OT_MAC_BLACKLIST_ENABLED, &Result); - return Result != FALSE; + if (aInstance) + { + (void)SetIOCTL(aInstance, IOCTL_OTLWF_OT_CLEAR_MAC_FIXED_RSS); + } } -OTAPI -otError +OTAPI +otError OTCALL -otLinkGetAssignLinkQuality( - _In_ otInstance *aInstance, - const uint8_t *aExtAddr, - _Out_ uint8_t *aLinkQuality - ) -{ - if (aInstance == nullptr) return OT_ERROR_INVALID_ARGS; - return DwordToThreadError(QueryIOCTL(aInstance, IOCTL_OTLWF_OT_ASSIGN_LINK_QUALITY, (otExtAddress*)aExtAddr, aLinkQuality)); -} - -OTAPI -void -OTCALL -otLinkSetAssignLinkQuality( +otLinkFilterGetNextRssIn( _In_ otInstance *aInstance, - const uint8_t *aExtAddr, - uint8_t aLinkQuality + _Inout_ otMacFilterIterator *aIterator, + _Out_ otMacFilterEntry *aEntry ) { - if (aInstance == nullptr) return; - PackedBuffer3 Buffer(aInstance->InterfaceGuid, *(otExtAddress*)aExtAddr, aLinkQuality); - (void)SendIOCTL(aInstance->ApiHandle, IOCTL_OTLWF_OT_ASSIGN_LINK_QUALITY, &Buffer, sizeof(Buffer), nullptr, 0); + otError error = OT_ERROR_NONE; + + if (aInstance == nullptr || aIterator == nullptr|| aEntry == nullptr) + { + error = OT_ERROR_INVALID_ARGS; + } + else + { + return DwordToThreadError(QueryIOCTL(aInstance, IOCTL_OTLWF_OT_NEXT_MAC_FIXED_RSS, aIterator, aEntry)); + } + return OT_ERROR_NOT_FOUND; } OTAPI diff --git a/examples/drivers/windows/otLwf/iocontrol.c b/examples/drivers/windows/otLwf/iocontrol.c index a1b343ded..c26c2e80c 100644 --- a/examples/drivers/windows/otLwf/iocontrol.c +++ b/examples/drivers/windows/otLwf/iocontrol.c @@ -82,7 +82,7 @@ OTLWF_IOCTL_HANDLER IoCtls[] = { "IOCTL_OTLWF_OT_MAC_WHITELIST_ENABLED", REF_IOCTL_FUNC_WITH_TUN(otMacWhitelistEnabled) }, { "IOCTL_OTLWF_OT_ADD_MAC_WHITELIST", REF_IOCTL_FUNC_WITH_TUN(otAddMacWhitelist) }, { "IOCTL_OTLWF_OT_REMOVE_MAC_WHITELIST", REF_IOCTL_FUNC_WITH_TUN(otRemoveMacWhitelist) }, - { "IOCTL_OTLWF_OT_MAC_WHITELIST_ENTRY", REF_IOCTL_FUNC(otMacWhitelistEntry) }, + { "IOCTL_OTLWF_OT_NEXT_MAC_WHITELIST", REF_IOCTL_FUNC(otNextMacWhitelist) }, { "IOCTL_OTLWF_OT_CLEAR_MAC_WHITELIST", REF_IOCTL_FUNC_WITH_TUN(otClearMacWhitelist) }, { "IOCTL_OTLWF_OT_DEVICE_ROLE", REF_IOCTL_FUNC_WITH_TUN(otDeviceRole) }, { "IOCTL_OTLWF_OT_CHILD_INFO_BY_ID", REF_IOCTL_FUNC(otChildInfoById) }, @@ -100,13 +100,12 @@ OTLWF_IOCTL_HANDLER IoCtls[] = { "IOCTL_OTLWF_OT_MAC_BLACKLIST_ENABLED", REF_IOCTL_FUNC(otMacBlacklistEnabled) }, { "IOCTL_OTLWF_OT_ADD_MAC_BLACKLIST", REF_IOCTL_FUNC(otAddMacBlacklist) }, { "IOCTL_OTLWF_OT_REMOVE_MAC_BLACKLIST", REF_IOCTL_FUNC(otRemoveMacBlacklist) }, - { "IOCTL_OTLWF_OT_MAC_BLACKLIST_ENTRY", REF_IOCTL_FUNC(otMacBlacklistEntry) }, + { "IOCTL_OTLWF_OT_NEXT_MAC_BLACKLIST", REF_IOCTL_FUNC(otNextMacBlacklist) }, { "IOCTL_OTLWF_OT_CLEAR_MAC_BLACKLIST", REF_IOCTL_FUNC(otClearMacBlacklist) }, { "IOCTL_OTLWF_OT_MAX_TRANSMIT_POWER", REF_IOCTL_FUNC(otMaxTransmitPower) }, { "IOCTL_OTLWF_OT_NEXT_ON_MESH_PREFIX", REF_IOCTL_FUNC(otNextOnMeshPrefix) }, { "IOCTL_OTLWF_OT_POLL_PERIOD", REF_IOCTL_FUNC(otPollPeriod) }, { "IOCTL_OTLWF_OT_LOCAL_LEADER_PARTITION_ID", REF_IOCTL_FUNC(otLocalLeaderPartitionId) }, - { "IOCTL_OTLWF_OT_ASSIGN_LINK_QUALITY", REF_IOCTL_FUNC(otAssignLinkQuality) }, { "IOCTL_OTLWF_OT_PLATFORM_RESET", REF_IOCTL_FUNC_WITH_TUN(otPlatformReset) }, { "IOCTL_OTLWF_OT_PARENT_INFO", REF_IOCTL_FUNC_WITH_TUN(otParentInfo) }, { "IOCTL_OTLWF_OT_SINGLETON", REF_IOCTL_FUNC(otSingleton) }, @@ -142,9 +141,14 @@ OTLWF_IOCTL_HANDLER IoCtls[] = { "IOCTL_OTLWF_OT_PREFERRED_ROUTER_ID", REF_IOCTL_FUNC(otThreadPreferredRouterId) }, { "IOCTL_OTLWF_OT_PSKC", REF_IOCTL_FUNC_WITH_TUN(otPSKc) }, { "IOCTL_OTLWF_OT_PARENT_PRIORITY", REF_IOCTL_FUNC(otParentPriority) }, + { "IOCTL_OTLWF_OT_ADD_MAC_FIXED_RSS", REF_IOCTL_FUNC_WITH_TUN(otAddMacFixedRss) }, + { "IOCTL_OTLWF_OT_REMOVE_MAC_FIXED_RSS", REF_IOCTL_FUNC_WITH_TUN(otRemoveMacFixedRss) }, + { "IOCTL_OTLWF_OT_NEXT_MAC_FIXED_RSS", REF_IOCTL_FUNC(otNextMacFixedRss) }, + { "IOCTL_OTLWF_OT_CLEAR_MAC_FIXED_RSS", REF_IOCTL_FUNC_WITH_TUN(otClearMacFixedRss) }, }; -static_assert(ARRAYSIZE(IoCtls) == (MAX_OTLWF_IOCTL_FUNC_CODE - MIN_OTLWF_IOCTL_FUNC_CODE) + 1, +// intentionally -1 in the end due to that IOCTL_OTLWF_OT_ASSIGN_LINK_QUALITY (#161) is removed now. +static_assert(ARRAYSIZE(IoCtls) == (MAX_OTLWF_IOCTL_FUNC_CODE - MIN_OTLWF_IOCTL_FUNC_CODE) + 1 - 1, "The IoCtl strings should be up to date with the actual IoCtl list."); const char* @@ -3440,13 +3444,15 @@ otLwfIoCtl_otMacWhitelistEnabled( if (InBufferLength >= sizeof(BOOLEAN)) { BOOLEAN aEnabled = *(BOOLEAN*)InBuffer; - otLinkSetWhitelistEnabled(pFilter->otCtx, aEnabled); - status = STATUS_SUCCESS; + otMacFilterAddressMode mode = + aEnabled ? OT_MAC_FILTER_ADDRESS_MODE_WHITELIST : OT_MAC_FILTER_ADDRESS_MODE_DISABLED; + status = ThreadErrorToNtstatus(otLinkFilterSetAddressMode(pFilter->otCtx, mode)); *OutBufferLength = 0; } else if (*OutBufferLength >= sizeof(BOOLEAN)) { - *(BOOLEAN*)OutBuffer = otLinkIsWhitelistEnabled(pFilter->otCtx) ? TRUE : FALSE; + otMacFilterAddressMode mode = otLinkFilterGetAddressMode(pFilter->otCtx); + *(BOOLEAN*)OutBuffer = (mode == OT_MAC_FILTER_ADDRESS_MODE_WHITELIST) ? TRUE : FALSE; status = STATUS_SUCCESS; *OutBufferLength = sizeof(BOOLEAN); } @@ -3536,25 +3542,29 @@ otLwfIoCtl_otAddMacWhitelist( ) { NTSTATUS status = STATUS_INVALID_PARAMETER; - UNREFERENCED_PARAMETER(OutBuffer); *OutBufferLength = 0; + int8_t aRss = OT_MAC_FILTER_FIXED_RSS_DISABLED; if (InBufferLength >= sizeof(otExtAddress) + sizeof(int8_t)) { - int8_t aRssi = *(int8_t*)(InBuffer + sizeof(otExtAddress)); - status = ThreadErrorToNtstatus(otLinkAddWhitelistRssi(pFilter->otCtx, (uint8_t*)InBuffer, aRssi)); + aRss = *(int8_t*)(InBuffer + sizeof(otExtAddress)); } - else if (InBufferLength >= sizeof(otExtAddress)) + + otError error = otLinkFilterAddAddress(pFilter->otCtx, (otExtAddress *)InBuffer); + + if ((error == OT_ERROR_NONE || error == OT_ERROR_ALREADY) && + (aRss != OT_MAC_FILTER_FIXED_RSS_DISABLED)) { - status = ThreadErrorToNtstatus(otLinkAddWhitelist(pFilter->otCtx, (uint8_t*)InBuffer)); + + error = otLinkFilterAddRssIn(pFilter->otCtx, (otExtAddress *)InBuffer, aRss); } + status = ThreadErrorToNtstatus(error); + return status; } -#define RSSI_OVERRIDE_DISABLED 127 // Used for PROP_MAC_WHITELIST - _IRQL_requires_max_(PASSIVE_LEVEL) NTSTATUS otLwfTunIoCtl_otAddMacWhitelist( @@ -3572,10 +3582,10 @@ otLwfTunIoCtl_otAddMacWhitelist( if (InBufferLength >= sizeof(otExtAddress)) { - int8_t aRssi = RSSI_OVERRIDE_DISABLED; + int8_t aRss = OT_MAC_FILTER_FIXED_RSS_DISABLED; if (InBufferLength >= sizeof(otExtAddress) + sizeof(int8_t)) { - aRssi = *(int8_t*)(InBuffer + sizeof(otExtAddress)); + aRss = *(int8_t*)(InBuffer + sizeof(otExtAddress)); } status = @@ -3588,7 +3598,7 @@ otLwfTunIoCtl_otAddMacWhitelist( sizeof(otExtAddress) + sizeof(int8_t), "Ec", (otExtAddress*)InBuffer, - &aRssi); + &aRss); } return status; @@ -3613,8 +3623,8 @@ otLwfIoCtl_otRemoveMacWhitelist( if (InBufferLength >= sizeof(otExtAddress)) { - otLinkRemoveWhitelist(pFilter->otCtx, (uint8_t*)InBuffer); - status = STATUS_SUCCESS; + status = ThreadErrorToNtstatus(otLinkFilterRemoveAddress(pFilter->otCtx, + (otExtAddress *)InBuffer)); } return status; @@ -3654,7 +3664,7 @@ otLwfTunIoCtl_otRemoveMacWhitelist( _IRQL_requires_max_(PASSIVE_LEVEL) NTSTATUS -otLwfIoCtl_otMacWhitelistEntry( +otLwfIoCtl_otNextMacWhitelist( _In_ PMS_FILTER pFilter, _In_reads_bytes_(InBufferLength) PUCHAR InBuffer, @@ -3667,15 +3677,25 @@ otLwfIoCtl_otMacWhitelistEntry( NTSTATUS status = STATUS_INVALID_PARAMETER; if (InBufferLength >= sizeof(uint8_t) && - *OutBufferLength >= sizeof(otMacWhitelistEntry)) + *OutBufferLength >= sizeof(uint8_t) + sizeof(otMacFilterEntry)) { + uint8_t aIterator = *(uint8_t*)(InBuffer); + otMacFilterEntry *aEntry = (otMacFilterEntry*)((PUCHAR)OutBuffer + sizeof(uint8_t)); + status = ThreadErrorToNtstatus( - otLinkGetWhitelistEntry( + otLinkFilterGetNextAddress( pFilter->otCtx, - *(uint8_t*)InBuffer, - (otMacWhitelistEntry*)OutBuffer) + &aIterator, + aEntry + ) ); - *OutBufferLength = sizeof(otMacWhitelistEntry); + + *OutBufferLength = sizeof(otMacFilterEntry) + sizeof(uint8_t); + + if (status == STATUS_SUCCESS) + { + *(uint8_t*)OutBuffer = aIterator; + } } else { @@ -3704,7 +3724,7 @@ otLwfIoCtl_otClearMacWhitelist( UNREFERENCED_PARAMETER(OutBuffer); *OutBufferLength = 0; - otLinkClearWhitelist(pFilter->otCtx); + otLinkFilterClearAddresses(pFilter->otCtx); return status; } @@ -4625,13 +4645,15 @@ otLwfIoCtl_otMacBlacklistEnabled( if (InBufferLength >= sizeof(BOOLEAN)) { BOOLEAN aEnabled = *(BOOLEAN*)InBuffer; - otLinkSetBlacklistEnabled(pFilter->otCtx, aEnabled); - status = STATUS_SUCCESS; + otMacFilterAddressMode mode = + aEnabled ? OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST : OT_MAC_FILTER_ADDRESS_MODE_DISABLED; + status = ThreadErrorToNtstatus(otLinkFilterSetAddressMode(pFilter->otCtx, mode)); *OutBufferLength = 0; } else if (*OutBufferLength >= sizeof(BOOLEAN)) { - *(BOOLEAN*)OutBuffer = otLinkIsBlacklistEnabled(pFilter->otCtx) ? TRUE : FALSE; + otMacFilterAddressMode mode = otLinkFilterGetAddressMode(pFilter->otCtx); + *(BOOLEAN*)OutBuffer = mode == (OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST) ? TRUE : FALSE; status = STATUS_SUCCESS; *OutBufferLength = sizeof(BOOLEAN); } @@ -4662,7 +4684,8 @@ otLwfIoCtl_otAddMacBlacklist( if (InBufferLength >= sizeof(otExtAddress)) { - status = ThreadErrorToNtstatus(otLinkAddBlacklist(pFilter->otCtx, (uint8_t*)InBuffer)); + status = ThreadErrorToNtstatus(otLinkFilterAddAddress(pFilter->otCtx, + (otExtAddress *)InBuffer)); } return status; @@ -4687,8 +4710,8 @@ otLwfIoCtl_otRemoveMacBlacklist( if (InBufferLength >= sizeof(otExtAddress)) { - otLinkRemoveBlacklist(pFilter->otCtx, (uint8_t*)InBuffer); - status = STATUS_SUCCESS; + status = ThreadErrorToNtstatus(otLinkFilterRemoveAddress(pFilter->otCtx, + (otExtAddress *)InBuffer)); } return status; @@ -4696,7 +4719,7 @@ otLwfIoCtl_otRemoveMacBlacklist( _IRQL_requires_max_(PASSIVE_LEVEL) NTSTATUS -otLwfIoCtl_otMacBlacklistEntry( +otLwfIoCtl_otNextMacBlacklist( _In_ PMS_FILTER pFilter, _In_reads_bytes_(InBufferLength) PUCHAR InBuffer, @@ -4709,16 +4732,27 @@ otLwfIoCtl_otMacBlacklistEntry( NTSTATUS status = STATUS_INVALID_PARAMETER; if (InBufferLength >= sizeof(uint8_t) && - *OutBufferLength >= sizeof(otMacBlacklistEntry)) + *OutBufferLength >= sizeof(uint8_t) + sizeof(otMacFilterEntry)) { + uint8_t aIterator = *(uint8_t*)(InBuffer); + otMacFilterEntry *aEntry = (otMacFilterEntry*)((PUCHAR)OutBuffer + sizeof(uint8_t)); + status = ThreadErrorToNtstatus( - otLinkGetBlacklistEntry( + otLinkFilterGetNextAddress( pFilter->otCtx, - *(uint8_t*)InBuffer, - (otMacBlacklistEntry*)OutBuffer) + &aIterator, + aEntry + ) ); - *OutBufferLength = sizeof(otMacBlacklistEntry); + + *OutBufferLength = sizeof(otMacFilterEntry) + sizeof(uint8_t); + + if (status == STATUS_SUCCESS) + { + *(uint8_t*)OutBuffer = aIterator; + } } + else { *OutBufferLength = 0; @@ -4746,7 +4780,7 @@ otLwfIoCtl_otClearMacBlacklist( UNREFERENCED_PARAMETER(OutBuffer); *OutBufferLength = 0; - otLinkClearBlacklist(pFilter->otCtx); + otLinkFilterClearAddresses(pFilter->otCtx); return status; } @@ -4905,48 +4939,6 @@ otLwfIoCtl_otLocalLeaderPartitionId( return status; } -_IRQL_requires_max_(PASSIVE_LEVEL) -NTSTATUS -otLwfIoCtl_otAssignLinkQuality( - _In_ PMS_FILTER pFilter, - _In_reads_bytes_(InBufferLength) - PUCHAR InBuffer, - _In_ ULONG InBufferLength, - _Out_writes_bytes_(*OutBufferLength) - PVOID OutBuffer, - _Inout_ PULONG OutBufferLength - ) -{ - NTSTATUS status = STATUS_INVALID_PARAMETER; - - if (InBufferLength >= sizeof(otExtAddress) + sizeof(uint8_t)) - { - otLinkSetAssignLinkQuality( - pFilter->otCtx, - (uint8_t*)InBuffer, - *(uint8_t*)(InBuffer + sizeof(otExtAddress))); - status = STATUS_SUCCESS; - *OutBufferLength = 0; - } - else if (InBufferLength >= sizeof(otExtAddress) && - *OutBufferLength >= sizeof(uint8_t)) - { - status = ThreadErrorToNtstatus( - otLinkGetAssignLinkQuality( - pFilter->otCtx, - (uint8_t*)InBuffer, - (uint8_t*)OutBuffer) - ); - *OutBufferLength = sizeof(uint32_t); - } - else - { - *OutBufferLength = 0; - } - - return status; -} - _IRQL_requires_max_(PASSIVE_LEVEL) NTSTATUS otLwfIoCtl_otPlatformReset( @@ -6261,3 +6253,253 @@ otLwfIoCtl_otParentPriority( return status; } +_IRQL_requires_max_(PASSIVE_LEVEL) +NTSTATUS +otLwfIoCtl_otAddMacFixedRss( + _In_ PMS_FILTER pFilter, + _In_reads_bytes_(InBufferLength) + PUCHAR InBuffer, + _In_ ULONG InBufferLength, + _Out_writes_bytes_(*OutBufferLength) + PVOID OutBuffer, + _Inout_ PULONG OutBufferLength + ) +{ + NTSTATUS status = STATUS_INVALID_PARAMETER; + + UNREFERENCED_PARAMETER(OutBuffer); + *OutBufferLength = 0; + + if (InBufferLength >= sizeof(otExtAddress) + sizeof(int8_t)) + { + int8_t aRss = *(int8_t*)(InBuffer + sizeof(otExtAddress)); + status = ThreadErrorToNtstatus(otLinkFilterAddRssIn(pFilter->otCtx, + (otExtAddress *)InBuffer, aRss)); + } + else if (InBufferLength >= sizeof(int8_t)) + { + status = ThreadErrorToNtstatus(otLinkFilterAddRssIn(pFilter->otCtx, NULL, + *(int8_t *)InBuffer)); + } + + return status; +} + +_IRQL_requires_max_(PASSIVE_LEVEL) +NTSTATUS +otLwfTunIoCtl_otAddMacFixedRss( + _In_ PMS_FILTER pFilter, + _In_ PIRP pIrp, + _In_reads_bytes_(InBufferLength) + PUCHAR InBuffer, + _In_ ULONG InBufferLength, + _In_ ULONG OutBufferLength + ) +{ + NTSTATUS status = STATUS_INVALID_PARAMETER; + + UNREFERENCED_PARAMETER(OutBufferLength); + + if (InBufferLength >= sizeof(otExtAddress) + sizeof(int8_t)) + { + int8_t aRss = OT_MAC_FILTER_FIXED_RSS_DISABLED; + aRss = *(int8_t*)(InBuffer + sizeof(otExtAddress)); + status = + otLwfTunSendCommandForIrp( + pFilter, + pIrp, + NULL, + SPINEL_CMD_PROP_VALUE_INSERT, + SPINEL_PROP_MAC_FIXED_RSS, + sizeof(otExtAddress) + sizeof(int8_t), + "Ec", + (otExtAddress*)InBuffer, + &aRss); + } + else + { + status = + otLwfTunSendCommandForIrp( + pFilter, + pIrp, + NULL, + SPINEL_CMD_PROP_VALUE_INSERT, + SPINEL_PROP_MAC_FIXED_RSS, + sizeof(int8_t), + "c", + (int8_t*)InBuffer); + } + + return status; +} + +_IRQL_requires_max_(PASSIVE_LEVEL) +NTSTATUS +otLwfIoCtl_otRemoveMacFixedRss( + _In_ PMS_FILTER pFilter, + _In_reads_bytes_(InBufferLength) + PUCHAR InBuffer, + _In_ ULONG InBufferLength, + _Out_writes_bytes_(*OutBufferLength) + PVOID OutBuffer, + _Inout_ PULONG OutBufferLength + ) +{ + NTSTATUS status = STATUS_INVALID_PARAMETER; + + UNREFERENCED_PARAMETER(OutBuffer); + *OutBufferLength = 0; + + if (InBufferLength >= sizeof(otExtAddress)) + { + status = ThreadErrorToNtstatus(otLinkFilterRemoveRssIn(pFilter->otCtx, (otExtAddress *)InBuffer)); + } + else + { + status = ThreadErrorToNtstatus(otLinkFilterRemoveRssIn(pFilter->otCtx, NULL)); + } + + return status; +} + +_IRQL_requires_max_(PASSIVE_LEVEL) +NTSTATUS +otLwfTunIoCtl_otRemoveMacFixedRss( + _In_ PMS_FILTER pFilter, + _In_ PIRP pIrp, + _In_reads_bytes_(InBufferLength) + PUCHAR InBuffer, + _In_ ULONG InBufferLength, + _In_ ULONG OutBufferLength + ) +{ + NTSTATUS status = STATUS_INVALID_PARAMETER; + + UNREFERENCED_PARAMETER(OutBufferLength); + + if (InBufferLength >= sizeof(otExtAddress)) + { + status = + otLwfTunSendCommandForIrp( + pFilter, + pIrp, + NULL, + SPINEL_CMD_PROP_VALUE_REMOVE, + SPINEL_PROP_MAC_FIXED_RSS, + sizeof(otExtAddress), + "E", + (otExtAddress*)InBuffer); + } + else + { + status = + otLwfTunSendCommandForIrp( + pFilter, + pIrp, + NULL, + SPINEL_CMD_PROP_VALUE_REMOVE, + SPINEL_PROP_MAC_FIXED_RSS, + 0, + NULL); + + } + + return status; +} + +_IRQL_requires_max_(PASSIVE_LEVEL) +NTSTATUS +otLwfIoCtl_otClearMacFixedRss( + _In_ PMS_FILTER pFilter, + _In_reads_bytes_(InBufferLength) + PUCHAR InBuffer, + _In_ ULONG InBufferLength, + _Out_writes_bytes_(*OutBufferLength) + PVOID OutBuffer, + _Inout_ PULONG OutBufferLength + ) +{ + NTSTATUS status = STATUS_SUCCESS; + + UNREFERENCED_PARAMETER(InBuffer); + UNREFERENCED_PARAMETER(InBufferLength); + UNREFERENCED_PARAMETER(OutBuffer); + *OutBufferLength = 0; + + otLinkFilterClearRssIn(pFilter->otCtx); + + return status; +} + +_IRQL_requires_max_(PASSIVE_LEVEL) +NTSTATUS +otLwfTunIoCtl_otClearMacFixedRss( + _In_ PMS_FILTER pFilter, + _In_ PIRP pIrp, + _In_reads_bytes_(InBufferLength) + PUCHAR InBuffer, + _In_ ULONG InBufferLength, + _In_ ULONG OutBufferLength + ) +{ + NTSTATUS status = STATUS_INVALID_PARAMETER; + + UNREFERENCED_PARAMETER(InBuffer); + UNREFERENCED_PARAMETER(InBufferLength); + UNREFERENCED_PARAMETER(OutBufferLength); + + status = + otLwfTunSendCommandForIrp( + pFilter, + pIrp, + NULL, + SPINEL_CMD_PROP_VALUE_SET, + SPINEL_PROP_MAC_FIXED_RSS, + 0, + NULL); + + return status; +} + +_IRQL_requires_max_(PASSIVE_LEVEL) +NTSTATUS +otLwfIoCtl_otNextMacFixedRss( + _In_ PMS_FILTER pFilter, + _In_reads_bytes_(InBufferLength) + PUCHAR InBuffer, + _In_ ULONG InBufferLength, + _Out_writes_bytes_(*OutBufferLength) + PVOID OutBuffer, + _Inout_ PULONG OutBufferLength + ) +{ + NTSTATUS status = STATUS_INVALID_PARAMETER; + + if (InBufferLength >= sizeof(uint8_t) && + *OutBufferLength >= sizeof(uint8_t) + sizeof(otMacFilterEntry)) + { + uint8_t aIterator = *(uint8_t*)(InBuffer); + otMacFilterEntry *aEntry = (otMacFilterEntry*)((PUCHAR)OutBuffer + sizeof(uint8_t)); + + status = ThreadErrorToNtstatus( + otLinkFilterGetNextRssIn( + pFilter->otCtx, + &aIterator, + aEntry + ) + ); + + *OutBufferLength = sizeof(otMacFilterEntry) + sizeof(uint8_t); + + if (status == STATUS_SUCCESS) + { + *(uint8_t*)OutBuffer = aIterator; + } + } + else + { + *OutBufferLength = 0; + } + + return status; +} diff --git a/examples/drivers/windows/otLwf/iocontrol.h b/examples/drivers/windows/otLwf/iocontrol.h index 67eb72416..b80a6da0a 100644 --- a/examples/drivers/windows/otLwf/iocontrol.h +++ b/examples/drivers/windows/otLwf/iocontrol.h @@ -168,7 +168,7 @@ DECL_IOCTL_FUNC_WITH_TUN(otReleaseRouterId); DECL_IOCTL_FUNC_WITH_TUN2(otMacWhitelistEnabled); DECL_IOCTL_FUNC_WITH_TUN(otAddMacWhitelist); DECL_IOCTL_FUNC_WITH_TUN(otRemoveMacWhitelist); -DECL_IOCTL_FUNC(otMacWhitelistEntry); +DECL_IOCTL_FUNC(otNextMacWhitelist); DECL_IOCTL_FUNC_WITH_TUN(otClearMacWhitelist); DECL_IOCTL_FUNC_WITH_TUN2(otDeviceRole); DECL_IOCTL_FUNC(otChildInfoById); @@ -186,13 +186,12 @@ DECL_IOCTL_FUNC_WITH_TUN2(otStableNetworkDataVersion); DECL_IOCTL_FUNC(otMacBlacklistEnabled); DECL_IOCTL_FUNC(otAddMacBlacklist); DECL_IOCTL_FUNC(otRemoveMacBlacklist); -DECL_IOCTL_FUNC(otMacBlacklistEntry); +DECL_IOCTL_FUNC(otNextMacBlacklist); DECL_IOCTL_FUNC(otClearMacBlacklist); DECL_IOCTL_FUNC(otMaxTransmitPower); DECL_IOCTL_FUNC(otNextOnMeshPrefix); DECL_IOCTL_FUNC(otPollPeriod); DECL_IOCTL_FUNC(otLocalLeaderPartitionId); -DECL_IOCTL_FUNC(otAssignLinkQuality); DECL_IOCTL_FUNC_WITH_TUN(otPlatformReset); DECL_IOCTL_FUNC_WITH_TUN2(otParentInfo); DECL_IOCTL_FUNC(otSingleton); @@ -228,5 +227,9 @@ DECL_IOCTL_FUNC(otThreadAutoStart); DECL_IOCTL_FUNC(otThreadPreferredRouterId); DECL_IOCTL_FUNC_WITH_TUN2(otPSKc); DECL_IOCTL_FUNC(otParentPriority); +DECL_IOCTL_FUNC_WITH_TUN(otAddMacFixedRss); +DECL_IOCTL_FUNC_WITH_TUN(otRemoveMacFixedRss); +DECL_IOCTL_FUNC(otNextMacFixedRss); +DECL_IOCTL_FUNC_WITH_TUN(otClearMacFixedRss); #endif // _IOCONTROL_H diff --git a/examples/drivers/windows/otNodeApi/otNodeApi.cpp b/examples/drivers/windows/otNodeApi/otNodeApi.cpp index 381412d09..48168ebc5 100644 --- a/examples/drivers/windows/otNodeApi/otNodeApi.cpp +++ b/examples/drivers/windows/otNodeApi/otNodeApi.cpp @@ -1069,7 +1069,7 @@ OTNODEAPI int32_t OTCALL otNodeClearWhitelist(otNode* aNode) otLogFuncEntryMsg("[%d]", aNode->mId); printf("%d: whitelist clear\r\n", aNode->mId); - otLinkClearWhitelist(aNode->mInstance); + otLinkFilterClearAddresses(aNode->mInstance); otLogFuncExit(); return 0; } @@ -1079,9 +1079,9 @@ OTNODEAPI int32_t OTCALL otNodeEnableWhitelist(otNode* aNode) otLogFuncEntryMsg("[%d]", aNode->mId); printf("%d: whitelist enable\r\n", aNode->mId); - otLinkSetWhitelistEnabled(aNode->mInstance, true); + otError error = otLinkFilterSetAddressMode(aNode->mInstance, OT_MAC_FILTER_ADDRESS_MODE_WHITELIST); otLogFuncExit(); - return 0; + return error; } OTNODEAPI int32_t OTCALL otNodeDisableWhitelist(otNode* aNode) @@ -1089,31 +1089,36 @@ OTNODEAPI int32_t OTCALL otNodeDisableWhitelist(otNode* aNode) otLogFuncEntryMsg("[%d]", aNode->mId); printf("%d: whitelist disable\r\n", aNode->mId); - otLinkSetWhitelistEnabled(aNode->mInstance, false); + otError error = otLinkFilterSetAddressMode(aNode->mInstance, OT_MAC_FILTER_ADDRESS_MODE_DISABLED); otLogFuncExit(); - return 0; + return error; } -OTNODEAPI int32_t OTCALL otNodeAddWhitelist(otNode* aNode, const char *aExtAddr, int8_t aRssi) +OTNODEAPI int32_t OTCALL otNodeAddWhitelist(otNode* aNode, + const char *aExtAddr, int8_t aRssi = OT_MAC_FILTER_FIXED_RSS_DISABLED) { otLogFuncEntryMsg("[%d]", aNode->mId); - if (aRssi == 0) - printf("%d: whitelist add %s\r\n", aNode->mId, aExtAddr); - else printf("%d: whitelist add %s %d\r\n", aNode->mId, aExtAddr, aRssi); + otError error = OT_ERROR_NONE; - uint8_t extAddr[8]; - if (Hex2Bin(aExtAddr, extAddr, sizeof(extAddr)) != sizeof(extAddr)) + otExtAddress extAddress; + if (Hex2Bin(aExtAddr, extAddress.m8, OT_EXT_ADDRESS_SIZE) != OT_EXT_ADDRESS_SIZE) return OT_ERROR_PARSE; - otError error; - if (aRssi == 0) + printf("%d: whitelist add %s", aNode->mId, aExtAddr); + + error = otLinkFilterAddAddress(aNode->mInstance, &extAddress); + + if (error == OT_ERROR_NONE || error == OT_ERROR_ALREADY) { - error = otLinkAddWhitelist(aNode->mInstance, extAddr); - } - else - { - error = otLinkAddWhitelistRssi(aNode->mInstance, extAddr, aRssi); + if (aRssi != OT_MAC_FILTER_FIXED_RSS_DISABLED) + { + error = otLinkFilterAddRssIn(aNode->mInstance, &extAddress, aRssi); + printf(" %d", aRssi); + } } + + printf("\r\n"); + otLogFuncExit(); return error; } @@ -1123,13 +1128,13 @@ OTNODEAPI int32_t OTCALL otNodeRemoveWhitelist(otNode* aNode, const char *aExtAd otLogFuncEntryMsg("[%d]", aNode->mId); printf("%d: whitelist remove %s\r\n", aNode->mId, aExtAddr); - uint8_t extAddr[8]; - if (Hex2Bin(aExtAddr, extAddr, sizeof(extAddr)) != sizeof(extAddr)) - return OT_ERROR_INVALID_ARGS; + otExtAddress extAddress; + if (Hex2Bin(aExtAddr, extAddress.m8, OT_EXT_ADDRESS_SIZE) != OT_EXT_ADDRESS_SIZE) + return OT_ERROR_PARSE; - otLinkRemoveWhitelist(aNode->mInstance, extAddr); + otError error = otLinkFilterRemoveAddress(aNode->mInstance, &extAddress); otLogFuncExit(); - return 0; + return error; } OTNODEAPI uint16_t OTCALL otNodeGetAddr16(otNode* aNode) diff --git a/examples/drivers/windows/otNodeApi/precomp.h b/examples/drivers/windows/otNodeApi/precomp.h index 671d5d94b..a0d8e85ce 100644 --- a/examples/drivers/windows/otNodeApi/precomp.h +++ b/examples/drivers/windows/otNodeApi/precomp.h @@ -55,6 +55,7 @@ using namespace std; #include #include #include +#include #include #include diff --git a/include/openthread-windows-config.h b/include/openthread-windows-config.h index a89b9cbf2..83f65d6ba 100644 --- a/include/openthread-windows-config.h +++ b/include/openthread-windows-config.h @@ -64,7 +64,7 @@ #define OPENTHREAD_ENABLE_DHCP6_SERVER 1 /* Define to 1 to enable MAC whitelist/blacklist feature. */ -#define OPENTHREAD_ENABLE_MAC_WHITELIST 1 +#define OPENTHREAD_ENABLE_MAC_FILTER 1 /* Define to 1 to enable TMF proxy feature. */ #define OPENTHREAD_ENABLE_TMF_PROXY 0 diff --git a/include/openthread/link.h b/include/openthread/link.h index 7f1dc3253..1a16a8dc3 100644 --- a/include/openthread/link.h +++ b/include/openthread/link.h @@ -306,226 +306,255 @@ OTAPI void OTCALL otLinkSetPollPeriod(otInstance *aInstance, uint32_t aPollPerio OTAPI otShortAddress OTCALL otLinkGetShortAddress(otInstance *aInstance); /** - * Add an IEEE 802.15.4 Extended Address to the MAC whitelist. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aExtAddr A pointer to the IEEE 802.15.4 Extended Address. - * - * @retval OT_ERROR_NONE Successfully added to the MAC whitelist. - * @retval OT_ERROR_NO_BUFS No buffers available for a new MAC whitelist entry. - * - * @sa otLinkAddWhitelistRssi - * @sa otLinkRemoveWhitelist - * @sa otLinkClearWhitelist - * @sa otLinkGetWhitelistEntry - * @sa otLinkSetWhitelistEnabled - */ -OTAPI otError OTCALL otLinkAddWhitelist(otInstance *aInstance, const uint8_t *aExtAddr); - -/** - * Add an IEEE 802.15.4 Extended Address to the MAC whitelist and fix the RSSI value. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aExtAddr A pointer to the IEEE 802.15.4 Extended Address. - * @param[in] aRssi The RSSI in dBm to use when receiving messages from aExtAddr. - * - * @retval OT_ERROR_NONE Successfully added to the MAC whitelist. - * @retval OT_ERROR_NO_BUFS No buffers available for a new MAC whitelist entry. - * - * @sa otLinkAddWhitelistRssi - * @sa otLinkRemoveWhitelist - * @sa otLinkClearWhitelist - * @sa otLinkGetWhitelistEntry - * @sa otLinkIsWhitelistEnabled - * @sa otLinkSetWhitelistEnabled - */ -OTAPI otError OTCALL otLinkAddWhitelistRssi(otInstance *aInstance, const uint8_t *aExtAddr, int8_t aRssi); - -/** - * Remove an IEEE 802.15.4 Extended Address from the MAC whitelist. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aExtAddr A pointer to the IEEE 802.15.4 Extended Address. - * - * @sa otLinkAddWhitelist - * @sa otLinkAddWhitelistRssi - * @sa otLinkClearWhitelist - * @sa otLinkGetWhitelistEntry - * @sa otLinkIsWhitelistEnabled - * @sa otLinkSetWhitelistEnabled - */ -OTAPI void OTCALL otLinkRemoveWhitelist(otInstance *aInstance, const uint8_t *aExtAddr); - -/** - * This function gets a MAC whitelist entry. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aIndex An index into the MAC whitelist table. - * @param[out] aEntry A pointer to where the information is placed. - * - * @retval OT_ERROR_NONE Successfully retrieved the MAC whitelist entry. - * @retval OT_ERROR_INVALID_ARGS @p aIndex is out of bounds or @p aEntry is NULL. - * - */ -OTAPI otError OTCALL otLinkGetWhitelistEntry(otInstance *aInstance, uint8_t aIndex, otMacWhitelistEntry *aEntry); - -/** - * Remove all entries from the MAC whitelist. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * - * @sa otLinkAddWhitelist - * @sa otLinkAddWhitelistRssi - * @sa otLinkRemoveWhitelist - * @sa otLinkGetWhitelistEntry - * @sa otLinkIsWhitelistEnabled - * @sa otLinkSetWhitelistEnabled - */ -OTAPI void OTCALL otLinkClearWhitelist(otInstance *aInstance); - -/** - * Enable MAC whitelist filtering. + * This function gets the address mode of MAC filter. * * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aEnabled TRUE to enable the whitelist, FALSE otherwise. * - * @sa otLinkAddWhitelist - * @sa otLinkAddWhitelistRssi - * @sa otLinkRemoveWhitelist - * @sa otLinkClearWhitelist - * @sa otLinkGetWhitelistEntry - * @sa otLinkIsWhitelistEnabled - */ -OTAPI void OTCALL otLinkSetWhitelistEnabled(otInstance *aInstance, bool aEnabled); - -/** - * This function indicates whether or not the MAC whitelist is enabled. + * @returns the address mode. * - * @param[in] aInstance A pointer to an OpenThread instance. - * - * @returns TRUE if the MAC whitelist is enabled, FALSE otherwise. - * - * @sa otLinkAddWhitelist - * @sa otLinkAddWhitelistRssi - * @sa otLinkRemoveWhitelist - * @sa otLinkClearWhitelist - * @sa otLinkGetWhitelistEntry - * @sa otLinkSetWhitelistEnabled - */ -OTAPI bool OTCALL otLinkIsWhitelistEnabled(otInstance *aInstance); - -/** - * Add an IEEE 802.15.4 Extended Address to the MAC blacklist. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aExtAddr A pointer to the IEEE 802.15.4 Extended Address. - * - * @retval OT_ERROR_NONE Successfully added to the MAC blacklist. - * @retval OT_ERROR_NO_BUFS No buffers available for a new MAC blacklist entry. - * - * @sa otLinkRemoveBlacklist - * @sa otLinkClearBlacklist - * @sa otLinkGetBlacklistEntry - * @sa otLinkIsBlacklistEnabled - * @sa otLinkSetBlacklistEnabled - */ -OTAPI otError OTCALL otLinkAddBlacklist(otInstance *aInstance, const uint8_t *aExtAddr); - -/** - * Remove an IEEE 802.15.4 Extended Address from the MAC blacklist. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aExtAddr A pointer to the IEEE 802.15.4 Extended Address. - * - * @sa otLinkAddBlacklist - * @sa otLinkClearBlacklist - * @sa otLinkGetBlacklistEntry - * @sa otLinkIsBlacklistEnabled - * @sa otLinkSetBlacklistEnabled - */ -OTAPI void OTCALL otLinkRemoveBlacklist(otInstance *aInstance, const uint8_t *aExtAddr); - -/** - * This function gets a MAC Blacklist entry. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aIndex An index into the MAC Blacklist table. - * @param[out] aEntry A pointer to where the information is placed. - * - * @retval OT_ERROR_NONE Successfully retrieved the MAC Blacklist entry. - * @retval OT_ERROR_INVALID_ARGS @p aIndex is out of bounds or @p aEntry is NULL. + * @sa otLinkFilterSetAddressMode + * @sa otLinkFilterAddAddress + * @sa otLinkFilterRemoveAddress + * @sa otLinkFilterClearAddresses + * @sa otLinkFilterGetNextAddress + * @sa otLinkFilterAddRssIn + * @sa otLinkFilterRemoveRssIn + * @sa otLinkFilterClearRssIn + * @sa otLinkFilterGetNextRssIn * */ -OTAPI otError OTCALL otLinkGetBlacklistEntry(otInstance *aInstance, uint8_t aIndex, otMacBlacklistEntry *aEntry); +OTAPI otMacFilterAddressMode OTCALL otLinkFilterGetAddressMode(otInstance *aInstance); /** - * Remove all entries from the MAC Blacklist. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * - * @sa otLinkAddBlacklist - * @sa otLinkRemoveBlacklist - * @sa otLinkGetBlacklistEntry - * @sa otLinkIsBlacklistEnabled - * @sa otLinkSetBlacklistEnabled - */ -OTAPI void OTCALL otLinkClearBlacklist(otInstance *aInstance); - -/** - * Enable MAC Blacklist filtering. + * This function sets the address mode of MAC filter. * * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aEnabled TRUE to enable the blacklist, FALSE otherwise. + * @param[in] aMode The address mode to set. + * + * @retval OT_ERROR_NONE Successfully set the address mode. + * @retval OT_ERROR_INVALID_ARGS @p aMode is not valid. + * + * @sa otLinkFilterGetAddressMode + * @sa otLinkFilterAddAddress + * @sa otLinkFilterRemoveAddress + * @sa otLinkFilterClearAddresses + * @sa otLinkFilterGetNextAddress + * @sa otLinkFilterAddRssIn + * @sa otLinkFilterRemoveRssIn + * @sa otLinkFilterClearRssIn + * @sa otLinkFilterGetNextRssIn * - * @sa otLinkAddBlacklist - * @sa otLinkRemoveBlacklist - * @sa otLinkClearBlacklist - * @sa otLinkGetBlacklistEntry - * @sa otLinkIsBlacklistEnabled */ -OTAPI void OTCALL otLinkSetBlacklistEnabled(otInstance *aInstance, bool aEnabled); +OTAPI otError OTCALL otLinkFilterSetAddressMode(otInstance *aInstance, otMacFilterAddressMode aMode); /** - * This function indicates whether or not the MAC Blacklist is enabled. + * This method adds an Extended Address to MAC filter. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aExtAddress A reference to the Extended Address. + * + * @retval OT_ERROR_NONE Successfully added @p aExtAddress to MAC filter. + * @retval OT_ERROR_ALREADY If @p aExtAddress was already in MAC filter. + * @retval OT_ERROR_INVALID_ARGS If @p aExtAddress is NULL. + * @retval OT_ERROR_NO_BUFS No available entry exists. + * + * @sa otLinkFilterGetAddressMode + * @sa otLinkFilterSetAddressMode + * @sa otLinkFilterRemoveAddress + * @sa otLinkFilterClearAddresses + * @sa otLinkFilterGetNextAddress + * @sa otLinkFilterAddRssIn + * @sa otLinkFilterRemoveRssIn + * @sa otLinkFilterClearRssIn + * @sa otLinkFilterGetNextRssIn + * + */ +OTAPI otError OTCALL otLinkFilterAddAddress(otInstance *aInstance, const otExtAddress *aExtAddress); + +/** + * This method removes an Extended Address from MAC filter. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aExtAddress A reference to the Extended Address. + * + * @retval OT_ERROR_NONE Successfully removed @p aExtAddress from MAC filter. + * @retval OT_ERROR_INVALID_ARGS If @p aExtAddress is NULL. + * @retval OT_ERROR_NOT_FOUND @p aExtAddress is not in MAC filter. + * + * @sa otLinkFilterGetAddressMode + * @sa otLinkFilterSetAddressMode + * @sa otLinkFilterAddAddress + * @sa otLinkFilterClearAddresses + * @sa otLinkFilterGetNextAddress + * @sa otLinkFilterAddRssIn + * @sa otLinkFilterRemoveRssIn + * @sa otLinkFilterClearRssIn + * @sa otLinkFilterGetNextRssIn + * + */ +OTAPI otError OTCALL otLinkFilterRemoveAddress(otInstance *aInstance, const otExtAddress *aExtAddress); + +/** + * This method clears all the Extended Addresses from MAC filter. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @sa otLinkFilterGetAddressMode + * @sa otLinkFilterSetAddressMode + * @sa otLinkFilterAddAddress + * @sa otLinkFilterRemoveAddress + * @sa otLinkFilterGetNextAddress + * @sa otLinkFilterAddRssIn + * @sa otLinkFilterRemoveRssIn + * @sa otLinkFilterClearRssIn + * @sa otLinkFilterGetNextRssIn + * + */ +OTAPI void OTCALL otLinkFilterClearAddresses(otInstance *aInstance); + +/** + * This method gets an in-use address filter entry. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[inout] aIterator A pointer to the MAC filter iterator context. To get the first in-use address filter entry, + * it should be set to OT_MAC_FILTER_ITERATOR_INIT. + * @param[out] aEntry A pointer to where the information is placed. + * + * @retval OT_ERROR_NONE Successfully retrieved an in-use address filter entry. + * @retval OT_ERROR_INVALID_ARGS If @p aIterator or @p aEntry is NULL. + * @retval OT_ERROR_NOT_FOUND No subsequent entry exists. + * + * @sa otLinkFilterGetAddressMode + * @sa otLinkFilterSetAddressMode + * @sa otLinkFilterAddAddress + * @sa otLinkFilterRemoveAddress + * @sa otLinkFilterClearAddresses + * @sa otLinkFilterAddRssIn + * @sa otLinkFilterRemoveRssIn + * @sa otLinkFilterClearRssIn + * @sa otLinkFilterGetNextRssIn + * + */ +OTAPI otError OTCALL otLinkFilterGetNextAddress(otInstance *aInstance, + otMacFilterIterator *aIterator, otMacFilterEntry *aEntry); + +/** + * This method sets the received signal strength (in dBm) for the messages from the Extended Address. + * The default received signal strength for all received messages would be set if no Extended Address is specified. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aExtAddress A pointer to the IEEE 802.15.4 Extended Address, or NULL to set the default received signal + * strength. + * @param[in] aRss The received signal strength (in dBm) to set. + * + * @retval OT_ERROR_NONE Successfully set @p aRss for @p aExtAddress or set the default @p aRss for all + * received messages if @p aExtAddress is NULL. + * @retval OT_ERROR_NO_BUFS No available entry exists. + * + * @sa otLinkFilterGetAddressMode + * @sa otLinkFilterSetAddressMode + * @sa otLinkFilterAddAddress + * @sa otLinkFilterRemoveAddress + * @sa otLinkFilterClearAddresses + * @sa otLinkFilterGetNextAddress + * @sa otLinkFilterRemoveRssIn + * @sa otLinkFilterClearRssIn + * @sa otLinkFilterGetNextRssIn + * + */ +OTAPI otError OTCALL otLinkFilterAddRssIn(otInstance *aInstance, const otExtAddress *aExtAddress, int8_t aRss); + +/** + * This method removes the received signal strength setting for the received messages from the Extended Address or + * removes the default received signal strength setting if no Extended Address is specified. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aExtAddress A pointer to the IEEE 802.15.4 Extended Address, or NULL to reset the default received + * signal strength. + * + * @retval OT_ERROR_NONE Successfully removed received signal strength setting for @p aExtAddress or + * removed the default received signal strength setting if @p aExtAddress is NULL. + * @retval OT_ERROR_NOT_FOUND @p aExtAddress is not in MAC filter if it is not NULL. + * + * @sa otLinkFilterGetAddressMode + * @sa otLinkFilterSetAddressMode + * @sa otLinkFilterAddAddress + * @sa otLinkFilterRemoveAddress + * @sa otLinkFilterClearAddresses + * @sa otLinkFilterGetNextAddress + * @sa otLinkFilterAddRssIn + * @sa otLinkFilterClearRssIn + * @sa otLinkFilterGetNextRssIn + * + */ +OTAPI otError OTCALL otLinkFilterRemoveRssIn(otInstance *aInstance, const otExtAddress *aExtAddress); + +/** + * This method clears all the received signal strength settings. * * @param[in] aInstance A pointer to an OpenThread instance. * - * @returns TRUE if the MAC Blacklist is enabled, FALSE otherwise. - * - * @sa otLinkAddBlacklist - * @sa otLinkRemoveBlacklist - * @sa otLinkClearBlacklist - * @sa otLinkGetBlacklistEntry - * @sa otLinkSetBlacklistEnabled + * @sa otLinkFilterGetAddressMode + * @sa otLinkFilterSetAddressMode + * @sa otLinkFilterAddAddress + * @sa otLinkFilterRemoveAddress + * @sa otLinkFilterClearAddresses + * @sa otLinkFilterGetNextAddress + * @sa otLinkFilterAddRssIn + * @sa otLinkFilterRemoveRssIn + * @sa otLinkFilterGetNextRssIn * */ -OTAPI bool OTCALL otLinkIsBlacklistEnabled(otInstance *aInstance); +OTAPI void OTCALL otLinkFilterClearRssIn(otInstance *aInstance); /** - * Get the assigned link quality which is on the link to a given extended address. + * This method gets an in-use RssIn filter entry. * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aExtAddr A pointer to the IEEE 802.15.4 Extended Address. - * @param[in] aLinkQuality A pointer to the assigned link quality. + * @param[in] aInstance A pointer to an OpenThread instance. + * @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. + * @param[out] aEntry A reference to where the information is placed. The last entry would have the extended + * address as all 0xff to indicate the default received signal strength if it was set. * - * @retval OT_ERROR_NONE Successfully retrieved the link quality to aLinkQuality. - * @retval OT_ERROR_INVALID_STATE No attached child matches with a given extended address. + * @retval OT_ERROR_NONE Successfully retrieved an in-use RssIn Filter entry. + * @retval OT_ERROR_INVALID_ARGS If @p aIterator or @p aEntry is NULL. + * @retval OT_ERROR_NOT_FOUND No subsequent entry exists. + * + * @sa otLinkFilterGetAddressMode + * @sa otLinkFilterSetAddressMode + * @sa otLinkFilterAddAddress + * @sa otLinkFilterRemoveAddress + * @sa otLinkFilterClearAddresses + * @sa otLinkFilterGetNextAddress + * @sa otLinkFilterAddRssIn + * @sa otLinkFilterRemoveRssIn + * @sa otLinkFilterClearRssIn * - * @sa otLinkSetAssignLinkQuality */ -OTAPI otError OTCALL otLinkGetAssignLinkQuality(otInstance *aInstance, const uint8_t *aExtAddr, - uint8_t *aLinkQuality); +OTAPI otError OTCALL otLinkFilterGetNextRssIn(otInstance *aInstance, + otMacFilterIterator *aIterator, otMacFilterEntry *aEntry); /** - * Set the link quality which is on the link to a given extended address. + * This method converts received signal strength to link quality. * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aExtAddr A pointer to the IEEE 802.15.4 Extended Address. - * @param[in] aLinkQuality The link quality to be set on the link. + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aRss The received signal strength value to be converted. + * + * @return Link quality value mapping to @p aRss. * - * @sa otLinkGetAssignLinkQuality */ -OTAPI void OTCALL otLinkSetAssignLinkQuality(otInstance *aInstance, const uint8_t *aExtAddr, uint8_t aLinkQuality); +uint8_t otLinkConvertRssToLinkQuality(otInstance *aInstance, int8_t aRss); + +/** + * This method converts link quality to typical received signal strength. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aLinkQuality LinkQuality value, should be in range [0,3]. + * + * @return Typical platform received signal strength mapping to @p aLinkQuality. + * + */ +int8_t otLinkConvertLinkQualityToRss(otInstance *aInstance, uint8_t aLinkQuality); /** * Get the MAC layer counters. diff --git a/include/openthread/types.h b/include/openthread/types.h index 594975df1..0beb84468 100644 --- a/include/openthread/types.h +++ b/include/openthread/types.h @@ -765,26 +765,34 @@ typedef enum otRoutePreference } otRoutePreference; /** - * This structure represents a whitelist entry. - * + * Used to indicate no fixed received signal strength was set */ -typedef struct otMacWhitelistEntry -{ - otExtAddress mExtAddress; ///< IEEE 802.15.4 Extended Address - int8_t mRssi; ///< RSSI value - bool mValid : 1; ///< Indicates whether or not the whitelist entry is valid - bool mFixedRssi : 1; ///< Indicates whether or not the RSSI value is fixed. -} otMacWhitelistEntry; +#define OT_MAC_FILTER_FIXED_RSS_DISABLED 127 + +#define OT_MAC_FILTER_ITERATOR_INIT 0 ///< Initializer for otMacFilterIterator. + +typedef uint8_t otMacFilterIterator; ///< Used to iterate through mac filter entries. /** - * This structure represents a blacklist entry. + * Defines address mode of the mac filter. + */ +typedef enum otMacFilterAddressMode +{ + OT_MAC_FILTER_ADDRESS_MODE_DISABLED, ///< Address filter is disabled. + OT_MAC_FILTER_ADDRESS_MODE_WHITELIST, ///< Whitelist address filter mode is enabled. + OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST, ///< Blacklist address filter mode is enabled. +} otMacFilterAddressMode; + +/** + * This structure represents a Mac Filter entry. * */ -typedef struct otMacBlacklistEntry +typedef struct otMacFilterEntry { otExtAddress mExtAddress; ///< IEEE 802.15.4 Extended Address - bool mValid; ///< Indicates whether or not the blacklist entry is valid -} otMacBlacklistEntry; + int8_t mRssIn; ///< Received signal strength + bool mFiltered; ///< Indicates whether or not this entry is filtered. +} otMacFilterEntry; /** * Represents a Thread device role. diff --git a/src/cli/README.md b/src/cli/README.md index 21440a3aa..8b300d377 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -8,7 +8,6 @@ OpenThread test scripts use the CLI to execute test cases. ## OpenThread Command List * [autostart](#autostart) -* [blacklist](#blacklist) * [bufferinfo](#bufferinfo) * [channel](#channel) * [child](#child-list) @@ -38,6 +37,7 @@ OpenThread test scripts use the CLI to execute test cases. * [leaderpartitionid](#leaderpartitionid) * [leaderweight](#leaderweight) * [linkquality](#linkquality-extaddr) +* [macfilter](#macfilter) * [masterkey](#masterkey) * [mode](#mode) * [netdataregister](#netdataregister) @@ -66,7 +66,6 @@ OpenThread test scripts use the CLI to execute test cases. * [thread](#thread-start) * [txpowermax](#txpowermax) * [version](#version) -* [whitelist](#whitelist) * [diag](#diag) ## OpenThread Command Details @@ -99,63 +98,6 @@ false Done ``` -### blacklist - -List the blacklist entries. - -```bash -> blacklist -Enabled -166e0a0000000002 -166e0a0000000003 -Done -``` - -### blacklist add \ - -Add an IEEE 802.15.4 Extended Address to the blacklist. - -```bash -> blacklist add 166e0a0000000002 -Done -``` - -### blacklist clear - -Clear all entries from the blacklist. - -```bash -> blacklist clear -Done -``` - -### blacklist disable - -Disable MAC blacklist filtering. - -```bash -> blacklist disable -Done -``` - -### blacklist enable - -Enable MAC blacklist filtering. - -```bash -> blacklist enable -Done -``` - -### blacklist remove \ - -Remove an IEEE 802.15.4 Extended Address from the blacklist. - -```bash -> blacklist remove 166e0a0000000002 -Done -``` - ### bufferinfo Show the current message buffer information. @@ -1630,61 +1572,158 @@ OPENTHREAD/gf4f2f04; Jul 1 2016 17:00:09 Done ``` -### whitelist +### macfilter -List the whitelist entries. +List the macfilter status, including address and received signal strength filter settings. ```bash -> whitelist -Enabled -e2b3540590b0fd87 -d38d7f875888fccb -c467a90a2060fa0e +> macfilter +Address Mode: Whitelist +0f6127e33af6b403 : rss -95 (lqi 1) +0f6127e33af6b402 +RssIn List: +0f6127e33af6b403 : rss -95 (lqi 1) +Default rss : -50 (lqi 3) Done ``` -### whitelist add \ +### macfilter addr -Add an IEEE 802.15.4 Extended Address to the whitelist. +List the address filter status. ```bash -> whitelist add dead00beef00cafe +> macfilter addr +Whitelist +0f6127e33af6b403 : rss -95 (lqi 1) +0f6127e33af6b402 Done ``` -### whitelist clear +### macfilter addr disable -Clear all entries from the whitelist. +Disable address filter mode. ```bash -> whitelist clear +> macfilter addr disable Done ``` -### whitelist disable +### macfilter addr whitelist -Disable MAC whitelist filtering. +Enable whitelist address filter mode. ```bash -> whitelist disable +> macfilter addr whitelist Done ``` -### whitelist enable +### macfilter addr blacklist -Enable MAC whitelist filtering. +Enable blacklist address filter mode. ```bash -> whitelist enable +> macfilter addr blacklist Done ``` -### whitelist remove \ +### macfilter addr add \ \[rss\] -Remove an IEEE 802.15.4 Extended Address from the whitelist. +Add an IEEE 802.15.4 Extended Address to the address filter, and fixed the received singal strength for +the messages from the address if rss is specified. ```bash -> whitelist remove dead00beef00cafe +> macfilter addr add 0f6127e33af6b403 -95 +Done +``` + +```bash +> macfilter addr add 0f6127e33af6b402 +Done +``` + +### macfilter addr remove \ + +Remove the IEEE802.15.4 Extended Address from the address filter. + +```bash +> macfilter addr remove 0f6127e33af6b402 +Done +``` + +### macfilter addr clear + +Clear all the IEEE802.15.4 Extended Addresses from the address filter. + +```bash +> macfilter addr clear +Done +``` + +### macfilter rss + +List the rss filter status + +```bash +> macfilter rss +0f6127e33af6b403 : rss -95 (lqi 1) +Default rss: -50 (lqi 3) +Done +``` + +### macfilter rss add \ \ + +Set the received signal strength for the messages from the IEEE802.15.4 Extended Address. +If extaddr is \*, default received signal strength for all received messages would be set. + + +```bash +> macfilter rss add * -50 +Done +``` + +```bash +> macfilter rss add 0f6127e33af6b404 -85 +Done +``` + +### macfilter rss add-lqi \ \ + +Set the received link quality for the messages from the IEEE802.15.4 Extended Address. Valid lqi range [0,3] +If extaddr is \*, default received link quality for all received messages would be set. +Equivalent with 'filter rss add' with similar usage + + +```bash +> macfilter rss add-lqi * 3 +Done +``` + +```bash +> macfilter rss add 0f6127e33af6b404 2 +Done +``` + +### macfilter rss remove \ + +Removes the received signal strength or received link quality setting on the Extended Address. +If extaddr is \*, default received signal strength or link quality for all received messages would be unset. + +```bash +> macfilter rss remove * +Done +``` + +```bash +> macfilter rss remove 0f6127e33af6b404 +Done +``` + +### macfilter rss clear + +Clear all the the received signal strength or received link quality settings. + +```bash +> macfilter rss clear Done ``` diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 4d0a7689e..d0c17c9c3 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #if OPENTHREAD_FTD #include @@ -89,7 +90,6 @@ const struct Command Interpreter::sCommands[] = { { "help", &Interpreter::ProcessHelp }, { "autostart", &Interpreter::ProcessAutoStart }, - { "blacklist", &Interpreter::ProcessBlacklist }, { "bufferinfo", &Interpreter::ProcessBufferInfo }, { "channel", &Interpreter::ProcessChannel }, #if OPENTHREAD_FTD @@ -150,7 +150,9 @@ const struct Command Interpreter::sCommands[] = { "leaderpartitionid", &Interpreter::ProcessLeaderPartitionId }, { "leaderweight", &Interpreter::ProcessLeaderWeight }, #endif - { "linkquality", &Interpreter::ProcessLinkQuality }, +#if OPENTHREAD_ENABLE_MAC_FILTER + { "macfilter", &Interpreter::ProcessMacFilter }, +#endif { "masterkey", &Interpreter::ProcessMasterKey }, { "mode", &Interpreter::ProcessMode }, #if OPENTHREAD_ENABLE_BORDER_ROUTER @@ -200,7 +202,6 @@ const struct Command Interpreter::sCommands[] = { "thread", &Interpreter::ProcessThread }, { "txpowermax", &Interpreter::ProcessTxPowerMax }, { "version", &Interpreter::ProcessVersion }, - { "whitelist", &Interpreter::ProcessWhitelist }, }; #ifdef OTDLL @@ -401,72 +402,6 @@ void Interpreter::ProcessAutoStart(int argc, char *argv[]) AppendResult(error); } -void Interpreter::ProcessBlacklist(int argc, char *argv[]) -{ - otError error = OT_ERROR_NONE; - otMacBlacklistEntry entry; - int argcur = 0; - uint8_t extAddr[8]; - - if (argcur >= argc) - { - if (otLinkIsBlacklistEnabled(mInstance)) - { - mServer->OutputFormat("Enabled\r\n"); - } - else - { - mServer->OutputFormat("Disabled\r\n"); - } - - for (uint8_t i = 0; ; i++) - { - if (otLinkGetBlacklistEntry(mInstance, i, &entry) != OT_ERROR_NONE) - { - break; - } - - if (entry.mValid == false) - { - continue; - } - - OutputBytes(entry.mExtAddress.m8, OT_EXT_ADDRESS_SIZE); - - mServer->OutputFormat("\r\n"); - } - } - else if (strcmp(argv[argcur], "add") == 0) - { - VerifyOrExit(++argcur < argc, error = OT_ERROR_PARSE); - VerifyOrExit(Hex2Bin(argv[argcur], extAddr, sizeof(extAddr)) == sizeof(extAddr), error = OT_ERROR_PARSE); - - otLinkAddBlacklist(mInstance, extAddr); - VerifyOrExit(otLinkAddBlacklist(mInstance, extAddr) == OT_ERROR_NONE, error = OT_ERROR_PARSE); - } - else if (strcmp(argv[argcur], "clear") == 0) - { - otLinkClearBlacklist(mInstance); - } - else if (strcmp(argv[argcur], "disable") == 0) - { - otLinkSetBlacklistEnabled(mInstance, false); - } - else if (strcmp(argv[argcur], "enable") == 0) - { - otLinkSetBlacklistEnabled(mInstance, true); - } - else if (strcmp(argv[argcur], "remove") == 0) - { - VerifyOrExit(++argcur < argc, error = OT_ERROR_PARSE); - VerifyOrExit(Hex2Bin(argv[argcur], extAddr, sizeof(extAddr)) == sizeof(extAddr), error = OT_ERROR_PARSE); - otLinkRemoveBlacklist(mInstance, extAddr); - } - -exit: - AppendResult(error); -} - void Interpreter::ProcessBufferInfo(int argc, char *argv[]) { otBufferInfo bufferInfo; @@ -1309,32 +1244,6 @@ exit: } #endif // OPENTHREAD_FTD -void Interpreter::ProcessLinkQuality(int argc, char *argv[]) -{ - otError error = OT_ERROR_NONE; - uint8_t extAddress[8]; - uint8_t linkQuality; - long value; - - VerifyOrExit(argc > 0, error = OT_ERROR_INVALID_ARGS); - VerifyOrExit(Hex2Bin(argv[0], extAddress, OT_EXT_ADDRESS_SIZE) >= 0, error = OT_ERROR_PARSE); - - if (argc == 1) - { - VerifyOrExit(otLinkGetAssignLinkQuality(mInstance, extAddress, &linkQuality) == OT_ERROR_NONE, - error = OT_ERROR_INVALID_ARGS); - mServer->OutputFormat("%d\r\n", linkQuality); - } - else - { - SuccessOrExit(error = ParseLong(argv[1], value)); - otLinkSetAssignLinkQuality(mInstance, extAddress, static_cast(value)); - } - -exit: - AppendResult(error); -} - #if OPENTHREAD_FTD void Interpreter::ProcessPSKc(int argc, char *argv[]) { @@ -2932,86 +2841,332 @@ exit: } #endif -void Interpreter::ProcessWhitelist(int argc, char *argv[]) +#if OPENTHREAD_ENABLE_MAC_FILTER +void Interpreter::ProcessMacFilter(int argc, char *argv[]) { otError error = OT_ERROR_NONE; - otMacWhitelistEntry entry; - int argcur = 0; - uint8_t extAddr[8]; - int8_t rssi; - if (argcur >= argc) + if (argc == 0) { - if (otLinkIsWhitelistEnabled(mInstance)) + PrintMacFilter(); + } + else + { + if (strcmp(argv[0], "addr") == 0) { - mServer->OutputFormat("Enabled\r\n"); + error = ProcessMacFilterAddress(argc - 1, argv + 1); } + +#ifndef OTDLL + else if (strcmp(argv[0], "rss") == 0) + { + error = ProcessMacFilterRss(argc - 1, argv + 1); + } + +#endif else { - mServer->OutputFormat("Disabled\r\n"); + error = OT_ERROR_INVALID_ARGS; + } + } + + if (error != OT_ERROR_NONE) + { + otThreadErrorToString(error); + } + + AppendResult(error); +} + +void Interpreter::PrintMacFilter(void) +{ + otMacFilterEntry entry; + otMacFilterIterator iterator = OT_MAC_FILTER_ITERATOR_INIT; + otMacFilterAddressMode mode = otLinkFilterGetAddressMode(mInstance); + + if (mode == OT_MAC_FILTER_ADDRESS_MODE_DISABLED) + { + mServer->OutputFormat("Address Mode: Disabled\r\n"); + } + else if (mode == OT_MAC_FILTER_ADDRESS_MODE_WHITELIST) + { + mServer->OutputFormat("Address Mode: Whitelist\r\n"); + } + else if (mode == OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST) + { + mServer->OutputFormat("Address Mode: Blacklist\r\n"); + } + + while (otLinkFilterGetNextAddress(mInstance, &iterator, &entry) == OT_ERROR_NONE) + { + OutputBytes(entry.mExtAddress.m8, OT_EXT_ADDRESS_SIZE); + + if (entry.mRssIn != OT_MAC_FILTER_FIXED_RSS_DISABLED) + { +#ifndef OTDLL + + mServer->OutputFormat(" : rss %d (lqi %d)", entry.mRssIn, + otLinkConvertRssToLinkQuality(mInstance, entry.mRssIn)); + +#else + + mServer->OutputFormat(" : rss %d", entry.mRssIn); + +#endif // OTDLL } - for (uint8_t i = 0; ; i++) + mServer->OutputFormat("\r\n"); + } + +#ifndef OTDLL + + iterator = OT_MAC_FILTER_ITERATOR_INIT; + mServer->OutputFormat("RssIn List:\r\n"); + + while (otLinkFilterGetNextRssIn(mInstance, &iterator, &entry) == OT_ERROR_NONE) + { + uint8_t i = 0; + + for (; i < OT_EXT_ADDRESS_SIZE; i++) { - if (otLinkGetWhitelistEntry(mInstance, i, &entry) != OT_ERROR_NONE) + if (entry.mExtAddress.m8[i] != 0xff) { break; } + } - if (entry.mValid == false) - { - continue; - } + if (i == OT_EXT_ADDRESS_SIZE) + { + mServer->OutputFormat("Default rss : %d (lqi %d)\r\n", + entry.mRssIn, otLinkConvertRssToLinkQuality(mInstance, entry.mRssIn)); + } + else + { + OutputBytes(entry.mExtAddress.m8, OT_EXT_ADDRESS_SIZE); + mServer->OutputFormat(" : rss %d (lqi %d)\r\n", + entry.mRssIn, otLinkConvertRssToLinkQuality(mInstance, entry.mRssIn)); + } + } +#endif // OTDLL +} + +otError Interpreter::ProcessMacFilterAddress(int argc, char *argv[]) +{ + otError error = OT_ERROR_NONE; + otExtAddress extAddr; + otMacFilterEntry entry; + otMacFilterIterator iterator = OT_MAC_FILTER_ITERATOR_INIT; + otMacFilterAddressMode mode = otLinkFilterGetAddressMode(mInstance); + long value; + + if (argc == 0) + { + if (mode == OT_MAC_FILTER_ADDRESS_MODE_DISABLED) + { + mServer->OutputFormat("Disabled\r\n"); + } + else if (mode == OT_MAC_FILTER_ADDRESS_MODE_WHITELIST) + { + mServer->OutputFormat("Whitelist\r\n"); + } + else if (mode == OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST) + { + mServer->OutputFormat("Blacklist\r\n"); + } + + while (otLinkFilterGetNextAddress(mInstance, &iterator, &entry) == OT_ERROR_NONE) + { OutputBytes(entry.mExtAddress.m8, OT_EXT_ADDRESS_SIZE); - if (entry.mFixedRssi) + if (entry.mRssIn != OT_MAC_FILTER_FIXED_RSS_DISABLED) { - mServer->OutputFormat(" %d", entry.mRssi); +#ifndef OTDLL + + mServer->OutputFormat(" : rss %d (lqi %d)", entry.mRssIn, + otLinkConvertRssToLinkQuality(mInstance, entry.mRssIn)); + +#else + + mServer->OutputFormat(" : rss %d", entry.mRssIn); + +#endif // OTDLL } mServer->OutputFormat("\r\n"); } } - else if (strcmp(argv[argcur], "add") == 0) + else { - VerifyOrExit(++argcur < argc, error = OT_ERROR_PARSE); - VerifyOrExit(Hex2Bin(argv[argcur], extAddr, sizeof(extAddr)) == sizeof(extAddr), error = OT_ERROR_PARSE); - - if (++argcur < argc) + if (strcmp(argv[0], "disable") == 0) { - rssi = static_cast(strtol(argv[argcur], NULL, 0)); - VerifyOrExit(otLinkAddWhitelistRssi(mInstance, extAddr, rssi) == OT_ERROR_NONE, error = OT_ERROR_PARSE); + VerifyOrExit(argc == 1, error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = otLinkFilterSetAddressMode(mInstance, OT_MAC_FILTER_ADDRESS_MODE_DISABLED)); + } + else if (strcmp(argv[0], "whitelist") == 0) + { + VerifyOrExit(argc == 1, error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = otLinkFilterSetAddressMode(mInstance, OT_MAC_FILTER_ADDRESS_MODE_WHITELIST)); + } + else if (strcmp(argv[0], "blacklist") == 0) + { + VerifyOrExit(argc == 1, error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = otLinkFilterSetAddressMode(mInstance, OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST)); + } + else if (strcmp(argv[0], "add") == 0) + { + VerifyOrExit(argc >= 2, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(Hex2Bin(argv[1], extAddr.m8, OT_EXT_ADDRESS_SIZE) == OT_EXT_ADDRESS_SIZE, + error = OT_ERROR_PARSE); + + error = otLinkFilterAddAddress(mInstance, &extAddr); + + VerifyOrExit(error == OT_ERROR_NONE || error == OT_ERROR_ALREADY); + + if (argc > 2) + { + int8_t rss = 0; + VerifyOrExit(argc == 3, error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = ParseLong(argv[2], value)); + rss = static_cast(value); + SuccessOrExit(error = otLinkFilterAddRssIn(mInstance, &extAddr, rss)); + } + } + else if (strcmp(argv[0], "remove") == 0) + { + VerifyOrExit(argc == 2, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(Hex2Bin(argv[1], extAddr.m8, OT_EXT_ADDRESS_SIZE) == OT_EXT_ADDRESS_SIZE, + error = OT_ERROR_PARSE); + SuccessOrExit(error = otLinkFilterRemoveAddress(mInstance, &extAddr)); + } + else if (strcmp(argv[0], "clear") == 0) + { + VerifyOrExit(argc == 1, error = OT_ERROR_INVALID_ARGS); + otLinkFilterClearAddresses(mInstance); } else { - otLinkAddWhitelist(mInstance, extAddr); - VerifyOrExit(otLinkAddWhitelist(mInstance, extAddr) == OT_ERROR_NONE, error = OT_ERROR_PARSE); + error = OT_ERROR_INVALID_ARGS; } } - else if (strcmp(argv[argcur], "clear") == 0) - { - otLinkClearWhitelist(mInstance); - } - else if (strcmp(argv[argcur], "disable") == 0) - { - otLinkSetWhitelistEnabled(mInstance, false); - } - else if (strcmp(argv[argcur], "enable") == 0) - { - otLinkSetWhitelistEnabled(mInstance, true); - } - else if (strcmp(argv[argcur], "remove") == 0) - { - VerifyOrExit(++argcur < argc, error = OT_ERROR_PARSE); - VerifyOrExit(Hex2Bin(argv[argcur], extAddr, sizeof(extAddr)) == sizeof(extAddr), error = OT_ERROR_PARSE); - otLinkRemoveWhitelist(mInstance, extAddr); - } exit: - AppendResult(error); + return error; } +#ifndef OTDLL + +otError Interpreter::ProcessMacFilterRss(int argc, char *argv[]) +{ + otError error = OT_ERROR_NONE; + otMacFilterEntry entry; + otMacFilterIterator iterator = OT_MAC_FILTER_ITERATOR_INIT; + otExtAddress extAddr; + long value; + int8_t rss; + + if (argc == 0) + { + while (otLinkFilterGetNextRssIn(mInstance, &iterator, &entry) == OT_ERROR_NONE) + { + uint8_t i = 0; + + for (; i < OT_EXT_ADDRESS_SIZE; i++) + { + if (entry.mExtAddress.m8[i] != 0xff) + { + break; + } + } + + if (i == OT_EXT_ADDRESS_SIZE) + { + mServer->OutputFormat("Default rss: %d (lqi %d)\r\n", + entry.mRssIn, otLinkConvertRssToLinkQuality(mInstance, entry.mRssIn)); + } + else + { + OutputBytes(entry.mExtAddress.m8, OT_EXT_ADDRESS_SIZE); + mServer->OutputFormat(" : rss %d (lqi %d)\r\n", + entry.mRssIn, otLinkConvertRssToLinkQuality(mInstance, entry.mRssIn)); + } + } + } + else + { + if (strcmp(argv[0], "add-lqi") == 0) + { + uint8_t linkquality = 0; + VerifyOrExit(argc == 3, error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = ParseLong(argv[2], value)); + linkquality = static_cast(value); + VerifyOrExit(linkquality <= 3, error = OT_ERROR_PARSE); + rss = otLinkConvertLinkQualityToRss(mInstance, linkquality); + + if (strcmp(argv[1], "*") == 0) + { + SuccessOrExit(error = otLinkFilterAddRssIn(mInstance, NULL, rss)); + } + else + { + VerifyOrExit(Hex2Bin(argv[1], extAddr.m8, OT_EXT_ADDRESS_SIZE) == OT_EXT_ADDRESS_SIZE, + error = OT_ERROR_PARSE); + + SuccessOrExit(error = otLinkFilterAddRssIn(mInstance, &extAddr, rss)); + } + } + else if (strcmp(argv[0], "add") == 0) + { + VerifyOrExit(argc == 3, error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = ParseLong(argv[2], value)); + rss = static_cast(value); + + if (strcmp(argv[1], "*") == 0) + { + SuccessOrExit(error = otLinkFilterAddRssIn(mInstance, NULL, rss)); + } + else + { + VerifyOrExit(Hex2Bin(argv[1], extAddr.m8, OT_EXT_ADDRESS_SIZE) == OT_EXT_ADDRESS_SIZE, + error = OT_ERROR_PARSE); + + SuccessOrExit(error = otLinkFilterAddRssIn(mInstance, &extAddr, rss)); + } + } + else if (strcmp(argv[0], "remove") == 0) + { + VerifyOrExit(argc == 2, error = OT_ERROR_INVALID_ARGS); + + if (strcmp(argv[1], "*") == 0) + { + SuccessOrExit(error = otLinkFilterRemoveRssIn(mInstance, NULL)); + } + else + { + VerifyOrExit(Hex2Bin(argv[1], extAddr.m8, OT_EXT_ADDRESS_SIZE) == OT_EXT_ADDRESS_SIZE, + error = OT_ERROR_PARSE); + + SuccessOrExit(error = otLinkFilterRemoveRssIn(mInstance, &extAddr)); + } + } + else if (strcmp(argv[0], "clear") == 0) + { + otLinkFilterClearRssIn(mInstance); + } + else + { + error = OT_ERROR_INVALID_ARGS; + } + } + +exit: + return error; +} + +#endif // OTDLL + +#endif // OPENTHREAD_ENABLE_MAC_FILTER + #if OPENTHREAD_ENABLE_DIAG void Interpreter::ProcessDiag(int argc, char *argv[]) { diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 90c9aed0e..9568273bd 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -162,7 +162,6 @@ private: void ProcessHelp(int argc, char *argv[]); void ProcessAutoStart(int argc, char *argv[]); void ProcessBufferInfo(int argc, char *argv[]); - void ProcessBlacklist(int argc, char *argv[]); void ProcessChannel(int argc, char *argv[]); #if OPENTHREAD_FTD void ProcessChild(int argc, char *argv[]); @@ -274,7 +273,14 @@ private: void ProcessThread(int argc, char *argv[]); void ProcessTxPowerMax(int argc, char *argv[]); void ProcessVersion(int argc, char *argv[]); - void ProcessWhitelist(int argc, char *argv[]); +#if OPENTHREAD_ENABLE_MAC_FILTER + void ProcessMacFilter(int argc, char *argv[]); + void PrintMacFilter(void); + otError ProcessMacFilterAddress(int argc, char *argv[]); +#ifndef OTDLL + otError ProcessMacFilterRss(int argc, char *argv[]); +#endif // OTDLL +#endif // OPENTHREAD_ENABLE_MAC_FILTER #ifdef OTDLL void ProcessInstanceList(int argc, char *argv[]); diff --git a/src/core/Makefile.am b/src/core/Makefile.am index c2f0682ed..c5fa42b87 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -133,9 +133,8 @@ SOURCES_COMMON = \ crypto/pbkdf2_cmac.cpp \ crypto/sha256.cpp \ mac/mac.cpp \ - mac/mac_blacklist.cpp \ + mac/mac_filter.cpp \ mac/mac_frame.cpp \ - mac/mac_whitelist.cpp \ meshcop/announce_begin_client.cpp \ meshcop/commissioner.cpp \ meshcop/dataset.cpp \ @@ -229,13 +228,8 @@ HEADERS_COMMON = \ crypto/pbkdf2_cmac.h \ crypto/sha256.hpp \ mac/mac.hpp \ - mac/mac_blacklist.hpp \ - mac/mac_blacklist_impl.hpp \ - mac/mac_blacklist_stub.hpp \ + mac/mac_filter.hpp \ mac/mac_frame.hpp \ - mac/mac_whitelist.hpp \ - mac/mac_whitelist_impl.hpp \ - mac/mac_whitelist_stub.hpp \ meshcop/announce_begin_client.hpp \ meshcop/commissioner.hpp \ meshcop/dataset.hpp \ diff --git a/src/core/api/link_api.cpp b/src/core/api/link_api.cpp index 2ee5bbc15..803bb4114 100644 --- a/src/core/api/link_api.cpp +++ b/src/core/api/link_api.cpp @@ -144,125 +144,101 @@ otShortAddress otLinkGetShortAddress(otInstance *aInstance) return aInstance->mThreadNetif.GetMac().GetShortAddress(); } -otError otLinkAddWhitelist(otInstance *aInstance, const uint8_t *aExtAddr) +#if OPENTHREAD_ENABLE_MAC_FILTER + +otMacFilterAddressMode otLinkFilterGetAddressMode(otInstance *aInstance) { - otError error = OT_ERROR_NONE; - - if (aInstance->mThreadNetif.GetMac().GetWhitelist().Add(*reinterpret_cast(aExtAddr)) == NULL) - { - error = OT_ERROR_NO_BUFS; - } - - return error; + return aInstance->mThreadNetif.GetMac().GetFilter().GetAddressMode(); } -otError otLinkAddWhitelistRssi(otInstance *aInstance, const uint8_t *aExtAddr, int8_t aRssi) +otError otLinkFilterSetAddressMode(otInstance *aInstance, otMacFilterAddressMode aMode) +{ + return aInstance->mThreadNetif.GetMac().GetFilter().SetAddressMode(aMode); +} + +otError otLinkFilterAddAddress(otInstance *aInstance, const otExtAddress *aExtAddress) { otError error = OT_ERROR_NONE; - otMacWhitelistEntry *entry; - entry = aInstance->mThreadNetif.GetMac().GetWhitelist().Add(*reinterpret_cast(aExtAddr)); - VerifyOrExit(entry != NULL, error = OT_ERROR_NO_BUFS); - aInstance->mThreadNetif.GetMac().GetWhitelist().SetFixedRssi(*entry, aRssi); + VerifyOrExit(aExtAddress != NULL, error = OT_ERROR_INVALID_ARGS); + + error = aInstance->mThreadNetif.GetMac().GetFilter().AddAddress(*static_cast(aExtAddress)); exit: return error; } -void otLinkRemoveWhitelist(otInstance *aInstance, const uint8_t *aExtAddr) -{ - aInstance->mThreadNetif.GetMac().GetWhitelist().Remove(*reinterpret_cast(aExtAddr)); -} - -void otLinkClearWhitelist(otInstance *aInstance) -{ - aInstance->mThreadNetif.GetMac().GetWhitelist().Clear(); -} - -otError otLinkGetWhitelistEntry(otInstance *aInstance, uint8_t aIndex, otMacWhitelistEntry *aEntry) +otError otLinkFilterRemoveAddress(otInstance *aInstance, const otExtAddress *aExtAddress) { otError error = OT_ERROR_NONE; - VerifyOrExit(aEntry != NULL, error = OT_ERROR_INVALID_ARGS); - error = aInstance->mThreadNetif.GetMac().GetWhitelist().GetEntry(aIndex, *aEntry); + VerifyOrExit(aExtAddress != NULL, error = OT_ERROR_INVALID_ARGS); + + error = aInstance->mThreadNetif.GetMac().GetFilter().RemoveAddress( + *static_cast(aExtAddress)); exit: return error; } -void otLinkSetWhitelistEnabled(otInstance *aInstance, bool aEnabled) +void otLinkFilterClearAddresses(otInstance *aInstance) { - aInstance->mThreadNetif.GetMac().GetWhitelist().SetEnabled(aEnabled); + return aInstance->mThreadNetif.GetMac().GetFilter().ClearAddresses(); } -bool otLinkIsWhitelistEnabled(otInstance *aInstance) +otError otLinkFilterGetNextAddress(otInstance *aInstance, otMacFilterIterator *aIterator, otMacFilterEntry *aEntry) { - return aInstance->mThreadNetif.GetMac().GetWhitelist().IsEnabled(); -} -otError otLinkAddBlacklist(otInstance *aInstance, const uint8_t *aExtAddr) -{ otError error = OT_ERROR_NONE; - if (aInstance->mThreadNetif.GetMac().GetBlacklist().Add(*reinterpret_cast(aExtAddr)) == NULL) - { - error = OT_ERROR_NO_BUFS; - } + VerifyOrExit(aIterator != NULL && aEntry != NULL, error = OT_ERROR_INVALID_ARGS); - return error; -} - -void otLinkRemoveBlacklist(otInstance *aInstance, const uint8_t *aExtAddr) -{ - aInstance->mThreadNetif.GetMac().GetBlacklist().Remove(*reinterpret_cast(aExtAddr)); -} - -void otLinkClearBlacklist(otInstance *aInstance) -{ - aInstance->mThreadNetif.GetMac().GetBlacklist().Clear(); -} - -otError otLinkGetBlacklistEntry(otInstance *aInstance, uint8_t aIndex, otMacBlacklistEntry *aEntry) -{ - otError error = OT_ERROR_NONE; - - VerifyOrExit(aEntry != NULL, error = OT_ERROR_INVALID_ARGS); - error = aInstance->mThreadNetif.GetMac().GetBlacklist().GetEntry(aIndex, *aEntry); + error = aInstance->mThreadNetif.GetMac().GetFilter().GetNextAddress(*aIterator, *aEntry); exit: return error; } -void otLinkSetBlacklistEnabled(otInstance *aInstance, bool aEnabled) +otError otLinkFilterAddRssIn(otInstance *aInstance, const otExtAddress *aExtAddress, int8_t aRss) { - aInstance->mThreadNetif.GetMac().GetBlacklist().SetEnabled(aEnabled); + return aInstance->mThreadNetif.GetMac().GetFilter().AddRssIn( + static_cast(aExtAddress), aRss); } -bool otLinkIsBlacklistEnabled(otInstance *aInstance) +otError otLinkFilterRemoveRssIn(otInstance *aInstance, const otExtAddress *aExtAddress) { - return aInstance->mThreadNetif.GetMac().GetBlacklist().IsEnabled(); + return aInstance->mThreadNetif.GetMac().GetFilter().RemoveRssIn(static_cast(aExtAddress)); } -otError otLinkGetAssignLinkQuality(otInstance *aInstance, const uint8_t *aExtAddr, uint8_t *aLinkQuality) +void otLinkFilterClearRssIn(otInstance *aInstance) { - Mac::ExtAddress extAddress; - - memset(&extAddress, 0, sizeof(extAddress)); - memcpy(extAddress.m8, aExtAddr, OT_EXT_ADDRESS_SIZE); - - return aInstance->mThreadNetif.GetMle().GetAssignLinkQuality(extAddress, *aLinkQuality); + aInstance->mThreadNetif.GetMac().GetFilter().ClearRssIn(); } -void otLinkSetAssignLinkQuality(otInstance *aInstance, const uint8_t *aExtAddr, uint8_t aLinkQuality) +otError otLinkFilterGetNextRssIn(otInstance *aInstance, otMacFilterIterator *aIterator, otMacFilterEntry *aEntry) { - Mac::ExtAddress extAddress; + otError error = OT_ERROR_NONE; - memset(&extAddress, 0, sizeof(extAddress)); - memcpy(extAddress.m8, aExtAddr, OT_EXT_ADDRESS_SIZE); + VerifyOrExit(aIterator != NULL && aEntry != NULL, error = OT_ERROR_INVALID_ARGS); - aInstance->mThreadNetif.GetMle().SetAssignLinkQuality(extAddress, aLinkQuality); + error = aInstance->mThreadNetif.GetMac().GetFilter().GetNextRssIn(*aIterator, *aEntry); + +exit: + return error; } +uint8_t otLinkConvertRssToLinkQuality(otInstance *aInstance, int8_t aRss) +{ + return LinkQualityInfo::ConvertRssToLinkQuality(aInstance->mThreadNetif.GetMac().GetNoiseFloor(), aRss); +} + +int8_t otLinkConvertLinkQualityToRss(otInstance *aInstance, uint8_t aLinkQuality) +{ + return LinkQualityInfo::ConvertLinkQualityToRss(aInstance->mThreadNetif.GetMac().GetNoiseFloor(), aLinkQuality); +} + +#endif // OPENTHREAD_ENABLE_MAC_FILTER + void otLinkSetPcapCallback(otInstance *aInstance, otLinkPcapCallback aPcapCallback, void *aCallbackContext) { aInstance->mThreadNetif.GetMac().SetPcapCallback(aPcapCallback, aCallbackContext); @@ -296,7 +272,8 @@ otError otLinkActiveScan(otInstance *aInstance, uint32_t aScanChannels, uint16_t { aInstance->mActiveScanCallback = aCallback; aInstance->mActiveScanCallbackContext = aCallbackContext; - return aInstance->mThreadNetif.GetMac().ActiveScan(aScanChannels, aScanDuration, &HandleActiveScanResult, aInstance); + return aInstance->mThreadNetif.GetMac().ActiveScan(aScanChannels, aScanDuration, + &HandleActiveScanResult, aInstance); } bool otLinkIsActiveScanInProgress(otInstance *aInstance) @@ -322,7 +299,8 @@ otError otLinkEnergyScan(otInstance *aInstance, uint32_t aScanChannels, uint16_t { aInstance->mEnergyScanCallback = aCallback; aInstance->mEnergyScanCallbackContext = aCallbackContext; - return aInstance->mThreadNetif.GetMac().EnergyScan(aScanChannels, aScanDuration, &HandleEnergyScanResult, aInstance); + return aInstance->mThreadNetif.GetMac().EnergyScan(aScanChannels, aScanDuration, + &HandleEnergyScanResult, aInstance); } void HandleEnergyScanResult(void *aContext, otEnergyScanResult *aResult) diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 91c53b7e3..131fbf807 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -136,8 +136,9 @@ Mac::Mac(ThreadNetif &aThreadNetif): mEnergyScanSampleRssiTask(aThreadNetif.GetIp6().mTaskletScheduler, &Mac::HandleEnergyScanSampleRssi, this), mPcapCallback(NULL), mPcapCallbackContext(NULL), - mWhitelist(), - mBlacklist(), +#if OPENTHREAD_ENABLE_MAC_FILTER + mFilter(), +#endif // OPENTHREAD_ENABLE_MAC_FILTER mTxFrame(static_cast(otPlatRadioGetTransmitBuffer(aThreadNetif.GetInstance()))), mKeyIdMode2FrameCounter(0), #if OPENTHREAD_CONFIG_STAY_AWAKE_BETWEEN_FRAGMENTS @@ -1468,12 +1469,13 @@ void Mac::ReceiveDoneTask(Frame *aFrame, otError aError) Address dstaddr; PanId panid; Neighbor *neighbor; - otMacWhitelistEntry *whitelistEntry; - int8_t rssi; bool receive = false; uint8_t commandId; bool scheduleNextTrasmission = false; otError error = aError; +#if OPENTHREAD_ENABLE_MAC_FILTER + int8_t rssi = OT_MAC_FILTER_FIXED_RSS_DISABLED; +#endif // OPENTHREAD_ENABLE_MAC_FILTER mCounters.mRxTotal++; @@ -1525,22 +1527,22 @@ void Mac::ReceiveDoneTask(Frame *aFrame, otError aError) ExitNow(error = OT_ERROR_INVALID_SOURCE_ADDRESS); } - // Source Whitelist Processing - if (srcaddr.mLength != 0 && mWhitelist.IsEnabled()) - { - VerifyOrExit((whitelistEntry = mWhitelist.Find(srcaddr.mExtAddress)) != NULL, error = OT_ERROR_WHITELIST_FILTERED); +#if OPENTHREAD_ENABLE_MAC_FILTER - if (mWhitelist.GetFixedRssi(*whitelistEntry, rssi) == OT_ERROR_NONE) + // Source filter Processing. + if (srcaddr.mLength != 0) + { + // check if filtered out by whitelist or blacklist. + SuccessOrExit(error = mFilter.Apply(srcaddr.mExtAddress, rssi)); + + // override with the rssi in setting + if (rssi != OT_MAC_FILTER_FIXED_RSS_DISABLED) { aFrame->mPower = rssi; } } - // Source Blacklist Processing - if (srcaddr.mLength != 0 && mBlacklist.IsEnabled()) - { - VerifyOrExit((mBlacklist.Find(srcaddr.mExtAddress)) == NULL, error = OT_ERROR_BLACKLIST_FILTERED); - } +#endif // OPENTHREAD_ENABLE_MAC_FILTER // Destination Address Filtering aFrame->GetDstAddr(dstaddr); @@ -1582,6 +1584,16 @@ void Mac::ReceiveDoneTask(Frame *aFrame, otError aError) if (neighbor != NULL) { +#if OPENTHREAD_ENABLE_MAC_FILTER + + // make assigned rssi to take effect quickly + if (rssi != OT_MAC_FILTER_FIXED_RSS_DISABLED) + { + neighbor->GetLinkInfo().Clear(); + } + +#endif // OPENTHREAD_ENABLE_MAC_FILTER + neighbor->GetLinkInfo().AddRss(GetNoiseFloor(), aFrame->mPower); if (aFrame->GetSecurityEnabled() == true) diff --git a/src/core/mac/mac.hpp b/src/core/mac/mac.hpp index 8e3533f34..90a233413 100644 --- a/src/core/mac/mac.hpp +++ b/src/core/mac/mac.hpp @@ -41,9 +41,8 @@ #include "common/locator.hpp" #include "common/tasklet.hpp" #include "common/timer.hpp" -#include "mac/mac_blacklist.hpp" #include "mac/mac_frame.hpp" -#include "mac/mac_whitelist.hpp" +#include "mac/mac_filter.hpp" #include "thread/key_manager.hpp" #include "thread/network_diagnostic_tlvs.hpp" #include "thread/topology.hpp" @@ -482,21 +481,15 @@ public: */ otError SetExtendedPanId(const uint8_t *aExtPanId); +#if OPENTHREAD_ENABLE_MAC_FILTER /** - * This method returns the MAC whitelist filter. + * This method returns the MAC filter. * - * @returns A reference to the MAC whitelist filter. + * @returns A reference to the MAC filter. * */ - Whitelist &GetWhitelist(void) { return mWhitelist; } - - /** - * This method returns the MAC blacklist filter. - * - * @returns A reference to the MAC blacklist filter. - * - */ - Blacklist &GetBlacklist(void) { return mBlacklist; } + Filter &GetFilter(void) { return mFilter; } +#endif // OPENTHREAD_ENABLE_MAC_FILTER /** * This method is called to handle receive events. @@ -744,8 +737,9 @@ private: otLinkPcapCallback mPcapCallback; void *mPcapCallbackContext; - Whitelist mWhitelist; - Blacklist mBlacklist; +#if OPENTHREAD_ENABLE_MAC_FILTER + Filter mFilter; +#endif // OPENTHREAD_ENABLE_MAC_FILTER Frame *mTxFrame; diff --git a/src/core/mac/mac_blacklist.cpp b/src/core/mac/mac_blacklist.cpp deleted file mode 100644 index e87b05833..000000000 --- a/src/core/mac/mac_blacklist.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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 - * This file implements blacklist IEEE 802.15.4 frame filtering based on MAC address. - */ - -#include - -#include "mac_blacklist.hpp" - -#include "utils/wrap_string.h" - -#include "common/code_utils.hpp" - -#if OPENTHREAD_ENABLE_MAC_WHITELIST - -namespace ot { -namespace Mac { - -Blacklist::Blacklist(void) -{ - mEnabled = false; - - for (int i = 0; i < kMaxEntries; i++) - { - mBlacklist[i].mValid = false; - } -} - -otError Blacklist::GetEntry(uint8_t aIndex, Entry &aEntry) const -{ - otError error = OT_ERROR_NONE; - - VerifyOrExit(aIndex < kMaxEntries, error = OT_ERROR_INVALID_ARGS); - - memcpy(&aEntry.mExtAddress, &mBlacklist[aIndex].mExtAddress, sizeof(aEntry.mExtAddress)); - aEntry.mValid = mBlacklist[aIndex].mValid; - -exit: - return error; -} - -Blacklist::Entry *Blacklist::Add(const ExtAddress &address) -{ - Entry *rval; - - VerifyOrExit((rval = Find(address)) == NULL); - - for (int i = 0; i < kMaxEntries; i++) - { - if (mBlacklist[i].mValid) - { - continue; - } - - memcpy(&mBlacklist[i].mExtAddress, &address, sizeof(mBlacklist[i].mExtAddress)); - mBlacklist[i].mValid = true; - ExitNow(rval = &mBlacklist[i]); - } - -exit: - return rval; -} - -void Blacklist::Clear(void) -{ - for (int i = 0; i < kMaxEntries; i++) - { - mBlacklist[i].mValid = false; - } -} - -void Blacklist::Remove(const ExtAddress &address) -{ - Entry *entry; - - VerifyOrExit((entry = Find(address)) != NULL); - memset(entry, 0, sizeof(*entry)); - -exit: - return; -} - -Blacklist::Entry *Blacklist::Find(const ExtAddress &address) -{ - Entry *rval = NULL; - - for (int i = 0; i < kMaxEntries; i++) - { - if (!mBlacklist[i].mValid) - { - continue; - } - - if (memcmp(&mBlacklist[i].mExtAddress, &address, sizeof(mBlacklist[i].mExtAddress)) == 0) - { - ExitNow(rval = &mBlacklist[i]); - } - } - -exit: - return rval; -} - -} // namespace Mac -} // namespace ot - -#endif // OPENTHREAD_ENABLE_MAC_WHITELIST diff --git a/src/core/mac/mac_blacklist.hpp b/src/core/mac/mac_blacklist.hpp deleted file mode 100644 index ddcaeaf39..000000000 --- a/src/core/mac/mac_blacklist.hpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - */ - -#include - -#if OPENTHREAD_ENABLE_MAC_WHITELIST -#include "mac_blacklist_impl.hpp" -#else -#include "mac_blacklist_stub.hpp" -#endif diff --git a/src/core/mac/mac_blacklist_impl.hpp b/src/core/mac/mac_blacklist_impl.hpp deleted file mode 100644 index cd74f81c6..000000000 --- a/src/core/mac/mac_blacklist_impl.hpp +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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 - * This file includes definitions for IEEE 802.15.4 frame filtering based on MAC address. - */ - -#ifndef MAC_BLACKLIST_HPP_ -#define MAC_BLACKLIST_HPP_ - -#include "utils/wrap_stdint.h" - -#include - -#include "mac/mac_frame.hpp" - -namespace ot { -namespace Mac { - -/** - * @addtogroup core-mac - * - * @{ - * - */ - -/** - * This class implements blacklist filtering on IEEE 802.15.4 frames. - * - */ -class Blacklist -{ -public: - typedef otMacBlacklistEntry Entry; - - enum - { - kMaxEntries = OPENTHREAD_CONFIG_MAC_BLACKLIST_SIZE, - }; - - /** - * This constructor initializes the blacklist filter. - * - */ - Blacklist(void); - - /** - * This method indicates whether or not the blacklist filter is enabled. - * - * @retval TRUE If the blacklist filter is enabled. - * @retval FALSE If the blacklist filter is disabled. - * - */ - bool IsEnabled(void) const { return mEnabled; } - - /** - * This method enables the blacklist filter. - * - * @param[in] aEnabled TRUE to enable the blacklist filter, FALSE otherwise. - * - */ - void SetEnabled(bool aEnabled) { mEnabled = aEnabled; } - - /** - * This method returns the maximum number of blacklist entries. - * - * @returns The maximum number of blacklist entries. - * - */ - int GetMaxEntries(void) const { return kMaxEntries; } - - /** - * This method gets a blacklist entry. - * - * @param[in] aIndex An index into the MAC blacklist table. - * @param[out] aEntry A reference to where the information is placed. - * - * @retval OT_ERROR_NONE Successfully retrieved the MAC blacklist entry. - * @retval OT_ERROR_INVALID_ARGS @p aIndex is out of bounds or @p aEntry is NULL. - * - */ - otError GetEntry(uint8_t aIndex, Entry &aEntry) const; - - /** - * This method adds an Extended Address to the blacklist filter. - * - * @param[in] aAddress A reference to the Extended Address. - * - * @returns A pointer to the blacklist entry or NULL if there are no available entries. - * - */ - Entry *Add(const ExtAddress &aAddress); - - /** - * This method removes an Extended Address to the blacklist filter. - * - * @param[in] aAddress A reference to the Extended Address. - * - */ - void Remove(const ExtAddress &aAddress); - - /** - * This method removes all entries from the blacklist filter. - * - */ - void Clear(void); - - /** - * This method finds a blacklist entry. - * - * @param[in] aAddress A reference to the Extended Address. - * - * @returns A pointer to the blacklist entry or NULL if the entry could not be found. - * - */ - Entry *Find(const ExtAddress &aAddress); - -private: - Entry mBlacklist[kMaxEntries]; - - bool mEnabled; -}; - -/** - * @} - * - */ - -} // namespace Mac -} // namespace ot - -#endif // MAC_BLACKLIST_HPP_ diff --git a/src/core/mac/mac_blacklist_stub.hpp b/src/core/mac/mac_blacklist_stub.hpp deleted file mode 100644 index 854cdd5ed..000000000 --- a/src/core/mac/mac_blacklist_stub.hpp +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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 - * This file includes definitions for IEEE 802.15.4 frame filtering based on MAC address. - */ - -#ifndef MAC_BLACKLIST_HPP_ -#define MAC_BLACKLIST_HPP_ - -#include "utils/wrap_stdint.h" - -#include - -#include "mac/mac_frame.hpp" - -namespace ot { -namespace Mac { - -class Blacklist -{ -public: - typedef otMacBlacklistEntry Entry; - - Blacklist(void) { } - - bool IsEnabled(void) const { return false; } - - void SetEnabled(bool) { } - - int GetMaxEntries(void) const { return 0; } - - otError GetEntry(uint8_t, Entry &) const { return OT_ERROR_NOT_IMPLEMENTED; } - - Entry *Add(const ExtAddress &) { return NULL; } - - void Remove(const ExtAddress &) { } - - void Clear(void) { } - - Entry *Find(const ExtAddress &) { return NULL; } -}; - -} // namespace Mac -} // namespace ot - -#endif // MAC_BLACKLIST_HPP_ diff --git a/src/core/mac/mac_filter.cpp b/src/core/mac/mac_filter.cpp new file mode 100644 index 000000000..aa31bfcc3 --- /dev/null +++ b/src/core/mac/mac_filter.cpp @@ -0,0 +1,291 @@ +/* + * Copyright (c) 2017, 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 + * This file implements Filter IEEE 802.15.4 frame filtering based on MAC address. + */ + +#include + +#include "mac_filter.hpp" + +#include "openthread/types.h" +#include "utils/wrap_string.h" + +#include "common/code_utils.hpp" + +#if OPENTHREAD_ENABLE_MAC_FILTER + +namespace ot { +namespace Mac { + +Filter::Filter(void) : + mAddressMode(OT_MAC_FILTER_ADDRESS_MODE_DISABLED), + mRssIn(OT_MAC_FILTER_FIXED_RSS_DISABLED) +{ + for (int i = 0; i < GetMaxEntries(); i++) + { + memset(&mEntries[i], 0, sizeof(Entry)); + + mEntries[i].mFiltered = false; + mEntries[i].mRssIn = OT_MAC_FILTER_FIXED_RSS_DISABLED; + } +} + +Filter::Entry *Filter::FindEntry(const ExtAddress &aExtAddress) +{ + Entry *entry = NULL; + + for (uint8_t i = 0; i < GetMaxEntries(); i++) + { + if ((mEntries[i].mFiltered || mEntries[i].mRssIn != OT_MAC_FILTER_FIXED_RSS_DISABLED) && + memcmp(&aExtAddress, &mEntries[i].mExtAddress, OT_EXT_ADDRESS_SIZE) == 0) + { + ExitNow(entry = &mEntries[i]); + } + } + +exit: + return entry; +} + +Filter::Entry *Filter::FindAvailEntry(void) +{ + Entry *entry = NULL; + + for (uint8_t i = 0; i < GetMaxEntries(); i++) + { + if (!mEntries[i].mFiltered && mEntries[i].mRssIn == OT_MAC_FILTER_FIXED_RSS_DISABLED) + { + ExitNow(entry = &mEntries[i]); + } + } + +exit: + return entry; +} + +otError Filter::SetAddressMode(otMacFilterAddressMode aMode) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(aMode == OT_MAC_FILTER_ADDRESS_MODE_DISABLED || + aMode == OT_MAC_FILTER_ADDRESS_MODE_WHITELIST || + aMode == OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST, + error = OT_ERROR_INVALID_ARGS); + + mAddressMode = aMode; + +exit: + return error; +} + +otError Filter::AddAddress(const ExtAddress &aExtAddress) +{ + otError error = OT_ERROR_NONE; + Entry *entry = FindEntry(aExtAddress); + + if (entry == NULL) + { + VerifyOrExit((entry = FindAvailEntry()) != NULL, error = OT_ERROR_NO_BUFS); + memcpy(&entry->mExtAddress, &aExtAddress, OT_EXT_ADDRESS_SIZE); + } + + if (entry->mFiltered) + { + ExitNow(error = OT_ERROR_ALREADY); + } + + entry->mFiltered = true; + +exit: + return error; +} + +otError Filter::RemoveAddress(const ExtAddress &aExtAddress) +{ + otError error = OT_ERROR_NONE; + Entry *entry = FindEntry(aExtAddress); + + if (entry == NULL || !entry->mFiltered) + { + ExitNow(error = OT_ERROR_NOT_FOUND); + } + + entry->mFiltered = false; + +exit: + return error; +} + +void Filter::ClearAddresses(void) +{ + for (uint8_t i = 0; i < GetMaxEntries(); i++) + { + mEntries[i].mFiltered = false; + } +} + +otError Filter::GetNextAddress(otMacFilterIterator &aIterator, Entry &aEntry) +{ + otError error = OT_ERROR_NOT_FOUND; + uint8_t i = *reinterpret_cast(&aIterator); + + for (; i < GetMaxEntries(); i++) + { + if (mEntries[i].mFiltered) + { + aEntry = mEntries[i]; + aIterator = *reinterpret_cast(&(++i)); + ExitNow(error = OT_ERROR_NONE); + } + } + +exit: + return error; +} + + +otError Filter::AddRssIn(const ExtAddress *aExtAddress, int8_t aRss) +{ + otError error = OT_ERROR_NONE; + + // set the default RssIn for all received messages. + if (aExtAddress == NULL) + { + mRssIn = aRss; + ExitNow(); + } + else + { + Entry *entry = FindEntry(*aExtAddress); + + if (entry == NULL) + { + VerifyOrExit((entry = FindAvailEntry()) != NULL, error = OT_ERROR_NO_BUFS); + memcpy(&entry->mExtAddress, aExtAddress, OT_EXT_ADDRESS_SIZE); + } + + entry->mRssIn = aRss; + } + +exit: + return error; +} + +otError Filter::RemoveRssIn(const ExtAddress *aExtAddress) +{ + otError error = OT_ERROR_NONE; + + if (aExtAddress == NULL) + { + mRssIn = OT_MAC_FILTER_FIXED_RSS_DISABLED; + } + else + { + Entry *entry = FindEntry(*aExtAddress); + VerifyOrExit(entry != NULL, error = OT_ERROR_NOT_FOUND); + entry->mRssIn = OT_MAC_FILTER_FIXED_RSS_DISABLED; + } + +exit: + return error; +} + +void Filter::ClearRssIn(void) +{ + mRssIn = OT_MAC_FILTER_FIXED_RSS_DISABLED; + + for (uint8_t i = 0; i < GetMaxEntries(); i++) + { + mEntries[i].mRssIn = OT_MAC_FILTER_FIXED_RSS_DISABLED; + } +} + +otError Filter::GetNextRssIn(otMacFilterIterator &aIterator, Entry &aEntry) +{ + otError error = OT_ERROR_NOT_FOUND; + uint8_t i = *reinterpret_cast(&aIterator); + + for (; i < GetMaxEntries(); i++) + { + if (mEntries[i].mRssIn != OT_MAC_FILTER_FIXED_RSS_DISABLED) + { + aEntry = mEntries[i]; + aIterator = *reinterpret_cast(&(++i)); + ExitNow(error = OT_ERROR_NONE); + } + } + + // return default rssin setting if no more rssin filter entry. + if (i == GetMaxEntries() && mRssIn != OT_MAC_FILTER_FIXED_RSS_DISABLED) + { + memset(&aEntry.mExtAddress, 0xff, OT_EXT_ADDRESS_SIZE); + aEntry.mRssIn = mRssIn; + aIterator = *reinterpret_cast(&(++i)); + ExitNow(error = OT_ERROR_NONE); + } + +exit: + return error; +} + +otError Filter::Apply(const ExtAddress &aExtAddress, int8_t &aRss) +{ + otError error = OT_ERROR_NONE; + + otMacFilterEntry *entry = FindEntry(aExtAddress); + + // assign the default RssIn setting for all receiving messages first. + aRss = mRssIn; + + // check AddressFilter. + if (mAddressMode == OT_MAC_FILTER_ADDRESS_MODE_WHITELIST) + { + VerifyOrExit(entry != NULL && entry->mFiltered, error = OT_ERROR_WHITELIST_FILTERED); + } + else if (mAddressMode == OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST) + { + VerifyOrExit(entry == NULL || !entry->mFiltered, error = OT_ERROR_BLACKLIST_FILTERED); + } + + // not override the default RssIn setting if no specific RssIn on the Extended Address. + if (entry != NULL && entry->mRssIn != OT_MAC_FILTER_FIXED_RSS_DISABLED) + { + aRss = entry->mRssIn; + } + +exit: + return error; +} + +} // namespace Mac +} // namespace ot + +#endif // OPENTHREAD_ENABLE_MAC_FILTER diff --git a/src/core/mac/mac_filter.hpp b/src/core/mac/mac_filter.hpp new file mode 100644 index 000000000..bce4e07cd --- /dev/null +++ b/src/core/mac/mac_filter.hpp @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2017, 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 + * This file includes definitions for IEEE 802.15.4 frame filtering based on MAC address. + */ + +#ifndef MAC_FILTER_HPP_ +#define MAC_FILTER_HPP_ + +#include "utils/wrap_stdint.h" + +#include + +#include "mac/mac_frame.hpp" + +#if OPENTHREAD_ENABLE_MAC_FILTER + +namespace ot { +namespace Mac { + +/** + * @addtogroup core-mac + * + * @{ + * + */ + +/** + * This class implements Mac Filter on IEEE 802.15.4 frames. + * + */ +class Filter +{ +public: + typedef otMacFilterEntry Entry; + + enum + { + kMaxEntries = OPENTHREAD_CONFIG_MAC_FILTER_SIZE, + }; + + /** + * This constructor initializes the filter. + * + */ + 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. + * + * @returns the address mode. + * + */ + otMacFilterAddressMode GetAddressMode(void) const { return mAddressMode; } + + /** + * This function sets the address mode of the filter. + * + * @param[in] aMode The address mode to set. + * + * @retval OT_ERROR_NONE Successfully set the AddressFilter mode. + * @retval OT_ERROR_INVALID_ARGS @p aMode is not valid address mode. + * + */ + otError SetAddressMode(otMacFilterAddressMode aMode); + + /** + * This method adds an Extended Address to filter. + * + * @param[in] aExtAddress A reference to the Extended Address. + * + * @retval OT_ERROR_NONE Successfully added @p aExtAddress to the filter. + * @retval OT_ERROR_ALREADY If @p aExtAddress was already in the Filter. + * @retval OT_ERROR_NO_BUFS No available entry exists. + * + */ + otError AddAddress(const ExtAddress &aExtAddress); + + /** + * This method removes an Extended Address from the filter. + * + * @param[in] aExtAddress A reference to the Extended Address. + * + * @retval OT_ERROR_NONE Successfully removed @p aExtAddress from the filter. + * @retval OT_ERROR_NOT_FOUND @p aExtAddress is not in the filter. + * + */ + otError RemoveAddress(const ExtAddress &aExtAddress); + + /** + * This method clears all Extended Addresses from the filter. + * + */ + void ClearAddresses(void); + + /** + * This method gets an in-use address filter entry. + * + * @param[inout] aIterator A reference to the MAC filter iterator context. To get the first in-use address filter + * entry, it should be set to OT_MAC_FILTER_ITERATOR_INIT. + * @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_NOT_FOUND No subsequent entry exists. + * + */ + otError GetNextAddress(otMacFilterIterator &aIterator, Entry &aEntry); + + /** + * This method sets the received signal strength for the messsages from the Extended Address. + * The default received signal strength for all received messages would be set if no Extended Address is specified. + * + * @param[in] aExtAddress A pointer to the Extended Address, or NULL to set the default received signal strength. + * @param[in] aRss The received signal strength to set. + * + * @retval OT_ERROR_NONE Successfully set @p aRss for @p aExtAddress, or + * set the default @p aRss for all received messages if @p aExtAddress is NULL. + * @retval OT_ERROR_NO_BUFS No available entry exists. + * + */ + otError AddRssIn(const ExtAddress *aExtAddress, int8_t aRss); + + /** + * This method removes the received signal strength setting for the received messages from the Extended Address, + * or removes the default received signal strength setting if no Extended Address is specified. + * + * @param[in] aExtAddress A pointer to the Extended Address. + * + * @retval OT_ERROR_NONE Successfully removed the received signal strength setting for the received + * messages from @p aExtAddress or removed the default received signal strength + * setting if @p aExtAddress is NULL. + * @retval OT_ERROR_NOT_FOUND @p aExtAddress is not in the RssIn filter if it is not NULL. + * + */ + otError RemoveRssIn(const ExtAddress *aExtAddress); + + /** + * This method clears all the received signal strength settings. + * + */ + void ClearRssIn(void); + + /** + * This method gets an in-use RssIn filter entry. + * + * @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. + * @param[out] aEntry A reference to where the information is placed. The last entry would have the + * Extended Address as all 0xff to indicate the default received signal strength + * if it was set. + * + * @retval OT_ERROR_NONE Successfully retrieved the in-use RssIn filter entry. + * @retval OT_ERROR_NOT_FOUND No subsequent entry exists. + * + */ + otError GetNextRssIn(otMacFilterIterator &aIterator, Entry &aEntry); + + /** + * This method applies the filter rules on 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. + * + * @retval OT_ERROR_NONE Successfully applied the filter rules on @p aExtAddress. + * @retval OT_ERROR_WHITELIST_FILTERED Whitelist is enabled and @p aExtAddress is not in the whitelist + * @retval OT_ERROR_BLACKLIST_FILTERED Blacklist is enabled and @p aExtAddress is in the blacklist + * + */ + otError Apply(const ExtAddress &aExtAddress, int8_t &aRss); + +private: + Entry *FindAvailEntry(void); + Entry *FindEntry(const ExtAddress &aExtAddress); + + Entry mEntries[kMaxEntries]; + otMacFilterAddressMode mAddressMode; + int8_t mRssIn; +}; + +/** + * @} + * + */ + +} // namespace Mac +} // namespace ot + +#endif // OPENTHREAD_ENABLE_MAC_FILTER + +#endif // MAC_FILTER_HPP_ diff --git a/src/core/mac/mac_whitelist.cpp b/src/core/mac/mac_whitelist.cpp deleted file mode 100644 index 388d0a3e6..000000000 --- a/src/core/mac/mac_whitelist.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/* - * 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 - * This file implements whitelist IEEE 802.15.4 frame filtering based on MAC address. - */ - -#include - -#include "mac_whitelist.hpp" - -#include "utils/wrap_string.h" - -#include "common/code_utils.hpp" - -#if OPENTHREAD_ENABLE_MAC_WHITELIST - -namespace ot { -namespace Mac { - -Whitelist::Whitelist(void) -{ - mEnabled = false; - - for (int i = 0; i < kMaxEntries; i++) - { - mWhitelist[i].mValid = false; - } -} - -otError Whitelist::GetEntry(uint8_t aIndex, Entry &aEntry) const -{ - otError error = OT_ERROR_NONE; - - VerifyOrExit(aIndex < kMaxEntries, error = OT_ERROR_INVALID_ARGS); - - memcpy(&aEntry.mExtAddress, &mWhitelist[aIndex].mExtAddress, sizeof(aEntry.mExtAddress)); - aEntry.mRssi = mWhitelist[aIndex].mRssi; - aEntry.mValid = mWhitelist[aIndex].mValid; - aEntry.mFixedRssi = mWhitelist[aIndex].mFixedRssi; - -exit: - return error; -} - -Whitelist::Entry *Whitelist::Add(const ExtAddress &address) -{ - Entry *rval; - - VerifyOrExit((rval = Find(address)) == NULL); - - for (int i = 0; i < kMaxEntries; i++) - { - if (mWhitelist[i].mValid) - { - continue; - } - - memcpy(&mWhitelist[i].mExtAddress, &address, sizeof(mWhitelist[i].mExtAddress)); - mWhitelist[i].mValid = true; - mWhitelist[i].mFixedRssi = false; - ExitNow(rval = &mWhitelist[i]); - } - -exit: - return rval; -} - -void Whitelist::Clear(void) -{ - for (int i = 0; i < kMaxEntries; i++) - { - mWhitelist[i].mValid = false; - } -} - -void Whitelist::Remove(const ExtAddress &address) -{ - Entry *entry; - - VerifyOrExit((entry = Find(address)) != NULL); - memset(entry, 0, sizeof(*entry)); - -exit: - return; -} - -Whitelist::Entry *Whitelist::Find(const ExtAddress &address) -{ - Entry *rval = NULL; - - for (int i = 0; i < kMaxEntries; i++) - { - if (!mWhitelist[i].mValid) - { - continue; - } - - if (memcmp(&mWhitelist[i].mExtAddress, &address, sizeof(mWhitelist[i].mExtAddress)) == 0) - { - ExitNow(rval = &mWhitelist[i]); - } - } - -exit: - return rval; -} - -void Whitelist::ClearFixedRssi(Entry &aEntry) -{ - aEntry.mFixedRssi = false; -} - -otError Whitelist::GetFixedRssi(Entry &aEntry, int8_t &rssi) const -{ - otError error = OT_ERROR_NONE; - - VerifyOrExit(aEntry.mValid && aEntry.mFixedRssi, error = OT_ERROR_INVALID_ARGS); - rssi = aEntry.mRssi; - -exit: - return error; -} - -void Whitelist::SetFixedRssi(Entry &aEntry, int8_t aRssi) -{ - aEntry.mFixedRssi = true; - aEntry.mRssi = aRssi; -} - -} // namespace Mac -} // namespace ot - -#endif // OPENTHREAD_ENABLE_MAC_WHITELIST diff --git a/src/core/mac/mac_whitelist.hpp b/src/core/mac/mac_whitelist.hpp deleted file mode 100644 index 175c64b79..000000000 --- a/src/core/mac/mac_whitelist.hpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - */ - -#include - -#if OPENTHREAD_ENABLE_MAC_WHITELIST -#include "mac_whitelist_impl.hpp" -#else -#include "mac_whitelist_stub.hpp" -#endif diff --git a/src/core/mac/mac_whitelist_impl.hpp b/src/core/mac/mac_whitelist_impl.hpp deleted file mode 100644 index 4ec547455..000000000 --- a/src/core/mac/mac_whitelist_impl.hpp +++ /dev/null @@ -1,187 +0,0 @@ -/* - * 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 - * This file includes definitions for IEEE 802.15.4 frame filtering based on MAC address. - */ - -#ifndef MAC_WHITELIST_HPP_ -#define MAC_WHITELIST_HPP_ - -#include "utils/wrap_stdint.h" - -#include - -#include "mac/mac_frame.hpp" - -namespace ot { -namespace Mac { - -/** - * @addtogroup core-mac - * - * @{ - * - */ - -/** - * This class implements whitelist filtering on IEEE 802.15.4 frames. - * - */ -class Whitelist -{ -public: - typedef otMacWhitelistEntry Entry; - - enum - { - kMaxEntries = OPENTHREAD_CONFIG_MAC_WHITELIST_SIZE, - }; - - /** - * This constructor initializes the whitelist filter. - * - */ - Whitelist(void); - - /** - * This method indicates whether or not the whitelist filter is enabled. - * - * @retval TRUE If the whitelist filter is enabled. - * @retval FALSE If the whitelist filter is disabled. - * - */ - bool IsEnabled(void) const { return mEnabled; } - - /** - * This method enables the whitelist filter. - * - * @param[in] aEnabled TRUE to enable the whitelist filter, FALSE otherwise. - * - */ - void SetEnabled(bool aEnabled) { mEnabled = aEnabled; } - - /** - * This method returns the maximum number of whitelist entries. - * - * @returns The maximum number of whitelist entries. - * - */ - int GetMaxEntries(void) const { return kMaxEntries; } - - /** - * This method gets a whitelist entry. - * - * @param[in] aIndex An index into the MAC whitelist table. - * @param[out] aEntry A reference to where the information is placed. - * - * @retval OT_ERROR_NONE Successfully retrieved the MAC whitelist entry. - * @retval OT_ERROR_INVALID_ARGS @p aIndex is out of bounds or @p aEntry is NULL. - * - */ - otError GetEntry(uint8_t aIndex, Entry &aEntry) const; - - /** - * This method adds an Extended Address to the whitelist filter. - * - * @param[in] aAddress A reference to the Extended Address. - * - * @returns A pointer to the whitelist entry or NULL if there are no available entries. - * - */ - Entry *Add(const ExtAddress &aAddress); - - /** - * This method removes an Extended Address to the whitelist filter. - * - * @param[in] aAddress A reference to the Extended Address. - * - */ - void Remove(const ExtAddress &aAddress); - - /** - * This method removes all entries from the whitelist filter. - * - */ - void Clear(void); - - /** - * This method finds a whitelist entry. - * - * @param[in] aAddress A reference to the Extended Address. - * - * @returns A pointer to the whitelist entry or NULL if the entry could not be found. - * - */ - Entry *Find(const ExtAddress &aAddress); - - /** - * This method clears the fixed RSSI value and uses the measured value provided by the radio instead. - * - * @param[in] aEntry A reference to the whitelist entry. - * - */ - void ClearFixedRssi(Entry &aEntry); - - /** - * This method indicates whether or not the fixed RSSI is set. - * - * @param[in] aEntry A reference to the whitelist entry. - * @param[out] aRssi A reference to the RSSI variable. - * - * @retval OT_ERROR_NONE A fixed RSSI is set and written to @p aRssi. - * @retval OT_ERROR_INVALID_ARGS A fixed RSSI was not set. - * - */ - otError GetFixedRssi(Entry &aEntry, int8_t &aRssi) const; - - /** - * This method sets a fixed RSSI value for all received messages matching @p aEntry. - * - * @param[in] aEntry A reference to the whitelist entry. - * @param[in] aRssi An RSSI value in dBm. - * - */ - void SetFixedRssi(Entry &aEntry, int8_t aRssi); - -private: - Entry mWhitelist[kMaxEntries]; - - bool mEnabled; -}; - -/** - * @} - * - */ - -} // namespace Mac -} // namespace ot - -#endif // MAC_WHITELIST_HPP_ diff --git a/src/core/mac/mac_whitelist_stub.hpp b/src/core/mac/mac_whitelist_stub.hpp deleted file mode 100644 index 00ac6f3e4..000000000 --- a/src/core/mac/mac_whitelist_stub.hpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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 - * This file includes definitions for IEEE 802.15.4 frame filtering based on MAC address. - */ - -#ifndef MAC_WHITELIST_HPP_ -#define MAC_WHITELIST_HPP_ - -#include "utils/wrap_stdint.h" - -#include - -#include "mac/mac_frame.hpp" - -namespace ot { -namespace Mac { - -class Whitelist -{ -public: - typedef otMacWhitelistEntry Entry; - - Whitelist(void) { } - - bool IsEnabled(void) const { return false; } - - void SetEnabled(bool) { } - - int GetMaxEntries(void) const { return 0; } - - otError GetEntry(uint8_t, Entry &) const { return OT_ERROR_NOT_IMPLEMENTED; } - - Entry *Add(const ExtAddress &) { return NULL; } - - void Remove(const ExtAddress &) { } - - void Clear(void) { } - - Entry *Find(const ExtAddress &) { return NULL; } - - void ClearFixedRssi(Entry &) { } - - otError GetFixedRssi(Entry &, int8_t &) const { return OT_ERROR_NOT_IMPLEMENTED; } - - void SetFixedRssi(Entry &, int8_t) { } -}; - -} // namespace Mac -} // namespace ot - -#endif // MAC_WHITELIST_HPP_ diff --git a/src/core/openthread-core-default-config.h b/src/core/openthread-core-default-config.h index c781d00de..73eea78ed 100644 --- a/src/core/openthread-core-default-config.h +++ b/src/core/openthread-core-default-config.h @@ -402,24 +402,14 @@ #endif // OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT /** - * @def OPENTHREAD_CONFIG_MAC_BLACKLIST_SIZE + * @def OPENTHREAD_CONFIG_MAC_FILTER_SIZE * - * The number if MAC blacklist entries. + * The number of MAC Filter entries. * */ -#ifndef OPENTHREAD_CONFIG_MAC_BLACKLIST_SIZE -#define OPENTHREAD_CONFIG_MAC_BLACKLIST_SIZE 32 -#endif // OPENTHREAD_CONFIG_MAC_BLACKLIST_SIZE - -/** - * @def OPENTHREAD_CONFIG_MAC_WHITELIST_SIZE - * - * The number if MAC whitelist entries. - * - */ -#ifndef OPENTHREAD_CONFIG_MAC_WHITELIST_SIZE -#define OPENTHREAD_CONFIG_MAC_WHITELIST_SIZE 32 -#endif // OPENTHREAD_CONFIG_MAC_WHITELIST_SIZE +#ifndef OPENTHREAD_CONFIG_MAC_FILTER_SIZE +#define OPENTHREAD_CONFIG_MAC_FILTER_SIZE 32 +#endif // OPENTHREAD_CONFIG_MAC_FILTER_SIZE /** * @def OPENTHREAD_CONFIG_STORE_FRAME_COUNTER_AHEAD diff --git a/src/core/thread/link_quality.cpp b/src/core/thread/link_quality.cpp index 622d04e94..b91a87009 100644 --- a/src/core/thread/link_quality.cpp +++ b/src/core/thread/link_quality.cpp @@ -199,6 +199,32 @@ uint8_t LinkQualityInfo::ConvertRssToLinkQuality(int8_t aNoiseFloor, int8_t aRss return ConvertLinkMarginToLinkQuality(ConvertRssToLinkMargin(aNoiseFloor, aRss)); } +int8_t LinkQualityInfo::ConvertLinkQualityToRss(int8_t aNoiseFloor, uint8_t aLinkQuality) +{ + uint8_t linkmargin = 0; + + switch (aLinkQuality) + { + case 3: + linkmargin = kLinkQuality3LinkMargin; + break; + + case 2: + linkmargin = kLinkQuality2LinkMargin; + break; + + case 1: + linkmargin = kLinkQuality1LinkMargin; + break; + + default: + linkmargin = kLinkQuality0LinkMargin; + break; + } + + return linkmargin + aNoiseFloor; +} + uint8_t LinkQualityInfo::CalculateLinkQuality(uint8_t aLinkMargin, uint8_t aLastLinkQuality) { uint8_t threshold1, threshold2, threshold3; diff --git a/src/core/thread/link_quality.hpp b/src/core/thread/link_quality.hpp index 0221d58b4..2ca0593d5 100644 --- a/src/core/thread/link_quality.hpp +++ b/src/core/thread/link_quality.hpp @@ -160,6 +160,7 @@ private: */ class LinkQualityInfo { + public: enum { @@ -284,6 +285,18 @@ public: */ static uint8_t ConvertRssToLinkQuality(int8_t aNoiseFloor, int8_t aRss); + /** + * This method converts a link quality value to a typical received signal strength value . + * @note only for test + * + * @param[in] aNoiseFloor The noise floor value (in dBm). + * @param[in] aLinkQuality The link quality value in [0, 3]. + * + * @returns The typical platform rssi. + * + */ + static int8_t ConvertLinkQualityToRss(int8_t aNoiseFloor, uint8_t aLinkQuality); + private: enum { @@ -294,6 +307,13 @@ private: kThreshold1 = 2, // Link margin threshold for quality 1 link. kHysteresisThreshold = 2, // Link margin hysteresis threshold. + // constants for test: + + kLinkQuality3LinkMargin = 50, ///< link margin for Link Quality 3 (21 - 255) + kLinkQuality2LinkMargin = 15, ///< link margin for Link Quality 3 (21 - 255) + kLinkQuality1LinkMargin = 5, ///< link margin for Link Quality 3 (21 - 255) + kLinkQuality0LinkMargin = 0, ///< link margin for Link Quality 3 (21 - 255) + kNoLinkQuality = 0xff, // Used to indicate that there is no previous/last link quality. }; diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 9e4f29f8f..fa3f9e305 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -66,9 +66,6 @@ Mle::Mle(ThreadNetif &aThreadNetif) : mRetrieveNewNetworkData(false), mRole(OT_DEVICE_ROLE_DISABLED), mDeviceMode(ModeTlv::kModeRxOnWhenIdle | ModeTlv::kModeSecureDataRequest), - isAssignLinkQuality(false), - mAssignLinkQuality(0), - mAssignLinkMargin(0), mParentRequestState(kParentIdle), mReattachState(kReattachStop), mParentRequestTimer(aThreadNetif.GetIp6(), &Mle::HandleParentRequestTimer, this), @@ -170,8 +167,6 @@ Mle::Mle(ThreadNetif &aThreadNetif) : mNetifCallback.Set(&Mle::HandleNetifStateChanged, this); aThreadNetif.RegisterCallback(mNetifCallback); - - memset(&mAddr64, 0, sizeof(mAddr64)); } otError Mle::Enable(void) @@ -888,49 +883,6 @@ exit: return error; } -otError Mle::GetAssignLinkQuality(const Mac::ExtAddress aMacAddr, uint8_t &aLinkQuality) -{ - otError error; - - VerifyOrExit((memcmp(aMacAddr.m8, mAddr64.m8, OT_EXT_ADDRESS_SIZE)) == 0, error = OT_ERROR_INVALID_ARGS); - - aLinkQuality = mAssignLinkQuality; - - return OT_ERROR_NONE; - -exit: - return error; -} - -void Mle::SetAssignLinkQuality(const Mac::ExtAddress aMacAddr, uint8_t aLinkQuality) -{ - isAssignLinkQuality = true; - mAddr64 = aMacAddr; - - mAssignLinkQuality = aLinkQuality; - - switch (aLinkQuality) - { - case 3: - mAssignLinkMargin = kMinAssignedLinkMargin3; - break; - - case 2: - mAssignLinkMargin = kMinAssignedLinkMargin2; - break; - - case 1: - mAssignLinkMargin = kMinAssignedLinkMargin1; - break; - - case 0: - mAssignLinkMargin = kMinAssignedLinkMargin0; - - default: - break; - } -} - void Mle::GenerateNonce(const Mac::ExtAddress &aMacAddr, uint32_t aFrameCounter, uint8_t aSecurityLevel, uint8_t *aNonce) { diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index cad6f9f17..1e0d9b3de 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -822,27 +822,6 @@ public: */ otError GetLeaderData(otLeaderData &aLeaderData); - /** - * This method returns the link quality on the link to a given extended address. - * - * @param[in] aMacAddr The IEEE 802.15.4 Extended Mac Address. - * @param[in] aLinkQuality A reference to the assigned link quality. - * - * @retval OT_ERROR_NONE Successfully retrieve the link quality to aLinkQuality. - * @retval OT_ERROR_INVALID_ARGS No match found with a given extended address. - * - */ - otError GetAssignLinkQuality(const Mac::ExtAddress aMacAddr, uint8_t &aLinkQuality); - - /** - * This method sets the link quality on the link to a given extended address. - * - * @param[in] aMacAddr The IEEE 802.15.4 Extended Mac Address. - * @param[in] aLinkQaulity The link quality to be set on the link. - * - */ - void SetAssignLinkQuality(const Mac::ExtAddress aMacAddr, uint8_t aLinkQuality); - /** * This method returns the Child ID portion of an RLOC16. * @@ -1317,11 +1296,6 @@ protected: Router mParent; ///< Parent information. uint8_t mDeviceMode; ///< Device mode setting. - bool isAssignLinkQuality; ///< Indicating an assigned link quality is used on the link - uint8_t mAssignLinkQuality; ///< The assigned link quality value - uint8_t mAssignLinkMargin; ///< The maximum link margin corresponding to mAssignLinkQuality - Mac::ExtAddress mAddr64; ///< A given IEEE 802.15.4 Extended Address - /** * States when searching for a parent. * diff --git a/src/core/thread/mle_constants.hpp b/src/core/thread/mle_constants.hpp index eaba6f9dd..b374a2ac9 100644 --- a/src/core/thread/mle_constants.hpp +++ b/src/core/thread/mle_constants.hpp @@ -125,15 +125,6 @@ enum kLinkQuality0LinkCost = 16, ///< Link Cost for Link Quality 0 }; -// add for certification testing -enum -{ - kMinAssignedLinkMargin3 = 0x15, ///< minimal link margin for Link Quality 3 (21 - 255) - kMinAssignedLinkMargin2 = 0x0b, ///< minimal link margin for Link Quality 2 (11 - 20) - kMinAssignedLinkMargin1 = 0x03, ///< minimal link margin for Link Quality 1 (3 - 9) - kMinAssignedLinkMargin0 = 0x00, ///< minimal link margin for Link Quality 0 (0 - 2) -}; - /** * Multicast Forwarding Constants * diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 3d01972a0..7d1efe11b 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -784,13 +784,6 @@ otError MleRouter::SendLinkAccept(const Ip6::MessageInfo &aMessageInfo, Neighbor // always append a link margin, regardless of whether or not it was requested linkMargin = LinkQualityInfo::ConvertRssToLinkMargin(GetNetif().GetMac().GetNoiseFloor(), threadMessageInfo->mRss); - // add for certification testing - if (isAssignLinkQuality && aNeighbor != NULL && - (memcmp(&aNeighbor->GetExtAddress(), mAddr64.m8, OT_EXT_ADDRESS_SIZE) == 0)) - { - linkMargin = mAssignLinkMargin; - } - SuccessOrExit(error = AppendLinkMargin(*message, linkMargin)); if (aNeighbor != NULL && IsActiveRouter(aNeighbor->GetRloc16())) @@ -1130,12 +1123,6 @@ uint8_t MleRouter::GetLinkCost(uint8_t aRouterId) rval = router->GetLinkQualityOut(); } - // add for certification testing - if (isAssignLinkQuality && (memcmp(&router->GetExtAddress(), mAddr64.m8, OT_EXT_ADDRESS_SIZE) == 0)) - { - rval = mAssignLinkQuality; - } - rval = LinkQualityToCost(rval); exit: @@ -1924,18 +1911,8 @@ otError MleRouter::SendParentResponse(Child *aChild, const ChallengeTlv &challen aChild->GenerateChallenge(); SuccessOrExit(error = AppendChallenge(*message, aChild->GetChallenge(), aChild->GetChallengeSize())); - - if (isAssignLinkQuality && - (memcmp(mAddr64.m8, &aChild->GetExtAddress(), OT_EXT_ADDRESS_SIZE) == 0)) - { - // use assigned one to ensure the link quality - SuccessOrExit(error = AppendLinkMargin(*message, mAssignLinkMargin)); - } - else - { - error = AppendLinkMargin(*message, aChild->GetLinkInfo().GetLinkMargin(GetNetif().GetMac().GetNoiseFloor())); - SuccessOrExit(error); - } + error = AppendLinkMargin(*message, aChild->GetLinkInfo().GetLinkMargin(GetNetif().GetMac().GetNoiseFloor())); + SuccessOrExit(error); SuccessOrExit(error = AppendConnectivity(*message)); SuccessOrExit(error = AppendVersion(*message)); @@ -4459,16 +4436,7 @@ void MleRouter::FillRouteTlv(RouteTlv &tlv) tlv.SetRouteCost(routeCount, cost); tlv.SetLinkQualityOut(routeCount, mRouters[i].GetLinkQualityOut()); - - if (isAssignLinkQuality && - (memcmp(&mRouters[i].GetExtAddress(), mAddr64.m8, OT_EXT_ADDRESS_SIZE) == 0)) - { - tlv.SetLinkQualityIn(routeCount, mAssignLinkQuality); - } - else - { - tlv.SetLinkQualityIn(routeCount, mRouters[i].GetLinkInfo().GetLinkQuality()); - } + tlv.SetLinkQualityIn(routeCount, mRouters[i].GetLinkInfo().GetLinkQuality()); } routeCount++; diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index ead72374a..2f136f356 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -79,8 +79,6 @@ namespace ot { #define NCP_CHANGED_THREAD_ON_MESH_NETS (1U << 30) #define NCP_CHANGED_THREAD_OFF_MESH_ROUTES (1U << 29) -#define RSSI_OVERRIDE_DISABLED 127 // Used for PROP_MAC_WHITELIST - #define IGNORE_RETURN_VALUE(s) do { if (s){} } while (0) // ---------------------------------------------------------------------------- @@ -168,11 +166,12 @@ const NcpBase::GetPropertyHandlerEntry NcpBase::mGetPropertyHandlerTable[] = #if OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD NCP_GET_PROP_HANDLER_ENTRY(THREAD_COMMISSIONER_ENABLED), #endif -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER NCP_GET_PROP_HANDLER_ENTRY(MAC_WHITELIST), NCP_GET_PROP_HANDLER_ENTRY(MAC_WHITELIST_ENABLED), NCP_GET_PROP_HANDLER_ENTRY(MAC_BLACKLIST), NCP_GET_PROP_HANDLER_ENTRY(MAC_BLACKLIST_ENABLED), + NCP_GET_PROP_HANDLER_ENTRY(MAC_FIXED_RSS), #endif NCP_GET_PROP_HANDLER_ENTRY(THREAD_MODE), NCP_GET_PROP_HANDLER_ENTRY(THREAD_CHILD_TIMEOUT), @@ -304,11 +303,12 @@ const NcpBase::SetPropertyHandlerEntry NcpBase::mSetPropertyHandlerTable[] = NCP_SET_PROP_HANDLER_ENTRY(IPV6_ML_PREFIX), NCP_SET_PROP_HANDLER_ENTRY(IPV6_ICMP_PING_OFFLOAD), NCP_SET_PROP_HANDLER_ENTRY(THREAD_RLOC16_DEBUG_PASSTHRU), -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER NCP_SET_PROP_HANDLER_ENTRY(MAC_WHITELIST), NCP_SET_PROP_HANDLER_ENTRY(MAC_WHITELIST_ENABLED), NCP_SET_PROP_HANDLER_ENTRY(MAC_BLACKLIST), NCP_SET_PROP_HANDLER_ENTRY(MAC_BLACKLIST_ENABLED), + NCP_SET_PROP_HANDLER_ENTRY(MAC_FIXED_RSS), #endif #if OPENTHREAD_ENABLE_RAW_LINK_API NCP_SET_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_ENABLED), @@ -380,9 +380,10 @@ const NcpBase::InsertPropertyHandlerEntry NcpBase::mInsertPropertyHandlerTable[] #if OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD NCP_INSERT_PROP_HANDLER_ENTRY(THREAD_JOINERS), #endif -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER NCP_INSERT_PROP_HANDLER_ENTRY(MAC_WHITELIST), NCP_INSERT_PROP_HANDLER_ENTRY(MAC_BLACKLIST), + NCP_INSERT_PROP_HANDLER_ENTRY(MAC_FIXED_RSS), #endif }; @@ -400,9 +401,10 @@ const NcpBase::RemovePropertyHandlerEntry NcpBase::mRemovePropertyHandlerTable[] NCP_REMOVE_PROP_HANDLER_ENTRY(THREAD_ON_MESH_NETS), #endif NCP_REMOVE_PROP_HANDLER_ENTRY(THREAD_ASSISTING_PORTS), -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER NCP_REMOVE_PROP_HANDLER_ENTRY(MAC_WHITELIST), NCP_REMOVE_PROP_HANDLER_ENTRY(MAC_BLACKLIST), + NCP_REMOVE_PROP_HANDLER_ENTRY(MAC_FIXED_RSS), #endif #if OPENTHREAD_FTD NCP_REMOVE_PROP_HANDLER_ENTRY(THREAD_ACTIVE_ROUTER_IDS), @@ -2225,7 +2227,7 @@ otError NcpBase::GetPropertyHandler_CAPS(uint8_t aHeader, spinel_prop_key_t aKey SuccessOrExit(error = OutboundFrameFeedPacked(SPINEL_DATATYPE_UINT_PACKED_S, SPINEL_CAP_NET_THREAD_1_0)); SuccessOrExit(error = OutboundFrameFeedPacked(SPINEL_DATATYPE_UINT_PACKED_S, SPINEL_CAP_COUNTERS)); -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER SuccessOrExit(error = OutboundFrameFeedPacked(SPINEL_DATATYPE_UINT_PACKED_S, SPINEL_CAP_MAC_WHITELIST)); #endif @@ -3966,11 +3968,12 @@ otError NcpBase::GetPropertyHandler_DEBUG_NCP_LOG_LEVEL(uint8_t aHeader, spinel_ ); } -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER otError NcpBase::GetPropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_prop_key_t aKey) { - otMacWhitelistEntry entry; + otMacFilterEntry entry; + otMacFilterIterator iterator = OT_MAC_FILTER_ITERATOR_INIT; otError error = OT_ERROR_NONE; mDisableStreamWrite = true; @@ -3984,32 +3987,17 @@ otError NcpBase::GetPropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_prop_k aKey )); - for (uint8_t i = 0; (i != 255) && (error == OT_ERROR_NONE); i++) + while (otLinkFilterGetNextAddress(mInstance, &iterator, &entry) == OT_ERROR_NONE) { - error = otLinkGetWhitelistEntry(mInstance, i, &entry); - - if (error != OT_ERROR_NONE) - { - break; - } - - if (entry.mValid) - { - if (!entry.mFixedRssi) - { - entry.mRssi = RSSI_OVERRIDE_DISABLED; - } - - SuccessOrExit( + SuccessOrExit( error = OutboundFrameFeedPacked( - SPINEL_DATATYPE_STRUCT_S( - SPINEL_DATATYPE_EUI64_S // Extended address - SPINEL_DATATYPE_INT8_S // Rssi - ), - entry.mExtAddress.m8, - entry.mRssi - )); - } + SPINEL_DATATYPE_STRUCT_S( + SPINEL_DATATYPE_EUI64_S // Extended address + SPINEL_DATATYPE_INT8_S // Rss + ), + entry.mExtAddress.m8, + entry.mRssIn + )); } SuccessOrExit(error = OutboundFrameSend()); @@ -4021,18 +4009,22 @@ exit: otError NcpBase::GetPropertyHandler_MAC_WHITELIST_ENABLED(uint8_t aHeader, spinel_prop_key_t aKey) { + otMacFilterAddressMode mode = otLinkFilterGetAddressMode(mInstance); + bool isEnabled = (mode == OT_MAC_FILTER_ADDRESS_MODE_WHITELIST) ? true : false; + return SendPropertyUpdate( aHeader, SPINEL_CMD_PROP_VALUE_IS, aKey, SPINEL_DATATYPE_BOOL_S, - otLinkIsWhitelistEnabled(mInstance) + isEnabled ); } otError NcpBase::GetPropertyHandler_MAC_BLACKLIST(uint8_t aHeader, spinel_prop_key_t aKey) { - otMacBlacklistEntry entry; + otMacFilterEntry entry; + otMacFilterIterator iterator = OT_MAC_FILTER_ITERATOR_INIT; otError error = OT_ERROR_NONE; mDisableStreamWrite = true; @@ -4046,25 +4038,15 @@ otError NcpBase::GetPropertyHandler_MAC_BLACKLIST(uint8_t aHeader, spinel_prop_k aKey )); - for (uint8_t i = 0; (i != 255) && (error == OT_ERROR_NONE); i++) + while (otLinkFilterGetNextAddress(mInstance, &iterator, &entry) == OT_ERROR_NONE) { - error = otLinkGetBlacklistEntry(mInstance, i, &entry); - - if (error != OT_ERROR_NONE) - { - break; - } - - if (entry.mValid) - { - SuccessOrExit( + SuccessOrExit( error = OutboundFrameFeedPacked( - SPINEL_DATATYPE_STRUCT_S( - SPINEL_DATATYPE_EUI64_S // Extended address - ), - entry.mExtAddress.m8 - )); - } + SPINEL_DATATYPE_STRUCT_S( + SPINEL_DATATYPE_EUI64_S // Extended address + ), + entry.mExtAddress.m8 + )); } SuccessOrExit(error = OutboundFrameSend()); @@ -4076,17 +4058,56 @@ exit: otError NcpBase::GetPropertyHandler_MAC_BLACKLIST_ENABLED(uint8_t aHeader, spinel_prop_key_t aKey) { + otMacFilterAddressMode mode = otLinkFilterGetAddressMode(mInstance); + bool isEnabled = (mode == OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST) ? true : false; + return SendPropertyUpdate( aHeader, SPINEL_CMD_PROP_VALUE_IS, aKey, SPINEL_DATATYPE_BOOL_S, - otLinkIsBlacklistEnabled(mInstance) + isEnabled ); } +otError NcpBase::GetPropertyHandler_MAC_FIXED_RSS(uint8_t aHeader, spinel_prop_key_t aKey) +{ + otMacFilterEntry entry; + otMacFilterIterator iterator = OT_MAC_FILTER_ITERATOR_INIT; + otError error = OT_ERROR_NONE; -#endif // OPENTHREAD_ENABLE_MAC_WHITELIST + mDisableStreamWrite = true; + + SuccessOrExit(error = OutboundFrameBegin(aHeader)); + SuccessOrExit( + error = OutboundFrameFeedPacked( + SPINEL_DATATYPE_COMMAND_PROP_S, + aHeader, + SPINEL_CMD_PROP_VALUE_IS, + aKey + )); + + while (otLinkFilterGetNextRssIn(mInstance, &iterator, &entry) == OT_ERROR_NONE) + { + SuccessOrExit( + error = OutboundFrameFeedPacked( + SPINEL_DATATYPE_STRUCT_S( + SPINEL_DATATYPE_EUI64_S // Extended address + SPINEL_DATATYPE_INT8_S // Rss + ), + entry.mExtAddress.m8, + entry.mRssIn + )); + } + + SuccessOrExit(error = OutboundFrameSend()); + +exit: + mDisableStreamWrite = false; + return error; +} + +#endif // OPENTHREAD_ENABLE_MAC_FILTER #if OPENTHREAD_FTD otError NcpBase::GetPropertyHandler_NET_PSKC(uint8_t aHeader, spinel_prop_key_t aKey) @@ -5700,7 +5721,7 @@ exit: #endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER otError NcpBase::SetPropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_prop_key_t aKey, const uint8_t *aValuePtr, uint16_t aValueLen) @@ -5709,13 +5730,13 @@ otError NcpBase::SetPropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_prop_k spinel_ssize_t parsedLength; otError error = OT_ERROR_NONE; - // First, clear the whitelist. - otLinkClearWhitelist(mInstance); + // First, clear the address filter entries. + otLinkFilterClearAddresses(mInstance); while (aValueLen > 0) { otExtAddress *extAddress = NULL; - int8_t rssi = RSSI_OVERRIDE_DISABLED; + int8_t rss; parsedLength = spinel_datatype_unpack( aValuePtr, @@ -5725,12 +5746,12 @@ otError NcpBase::SetPropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_prop_k SPINEL_DATATYPE_INT8_S ), &extAddress, - &rssi + &rss ); if (parsedLength <= 0) { - rssi = RSSI_OVERRIDE_DISABLED; + rss = OT_MAC_FILTER_FIXED_RSS_DISABLED; parsedLength = spinel_datatype_unpack( aValuePtr, aValueLen, @@ -5743,13 +5764,13 @@ otError NcpBase::SetPropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_prop_k VerifyOrExit(parsedLength > 0, error = OT_ERROR_PARSE); - if (rssi == RSSI_OVERRIDE_DISABLED) + error = otLinkFilterAddAddress(mInstance, extAddress); + + VerifyOrExit(error == OT_ERROR_NONE || error == OT_ERROR_ALREADY); + + if (rss != OT_MAC_FILTER_FIXED_RSS_DISABLED) { - SuccessOrExit(error = otLinkAddWhitelist(mInstance, extAddress->m8)); - } - else - { - SuccessOrExit(error = otLinkAddWhitelistRssi(mInstance, extAddress->m8, rssi)); + SuccessOrExit(error = otLinkFilterAddRssIn(mInstance, extAddress, rss)); } aValuePtr += parsedLength; @@ -5761,7 +5782,7 @@ exit: // the state of the whitelist---so we need to report // those incomplete changes via an asynchronous // change event. - reportAsync = (error != OT_ERROR_NONE); + reportAsync = (error != OT_ERROR_NONE && error != OT_ERROR_ALREADY); error = SendSetPropertyResponse(aHeader, aKey, error); @@ -5779,6 +5800,7 @@ otError NcpBase::SetPropertyHandler_MAC_WHITELIST_ENABLED(uint8_t aHeader, spine bool enabled; spinel_ssize_t parsedLength; otError error = OT_ERROR_NONE; + otMacFilterAddressMode mode = OT_MAC_FILTER_ADDRESS_MODE_DISABLED; parsedLength = spinel_datatype_unpack( aValuePtr, @@ -5789,7 +5811,12 @@ otError NcpBase::SetPropertyHandler_MAC_WHITELIST_ENABLED(uint8_t aHeader, spine VerifyOrExit(parsedLength > 0, error = OT_ERROR_PARSE); - otLinkSetWhitelistEnabled(mInstance, enabled); + if (enabled) + { + mode = OT_MAC_FILTER_ADDRESS_MODE_WHITELIST; + } + + error = otLinkFilterSetAddressMode(mInstance, mode); exit: return SendSetPropertyResponse(aHeader, aKey, error); @@ -5802,12 +5829,12 @@ otError NcpBase::SetPropertyHandler_MAC_BLACKLIST(uint8_t aHeader, spinel_prop_k spinel_ssize_t parsedLength; otError error = OT_ERROR_NONE; - // First, clear the blacklist. - otLinkClearBlacklist(mInstance); + // First, clear the address filter entries. + otLinkFilterClearAddresses(mInstance); while (aValueLen > 0) { - otExtAddress *ext_addr = NULL; + otExtAddress *extAddress = NULL; parsedLength = spinel_datatype_unpack( aValuePtr, @@ -5815,12 +5842,14 @@ otError NcpBase::SetPropertyHandler_MAC_BLACKLIST(uint8_t aHeader, spinel_prop_k SPINEL_DATATYPE_STRUCT_S( SPINEL_DATATYPE_EUI64_S ), - &ext_addr + &extAddress ); VerifyOrExit(parsedLength > 0, error = OT_ERROR_PARSE); - SuccessOrExit(error = otLinkAddBlacklist(mInstance, ext_addr->m8)); + error = otLinkFilterRemoveAddress(mInstance, extAddress); + + VerifyOrExit(error == OT_ERROR_NONE || error == OT_ERROR_ALREADY); aValuePtr += parsedLength; aValueLen -= parsedLength; @@ -5831,7 +5860,7 @@ exit: // the state of the blacklist---so we need to report // those incomplete changes via an asynchronous // change event. - reportAsync = (error != OT_ERROR_NONE); + reportAsync = (error != OT_ERROR_NONE && error != OT_ERROR_ALREADY); error = SendSetPropertyResponse(aHeader, aKey, error); @@ -5849,6 +5878,7 @@ otError NcpBase::SetPropertyHandler_MAC_BLACKLIST_ENABLED(uint8_t aHeader, spine bool enabled; spinel_ssize_t parsedLength; otError error = OT_ERROR_NONE; + otMacFilterAddressMode mode = OT_MAC_FILTER_ADDRESS_MODE_DISABLED; parsedLength = spinel_datatype_unpack( aValuePtr, @@ -5859,13 +5889,82 @@ otError NcpBase::SetPropertyHandler_MAC_BLACKLIST_ENABLED(uint8_t aHeader, spine VerifyOrExit(parsedLength > 0, error = OT_ERROR_PARSE); - otLinkSetBlacklistEnabled(mInstance, enabled); + if (enabled) + { + mode = OT_MAC_FILTER_ADDRESS_MODE_BLACKLIST; + } + + error = otLinkFilterSetAddressMode(mInstance, mode); exit: return SendSetPropertyResponse(aHeader, aKey, error); } -#endif // OPENTHREAD_ENABLE_MAC_WHITELIST +otError NcpBase::SetPropertyHandler_MAC_FIXED_RSS(uint8_t aHeader, spinel_prop_key_t aKey, const uint8_t *aValuePtr, + uint16_t aValueLen) +{ + bool reportAsync; + spinel_ssize_t parsedLength; + otError error = OT_ERROR_NONE; + + // First, clear the address filter entries. + otLinkFilterClearRssIn(mInstance); + + while (aValueLen > 0) + { + otExtAddress *extAddress; + int8_t rss; + + parsedLength = spinel_datatype_unpack( + aValuePtr, + aValueLen, + SPINEL_DATATYPE_STRUCT_S( + SPINEL_DATATYPE_EUI64_S + SPINEL_DATATYPE_INT8_S + ), + &extAddress, + &rss + ); + + if (parsedLength < 0) + { + extAddress = NULL; + parsedLength = spinel_datatype_unpack( + aValuePtr, + aValueLen, + SPINEL_DATATYPE_STRUCT_S( + SPINEL_DATATYPE_INT8_S + ), + &rss + ); + } + + VerifyOrExit(parsedLength > 0, error = OT_ERROR_PARSE); + + SuccessOrExit(error = otLinkFilterAddRssIn(mInstance, extAddress, rss)); + + aValuePtr += parsedLength; + aValueLen -= parsedLength; + } + +exit: + // If we had an error, we may have actually changed + // the state of the RssIn filter---so we need to report + // those incomplete changes via an asynchronous + // change event. + reportAsync = (error != OT_ERROR_NONE); + + error = SendSetPropertyResponse(aHeader, aKey, error); + + if (reportAsync) + { + HandleCommandPropertyGet(SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0, aKey); + } + + return error; +} + +#endif // OPENTHREAD_ENABLE_MAC_FILTER #if OPENTHREAD_ENABLE_RAW_LINK_API @@ -6569,8 +6668,7 @@ otError NcpBase::InsertPropertyHandler_MAC_SRC_MATCH_SHORT_ADDRESSES(uint8_t aHe error = otLinkRawSrcMatchAddShortEntry(mInstance, short_address); - VerifyOrExit(error == OT_ERROR_NONE, - spinelError = ThreadErrorToSpinelStatus(error)); + VerifyOrExit(error == OT_ERROR_NONE, spinelError = ThreadErrorToSpinelStatus(error)); error = SendPropertyUpdate( aHeader, @@ -6609,8 +6707,7 @@ otError NcpBase::InsertPropertyHandler_MAC_SRC_MATCH_EXTENDED_ADDRESSES(uint8_t error = otLinkRawSrcMatchAddExtEntry(mInstance, extAddress); - VerifyOrExit(error == OT_ERROR_NONE, - spinelError = ThreadErrorToSpinelStatus(error)); + VerifyOrExit(error == OT_ERROR_NONE, spinelError = ThreadErrorToSpinelStatus(error)); error = SendPropertyUpdate( aHeader, @@ -6668,8 +6765,7 @@ otError NcpBase::InsertPropertyHandler_IPV6_ADDRESS_TABLE(uint8_t aHeader, spine error = otIp6AddUnicastAddress(mInstance, &netifAddr); - VerifyOrExit(error == OT_ERROR_NONE, - spinelError = ThreadErrorToSpinelStatus(error)); + VerifyOrExit(error == OT_ERROR_NONE, spinelError = ThreadErrorToSpinelStatus(error)); error = SendPropertyUpdate( aHeader, @@ -6852,7 +6948,7 @@ exit: return error; } -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER otError NcpBase::InsertPropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_prop_key_t aKey, const uint8_t *aValuePtr, uint16_t aValueLen) @@ -6861,7 +6957,7 @@ otError NcpBase::InsertPropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_pro otError error = OT_ERROR_NONE; spinel_status_t spinelError = SPINEL_STATUS_OK; otExtAddress *extAddress = NULL; - int8_t rssi = RSSI_OVERRIDE_DISABLED; + int8_t rss = OT_MAC_FILTER_FIXED_RSS_DISABLED; if (aValueLen > static_cast(sizeof(otExtAddress))) { @@ -6870,7 +6966,7 @@ otError NcpBase::InsertPropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_pro aValueLen, SPINEL_DATATYPE_EUI64_S SPINEL_DATATYPE_INT8_S, &extAddress, - &rssi + &rss ); } else @@ -6885,16 +6981,15 @@ otError NcpBase::InsertPropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_pro VerifyOrExit(parsedLength > 0, spinelError = SPINEL_STATUS_PARSE_ERROR); - if (rssi == RSSI_OVERRIDE_DISABLED) - { - error = otLinkAddWhitelist(mInstance, extAddress->m8); - } - else - { - error = otLinkAddWhitelistRssi(mInstance, extAddress->m8, rssi); - } + error = otLinkFilterAddAddress(mInstance, extAddress); - VerifyOrExit(error == OT_ERROR_NONE, spinelError = ThreadErrorToSpinelStatus(error)); + VerifyOrExit(error == OT_ERROR_NONE || error == OT_ERROR_ALREADY, spinelError = ThreadErrorToSpinelStatus(error)); + + if (rss != OT_MAC_FILTER_FIXED_RSS_DISABLED) + { + error = otLinkFilterAddRssIn(mInstance, extAddress, rss); + VerifyOrExit(error == OT_ERROR_NONE, spinelError = ThreadErrorToSpinelStatus(error)); + } error = SendPropertyUpdate( aHeader, @@ -6931,7 +7026,61 @@ otError NcpBase::InsertPropertyHandler_MAC_BLACKLIST(uint8_t aHeader, spinel_pro VerifyOrExit(parsedLength > 0, spinelError = SPINEL_STATUS_PARSE_ERROR); - error = otLinkAddBlacklist(mInstance, extAddress->m8); + error = otLinkFilterAddAddress(mInstance, extAddress); + + VerifyOrExit(error == OT_ERROR_NONE || error == OT_ERROR_ALREADY, spinelError = ThreadErrorToSpinelStatus(error)); + + error = SendPropertyUpdate( + aHeader, + SPINEL_CMD_PROP_VALUE_INSERTED, + aKey, + aValuePtr, + aValueLen + ); + +exit: + + if (spinelError != SPINEL_STATUS_OK) + { + error = SendLastStatus(aHeader, spinelError); + } + + return error; +} + +otError NcpBase::InsertPropertyHandler_MAC_FIXED_RSS(uint8_t aHeader, spinel_prop_key_t aKey, + const uint8_t *aValuePtr, uint16_t aValueLen) +{ + spinel_ssize_t parsedLength; + otError error = OT_ERROR_NONE; + spinel_status_t spinelError = SPINEL_STATUS_OK; + otExtAddress *extAddress = NULL; + int8_t rss = OT_MAC_FILTER_FIXED_RSS_DISABLED; + + if (aValueLen > sizeof(int8_t)) + { + parsedLength = spinel_datatype_unpack( + aValuePtr, + aValueLen, + SPINEL_DATATYPE_EUI64_S SPINEL_DATATYPE_INT8_S, + &extAddress, + &rss + ); + } + else + { + parsedLength = spinel_datatype_unpack( + aValuePtr, + aValueLen, + SPINEL_DATATYPE_INT8_S, + &rss + ); + } + + VerifyOrExit(parsedLength > 0, spinelError = SPINEL_STATUS_PARSE_ERROR); + + error = otLinkFilterAddRssIn(mInstance, extAddress, rss); + VerifyOrExit(error == OT_ERROR_NONE, spinelError = ThreadErrorToSpinelStatus(error)); error = SendPropertyUpdate( @@ -6952,7 +7101,7 @@ exit: return error; } -#endif // OPENTHREAD_ENABLE_MAC_WHITELIST +#endif // OPENTHREAD_ENABLE_MAC_FILTER #if OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD otError NcpBase::InsertPropertyHandler_THREAD_JOINERS(uint8_t aHeader, spinel_prop_key_t aKey, @@ -7318,7 +7467,7 @@ exit: } #endif // OPENTHREAD_FTD -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER otError NcpBase::RemovePropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_prop_key_t aKey, const uint8_t *aValuePtr, uint16_t aValueLen) @@ -7337,7 +7486,9 @@ otError NcpBase::RemovePropertyHandler_MAC_WHITELIST(uint8_t aHeader, spinel_pro VerifyOrExit(parsedLength > 0, spinelError = SPINEL_STATUS_PARSE_ERROR); - otLinkRemoveWhitelist(mInstance, extAddress->m8); + error = otLinkFilterRemoveAddress(mInstance, extAddress); + + VerifyOrExit(error == OT_ERROR_NONE, spinelError = ThreadErrorToSpinelStatus(error)); error = SendPropertyUpdate( aHeader, @@ -7374,7 +7525,9 @@ otError NcpBase::RemovePropertyHandler_MAC_BLACKLIST(uint8_t aHeader, spinel_pro VerifyOrExit(parsedLength > 0, spinelError = SPINEL_STATUS_PARSE_ERROR); - otLinkRemoveBlacklist(mInstance, extAddress->m8); + error = otLinkFilterRemoveAddress(mInstance, extAddress); + + VerifyOrExit(error == OT_ERROR_NONE, spinelError = ThreadErrorToSpinelStatus(error)); error = SendPropertyUpdate( aHeader, @@ -7394,7 +7547,49 @@ exit: return error; } -#endif // OPENTHREAD_ENABLE_MAC_WHITELIST +otError NcpBase::RemovePropertyHandler_MAC_FIXED_RSS(uint8_t aHeader, spinel_prop_key_t aKey, + const uint8_t *aValuePtr, uint16_t aValueLen) +{ + spinel_ssize_t parsedLength; + otError error = OT_ERROR_NONE; + spinel_status_t spinelError = SPINEL_STATUS_OK; + otExtAddress *extAddress = NULL; + + if (aValueLen > 0) + { + parsedLength = spinel_datatype_unpack( + aValuePtr, + aValueLen, + SPINEL_DATATYPE_EUI64_S, + &extAddress + ); + + VerifyOrExit(parsedLength > 0, spinelError = SPINEL_STATUS_PARSE_ERROR); + } + + error = otLinkFilterRemoveRssIn(mInstance, extAddress); + + VerifyOrExit(error == OT_ERROR_NONE, spinelError = ThreadErrorToSpinelStatus(error)); + + error = SendPropertyUpdate( + aHeader, + SPINEL_CMD_PROP_VALUE_REMOVED, + aKey, + aValuePtr, + aValueLen + ); + +exit: + + if (spinelError != SPINEL_STATUS_OK) + { + error = SendLastStatus(aHeader, spinelError); + } + + return error; +} + +#endif // OPENTHREAD_ENABLE_MAC_FILTER #if OPENTHREAD_ENABLE_LEGACY diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index 2c9e580b7..33e56a099 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -400,11 +400,12 @@ private: NCP_GET_PROP_HANDLER(NCP_CNTR); NCP_GET_PROP_HANDLER(IP_CNTR); NCP_GET_PROP_HANDLER(MSG_BUFFER_COUNTERS); -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER NCP_GET_PROP_HANDLER(MAC_WHITELIST); NCP_GET_PROP_HANDLER(MAC_WHITELIST_ENABLED); NCP_GET_PROP_HANDLER(MAC_BLACKLIST); NCP_GET_PROP_HANDLER(MAC_BLACKLIST_ENABLED); + NCP_GET_PROP_HANDLER(MAC_FIXED_RSS); #endif NCP_GET_PROP_HANDLER(THREAD_MODE); NCP_GET_PROP_HANDLER(THREAD_CHILD_TIMEOUT); @@ -479,11 +480,12 @@ private: #endif NCP_SET_PROP_HANDLER(MAC_PROMISCUOUS_MODE); NCP_SET_PROP_HANDLER(MAC_SCAN_PERIOD); -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER NCP_SET_PROP_HANDLER(MAC_WHITELIST); NCP_SET_PROP_HANDLER(MAC_WHITELIST_ENABLED); NCP_SET_PROP_HANDLER(MAC_BLACKLIST); NCP_SET_PROP_HANDLER(MAC_BLACKLIST_ENABLED); + NCP_SET_PROP_HANDLER(MAC_FIXED_RSS); #endif #if OPENTHREAD_ENABLE_RAW_LINK_API NCP_SET_PROP_HANDLER(MAC_SRC_MATCH_ENABLED); @@ -546,9 +548,10 @@ private: NCP_INSERT_PROP_HANDLER(THREAD_ON_MESH_NETS); #endif NCP_INSERT_PROP_HANDLER(THREAD_ASSISTING_PORTS); -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER NCP_INSERT_PROP_HANDLER(MAC_WHITELIST); NCP_INSERT_PROP_HANDLER(MAC_BLACKLIST); + NCP_INSERT_PROP_HANDLER(MAC_FIXED_RSS); #endif #if OPENTHREAD_ENABLE_COMMISSIONER NCP_INSERT_PROP_HANDLER(THREAD_JOINERS); @@ -566,9 +569,10 @@ private: NCP_REMOVE_PROP_HANDLER(THREAD_ON_MESH_NETS); #endif NCP_REMOVE_PROP_HANDLER(THREAD_ASSISTING_PORTS); -#if OPENTHREAD_ENABLE_MAC_WHITELIST +#if OPENTHREAD_ENABLE_MAC_FILTER NCP_REMOVE_PROP_HANDLER(MAC_WHITELIST); NCP_REMOVE_PROP_HANDLER(MAC_BLACKLIST); + NCP_REMOVE_PROP_HANDLER(MAC_FIXED_RSS); #endif #if OPENTHREAD_FTD NCP_REMOVE_PROP_HANDLER(THREAD_ACTIVE_ROUTER_IDS); diff --git a/src/ncp/spinel.c b/src/ncp/spinel.c index 2958f8ce9..2ed66ba43 100644 --- a/src/ncp/spinel.c +++ b/src/ncp/spinel.c @@ -1104,6 +1104,10 @@ spinel_prop_key_to_cstr(spinel_prop_key_t prop_key) ret = "PROP_MAC_BLACKLIST_ENABLED"; break; + case SPINEL_PROP_MAC_FIXED_RSS: + ret = "PROP_MAC_FIXED_RSS"; + break; + case SPINEL_PROP_NET_SAVED: ret = "PROP_NET_SAVED"; break; diff --git a/src/ncp/spinel.h b/src/ncp/spinel.h index 3e12d486f..fae5b0ff7 100644 --- a/src/ncp/spinel.h +++ b/src/ncp/spinel.h @@ -702,6 +702,17 @@ typedef enum /** Format: `b` */ SPINEL_PROP_MAC_BLACKLIST_ENABLED = SPINEL_PROP_MAC_EXT__BEGIN + 7, + + /// MAC Received Signal Strength Filter + /** Format: `A(t(Ec))` + * + * Structure Parameters: + * + * * `E`: Optional EUI64 address of node. Set default RSS if not included. + * * `c`: Fixed RSS. OT_MAC_FILTER_FIXED_RSS_OVERRIDE_DISABLED(127) means not set. + */ + SPINEL_PROP_MAC_FIXED_RSS = SPINEL_PROP_MAC_EXT__BEGIN + 8, + SPINEL_PROP_MAC_EXT__END = 0x1400, SPINEL_PROP_NET__BEGIN = 0x40, diff --git a/tests/scripts/thread-cert/Cert_5_2_04_REEDUpgrade.py b/tests/scripts/thread-cert/Cert_5_2_04_REEDUpgrade.py index 02fc987c9..65266962e 100755 --- a/tests/scripts/thread-cert/Cert_5_2_04_REEDUpgrade.py +++ b/tests/scripts/thread-cert/Cert_5_2_04_REEDUpgrade.py @@ -45,7 +45,6 @@ class Cert_5_2_4_REEDUpgrade(unittest.TestCase): self.nodes[LEADER].set_panid(0xface) self.nodes[LEADER].set_mode('rsdn') - self.nodes[LEADER].add_whitelist(self.nodes[ROUTER].get_addr64()) self.nodes[LEADER].enable_whitelist() for i in range(2,17): diff --git a/tests/scripts/thread-cert/Cert_6_5_02_ChildResetReattach.py b/tests/scripts/thread-cert/Cert_6_5_02_ChildResetReattach.py index 41cb82278..17d15dc14 100755 --- a/tests/scripts/thread-cert/Cert_6_5_02_ChildResetReattach.py +++ b/tests/scripts/thread-cert/Cert_6_5_02_ChildResetReattach.py @@ -78,7 +78,6 @@ class Cert_6_5_2_ChildResetReattach(unittest.TestCase): time.sleep(5) self.nodes[LEADER].add_whitelist(self.nodes[ED].get_addr64()) - self.nodes[ED].add_whitelist(self.nodes[LEADER].get_addr64()) time.sleep(5) self.assertEqual(self.nodes[ED].get_state(), 'child') diff --git a/tests/scripts/thread-cert/node_cli.py b/tests/scripts/thread-cert/node_cli.py index 39687c973..d4f8973d2 100644 --- a/tests/scripts/thread-cert/node_cli.py +++ b/tests/scripts/thread-cert/node_cli.py @@ -150,26 +150,31 @@ class otCli: self.pexpect.expect('Done') def clear_whitelist(self): - self.send_command('whitelist clear') + cmd = 'macfilter addr clear' + self.send_command(cmd) self.pexpect.expect('Done') def enable_whitelist(self): - self.send_command('whitelist enable') + cmd = 'macfilter addr whitelist' + self.send_command(cmd) self.pexpect.expect('Done') def disable_whitelist(self): - self.send_command('whitelist disable') + cmd = 'macfilter addr disable' + self.send_command(cmd) self.pexpect.expect('Done') def add_whitelist(self, addr, rssi=None): - cmd = 'whitelist add ' + addr + cmd = 'macfilter addr add ' + addr + if rssi != None: cmd += ' ' + str(rssi) + self.send_command(cmd) self.pexpect.expect('Done') def remove_whitelist(self, addr): - cmd = 'whitelist remove ' + addr + cmd = 'macfilter addr remove ' + addr self.send_command(cmd) self.pexpect.expect('Done') @@ -186,6 +191,7 @@ class otCli: i = self.pexpect.expect('([0-9a-fA-F]{16})') if i == 0: addr64 = self.pexpect.match.groups()[0].decode("utf-8") + self.pexpect.expect('Done') return addr64 diff --git a/tools/harness-thci/OpenThread.py b/tools/harness-thci/OpenThread.py index 1ef835548..ed5290e1d 100644 --- a/tools/harness-thci/OpenThread.py +++ b/tools/harness-thci/OpenThread.py @@ -355,73 +355,21 @@ class OpenThread(IThci): except Exception, e: ModuleHelper.WriteIntoDebugLogger("setRouterSelectionJitter() Error: " + str(e)) - def __enableWhiteList(self): - """enable white list filter + def __setAddressfilterMode(self, mode): + """set address filter mode Returns: - True: successful to enable white list filter - False: fail to enable white list filter + True: successful to set address filter mode. + False: fail to set address filter mode. """ - print 'call enableWhiteList' + print 'call setAddressFilterMode() ' + mode try: - if self.__sendCommand('whitelist enable')[0] == 'Done': - self.isWhiteListEnabled = True + cmd = 'macfilter addr ' + mode + if self.__sendCommand(cmd)[0] == 'Done': return True - else: - return False + return False except Exception, e: - ModuleHelper.WriteIntoDebugLogger("enableWhiteList() Error: " + str(e)) - - def __enableBlackList(self): - """enable black list filter - - Returns: - True: successful to enable black list filter - False: fail to enable black list filter - """ - print 'call enableBlackList' - try: - if self.__sendCommand('blacklist enable')[0] == 'Done': - self.isBlackListEnabled = True - return True - else: - return False - except Exception, e: - ModuleHelper.WriteIntoDebugLogger("enableBlackList() Error: " + str(e)) - - def __disableWhiteList(self): - """disable white list filter - - Returns: - True: successful to disable white list filter - False: fail to disable white list filter - """ - print 'call disableWhiteList' - try: - if self.__sendCommand('whitelist disable')[0] == 'Done': - self.isWhiteListEnabled = False - return True - else: - return False - except Exception, e: - ModuleHelper.WriteIntoDebugLogger("disableWhiteList() Error: " + str(e)) - - def __disableBlackList(self): - """disable black list filter - - Returns: - True: successful to disable black list filter - False: fail to disable black list filter - """ - print 'call disableBlackList' - try: - if self.__sendCommand('blacklist disable')[0] == 'Done': - self.isBlackListEnabled = False - return True - else: - return False - except Exception, e: - ModuleHelper.WriteIntoDebugLogger("disableBlackList() Error: " + str(e)) + ModuleHelper.WriteIntoDebugLogger("__setAddressFilterMode() Error: " + str(e)) def __startOpenThread(self): """start OpenThread stack @@ -438,17 +386,16 @@ class OpenThread(IThci): else: self.hasActiveDatasetToCommit = False - # restore whitelist if rejoin after reset - if self.isPowerDown and self.isWhiteListEnabled: - self.__enableWhiteList() - for addr in self._whiteList: - self.addAllowMAC(addr) - - # restore blacklist if rejoin after reset - if self.isPowerDown and self.isBlackListEnabled: - self.__enableBlackList() - for addr in self._blackList: - self.addBlockedMAC(addr) + # restore whitelist/blacklist address filter mode if rejoin after reset + if self.isPowerDown: + if self._addressfilterMode == 'whitelist': + if self.__setAddressfilterMode('whitelist'): + for addr in self._addressfilterSet: + self.addAllowMAC(addr) + elif self._addressfilterMode == 'blacklist': + if self.__setAddressfilterMode('blacklist'): + for addr in self._addressfilterSet: + self.addBlockedMAC(addr) if self.__sendCommand('ifconfig up')[0] == 'Done': self.__setRouterSelectionJitter(1) @@ -882,14 +829,14 @@ class OpenThread(IThci): return self.networkKey def addBlockedMAC(self, xEUI): - """add a given extended address to the black list entry + """add a given extended address to the blacklist entry Args: xEUI: extended address in hex format Returns: - True: successful to add a given extended address to the black list entry - False: fail to add a given extended address to the black list entry + True: successful to add a given extended address to the blacklist entry + False: fail to add a given extended address to the blacklist entry """ print '%s call addBlockedMAC' % self.port print xEUI @@ -904,25 +851,32 @@ class OpenThread(IThci): print 'block device itself' return True - if not self.isBlackListEnabled: - self.__enableBlackList() + if self._addressfilterMode != 'blacklist': + if self.__setAddressfilterMode('blacklist'): + self._addressfilterMode = 'blacklist' - cmd = 'blacklist add %s' % macAddr + cmd = 'macfilter addr add %s' % macAddr print cmd - self._blackList.add(macAddr) - return self.__sendCommand(cmd)[0] == 'Done' + ret = self.__sendCommand(cmd)[0] == 'Done' + + self._addressfilterSet.add(macAddr) + print 'current blacklist entries:' + for addr in self._addressfilterSet: + print addr + + return ret except Exception, e: ModuleHelper.WriteIntoDebugLogger("addBlockedMAC() Error: " + str(e)) def addAllowMAC(self, xEUI): - """add a given extended address to the white list entry + """add a given extended address to the whitelist addressfilter Args: xEUI: a given extended address in hex format Returns: - True: successful to add a given extended address to the white list entry - False: fail to add a given extended address to the white list entry + True: successful to add a given extended address to the whitelist entry + False: fail to add a given extended address to the whitelist entry """ print '%s call addAllowMAC' % self.port print xEUI @@ -932,54 +886,75 @@ class OpenThread(IThci): macAddr = self.__convertLongToString(xEUI) try: - if not self.isWhiteListEnabled: - self.__enableWhiteList() + if self._addressfilterMode != 'whitelist': + if self.__setAddressfilterMode('whitelist'): + self._addressfilterMode = 'whitelist' - cmd = 'whitelist add %s' % macAddr + cmd = 'macfilter addr add %s' % macAddr print cmd - self._whiteList.add(macAddr) - return self.__sendCommand(cmd)[0] == 'Done' + ret = self.__sendCommand(cmd)[0] == 'Done' + + self._addressfilterSet.add(macAddr) + print 'current whitelist entries:' + for addr in self._addressfilterSet: + print addr + return ret + except Exception, e: ModuleHelper.WriteIntoDebugLogger("addAllowMAC() Error: " + str(e)) def clearBlockList(self): - """clear all entries in black list table + """clear all entries in blacklist table Returns: - True: successful to clear the black list - False: fail to clear the black list + True: successful to clear the blacklist + False: fail to clear the blacklist """ print '%s call clearBlockList' % self.port - # remove all entries in black list + # remove all entries in blacklist try: - if self.__sendCommand('blacklist clear')[0] == 'Done': - self.__disableBlackList() - self._blackList.clear() - return True - else: - return False + print 'clearing blacklist entries:' + for addr in self._addressfilterSet: + print addr + + + # disable blacklist + if self.__setAddressfilterMode('disable'): + self._addressfilterMode = 'disable' + # clear ops + cmd = 'macfilter addr clear' + if self.__sendCommand(cmd)[0] == 'Done': + self._addressfilterSet.clear() + return True + return False except Exception, e: ModuleHelper.WriteIntoDebugLogger("clearBlockList() Error: " + str(e)) def clearAllowList(self): - """clear all entries in white list table + """clear all entries in whitelist table Returns: - True: successful to clear the white list - False: fail to clear the white list + True: successful to clear the whitelist + False: fail to clear the whitelist """ print '%s call clearAllowList' % self.port - # remove all entries in white list as well as in black list + # remove all entries in whitelist try: - if self.__sendCommand('whitelist clear')[0] == 'Done': - self.__disableWhiteList() - self._whiteList.clear() - self.clearBlockList() - return True - else: - return False + print 'clearing whitelist entries:' + for addr in self._addressfilterSet: + print addr + + # disable whitelist + if self.__setAddressfilterMode('disable'): + self._addressfilterMode = 'disable' + # clear ops + cmd = 'macfilter addr clear' + if self.__sendCommand(cmd)[0] == 'Done': + self._addressfilterSet.clear() + return True + return False except Exception, e: ModuleHelper.WriteIntoDebugLogger("clearAllowList() Error: " + str(e)) @@ -1273,10 +1248,8 @@ class OpenThread(IThci): self.joinCommissionedStatus = self.joinStatus['notstart'] self.networkDataRequirement = '' # indicate Thread device requests full or stable network data self.isPowerDown = False # indicate if Thread device experiences a power down event - self.isWhiteListEnabled = False # indicate if Thread device enables white list filter - self.isBlackListEnabled = False # indicate if Thread device enables black list filter - self._whiteList = set() # cache whitelist devices when white list filter is enabled - self._blackList = set() # cache blacklist devices when black list filter is enabled + self._addressfilterMode = 'disable' # indicate AddressFilter mode ['disable', 'whitelist', 'blacklist'] + self._addressfilterSet = set() # cache filter entries self.isActiveCommissioner = False # indicate if Thread device is an active commissioner self._lines = None # buffered lines read from device @@ -1326,11 +1299,11 @@ class OpenThread(IThci): ModuleHelper.WriteIntoDebugLogger("setPollingRate() Error: " + str(e)) def setLinkQuality(self, EUIadr, LinkQuality): - """set custom link quality on a link to Thread device with a given extended address + """set custom LinkQualityIn for all receiving messages from the specified EUIadr Args: EUIadr: a given extended address - LinkQuality: a given custom link quality for child devices + LinkQuality: a given custom link quality link quality/link margin mapping table 3: 21 - 255 (dB) 2: 11 - 20 (dB) @@ -1338,8 +1311,8 @@ class OpenThread(IThci): 0: 0 - 2 (dB) Returns: - True: successful to set the link quality on a link to a Thread device - False: fail to set the link quality on a link to a Thread device + True: successful to set the link quality + False: fail to set the link quality """ print '%s call setLinkQuality' % self.port print EUIadr @@ -1352,18 +1325,41 @@ class OpenThread(IThci): address64 = '' if '0x' in euiStr: address64 = euiStr.lstrip('0x') - # prepend 0 at the beginning if len(address64) < 16: - address64 = address64.zfill(16) - print address64 + address64 = address64.zfill(16) + print address64 - cmd = 'linkquality %s %s' % (address64, str(LinkQuality)) + cmd = 'macfilter rss add-lqi %s %s' % (address64, str(LinkQuality)) print cmd return self.__sendCommand(cmd)[0] == 'Done' except Exception, e: ModuleHelper.WriteIntoDebugLogger("setLinkQuality() Error: " + str(e)) + def setOutBoundLinkQuality(self, LinkQuality): + """set custom LinkQualityIn for all receiving messages from the any address + + Args: + LinkQuality: a given custom link quality + link quality/link margin mapping table + 3: 21 - 255 (dB) + 2: 11 - 20 (dB) + 1: 3 - 9 (dB) + 0: 0 - 2 (dB) + + Returns: + True: successful to set the link quality + False: fail to set the link quality + """ + print '%s call setOutBoundLinkQuality' % self.port + print LinkQuality + try: + cmd = 'macfilter rss add-lqi * %s' % str(LinkQuality) + print cmd + return self.__sendCommand(cmd)[0] == 'Done' + except Exception, e: + ModuleHelper.WriteIntoDebugLogger("setOutBoundLinkQuality() Error: " + str(e)) + def removeRouterPrefix(self, prefixEntry): """remove the configured prefix on a border router