diff --git a/src/app/util/af.h b/src/app/util/af.h index 78f7adab591bda..f747d50523a5d6 100644 --- a/src/app/util/af.h +++ b/src/app/util/af.h @@ -136,12 +136,6 @@ chip::EndpointId emberAfParentEndpointFromIndex(uint16_t index); */ uint16_t emberAfIndexFromEndpoint(chip::EndpointId endpoint); -/** - * Returns the index of a given endpoint; Does not ignore disabled endpoints. - * Will return 0xFFFF if this is not a valid endpoint id. - */ -uint16_t emberAfIndexFromEndpointIncludingDisabledEndpoints(chip::EndpointId endpoint); - /** * @brief Returns the index of the given endpoint in the list of all endpoints that might support the given cluster server. * @@ -189,35 +183,8 @@ uint16_t emberAfGetClusterServerEndpointIndex(chip::EndpointId endpoint, chip::C */ uint16_t emberAfFixedEndpointCount(void); -/** - *@brief Returns true if type is signed, false otherwise. - */ -bool emberAfIsTypeSigned(EmberAfAttributeType dataType); - /** @} END Attribute Storage */ -/** @name Miscellaneous */ -// @{ - -/** @brief Returns true if a given ZCL data type is a list type. */ -bool emberAfIsThisDataTypeAListType(EmberAfAttributeType dataType); - -/** - * @brief Simple integer comparison function. - * Compares two values of a known length as integers. - * Signed integer comparison are supported for numbers with length of - * 4 (bytes) or less. - * The integers are in native endianness. - * - * @return -1, if val1 is smaller - * 0, if they are the same or if two negative numbers with length - * greater than 4 is being compared - * 1, if val2 is smaller. - */ -int8_t emberAfCompareValues(const uint8_t * val1, const uint8_t * val2, uint16_t len, bool signedNumber); - -/** @} END Miscellaneous */ - /** @} END addtogroup */ /** diff --git a/src/app/util/attribute-storage.cpp b/src/app/util/attribute-storage.cpp index 62e4631297e7d0..6aa8e334a9380f 100644 --- a/src/app/util/attribute-storage.cpp +++ b/src/app/util/attribute-storage.cpp @@ -175,6 +175,36 @@ void UnregisterMatchingAttributeAccessInterfaces(F shouldUnregister) } } +bool emberAfIsThisDataTypeAListType(EmberAfAttributeType dataType) +{ + return dataType == ZCL_ARRAY_ATTRIBUTE_TYPE; +} + +uint16_t findIndexFromEndpoint(EndpointId endpoint, bool ignoreDisabledEndpoints) +{ + if (endpoint == kInvalidEndpointId) + { + return kEmberInvalidEndpointIndex; + } + + uint16_t epi; + for (epi = 0; epi < emberAfEndpointCount(); epi++) + { + if (emAfEndpoints[epi].endpoint == endpoint && + (!ignoreDisabledEndpoints || emAfEndpoints[epi].bitmask.Has(EmberAfEndpointOptions::isEnabled))) + { + return epi; + } + } + return kEmberInvalidEndpointIndex; +} + +// Returns the index of a given endpoint. Considers disabled endpoints. +uint16_t emberAfIndexFromEndpointIncludingDisabledEndpoints(EndpointId endpoint) +{ + return findIndexFromEndpoint(endpoint, false /* ignoreDisabledEndpoints */); +} + } // anonymous namespace // Initial configuration @@ -354,11 +384,6 @@ bool emberAfEndpointIndexIsEnabled(uint16_t index) return (emAfEndpoints[index].bitmask.Has(EmberAfEndpointOptions::isEnabled)); } -bool emberAfIsThisDataTypeAListType(EmberAfAttributeType dataType) -{ - return dataType == ZCL_ARRAY_ATTRIBUTE_TYPE; -} - // This function is used to call the per-cluster attribute changed callback void emAfClusterAttributeChangedCallback(const app::ConcreteAttributePath & attributePath) { @@ -845,25 +870,6 @@ const EmberAfCluster * emberAfFindClusterIncludingDisabledEndpoints(EndpointId e return nullptr; } -static uint16_t findIndexFromEndpoint(EndpointId endpoint, bool ignoreDisabledEndpoints) -{ - if (endpoint == kInvalidEndpointId) - { - return kEmberInvalidEndpointIndex; - } - - uint16_t epi; - for (epi = 0; epi < emberAfEndpointCount(); epi++) - { - if (emAfEndpoints[epi].endpoint == endpoint && - (!ignoreDisabledEndpoints || emAfEndpoints[epi].bitmask.Has(EmberAfEndpointOptions::isEnabled))) - { - return epi; - } - } - return kEmberInvalidEndpointIndex; -} - uint16_t emberAfGetClusterServerEndpointIndex(EndpointId endpoint, ClusterId cluster, uint16_t fixedClusterServerEndpointCount) { VerifyOrDie(fixedClusterServerEndpointCount <= FIXED_ENDPOINT_COUNT); @@ -980,12 +986,6 @@ uint16_t emberAfIndexFromEndpoint(EndpointId endpoint) return findIndexFromEndpoint(endpoint, true /* ignoreDisabledEndpoints */); } -// Returns the index of a given endpoint. Considers disabled endpoints. -uint16_t emberAfIndexFromEndpointIncludingDisabledEndpoints(EndpointId endpoint) -{ - return findIndexFromEndpoint(endpoint, false /* ignoreDisabledEndpoints */); -} - EndpointId emberAfEndpointFromIndex(uint16_t index) { return emAfEndpoints[index].endpoint; diff --git a/src/app/util/attribute-table.cpp b/src/app/util/attribute-table.cpp index de68b7e6a77f98..0b0f2143ecbc1c 100644 --- a/src/app/util/attribute-table.cpp +++ b/src/app/util/attribute-table.cpp @@ -25,10 +25,114 @@ #include #include +#if (CHIP_CONFIG_BIG_ENDIAN_TARGET) +#define EM_BIG_ENDIAN true +#else +#define EM_BIG_ENDIAN false +#endif + using chip::Protocols::InteractionModel::Status; using namespace chip; +namespace { + +// Zigbee spec says types between signed 8 bit and signed 64 bit +bool emberAfIsTypeSigned(EmberAfAttributeType dataType) +{ + return (dataType >= ZCL_INT8S_ATTRIBUTE_TYPE && dataType <= ZCL_INT64S_ATTRIBUTE_TYPE); +} + +/** + * @brief Simple integer comparison function. + * Compares two values of a known length as integers. + * Signed integer comparison are supported for numbers with length of + * 4 (bytes) or less. + * The integers are in native endianness. + * + * @return -1, if val1 is smaller + * 0, if they are the same or if two negative numbers with length + * greater than 4 is being compared + * 1, if val2 is smaller. + * + * You can pass in val1 as NULL, which will assume that it is + * pointing to an array of all zeroes. This is used so that + * default value of NULL is treated as all zeroes. + */ +int8_t emberAfCompareValues(const uint8_t * val1, const uint8_t * val2, uint16_t len, bool signedNumber) +{ + if (len == 0) + { + // no length means nothing to compare. Shouldn't even happen, since len is sizeof(some-integer-type). + return 0; + } + + if (signedNumber) + { // signed number comparison + if (len <= 4) + { // only number with 32-bits or less is supported + int32_t accum1 = 0x0; + int32_t accum2 = 0x0; + int32_t all1s = -1; + + for (uint16_t i = 0; i < len; i++) + { + uint8_t j = (val1 == nullptr ? 0 : (EM_BIG_ENDIAN ? val1[i] : val1[(len - 1) - i])); + accum1 |= j << (8 * (len - 1 - i)); + + uint8_t k = (EM_BIG_ENDIAN ? val2[i] : val2[(len - 1) - i]); + accum2 |= k << (8 * (len - 1 - i)); + } + + // sign extending, no need for 32-bits numbers + if (len < 4) + { + if ((accum1 & (1 << (8 * len - 1))) != 0) + { // check sign + accum1 |= all1s - ((1 << (len * 8)) - 1); + } + if ((accum2 & (1 << (8 * len - 1))) != 0) + { // check sign + accum2 |= all1s - ((1 << (len * 8)) - 1); + } + } + + if (accum1 > accum2) + { + return 1; + } + if (accum1 < accum2) + { + return -1; + } + + return 0; + } + + // not supported + return 0; + } + + // regular unsigned number comparison + for (uint16_t i = 0; i < len; i++) + { + uint8_t j = (val1 == nullptr ? 0 : (EM_BIG_ENDIAN ? val1[i] : val1[(len - 1) - i])); + uint8_t k = (EM_BIG_ENDIAN ? val2[i] : val2[(len - 1) - i]); + + if (j > k) + { + return 1; + } + if (k > j) + { + return -1; + } + } + return 0; +} + +} // namespace + Status emAfWriteAttributeExternal(EndpointId endpoint, ClusterId cluster, AttributeId attributeID, uint8_t * dataPtr, EmberAfAttributeType dataType) { diff --git a/src/app/util/util.cpp b/src/app/util/util.cpp index 10084e6c015376..c1b55ab836f223 100644 --- a/src/app/util/util.cpp +++ b/src/app/util/util.cpp @@ -140,93 +140,6 @@ void MatterPowerTopologyPluginServerInitCallback() {} void MatterElectricalEnergyMeasurementPluginServerInitCallback() {} void MatterElectricalPowerMeasurementPluginServerInitCallback() {} -#if (CHIP_CONFIG_BIG_ENDIAN_TARGET) -#define EM_BIG_ENDIAN true -#else -#define EM_BIG_ENDIAN false -#endif - -// You can pass in val1 as NULL, which will assume that it is -// pointing to an array of all zeroes. This is used so that -// default value of NULL is treated as all zeroes. -int8_t emberAfCompareValues(const uint8_t * val1, const uint8_t * val2, uint16_t len, bool signedNumber) -{ - if (len == 0) - { - // no length means nothing to compare. Shouldn't even happen, since len is sizeof(some-integer-type). - return 0; - } - - if (signedNumber) - { // signed number comparison - if (len <= 4) - { // only number with 32-bits or less is supported - int32_t accum1 = 0x0; - int32_t accum2 = 0x0; - int32_t all1s = -1; - - for (uint16_t i = 0; i < len; i++) - { - uint8_t j = (val1 == nullptr ? 0 : (EM_BIG_ENDIAN ? val1[i] : val1[(len - 1) - i])); - accum1 |= j << (8 * (len - 1 - i)); - - uint8_t k = (EM_BIG_ENDIAN ? val2[i] : val2[(len - 1) - i]); - accum2 |= k << (8 * (len - 1 - i)); - } - - // sign extending, no need for 32-bits numbers - if (len < 4) - { - if ((accum1 & (1 << (8 * len - 1))) != 0) - { // check sign - accum1 |= all1s - ((1 << (len * 8)) - 1); - } - if ((accum2 & (1 << (8 * len - 1))) != 0) - { // check sign - accum2 |= all1s - ((1 << (len * 8)) - 1); - } - } - - if (accum1 > accum2) - { - return 1; - } - if (accum1 < accum2) - { - return -1; - } - - return 0; - } - - // not supported - return 0; - } - - // regular unsigned number comparison - for (uint16_t i = 0; i < len; i++) - { - uint8_t j = (val1 == nullptr ? 0 : (EM_BIG_ENDIAN ? val1[i] : val1[(len - 1) - i])); - uint8_t k = (EM_BIG_ENDIAN ? val2[i] : val2[(len - 1) - i]); - - if (j > k) - { - return 1; - } - if (k > j) - { - return -1; - } - } - return 0; -} - -// Zigbee spec says types between signed 8 bit and signed 64 bit -bool emberAfIsTypeSigned(EmberAfAttributeType dataType) -{ - return (dataType >= ZCL_INT8S_ATTRIBUTE_TYPE && dataType <= ZCL_INT64S_ATTRIBUTE_TYPE); -} - bool emberAfContainsAttribute(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId) { return (emberAfGetServerAttributeIndexByAttributeId(endpoint, clusterId, attributeId) != UINT16_MAX);