|
| 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 "RefreshKeySender.h" |
| 19 | +#include "CheckInDelegate.h" |
| 20 | +#include "controller/InvokeInteraction.h" |
| 21 | +#include <app-common/zap-generated/cluster-objects.h> |
| 22 | +#include <app/CommandPathParams.h> |
| 23 | +#include <app/InteractionModelEngine.h> |
| 24 | +#include <app/OperationalSessionSetup.h> |
| 25 | +#include <memory> |
| 26 | + |
| 27 | +namespace chip { |
| 28 | +namespace app { |
| 29 | + |
| 30 | +RefreshKeySender::RefreshKeySender(CheckInDelegate * checkInDelegate, const ICDClientInfo & icdClientInfo, |
| 31 | + ICDClientStorage * icdClientStorage, const RefreshKeyBuffer & refreshKeyBuffer) : |
| 32 | + mICDClientInfo(icdClientInfo), |
| 33 | + mpICDClientStorage(icdClientStorage), mpCheckInDelegate(checkInDelegate), mOnConnectedCallback(HandleDeviceConnected, this), |
| 34 | + mOnConnectionFailureCallback(HandleDeviceConnectionFailure, this) |
| 35 | + |
| 36 | +{ |
| 37 | + mNewKey = refreshKeyBuffer; |
| 38 | +} |
| 39 | + |
| 40 | +CHIP_ERROR RefreshKeySender::RegisterClientWithNewKey(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle) |
| 41 | +{ |
| 42 | + auto onSuccess = [&](const ConcreteCommandPath & commandPath, const StatusIB & status, const auto & dataResponse) { |
| 43 | + ChipLogProgress(ICD, "RegisterClient command succeeded"); |
| 44 | + CHIP_ERROR error; |
| 45 | + |
| 46 | + // Update the ICDClientInfo with new key and start counter and store it to persistence |
| 47 | + mICDClientInfo.start_icd_counter = dataResponse.ICDCounter; |
| 48 | + mICDClientInfo.offset = 0; |
| 49 | + mpICDClientStorage->RemoveKey(mICDClientInfo); |
| 50 | + error = mpICDClientStorage->SetKey(mICDClientInfo, mNewKey.Span()); |
| 51 | + if (error != CHIP_NO_ERROR) |
| 52 | + { |
| 53 | + ChipLogError(ICD, "Failed to set the new key after re-registration: %" CHIP_ERROR_FORMAT, error.Format()); |
| 54 | + mpCheckInDelegate->OnKeyRefreshDone(this, error); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + error = mpICDClientStorage->StoreEntry(mICDClientInfo); |
| 59 | + if (error != CHIP_NO_ERROR) |
| 60 | + { |
| 61 | + ChipLogError(ICD, "Failed to store the new key after re-registration: %" CHIP_ERROR_FORMAT, error.Format()); |
| 62 | + mpCheckInDelegate->OnKeyRefreshDone(this, error); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + mpCheckInDelegate->OnCheckInComplete(mICDClientInfo); |
| 67 | + mpCheckInDelegate->OnKeyRefreshDone(this, CHIP_NO_ERROR); |
| 68 | + }; |
| 69 | + |
| 70 | + auto onFailure = [&](CHIP_ERROR error) { |
| 71 | + ChipLogError(ICD, "RegisterClient command failed: %" CHIP_ERROR_FORMAT, error.Format()); |
| 72 | + mpCheckInDelegate->OnKeyRefreshDone(this, error); |
| 73 | + }; |
| 74 | + |
| 75 | + EndpointId endpointId = 0; |
| 76 | + |
| 77 | + Clusters::IcdManagement::Commands::RegisterClient::Type registerClientCommand; |
| 78 | + registerClientCommand.checkInNodeID = mICDClientInfo.peer_node.GetNodeId(); |
| 79 | + registerClientCommand.monitoredSubject = mICDClientInfo.monitored_subject; |
| 80 | + registerClientCommand.key = mNewKey.Span(); |
| 81 | + return Controller::InvokeCommandRequest(&exchangeMgr, sessionHandle, endpointId, registerClientCommand, onSuccess, onFailure); |
| 82 | +} |
| 83 | + |
| 84 | +CHIP_ERROR RefreshKeySender::EstablishSessionToPeer() |
| 85 | +{ |
| 86 | + ChipLogProgress(ICD, "Trying to establish a CASE session for re-registering an ICD client"); |
| 87 | + auto * caseSessionManager = InteractionModelEngine::GetInstance()->GetCASESessionManager(); |
| 88 | + VerifyOrReturnError(caseSessionManager != nullptr, CHIP_ERROR_INVALID_CASE_PARAMETER); |
| 89 | + caseSessionManager->FindOrEstablishSession(mICDClientInfo.peer_node, &mOnConnectedCallback, &mOnConnectionFailureCallback); |
| 90 | + return CHIP_NO_ERROR; |
| 91 | +} |
| 92 | + |
| 93 | +void RefreshKeySender::HandleDeviceConnected(void * context, Messaging::ExchangeManager & exchangeMgr, |
| 94 | + const SessionHandle & sessionHandle) |
| 95 | +{ |
| 96 | + RefreshKeySender * const _this = static_cast<RefreshKeySender *>(context); |
| 97 | + VerifyOrDie(_this != nullptr); |
| 98 | + |
| 99 | + CHIP_ERROR err = _this->RegisterClientWithNewKey(exchangeMgr, sessionHandle); |
| 100 | + if (CHIP_NO_ERROR != err) |
| 101 | + { |
| 102 | + ChipLogError(ICD, "Failed to send register client command"); |
| 103 | + _this->mpCheckInDelegate->OnKeyRefreshDone(_this, err); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +void RefreshKeySender::HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId, CHIP_ERROR err) |
| 108 | +{ |
| 109 | + RefreshKeySender * const _this = static_cast<RefreshKeySender *>(context); |
| 110 | + VerifyOrDie(_this != nullptr); |
| 111 | + |
| 112 | + ChipLogError(ICD, "Failed to establish CASE for re-registration with error '%" CHIP_ERROR_FORMAT "'", err.Format()); |
| 113 | + _this->mpCheckInDelegate->OnKeyRefreshDone(_this, err); |
| 114 | +} |
| 115 | +} // namespace app |
| 116 | +} // namespace chip |
0 commit comments