From 96e1ffa307b369ff9259cd094626cc7377319a1d Mon Sep 17 00:00:00 2001 From: Zhanglong Xia Date: Wed, 9 Oct 2019 23:44:19 +0800 Subject: [PATCH] [posix] use common source match table implementation (#4160) --- examples/platforms/posix/radio.c | 142 +----------------- .../platforms/utils/soft_source_match_table.c | 52 +++---- .../platforms/utils/soft_source_match_table.h | 8 +- 3 files changed, 36 insertions(+), 166 deletions(-) diff --git a/examples/platforms/posix/radio.c b/examples/platforms/posix/radio.c index fc2eee3d2..ca815d519 100644 --- a/examples/platforms/posix/radio.c +++ b/examples/platforms/posix/radio.c @@ -39,6 +39,7 @@ #include #include "utils/code_utils.h" +#include "utils/soft_source_match_table.h" // The IPv4 group for receiving packets of radio simulation #define OT_RADIO_GROUP "224.0.0.116" @@ -87,8 +88,7 @@ enum enum { - POSIX_RECEIVE_SENSITIVITY = -100, // dBm - POSIX_MAX_SRC_MATCH_ENTRIES = OPENTHREAD_CONFIG_MLE_MAX_CHILDREN, + POSIX_RECEIVE_SENSITIVITY = -100, // dBm POSIX_HIGH_RSSI_SAMPLE = -30, // dBm POSIX_LOW_RSSI_SAMPLE = -98, // dBm @@ -141,46 +141,12 @@ static bool sPromiscuous = false; static bool sTxWait = false; static int8_t sTxPower = 0; -static uint16_t sShortAddressMatchTableCount = 0; -static uint16_t sExtAddressMatchTableCount = 0; -static uint16_t sShortAddressMatchTable[POSIX_MAX_SRC_MATCH_ENTRIES]; -static otExtAddress sExtAddressMatchTable[POSIX_MAX_SRC_MATCH_ENTRIES]; -static bool sSrcMatchEnabled = false; +static bool sSrcMatchEnabled = false; #if OPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE static bool sRadioCoexEnabled = true; #endif -static bool findShortAddress(uint16_t aShortAddress) -{ - uint8_t i; - - for (i = 0; i < sShortAddressMatchTableCount; ++i) - { - if (sShortAddressMatchTable[i] == aShortAddress) - { - break; - } - } - - return i < sShortAddressMatchTableCount; -} - -static bool findExtAddress(const otExtAddress *aExtAddress) -{ - uint8_t i; - - for (i = 0; i < sExtAddressMatchTableCount; ++i) - { - if (!memcmp(&sExtAddressMatchTable[i], aExtAddress, sizeof(otExtAddress))) - { - break; - } - } - - return i < sExtAddressMatchTableCount; -} - static inline bool isFrameTypeAck(const uint8_t *aFrame) { return (aFrame[0] & IEEE802154_FRAME_TYPE_MASK) == IEEE802154_FRAME_TYPE_ACK; @@ -244,7 +210,7 @@ static inline bool isDataRequestAndHasFramePending(const uint8_t *aFrame) if (sSrcMatchEnabled) { - hasFramePending = findShortAddress((uint16_t)(cur[1] << 8 | cur[0])); + hasFramePending = utilsSoftSrcMatchShortFindEntry((uint16_t)(cur[1] << 8 | cur[0])) >= 0; } cur += sizeof(otShortAddress); @@ -258,7 +224,7 @@ static inline bool isDataRequestAndHasFramePending(const uint8_t *aFrame) if (sSrcMatchEnabled) { - hasFramePending = findExtAddress((const otExtAddress *)cur); + hasFramePending = utilsSoftSrcMatchExtFindEntry((const otExtAddress *)cur) >= 0; } cur += sizeof(otExtAddress); @@ -377,6 +343,7 @@ void otPlatRadioSetPanId(otInstance *aInstance, uint16_t aPanid) assert(aInstance != NULL); sPanid = aPanid; + utilsSoftSrcMatchSetPanId(aPanid); } void otPlatRadioSetExtendedAddress(otInstance *aInstance, const otExtAddress *aExtAddress) @@ -966,103 +933,6 @@ void otPlatRadioEnableSrcMatch(otInstance *aInstance, bool aEnable) sSrcMatchEnabled = aEnable; } -otError otPlatRadioAddSrcMatchShortEntry(otInstance *aInstance, uint16_t aShortAddress) -{ - assert(aInstance != NULL); - - otError error = OT_ERROR_NONE; - otEXPECT_ACTION(sShortAddressMatchTableCount < sizeof(sShortAddressMatchTable) / sizeof(uint16_t), - error = OT_ERROR_NO_BUFS); - - for (uint8_t i = 0; i < sShortAddressMatchTableCount; ++i) - { - otEXPECT_ACTION(sShortAddressMatchTable[i] != aShortAddress, error = OT_ERROR_DUPLICATED); - } - - sShortAddressMatchTable[sShortAddressMatchTableCount++] = aShortAddress; - -exit: - return error; -} - -otError otPlatRadioAddSrcMatchExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress) -{ - assert(aInstance != NULL); - - otError error = OT_ERROR_NONE; - - otEXPECT_ACTION(sExtAddressMatchTableCount < sizeof(sExtAddressMatchTable) / sizeof(otExtAddress), - error = OT_ERROR_NO_BUFS); - - for (uint8_t i = 0; i < sExtAddressMatchTableCount; ++i) - { - otEXPECT_ACTION(memcmp(&sExtAddressMatchTable[i], aExtAddress, sizeof(otExtAddress)), - error = OT_ERROR_DUPLICATED); - } - - sExtAddressMatchTable[sExtAddressMatchTableCount++] = *aExtAddress; - -exit: - return error; -} - -otError otPlatRadioClearSrcMatchShortEntry(otInstance *aInstance, uint16_t aShortAddress) -{ - assert(aInstance != NULL); - - otError error = OT_ERROR_NOT_FOUND; - otEXPECT(sShortAddressMatchTableCount > 0); - - for (uint8_t i = 0; i < sShortAddressMatchTableCount; ++i) - { - if (sShortAddressMatchTable[i] == aShortAddress) - { - sShortAddressMatchTable[i] = sShortAddressMatchTable[--sShortAddressMatchTableCount]; - error = OT_ERROR_NONE; - goto exit; - } - } - -exit: - return error; -} - -otError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress) -{ - assert(aInstance != NULL); - - otError error = OT_ERROR_NOT_FOUND; - - otEXPECT(sExtAddressMatchTableCount > 0); - - for (uint8_t i = 0; i < sExtAddressMatchTableCount; ++i) - { - if (!memcmp(&sExtAddressMatchTable[i], aExtAddress, sizeof(otExtAddress))) - { - sExtAddressMatchTable[i] = sExtAddressMatchTable[--sExtAddressMatchTableCount]; - error = OT_ERROR_NONE; - goto exit; - } - } - -exit: - return error; -} - -void otPlatRadioClearSrcMatchShortEntries(otInstance *aInstance) -{ - assert(aInstance != NULL); - - sShortAddressMatchTableCount = 0; -} - -void otPlatRadioClearSrcMatchExtEntries(otInstance *aInstance) -{ - assert(aInstance != NULL); - - sExtAddressMatchTableCount = 0; -} - otError otPlatRadioEnergyScan(otInstance *aInstance, uint8_t aScanChannel, uint16_t aScanDuration) { assert(aInstance != NULL); diff --git a/examples/platforms/utils/soft_source_match_table.c b/examples/platforms/utils/soft_source_match_table.c index af53a34fb..65fa372a9 100644 --- a/examples/platforms/utils/soft_source_match_table.c +++ b/examples/platforms/utils/soft_source_match_table.c @@ -59,12 +59,12 @@ typedef struct srcMatchShortEntry static sSrcMatchShortEntry srcMatchShortEntry[RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM]; -int8_t utilsSoftSrcMatchShortFindEntry(const uint16_t aShortAddress) +int16_t utilsSoftSrcMatchShortFindEntry(uint16_t aShortAddress) { - int8_t entry = -1; + int16_t entry = -1; uint16_t checksum = aShortAddress + sPanId; - for (uint8_t i = 0; i < RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM; i++) + for (int16_t i = 0; i < RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM; i++) { if (checksum == srcMatchShortEntry[i].checksum && srcMatchShortEntry[i].allocated) { @@ -76,11 +76,11 @@ int8_t utilsSoftSrcMatchShortFindEntry(const uint16_t aShortAddress) return entry; } -static inline int8_t findSrcMatchShortAvailEntry(void) +static int16_t findSrcMatchShortAvailEntry(void) { - int8_t entry = -1; + int16_t entry = -1; - for (uint8_t i = 0; i < RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM; i++) + for (int16_t i = 0; i < RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM; i++) { if (!srcMatchShortEntry[i].allocated) { @@ -92,7 +92,7 @@ static inline int8_t findSrcMatchShortAvailEntry(void) return entry; } -static inline void addToSrcMatchShortIndirect(uint8_t entry, const uint16_t aShortAddress) +static inline void addToSrcMatchShortIndirect(uint16_t entry, uint16_t aShortAddress) { uint16_t checksum = aShortAddress + sPanId; @@ -100,43 +100,43 @@ static inline void addToSrcMatchShortIndirect(uint8_t entry, const uint16_t aSho srcMatchShortEntry[entry].allocated = true; } -static inline void removeFromSrcMatchShortIndirect(uint8_t entry) +static inline void removeFromSrcMatchShortIndirect(uint16_t entry) { srcMatchShortEntry[entry].allocated = false; srcMatchShortEntry[entry].checksum = 0; } -otError otPlatRadioAddSrcMatchShortEntry(otInstance *aInstance, const uint16_t aShortAddress) +otError otPlatRadioAddSrcMatchShortEntry(otInstance *aInstance, uint16_t aShortAddress) { OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NONE; - int8_t entry = -1; + int16_t entry = -1; entry = findSrcMatchShortAvailEntry(); otLogDebgPlat("Add ShortAddr entry: %d", entry); otEXPECT_ACTION(entry >= 0 && entry < RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM, error = OT_ERROR_NO_BUFS); - addToSrcMatchShortIndirect(entry, aShortAddress); + addToSrcMatchShortIndirect((uint16_t)entry, aShortAddress); exit: return error; } -otError otPlatRadioClearSrcMatchShortEntry(otInstance *aInstance, const uint16_t aShortAddress) +otError otPlatRadioClearSrcMatchShortEntry(otInstance *aInstance, uint16_t aShortAddress) { OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NONE; - int8_t entry = -1; + int16_t entry = -1; entry = utilsSoftSrcMatchShortFindEntry(aShortAddress); otLogDebgPlat("Clear ShortAddr entry: %d", entry); otEXPECT_ACTION(entry >= 0 && entry < RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM, error = OT_ERROR_NO_ADDRESS); - removeFromSrcMatchShortIndirect(entry); + removeFromSrcMatchShortIndirect((uint16_t)entry); exit: return error; @@ -161,9 +161,9 @@ typedef struct srcMatchExtEntry static sSrcMatchExtEntry srcMatchExtEntry[RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM]; -int8_t utilsSoftSrcMatchExtFindEntry(const otExtAddress *aExtAddress) +int16_t utilsSoftSrcMatchExtFindEntry(const otExtAddress *aExtAddress) { - int8_t entry = -1; + int16_t entry = -1; uint16_t checksum = sPanId; checksum += (uint16_t)aExtAddress->m8[0] | (uint16_t)(aExtAddress->m8[1] << 8); @@ -171,7 +171,7 @@ int8_t utilsSoftSrcMatchExtFindEntry(const otExtAddress *aExtAddress) checksum += (uint16_t)aExtAddress->m8[4] | (uint16_t)(aExtAddress->m8[5] << 8); checksum += (uint16_t)aExtAddress->m8[6] | (uint16_t)(aExtAddress->m8[7] << 8); - for (uint8_t i = 0; i < RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM; i++) + for (int16_t i = 0; i < RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM; i++) { if (checksum == srcMatchExtEntry[i].checksum && srcMatchExtEntry[i].allocated) { @@ -183,11 +183,11 @@ int8_t utilsSoftSrcMatchExtFindEntry(const otExtAddress *aExtAddress) return entry; } -static inline int8_t findSrcMatchExtAvailEntry(void) +static int16_t findSrcMatchExtAvailEntry(void) { - int8_t entry = -1; + int16_t entry = -1; - for (uint8_t i = 0; i < RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM; i++) + for (int16_t i = 0; i < RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM; i++) { if (!srcMatchExtEntry[i].allocated) { @@ -199,7 +199,7 @@ static inline int8_t findSrcMatchExtAvailEntry(void) return entry; } -static inline void addToSrcMatchExtIndirect(uint8_t entry, const otExtAddress *aExtAddress) +static inline void addToSrcMatchExtIndirect(uint16_t entry, const otExtAddress *aExtAddress) { uint16_t checksum = sPanId; @@ -212,7 +212,7 @@ static inline void addToSrcMatchExtIndirect(uint8_t entry, const otExtAddress *a srcMatchExtEntry[entry].allocated = true; } -static inline void removeFromSrcMatchExtIndirect(uint8_t entry) +static inline void removeFromSrcMatchExtIndirect(uint16_t entry) { srcMatchExtEntry[entry].allocated = false; srcMatchExtEntry[entry].checksum = 0; @@ -223,14 +223,14 @@ otError otPlatRadioAddSrcMatchExtEntry(otInstance *aInstance, const otExtAddress OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NONE; - int8_t entry = -1; + int16_t entry = -1; entry = findSrcMatchExtAvailEntry(); otLogDebgPlat("Add ExtAddr entry: %d", entry); otEXPECT_ACTION(entry >= 0 && entry < RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM, error = OT_ERROR_NO_BUFS); - addToSrcMatchExtIndirect(entry, aExtAddress); + addToSrcMatchExtIndirect((uint16_t)entry, aExtAddress); exit: return error; @@ -241,14 +241,14 @@ otError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const otExtAddre OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NONE; - int8_t entry = -1; + int16_t entry = -1; entry = utilsSoftSrcMatchExtFindEntry(aExtAddress); otLogDebgPlat("Clear ExtAddr entry: %d", entry); otEXPECT_ACTION(entry >= 0 && entry < RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM, error = OT_ERROR_NO_ADDRESS); - removeFromSrcMatchExtIndirect(entry); + removeFromSrcMatchExtIndirect((uint16_t)entry); exit: return error; diff --git a/examples/platforms/utils/soft_source_match_table.h b/examples/platforms/utils/soft_source_match_table.h index 4fcdedd15..f34427a80 100644 --- a/examples/platforms/utils/soft_source_match_table.h +++ b/examples/platforms/utils/soft_source_match_table.h @@ -46,11 +46,11 @@ extern "C" { #endif #ifndef RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM -#define RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM 0 +#define RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM OPENTHREAD_CONFIG_MLE_MAX_CHILDREN #endif #ifndef RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM -#define RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM 0 +#define RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM OPENTHREAD_CONFIG_MLE_MAX_CHILDREN #endif #if RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM || RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM @@ -58,11 +58,11 @@ void utilsSoftSrcMatchSetPanId(uint16_t aPanId); #endif // RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM || RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM #if RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM -int8_t utilsSoftSrcMatchShortFindEntry(const uint16_t aShortAddress); +int16_t utilsSoftSrcMatchShortFindEntry(uint16_t aShortAddress); #endif // RADIO_CONFIG_SRC_MATCH_SHORT_ENTRY_NUM #if RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM -int8_t utilsSoftSrcMatchExtFindEntry(const otExtAddress *aExtAddress); +int16_t utilsSoftSrcMatchExtFindEntry(const otExtAddress *aExtAddress); #endif // RADIO_CONFIG_SRC_MATCH_EXT_ENTRY_NUM #ifdef __cplusplus