Skip to content

Commit

Permalink
Fix location access on Darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarmelveilleux committed Jan 26, 2022
1 parent aca6232 commit f814fbd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/app/clusters/basic/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,12 @@ CHIP_ERROR BasicAttrAccess::ReadLocation(AttributeValueEncoder & aEncoder)
if ((err != CHIP_NO_ERROR) || (codeLen == 0))
{
strncpy(&location[0], "XX", kMaxLen + 1);
err = CHIP_NO_ERROR;
codeLen = strnlen(location, kMaxLen);
err = CHIP_NO_ERROR;
}

return EncodeStringOnSuccess(err, aEncoder, location, kMaxLen);
ReturnErrorOnFailure(err);
return encoder.Encode(chip::CharSpan(buf, codeLen));
}

CHIP_ERROR BasicAttrAccess::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder)
Expand Down
22 changes: 20 additions & 2 deletions src/platform/Darwin/PosixConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const PosixConfig::Key PosixConfig::kConfigKey_Breadcrumb = { kConfigNam
const char PosixConfig::kGroupKeyNamePrefix[] = "gk-";

uint16_t PosixConfig::mPosixSetupDiscriminator = 0xF00; // CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR
char PosixConfig::mPosixCountryCode[3] = "XX";

CHIP_ERROR PosixConfig::Init()
{
Expand Down Expand Up @@ -111,9 +112,17 @@ CHIP_ERROR PosixConfig::ReadConfigValue(Key key, uint64_t & val)
CHIP_ERROR PosixConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen)
{
CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
SuccessOrExit(err);

exit:
if (key == kConfigKey_CountryCode)
{
// Do no assume null-termination on read-out
constexpr size_t kMaxLen = sizeof(PosixConfig::mPosixCountryCode) - 1;
outLen = strnlen(PosixConfig::mPosixCountryCode, kMaxLen);
VerifyOrReturnError(bufSize >= kMaxLen, CHIP_ERROR_BUFFER_TOO_SMALL);
memcpy(&buf[0], &PosixConfig::mPosixCountryCode[0], outLen);
return CHIP_NO_ERROR;
}

return err;
}

Expand Down Expand Up @@ -167,6 +176,15 @@ CHIP_ERROR PosixConfig::WriteConfigValueStr(Key key, const char * str)

CHIP_ERROR PosixConfig::WriteConfigValueStr(Key key, const char * str, size_t strLen)
{
if (key == kConfigKey_CountryCode)
{
VerifyOrReturnError(strLen < sizeof(PosixConfig::mPosixCountryCode), CHIP_ERROR_INVALID_ARGUMENT);
memcpy(&PosixConfig::mPosixCountryCode[0], str, strLen);
// Internally null-terminate so we may be able to log later. Don't assume we got null-termination on input.
PosixConfig::mPosixCountryCode[strLen] = '\0';
return CHIP_NO_ERROR;
}

CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
SuccessOrExit(err);

Expand Down
1 change: 1 addition & 0 deletions src/platform/Darwin/PosixConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class PosixConfig
private:
// TODO: This is temporary until Darwin implements a proper ReadConfigValue
static uint16_t mPosixSetupDiscriminator;
static char mPosixCountryCode[2 + 1];
};

struct PosixConfig::Key
Expand Down

0 comments on commit f814fbd

Please sign in to comment.