Skip to content

Commit

Permalink
Merge branch 'master' into chef-laundry-washer
Browse files Browse the repository at this point in the history
  • Loading branch information
stingchang authored Oct 25, 2024
2 parents b23c83b + 41b6830 commit 91cb2a7
Show file tree
Hide file tree
Showing 13 changed files with 863 additions and 626 deletions.
16 changes: 14 additions & 2 deletions examples/chip-tool/commands/clusters/ComplexArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ class ComplexArgumentParser
// - 11 is the maximum length of a %d (-2147483648, 2147483647)
// - 2 is the length for the "[" and "]" characters.
snprintf(labelWithIndex, sizeof(labelWithIndex), "%.241s[%d]", label, i);
ReturnErrorOnFailure(ComplexArgumentParser::Setup(labelWithIndex, content[i], value[i]));
auto error = ComplexArgumentParser::Setup(labelWithIndex, content[i], value[i]);
if (CHIP_NO_ERROR != error)
{
chip::Platform::MemoryFree(content);
return error;
}
}

request = chip::app::DataModel::List<T>(content, value.size());
Expand Down Expand Up @@ -415,7 +420,14 @@ class TypedComplexArgument : public ComplexArgument
return ComplexArgumentParser::Setup(label, *mRequest, value);
}

void Reset() { *mRequest = T(); }
void Reset()
{
if (mRequest != nullptr)
{
ComplexArgumentParser::Finalize(*mRequest);
*mRequest = T();
}
}

private:
T * mRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ void ElectricalSensorManager::UpdateEPMAttributes(OperationalStateEnum state)
{
if (gEPMDelegate)
{
uint16_t updateState = to_underlying(state);
uint16_t ERROR_STATE_INDEX = ArraySize(kAttributes) - 1;
uint8_t updateState = to_underlying(state);

// Check state range
if ((updateState < 0) || (updateState > ERROR_STATE_INDEX))
if (updateState >= ArraySize(kAttributes))
{
updateState = ERROR_STATE_INDEX;
updateState = ArraySize(kAttributes) - 1;
}

ChipLogDetail(AppServer, "UpdateAllAttributes to Operational State : %d", updateState);
Expand Down
3 changes: 2 additions & 1 deletion examples/thermostat/silabs/include/SensorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class SensorManager

osTimerId_t mSensorTimer;

// Reads new generated sensor value, stores it, and updates local temperature attribute
static void SensorTimerEventHandler(void * arg);
// Reads new generated sensor value, stores it, and updates local temperature attribute
static void TemperatureUpdateEventHandler(AppEvent * aEvent);

static SensorManager sSensorManager;
};
Expand Down
9 changes: 9 additions & 0 deletions examples/thermostat/silabs/src/SensorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ CHIP_ERROR SensorManager::Init()
}

void SensorManager::SensorTimerEventHandler(void * arg)
{
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
event.Handler = TemperatureUpdateEventHandler;

AppTask::GetAppTask().PostEvent(&event);
}

