Skip to content

Commit

Permalink
chip-tool: allow any fabricId, up to a total of 17 (#17052)
Browse files Browse the repository at this point in the history
* chip-tool: allow any fabricId, up to a total of 17

* chip-tool: fix compiler warning on Linux
  • Loading branch information
bluebin14 authored and pull[bot] committed Aug 24, 2023
1 parent a8fc1f8 commit 33ccd68
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
46 changes: 37 additions & 9 deletions examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ constexpr chip::FabricId kIdentityNullFabricId = chip::kUndefinedFabricId;
constexpr chip::FabricId kIdentityAlphaFabricId = 1;
constexpr chip::FabricId kIdentityBetaFabricId = 2;
constexpr chip::FabricId kIdentityGammaFabricId = 3;
constexpr chip::FabricId kIdentityOtherFabricId = 4;

namespace {
const chip::Credentials::AttestationTrustStore * GetTestFileAttestationTrustStore(const char * paaTrustStorePath)
Expand Down Expand Up @@ -113,6 +114,13 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack()
ReturnLogErrorOnFailure(InitializeCommissioner(kIdentityBeta, kIdentityBetaFabricId, trustStore));
ReturnLogErrorOnFailure(InitializeCommissioner(kIdentityGamma, kIdentityGammaFabricId, trustStore));

std::string name = GetIdentity();
chip::FabricId fabricId = strtoull(name.c_str(), nullptr, 0);
if (fabricId >= kIdentityOtherFabricId)
{
ReturnLogErrorOnFailure(InitializeCommissioner(name.c_str(), fabricId, trustStore));
}

// Initialize Group Data, including IPK
for (auto it = mCommissioners.begin(); it != mCommissioners.end(); it++)
{
Expand Down Expand Up @@ -156,6 +164,13 @@ CHIP_ERROR CHIPCommand::MaybeTearDownStack()
ReturnLogErrorOnFailure(ShutdownCommissioner(kIdentityBeta));
ReturnLogErrorOnFailure(ShutdownCommissioner(kIdentityGamma));

std::string name = GetIdentity();
chip::FabricId fabricId = strtoull(name.c_str(), nullptr, 0);
if (fabricId >= kIdentityOtherFabricId)
{
ReturnLogErrorOnFailure(ShutdownCommissioner(name.c_str()));
}

StopTracing();

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -201,10 +216,10 @@ void CHIPCommand::SetIdentity(const char * identity)
{
std::string name = std::string(identity);
if (name.compare(kIdentityAlpha) != 0 && name.compare(kIdentityBeta) != 0 && name.compare(kIdentityGamma) != 0 &&
name.compare(kIdentityNull) != 0)
name.compare(kIdentityNull) != 0 && strtoull(name.c_str(), nullptr, 0) < kIdentityOtherFabricId)
{
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(), kIdentityAlpha,
kIdentityBeta, kIdentityGamma);
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s, 4, 5...]", name.c_str(),
kIdentityAlpha, kIdentityBeta, kIdentityGamma);
chipDie();
}

Expand All @@ -217,9 +232,22 @@ std::string CHIPCommand::GetIdentity()
if (name.compare(kIdentityAlpha) != 0 && name.compare(kIdentityBeta) != 0 && name.compare(kIdentityGamma) != 0 &&
name.compare(kIdentityNull) != 0)
{
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(), kIdentityAlpha,
kIdentityBeta, kIdentityGamma);
chipDie();
chip::FabricId fabricId = strtoull(name.c_str(), nullptr, 0);
if (fabricId >= kIdentityOtherFabricId)
{
// normalize name since it is used in persistent storage

char s[24];
sprintf(s, "%" PRIu64, fabricId);

name = s;
}
else
{
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s, 4, 5...]", name.c_str(),
kIdentityAlpha, kIdentityBeta, kIdentityGamma);
chipDie();
}
}

return name;
Expand All @@ -246,10 +274,10 @@ chip::FabricId CHIPCommand::CurrentCommissionerId()
{
id = kIdentityNullFabricId;
}
else
else if ((id = strtoull(name.c_str(), nullptr, 0)) < kIdentityOtherFabricId)
{
VerifyOrDieWithMsg(false, chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(),
kIdentityAlpha, kIdentityBeta, kIdentityGamma);
VerifyOrDieWithMsg(false, chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s, 4, 5...]",
name.c_str(), kIdentityAlpha, kIdentityBeta, kIdentityGamma);
}

return id;
Expand Down
2 changes: 2 additions & 0 deletions examples/chip-tool/include/CHIPProjectAppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#ifndef CHIPPROJECTCONFIG_H
#define CHIPPROJECTCONFIG_H

#define CHIP_CONFIG_MAX_FABRICS 17

#define CHIP_CONFIG_ENABLE_EPHEMERAL_UDP_PORT 1

#define CHIP_CONFIG_EVENT_LOGGING_NUM_EXTERNAL_CALLBACKS 2
Expand Down

0 comments on commit 33ccd68

Please sign in to comment.