From 33ccd687be46886b6b8f5fec2d01617e6756f668 Mon Sep 17 00:00:00 2001 From: bluebin14 <80577827+bluebin14@users.noreply.github.com> Date: Tue, 5 Apr 2022 16:22:59 +0200 Subject: [PATCH] chip-tool: allow any fabricId, up to a total of 17 (#17052) * chip-tool: allow any fabricId, up to a total of 17 * chip-tool: fix compiler warning on Linux --- .../chip-tool/commands/common/CHIPCommand.cpp | 46 +++++++++++++++---- .../chip-tool/include/CHIPProjectAppConfig.h | 2 + 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index a2547e9fe98cc9..81a586c0a32e4a 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -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) @@ -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++) { @@ -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; @@ -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(); } @@ -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; @@ -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; diff --git a/examples/chip-tool/include/CHIPProjectAppConfig.h b/examples/chip-tool/include/CHIPProjectAppConfig.h index 15e2281c6929ae..d813f281a5ee04 100644 --- a/examples/chip-tool/include/CHIPProjectAppConfig.h +++ b/examples/chip-tool/include/CHIPProjectAppConfig.h @@ -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