Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meter Identification example added #37961

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

popovdg
Copy link

@popovdg popovdg commented Mar 11, 2025

Meter Identification cluster added to all-clusters-app example.

Copy link

PR #37961: Size comparison from 0bae36f to 4d37dea

Full report (1 build for stm32)
platform target config section 0bae36f 4d37dea change % change
stm32 light STM32WB5MM-DK FLASH 460920 460920 0 0.0
RAM 141496 141496 0 0.0

@popovdg popovdg force-pushed the meter_identification-example branch from 4d37dea to b80b9d0 Compare March 11, 2025 16:14

static void SetCharSpan(DataModel::Nullable<CharSpan> & charSpan, const DataModel::Nullable<CharSpan> && value)
{
if (!value.IsNull())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the set work even if null?

const size_t len = value.Value().size();
char * str = (char *) chip::Platform::MemoryAlloc(len + 1);
strncpy(str, value.Value().data(), len);
str[len] = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? CharSpans are not null-terminated.

if (!value.IsNull())
{
const size_t len = value.Value().size();
char * str = (char *) chip::Platform::MemoryAlloc(len + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to handle allocation failure?

char * str = (char *) chip::Platform::MemoryAlloc(len + 1);
strncpy(str, value.Value().data(), len);
str[len] = 0;
charSpan = DataModel::MakeNullable(CharSpan::fromCharString(str));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
charSpan = DataModel::MakeNullable(CharSpan::fromCharString(str));
charSpan = DataModel::MakeNullable(CharSpan(str, len));

if (!value.IsNull())
{
const size_t len = value.Value().size();
char * str = (char *) chip::Platform::MemoryAlloc(len + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
char * str = (char *) chip::Platform::MemoryAlloc(len + 1);
auto * str = static_cast<char *>(chip::Platform::MemoryAlloc(len));

{
if (!mPointOfDelivery.IsNull())
{
chip::Platform::MemoryFree((void *) mPointOfDelivery.Value().data());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid hidden const_cast, please.

return CHIP_NO_ERROR;
}

if(static_cast<std::underlying_type<MeterTypeEnum>::type>(MeterTypeEnum::kUnknownEnumValue) <=
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't assume kUnknownEnumValue is larger than all valid enum values. The only thing you can do with kUnknownEnumValue is compare it for equality.

See also EnsureKnownEnumValue, which is the thing you may want here.

return CHIP_NO_ERROR;
}

if ((newValue.Value().powerThreshold.HasValue() && INT64_MIN == newValue.Value().powerThreshold.Value()) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is INT64_MIN coming from here? If this is meant exclude out-of-range values for a nullable, you should be using our existing mechanisms for checking for that. ExistingValueInEncodableRange on Nullable might do the right thing for you here?

if ((newValue.Value().powerThreshold.HasValue() && INT64_MIN == newValue.Value().powerThreshold.Value()) ||
(newValue.Value().apparentPowerThreshold.HasValue() && INT64_MIN == newValue.Value().apparentPowerThreshold.Value()) ||
(!newValue.Value().powerThresholdSource.IsNull() && static_cast<std::underlying_type<PowerThresholdSourceEnum>::type>(
PowerThresholdSourceEnum::kUnknownEnumValue) <= static_cast<std::underlying_type<PowerThresholdSourceEnum>::type>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above about kUnknownEnumValue


if (!dst.IsNull())
{
chip::Platform::MemoryFree((void *) dst.Value().data());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strongly recommend not doing heap allocation here, but if we do see comments above about how it should probably work.

@popovdg popovdg force-pushed the meter_identification-example branch from b80b9d0 to 4def522 Compare March 11, 2025 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants