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

[chip-tool] Create ChipToolCheckInDelegate #32885

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 12 additions & 1 deletion examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "CHIPCommand.h"

#include <commands/common/ChipToolCheckInDelegate.h>
#include <controller/CHIPDeviceControllerFactory.h>
#include <credentials/attestation_verifier/FileAttestationTrustStore.h>
#include <lib/core/CHIPConfig.h>
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -653,3 +654,13 @@ void CHIPCommand::ExecuteDeferredCleanups(intptr_t ignored)
}
sDeferredCleanups.clear();
}

void CHIPCommand::SetOnCheckInCompleteCallback(CheckInCompleteCallback * handler)
{
sCheckInDelegate.SetOnCheckInCompleteCallback(handler);
}

void CHIPCommand::UnsetOnCheckInCompleteCallback()
{
sCheckInDelegate.UnsetOnCheckInCompleteCallback();
}
8 changes: 6 additions & 2 deletions examples/chip-tool/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

#include <TracingCommandLineArgument.h>
#include <app/icd/client/CheckInHandler.h>
#include <app/icd/client/DefaultCheckInDelegate.h>
#include <app/icd/client/DefaultICDClientStorage.h>
#include <commands/common/ChipToolCheckInDelegate.h>
#include <commands/common/CredentialIssuerCommands.h>
#include <commands/example/ExampleCredentialIssuerCommands.h>
#include <credentials/GroupDataProviderImpl.h>
Expand Down Expand Up @@ -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;

Expand All @@ -180,6 +180,10 @@ class CHIPCommand : public Command

ChipDeviceCommissioner & GetCommissioner(std::string identity);

static void SetOnCheckInCompleteCallback(CheckInCompleteCallback * handler);

static void UnsetOnCheckInCompleteCallback();
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably better as:

Suggested change
static void UnsetOnCheckInCompleteCallback();
static void ClearOnCheckInCompleteCallback();


private:
CHIP_ERROR MaybeSetUpStack();
void MaybeTearDownStack();
Expand Down
95 changes: 95 additions & 0 deletions examples/chip-tool/commands/common/ChipToolCheckInDelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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 <app/icd/client/RefreshKeySender.h>
#include <crypto/CHIPCryptoPAL.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

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));
if (mpCheckInCompleteCallbacks != nullptr)
{
mpCheckInCompleteCallbacks->OnCheckInComplete(clientInfo);
}
}

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;
}

auto refreshKeySender = Platform::New<RefreshKeySender>(this, clientInfo, clientStorage, mpImEngine, newKey);
if (refreshKeySender == nullptr)
{
return nullptr;
}
return refreshKeySender;
}

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;
}
}

void ChipToolCheckInDelegate::SetOnCheckInCompleteCallback(CheckInCompleteCallback * handler)
{
mpCheckInCompleteCallbacks = handler;
}

void ChipToolCheckInDelegate::UnsetOnCheckInCompleteCallback()
{
mpCheckInCompleteCallbacks = nullptr;
}
74 changes: 74 additions & 0 deletions examples/chip-tool/commands/common/ChipToolCheckInDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
*
* 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 <set>

#include <app/InteractionModelEngine.h>
#include <app/icd/client/CheckInDelegate.h>
#include <app/icd/client/ICDClientStorage.h>

class CheckInCompleteCallback
{
public:
virtual ~CheckInCompleteCallback() {}

/**
* @brief Callback used to let the application know that a check-in message was received and validated.
*
* The callback will be executed in CHIP main loop. Implementations should avoid blocking operations in this callback.
*
* @param[in] clientInfo - ICDClientInfo object representing the state associated with the
* node that sent the check-in message.
*/
virtual void OnCheckInComplete(const chip::app::ICDClientInfo & clientInfo);
};

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;

/**
* @brief Sets a callback for when the Check-In processing completes.
*
* This method does not consider the race condition that the callback is changed during OnCheckInComplete.
*
* @param[in] handler - A pointer to the CheckInCompleteCallback to register.
*/
void SetOnCheckInCompleteCallback(CheckInCompleteCallback * handler);

/**
* @brief Unsets the callback for when the Check-In processing completes.
*
* This method does not consider the race condition that the callback is changed during OnCheckInComplete.
*/
void UnsetOnCheckInCompleteCallback();

private:
chip::app::ICDClientStorage * mpStorage = nullptr;
chip::app::InteractionModelEngine * mpImEngine = nullptr;

CheckInCompleteCallback * mpCheckInCompleteCallbacks;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this plural? There is one callback...

};
Loading