void SensorManager::TemperatureUpdateEventHandler(AppEvent * aEvent)
{
int16_t temperature = 0;
static int16_t lastTemperature = 0;
Expand Down
2 changes: 1 addition & 1 deletion integrations/docker/images/base/chip-build/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
86 : [Tizen] Pass runner's path as QEMU argument
87 : [Silabs] Delete SDK files to reduce the amount of flash consummed
13 changes: 10 additions & 3 deletions integrations/docker/images/stage-2/chip-build-efr32/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,28 @@ RUN wget https://github.com/SiliconLabs/simplicity_sdk/releases/download/v2024.6
&& unzip /tmp/simplicity_sdk.zip -d /tmp/simplicity_sdk \
&& rm -rf /tmp/simplicity_sdk.zip \
# Deleting files that are not needed to save space
&& rm -rf /tmp/simplicity_sdk/protocol/flex /tmp/simplicity_sdk/protocol/z-wave /tmp/simplicity_sdk/protocol/zigbee /tmp/simplicity_sdk/protocol/wisun \
&& rm -rf /tmp/simplicity_sdk/protocol/flex /tmp/simplicity_sdk/protocol/z-wave /tmp/simplicity_sdk/protocol/zigbee /tmp/simplicity_sdk/protocol/wisun /tmp/simplicity_sdk/util/third_party/tensorflow_extra \
/tmp/simplicity_sdk/util/third_party/sqlite /tmp/simplicity_sdk/util/third_party/ot-br-posix /tmp/simplicity_sdk/util/third_party/tflite-micro /tmp/simplicity_sdk/util/third_party/tflite-fatfs /tmp/simplicity_sdk/util/third_party/unity_test_framework \
/tmp/simplicity_sdk/platform/radio/efr32_multiphy_configurator \
&& find /tmp/simplicity_sdk/protocol/bluetooth /tmp/simplicity_sdk/platform -name "*.a" -type f -delete \
&& find /tmp/simplicity_sdk/protocol/openthread -name "*efr32mg21*" -delete \
&& find /tmp/simplicity_sdk \( -name "libsl_platform_ftd_efr32mg2*" -o -name "libsl_ot_stack_mtd_efr32mg2*" \) -type f -delete \
&& find /tmp/simplicity_sdk/hardware/board/config -mindepth 1 -maxdepth 1 -type d ! \( -name '*brd4186c*' -o -name '*brd4187c*' -o -name '*brd4186a*' -o -name '*brd4187a*' -o -name '*brd2601b*' -o -name '*brd2703a*' -o -name '*brd2704a*' \
-o -name '*brd4316a*' -o -name '*brd4317a*' -o -name '*brd4318a*' -o -name '*brd4319a*' -o -name '*brd4116a*' -o -name '*brd4117a*' -o -name '*brd4118a*' -o -name '*brd2608a*' \) -exec rm -rf {} + \
&& find /tmp/simplicity_sdk/platform/Device/SiliconLabs -mindepth 1 -maxdepth 1 -type d ! \( -name 'EFR32MG24' -o -name 'EFR32MG26' -o -name 'MGM24' \) -exec rm -rf {} + \
&& find /tmp/simplicity_sdk -name "*.slc*" -type f -delete \
&& : # last line

# Clone WiSeConnect Wi-Fi and Bluetooth Software 2.10.3 (b6d6cb5)
RUN git clone --depth=1 --single-branch --branch=2.10.3 https://github.com/SiliconLabs/wiseconnect-wifi-bt-sdk.git /tmp/wiseconnect-wifi-bt-sdk && \
cd /tmp/wiseconnect-wifi-bt-sdk && \
rm -rf .git \
rm -rf .git examples \
&& : # last line

# Clone WiSeConnect SDK v3.3.3 (a6390dd)
RUN git clone --depth=1 --single-branch --branch=v3.3.3 https://github.com/SiliconLabs/wiseconnect.git /tmp/wifi_sdk && \
cd /tmp/wifi_sdk && \
rm -rf .git \
rm -rf .git examples components/device/stm32 \
&& : # last line

# SLC-cli install
Expand Down
175 changes: 7 additions & 168 deletions src/app/codegen-data-model-provider/CodegenDataModelProvider_Read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <app/AttributeValueEncoder.h>
#include <app/GlobalAttributes.h>
#include <app/RequiredPrivilege.h>
#include <app/codegen-data-model-provider/EmberAttributeDataBuffer.h>
#include <app/codegen-data-model-provider/EmberMetadata.h>
#include <app/data-model/FabricScoped.h>
#include <app/util/af-types.h>
Expand All @@ -41,6 +42,7 @@
#include <app/util/odd-sized-integers.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/Span.h>

#include <zap-generated/endpoint_config.h>

Expand Down Expand Up @@ -85,173 +87,6 @@ std::optional<CHIP_ERROR> TryReadViaAccessInterface(const ConcreteAttributePath
return encoder.TriedEncode() ? std::make_optional(CHIP_NO_ERROR) : std::nullopt;
}

/// Metadata of what a ember/pascal short string means (prepended by a u8 length)
struct ShortPascalString
{
using LengthType = uint8_t;
static constexpr LengthType kNullLength = 0xFF;

static size_t GetLength(ByteSpan buffer)
{
VerifyOrDie(buffer.size() >= 1);
// NOTE: we do NOT use emberAfStringLength from ember-strings.h because that will result in 0
// length for null sizes (i.e. 0xFF is translated to 0 and we do not want that here)
return buffer[0];
}
};

/// Metadata of what a ember/pascal LONG string means (prepended by a u16 length)
struct LongPascalString
{
using LengthType = uint16_t;
static constexpr LengthType kNullLength = 0xFFFF;

static size_t GetLength(ByteSpan buffer)
{
// NOTE: we do NOT use emberAfLongStringLength from ember-strings.h because that will result in 0
// length for null sizes (i.e. 0xFFFF is translated to 0 and we do not want that here)
VerifyOrDie(buffer.size() >= 2);
const uint8_t * data = buffer.data();
return Encoding::LittleEndian::Read16(data);
}
};

// ember assumptions ... should just work
static_assert(sizeof(ShortPascalString::LengthType) == 1);
static_assert(sizeof(LongPascalString::LengthType) == 2);

/// Given a ByteSpan containing data from ember, interpret it
/// as a span of type OUT (i.e. ByteSpan or CharSpan) given a ENCODING
/// where ENCODING is Short or Long pascal strings.
template <class OUT_TYPE, class ENCODING>
std::optional<OUT_TYPE> ExtractEmberString(ByteSpan data)
{
constexpr size_t kLengthTypeSize = sizeof(typename ENCODING::LengthType);
VerifyOrDie(kLengthTypeSize <= data.size());
auto len = ENCODING::GetLength(data);

if (len == ENCODING::kNullLength)
{
return std::nullopt;
}

VerifyOrDie(len + sizeof(len) <= data.size());
return std::make_optional<OUT_TYPE>(reinterpret_cast<typename OUT_TYPE::pointer>(data.data() + kLengthTypeSize), len);
}

/// Encode a value inside `encoder`
///
/// The value encoded will be of type T (e.g. CharSpan or ByteSpan) and it will be decoded
/// via the given ENCODING (i.e. ShortPascalString or LongPascalString)
///
/// isNullable defines if the value of NULL is allowed to be encoded.
template <typename T, class ENCODING>
CHIP_ERROR EncodeStringLike(ByteSpan data, bool isNullable, AttributeValueEncoder & encoder)
{
std::optional<T> value = ExtractEmberString<T, ENCODING>(data);
if (!value.has_value())
{
if (isNullable)
{
return encoder.EncodeNull();
}
return CHIP_ERROR_INCORRECT_STATE;
}

// encode value as-is
return encoder.Encode(*value);
}

/// Encodes a numeric data value of type T from the given ember-encoded buffer `data`.
///
/// isNullable defines if the value of NULL is allowed to be encoded.
template <typename T>
CHIP_ERROR EncodeFromSpan(ByteSpan data, bool isNullable, AttributeValueEncoder & encoder)
{
typename NumericAttributeTraits<T>::StorageType value;

VerifyOrReturnError(data.size() >= sizeof(value), CHIP_ERROR_INVALID_ARGUMENT);
memcpy(&value, data.data(), sizeof(value));

if (isNullable && NumericAttributeTraits<T>::IsNullValue(value))
{
return encoder.EncodeNull();
}

if (!NumericAttributeTraits<T>::CanRepresentValue(isNullable, value))
{
return CHIP_ERROR_INCORRECT_STATE;
}

return encoder.Encode(NumericAttributeTraits<T>::StorageToWorking(value));
}

/// Converts raw ember data from `data` into the encoder
///
/// Uses the attribute `metadata` to determine how the data is encoded into `data` and
/// write a suitable value into `encoder`.
CHIP_ERROR EncodeEmberValue(ByteSpan data, const EmberAfAttributeMetadata * metadata, AttributeValueEncoder & encoder)
{
VerifyOrReturnError(metadata != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

const bool isNullable = metadata->IsNullable();

switch (AttributeBaseType(metadata->attributeType))
{
case ZCL_NO_DATA_ATTRIBUTE_TYPE: // No data
return encoder.EncodeNull();
case ZCL_BOOLEAN_ATTRIBUTE_TYPE: // Boolean
return EncodeFromSpan<bool>(data, isNullable, encoder);
case ZCL_INT8U_ATTRIBUTE_TYPE: // Unsigned 8-bit integer
return EncodeFromSpan<uint8_t>(data, isNullable, encoder);
case ZCL_INT16U_ATTRIBUTE_TYPE: // Unsigned 16-bit integer
return EncodeFromSpan<uint16_t>(data, isNullable, encoder);
case ZCL_INT24U_ATTRIBUTE_TYPE: // Unsigned 24-bit integer
return EncodeFromSpan<OddSizedInteger<3, false>>(data, isNullable, encoder);
case ZCL_INT32U_ATTRIBUTE_TYPE: // Unsigned 32-bit integer
return EncodeFromSpan<uint32_t>(data, isNullable, encoder);
case ZCL_INT40U_ATTRIBUTE_TYPE: // Unsigned 40-bit integer
return EncodeFromSpan<OddSizedInteger<5, false>>(data, isNullable, encoder);
case ZCL_INT48U_ATTRIBUTE_TYPE: // Unsigned 48-bit integer
return EncodeFromSpan<OddSizedInteger<6, false>>(data, isNullable, encoder);
case ZCL_INT56U_ATTRIBUTE_TYPE: // Unsigned 56-bit integer
return EncodeFromSpan<OddSizedInteger<7, false>>(data, isNullable, encoder);
case ZCL_INT64U_ATTRIBUTE_TYPE: // Unsigned 64-bit integer
return EncodeFromSpan<uint64_t>(data, isNullable, encoder);
case ZCL_INT8S_ATTRIBUTE_TYPE: // Signed 8-bit integer
return EncodeFromSpan<int8_t>(data, isNullable, encoder);
case ZCL_INT16S_ATTRIBUTE_TYPE: // Signed 16-bit integer
return EncodeFromSpan<int16_t>(data, isNullable, encoder);
case ZCL_INT24S_ATTRIBUTE_TYPE: // Signed 24-bit integer
return EncodeFromSpan<OddSizedInteger<3, true>>(data, isNullable, encoder);
case ZCL_INT32S_ATTRIBUTE_TYPE: // Signed 32-bit integer
return EncodeFromSpan<int32_t>(data, isNullable, encoder);
case ZCL_INT40S_ATTRIBUTE_TYPE: // Signed 40-bit integer
return EncodeFromSpan<OddSizedInteger<5, true>>(data, isNullable, encoder);
case ZCL_INT48S_ATTRIBUTE_TYPE: // Signed 48-bit integer
return EncodeFromSpan<OddSizedInteger<6, true>>(data, isNullable, encoder);
case ZCL_INT56S_ATTRIBUTE_TYPE: // Signed 56-bit integer
return EncodeFromSpan<OddSizedInteger<7, true>>(data, isNullable, encoder);
case ZCL_INT64S_ATTRIBUTE_TYPE: // Signed 64-bit integer
return EncodeFromSpan<int64_t>(data, isNullable, encoder);
case ZCL_SINGLE_ATTRIBUTE_TYPE: // 32-bit float
return EncodeFromSpan<float>(data, isNullable, encoder);
case ZCL_DOUBLE_ATTRIBUTE_TYPE: // 64-bit float
return EncodeFromSpan<double>(data, isNullable, encoder);
case ZCL_CHAR_STRING_ATTRIBUTE_TYPE: // Char string
return EncodeStringLike<CharSpan, ShortPascalString>(data, isNullable, encoder);
case ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE:
return EncodeStringLike<CharSpan, LongPascalString>(data, isNullable, encoder);
case ZCL_OCTET_STRING_ATTRIBUTE_TYPE: // Octet string
return EncodeStringLike<ByteSpan, ShortPascalString>(data, isNullable, encoder);
case ZCL_LONG_OCTET_STRING_ATTRIBUTE_TYPE:
return EncodeStringLike<ByteSpan, LongPascalString>(data, isNullable, encoder);
default:
ChipLogError(DataManagement, "Attribute type 0x%x not handled", static_cast<int>(metadata->attributeType));
return CHIP_IM_GLOBAL_STATUS(Failure);
}
}

} // namespace

/// separated-out ReadAttribute implementation (given existing complexity)
Expand Down Expand Up @@ -343,7 +178,11 @@ DataModel::ActionReturnStatus CodegenDataModelProvider::ReadAttribute(const Data
return CHIP_ERROR_IM_GLOBAL_STATUS_VALUE(status);
}

return EncodeEmberValue(gEmberAttributeIOBufferSpan, attributeMetadata, encoder);
VerifyOrReturnError(attributeMetadata != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

MutableByteSpan data = gEmberAttributeIOBufferSpan;
Ember::EmberAttributeDataBuffer emberData(attributeMetadata, data);
return encoder.Encode(emberData);
}

} // namespace app
Expand Down
Loading

0 comments on commit 91cb2a7

Please sign in to comment.