diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index 10d03c02ee295d..7c5fe3dafa0e45 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -58,6 +58,8 @@ static_library("chip-tool-utils") { "commands/common/BDXDiagnosticLogsServerDelegate.cpp", "commands/common/CHIPCommand.cpp", "commands/common/CHIPCommand.h", + "commands/common/ChipToolCheckInDelegate.cpp", + "commands/common/ChipToolCheckInDelegate.h", "commands/common/Command.cpp", "commands/common/Command.h", "commands/common/Commands.cpp", diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index c35eb11836d6c8..8bf82abe14fbd7 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -18,6 +18,7 @@ #include "CHIPCommand.h" +#include #include #include #include @@ -52,7 +53,7 @@ chip::Credentials::GroupDataProviderImpl CHIPCommand::sGroupDataProvider{ kMaxGr // All fabrics share the same ICD client storage. chip::app::DefaultICDClientStorage CHIPCommand::sICDClientStorage; chip::Crypto::RawKeySessionKeystore CHIPCommand::sSessionKeystore; -chip::app::DefaultCheckInDelegate CHIPCommand::sCheckInDelegate; +ChipToolCheckInDelegate CHIPCommand::sCheckInDelegate; chip::app::CheckInHandler CHIPCommand::sCheckInHandler; namespace { diff --git a/examples/chip-tool/commands/common/CHIPCommand.h b/examples/chip-tool/commands/common/CHIPCommand.h index 50ab851d284502..38e4811eadf361 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.h +++ b/examples/chip-tool/commands/common/CHIPCommand.h @@ -28,8 +28,8 @@ #include #include -#include #include +#include #include #include #include @@ -164,7 +164,7 @@ class CHIPCommand : public Command static chip::Credentials::GroupDataProviderImpl sGroupDataProvider; static chip::app::DefaultICDClientStorage sICDClientStorage; - static chip::app::DefaultCheckInDelegate sCheckInDelegate; + static ChipToolCheckInDelegate sCheckInDelegate; static chip::app::CheckInHandler sCheckInHandler; CredentialIssuerCommands * mCredIssuerCmds; diff --git a/examples/chip-tool/commands/common/ChipToolCheckInDelegate.cpp b/examples/chip-tool/commands/common/ChipToolCheckInDelegate.cpp new file mode 100644 index 00000000000000..1aa2f6ea705a67 --- /dev/null +++ b/examples/chip-tool/commands/common/ChipToolCheckInDelegate.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ChipToolCheckInDelegate.h" + +#include +#include +#include +#include + +using namespace chip; +using namespace chip::app; + +CHIP_ERROR ChipToolCheckInDelegate::Init(ICDClientStorage * storage, InteractionModelEngine * engine) +{ + VerifyOrReturnError(storage != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(mpStorage == nullptr, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(engine != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + mpStorage = storage; + mpImEngine = engine; + return CHIP_NO_ERROR; +} + +void ChipToolCheckInDelegate::OnCheckInComplete(const ICDClientInfo & clientInfo) +{ + ChipLogProgress( + ICD, "Check In Message processing complete: start_counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId, + clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node)); +} + +RefreshKeySender * ChipToolCheckInDelegate::OnKeyRefreshNeeded(ICDClientInfo & clientInfo, ICDClientStorage * clientStorage) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + RefreshKeySender::RefreshKeyBuffer newKey; + + err = Crypto::DRBG_get_bytes(newKey.Bytes(), newKey.Capacity()); + if (err != CHIP_NO_ERROR) + { + ChipLogError(ICD, "Generation of new key failed: %" CHIP_ERROR_FORMAT, err.Format()); + return nullptr; + } + + return Platform::New(this, clientInfo, clientStorage, mpImEngine, newKey); +} + +void ChipToolCheckInDelegate::OnKeyRefreshDone(RefreshKeySender * refreshKeySender, CHIP_ERROR error) +{ + if (error == CHIP_NO_ERROR) + { + ChipLogProgress(ICD, "Re-registration with new key completed successfully"); + } + else + { + ChipLogError(ICD, "Re-registration with new key failed with error : %" CHIP_ERROR_FORMAT, error.Format()); + // The callee can take corrective action based on the error received. + } + if (refreshKeySender != nullptr) + { + Platform::Delete(refreshKeySender); + refreshKeySender = nullptr; + } +} diff --git a/examples/chip-tool/commands/common/ChipToolCheckInDelegate.h b/examples/chip-tool/commands/common/ChipToolCheckInDelegate.h new file mode 100644 index 00000000000000..d0e456ae885dc5 --- /dev/null +++ b/examples/chip-tool/commands/common/ChipToolCheckInDelegate.h @@ -0,0 +1,40 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include +#include +#include + +class ChipToolCheckInDelegate : public chip::app::CheckInDelegate +{ +public: + virtual ~ChipToolCheckInDelegate() {} + CHIP_ERROR Init(chip::app::ICDClientStorage * storage, chip::app::InteractionModelEngine * engine); + void OnCheckInComplete(const chip::app::ICDClientInfo & clientInfo) override; + chip::app::RefreshKeySender * OnKeyRefreshNeeded(chip::app::ICDClientInfo & clientInfo, + chip::app::ICDClientStorage * clientStorage) override; + void OnKeyRefreshDone(chip::app::RefreshKeySender * refreshKeySender, CHIP_ERROR error) override; + +private: + chip::app::ICDClientStorage * mpStorage = nullptr; + chip::app::InteractionModelEngine * mpImEngine = nullptr; +}; diff --git a/examples/darwin-framework-tool/BUILD.gn b/examples/darwin-framework-tool/BUILD.gn index 665dccf4848df4..a4859c4c68f363 100644 --- a/examples/darwin-framework-tool/BUILD.gn +++ b/examples/darwin-framework-tool/BUILD.gn @@ -170,6 +170,8 @@ config("config") { executable("darwin-framework-tool") { sources = [ + "${chip_root}/examples/chip-tool/commands/common/ChipToolCheckInDelegate.cpp", + "${chip_root}/examples/chip-tool/commands/common/ChipToolCheckInDelegate.h", "${chip_root}/examples/chip-tool/commands/common/Command.cpp", "${chip_root}/examples/chip-tool/commands/common/Command.h", "${chip_root}/examples/chip-tool/commands/common/Commands.cpp", diff --git a/examples/tv-casting-app/tv-casting-common/BUILD.gn b/examples/tv-casting-app/tv-casting-common/BUILD.gn index 72f14fb7bffeee..52e5481e97c296 100644 --- a/examples/tv-casting-app/tv-casting-common/BUILD.gn +++ b/examples/tv-casting-app/tv-casting-common/BUILD.gn @@ -38,6 +38,8 @@ chip_data_model("tv-casting-common") { "${chip_root}/examples/chip-tool/commands/clusters/ModelCommand.h", "${chip_root}/examples/chip-tool/commands/common/BDXDiagnosticLogsServerDelegate.cpp", "${chip_root}/examples/chip-tool/commands/common/CHIPCommand.h", + "${chip_root}/examples/chip-tool/commands/common/ChipToolCheckInDelegate.cpp", + "${chip_root}/examples/chip-tool/commands/common/ChipToolCheckInDelegate.h", "${chip_root}/examples/chip-tool/commands/common/Command.cpp", "${chip_root}/examples/chip-tool/commands/common/Command.h", "${chip_root}/examples/chip-tool/commands/common/Commands.cpp",