Skip to content

Commit da166a7

Browse files
committed
[chip-tool] Create ChipToolCheckInDelegate
1 parent a7c9a7b commit da166a7

File tree

5 files changed

+190
-3
lines changed

5 files changed

+190
-3
lines changed

examples/chip-tool/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ static_library("chip-tool-utils") {
5858
"commands/common/BDXDiagnosticLogsServerDelegate.cpp",
5959
"commands/common/CHIPCommand.cpp",
6060
"commands/common/CHIPCommand.h",
61+
"commands/common/ChipToolCheckInDelegate.cpp",
62+
"commands/common/ChipToolCheckInDelegate.h",
6163
"commands/common/Command.cpp",
6264
"commands/common/Command.h",
6365
"commands/common/Commands.cpp",

examples/chip-tool/commands/common/CHIPCommand.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "CHIPCommand.h"
2020

21+
#include <commands/common/ChipToolCheckInDelegate.h>
2122
#include <controller/CHIPDeviceControllerFactory.h>
2223
#include <credentials/attestation_verifier/FileAttestationTrustStore.h>
2324
#include <lib/core/CHIPConfig.h>
@@ -50,7 +51,7 @@ chip::Credentials::GroupDataProviderImpl CHIPCommand::sGroupDataProvider{ kMaxGr
5051
// All fabrics share the same ICD client storage.
5152
chip::app::DefaultICDClientStorage CHIPCommand::sICDClientStorage;
5253
chip::Crypto::RawKeySessionKeystore CHIPCommand::sSessionKeystore;
53-
chip::app::DefaultCheckInDelegate CHIPCommand::sCheckInDelegate;
54+
ChipToolCheckInDelegate CHIPCommand::sCheckInDelegate;
5455
chip::app::CheckInHandler CHIPCommand::sCheckInHandler;
5556

5657
namespace {
@@ -651,3 +652,13 @@ void CHIPCommand::ExecuteDeferredCleanups(intptr_t ignored)
651652
}
652653
sDeferredCleanups.clear();
653654
}
655+
656+
void CHIPCommand::RegisterOnCheckInCompleteCallback(CheckInCompleteCallback * handler)
657+
{
658+
sCheckInDelegate.RegisterOnCheckInCompleteCallback(handler);
659+
}
660+
661+
void CHIPCommand::UnregisterOnCheckInCompleteCallback(CheckInCompleteCallback * handler)
662+
{
663+
sCheckInDelegate.UnregisterOnCheckInCompleteCallback(handler);
664+
}

examples/chip-tool/commands/common/CHIPCommand.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
#include <TracingCommandLineArgument.h>
3030
#include <app/icd/client/CheckInHandler.h>
31-
#include <app/icd/client/DefaultCheckInDelegate.h>
3231
#include <app/icd/client/DefaultICDClientStorage.h>
32+
#include <commands/common/ChipToolCheckInDelegate.h>
3333
#include <commands/common/CredentialIssuerCommands.h>
3434
#include <commands/example/ExampleCredentialIssuerCommands.h>
3535
#include <credentials/GroupDataProviderImpl.h>
@@ -164,7 +164,7 @@ class CHIPCommand : public Command
164164

165165
static chip::Credentials::GroupDataProviderImpl sGroupDataProvider;
166166
static chip::app::DefaultICDClientStorage sICDClientStorage;
167-
static chip::app::DefaultCheckInDelegate sCheckInDelegate;
167+
static ChipToolCheckInDelegate sCheckInDelegate;
168168
static chip::app::CheckInHandler sCheckInHandler;
169169
CredentialIssuerCommands * mCredIssuerCmds;
170170

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

181181
ChipDeviceCommissioner & GetCommissioner(std::string identity);
182182

183+
static void RegisterOnCheckInCompleteCallback(CheckInCompleteCallback * handler);
184+
185+
static void UnregisterOnCheckInCompleteCallback(CheckInCompleteCallback * handler);
186+
183187
private:
184188
CHIP_ERROR MaybeSetUpStack();
185189
void MaybeTearDownStack();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "ChipToolCheckInDelegate.h"
19+
20+
#include <app/icd/client/RefreshKeySender.h>
21+
#include <crypto/CHIPCryptoPAL.h>
22+
#include <lib/support/CodeUtils.h>
23+
#include <lib/support/logging/CHIPLogging.h>
24+
25+
using namespace chip;
26+
using namespace chip::app;
27+
28+
CHIP_ERROR ChipToolCheckInDelegate::Init(ICDClientStorage * storage, InteractionModelEngine * engine)
29+
{
30+
VerifyOrReturnError(storage != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
31+
VerifyOrReturnError(mpStorage == nullptr, CHIP_ERROR_INCORRECT_STATE);
32+
mpStorage = storage;
33+
mpImEngine = engine;
34+
return CHIP_NO_ERROR;
35+
}
36+
37+
void ChipToolCheckInDelegate::OnCheckInComplete(const ICDClientInfo & clientInfo)
38+
{
39+
ChipLogProgress(
40+
ICD, "Check In Message processing complete: start_counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId,
41+
clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node));
42+
for (auto handler : mCheckInCompleteCallbacks)
43+
{
44+
handler->OnCheckInComplete(clientInfo);
45+
}
46+
}
47+
48+
RefreshKeySender * ChipToolCheckInDelegate::OnKeyRefreshNeeded(ICDClientInfo & clientInfo, ICDClientStorage * clientStorage)
49+
{
50+
CHIP_ERROR err = CHIP_NO_ERROR;
51+
RefreshKeySender::RefreshKeyBuffer newKey;
52+
53+
err = Crypto::DRBG_get_bytes(newKey.Bytes(), newKey.Capacity());
54+
if (err != CHIP_NO_ERROR)
55+
{
56+
ChipLogError(ICD, "Generation of new key failed: %" CHIP_ERROR_FORMAT, err.Format());
57+
return nullptr;
58+
}
59+
60+
auto refreshKeySender = Platform::New<RefreshKeySender>(this, clientInfo, clientStorage, mpImEngine, newKey);
61+
if (refreshKeySender == nullptr)
62+
{
63+
return nullptr;
64+
}
65+
return refreshKeySender;
66+
}
67+
68+
void ChipToolCheckInDelegate::OnKeyRefreshDone(RefreshKeySender * refreshKeySender, CHIP_ERROR error)
69+
{
70+
if (error == CHIP_NO_ERROR)
71+
{
72+
ChipLogProgress(ICD, "Re-registration with new key completed successfully");
73+
}
74+
else
75+
{
76+
ChipLogError(ICD, "Re-registration with new key failed with error : %" CHIP_ERROR_FORMAT, error.Format());
77+
// The callee can take corrective action based on the error received.
78+
}
79+
if (refreshKeySender != nullptr)
80+
{
81+
Platform::Delete(refreshKeySender);
82+
refreshKeySender = nullptr;
83+
}
84+
}
85+
86+
void ChipToolCheckInDelegate::RegisterOnCheckInCompleteCallback(CheckInCompleteCallback * handler)
87+
{
88+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this, handler]() { mCheckInCompleteCallbacks.insert(handler); });
89+
}
90+
91+
void ChipToolCheckInDelegate::UnregisterOnCheckInCompleteCallback(CheckInCompleteCallback * handler)
92+
{
93+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this, handler]() { mCheckInCompleteCallbacks.insert(handler); });
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <set>
22+
23+
#include <app/InteractionModelEngine.h>
24+
#include <app/icd/client/CheckInDelegate.h>
25+
#include <app/icd/client/ICDClientStorage.h>
26+
27+
class CheckInCompleteCallback
28+
{
29+
public:
30+
virtual ~CheckInCompleteCallback() {}
31+
32+
/**
33+
* @brief Callback used to let the application know that a check-in message was received and validated.
34+
*
35+
* The callback will be executed in CHIP main loop. Implementations avoid blocking in this callback.
36+
*
37+
* @param[in] clientInfo - ICDClientInfo object representing the state associated with the
38+
* node that sent the check-in message.
39+
*/
40+
virtual void OnCheckInComplete(const chip::app::ICDClientInfo & clientInfo);
41+
};
42+
43+
class ChipToolCheckInDelegate : public chip::app::CheckInDelegate
44+
{
45+
public:
46+
virtual ~ChipToolCheckInDelegate() {}
47+
CHIP_ERROR Init(chip::app::ICDClientStorage * storage, chip::app::InteractionModelEngine * engine);
48+
void OnCheckInComplete(const chip::app::ICDClientInfo & clientInfo) override;
49+
chip::app::RefreshKeySender * OnKeyRefreshNeeded(chip::app::ICDClientInfo & clientInfo,
50+
chip::app::ICDClientStorage * clientStorage) override;
51+
void OnKeyRefreshDone(chip::app::RefreshKeySender * refreshKeySender, CHIP_ERROR error) override;
52+
53+
/**
54+
* @brief Reigsters a callback when the check-in completes.
55+
*
56+
* The registeration will be processed inside CHIP main loop.
57+
*
58+
* @param[in] handler - A pointer to CheckInCompleteCallback to register.
59+
*/
60+
void RegisterOnCheckInCompleteCallback(CheckInCompleteCallback * handler);
61+
62+
/**
63+
* @brief Unreigsters a callback when the check-in completes.
64+
*
65+
* The unregisteration will be processed inside CHIP main loop.
66+
*
67+
* @param[in] handler - A pointer to CheckInCompleteCallback to unregister.
68+
*/
69+
void UnregisterOnCheckInCompleteCallback(CheckInCompleteCallback * handler);
70+
71+
private:
72+
chip::app::ICDClientStorage * mpStorage = nullptr;
73+
chip::app::InteractionModelEngine * mpImEngine = nullptr;
74+
75+
std::set<CheckInCompleteCallback *> mCheckInCompleteCallbacks;
76+
};

0 commit comments

Comments
 (0)