|
| 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 | +} |
0 commit comments