|
| 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 "StayActiveSender.h" |
| 19 | + |
| 20 | +#include <app-common/zap-generated/cluster-objects.h> |
| 21 | +#include <app/ConcreteCommandPath.h> |
| 22 | +#include <controller/InvokeInteraction.h> |
| 23 | +#include <support/CHIPMem.h> |
| 24 | + |
| 25 | +CHIP_ERROR StayActiveSender::SendStayActiveCommand(uint32_t stayActiveDurationMs, const chip::ScopedNodeId & peerNode, |
| 26 | + chip::app::InteractionModelEngine * engine, OnDoneCallbackType onDone) |
| 27 | +{ |
| 28 | + ConstructorOnlyInternallyCallable internal; |
| 29 | + auto stayActiveSender = chip::Platform::New<StayActiveSender>(internal, stayActiveDurationMs, peerNode, |
| 30 | + chip::app::InteractionModelEngine::GetInstance(), onDone); |
| 31 | + VerifyOrReturnError(stayActiveSender != nullptr, CHIP_ERROR_NO_MEMORY); |
| 32 | + CHIP_ERROR err = stayActiveSender->EstablishSessionToPeer(); |
| 33 | + if (CHIP_NO_ERROR != err) |
| 34 | + { |
| 35 | + chip::Platform::Delete(stayActiveSender); |
| 36 | + } |
| 37 | + return err; |
| 38 | +} |
| 39 | + |
| 40 | +StayActiveSender::StayActiveSender(const ConstructorOnlyInternallyCallable & _, uint32_t stayActiveDurationMs, |
| 41 | + const chip::ScopedNodeId & peerNode, chip::app::InteractionModelEngine * engine, |
| 42 | + OnDoneCallbackType onDone) : |
| 43 | + mStayActiveDurationMs(stayActiveDurationMs), |
| 44 | + mPeerNode(peerNode), mpImEngine(engine), mOnDone(onDone), mOnConnectedCallback(HandleDeviceConnected, this), |
| 45 | + mOnConnectionFailureCallback(HandleDeviceConnectionFailure, this) |
| 46 | +{} |
| 47 | + |
| 48 | +CHIP_ERROR StayActiveSender::SendStayActiveCommand(chip::Messaging::ExchangeManager & exchangeMgr, |
| 49 | + const chip::SessionHandle & sessionHandle) |
| 50 | +{ |
| 51 | + auto onSuccess = [&](const chip::app::ConcreteCommandPath & commandPath, const chip::app::StatusIB & status, |
| 52 | + const auto & dataResponse) { |
| 53 | + uint32_t promisedActiveDurationMs = dataResponse.promisedActiveDuration; |
| 54 | + ChipLogProgress(ICD, "StayActive command succeeded with promised duration %u", promisedActiveDurationMs); |
| 55 | + mOnDone(promisedActiveDurationMs); |
| 56 | + chip::Platform::Delete(this); |
| 57 | + }; |
| 58 | + |
| 59 | + auto onFailure = [&](CHIP_ERROR error) { |
| 60 | + ChipLogError(ICD, "StayActive command failed: %" CHIP_ERROR_FORMAT, error.Format()); |
| 61 | + chip::Platform::Delete(this); |
| 62 | + }; |
| 63 | + |
| 64 | + chip::EndpointId endpointId = 0; |
| 65 | + chip::app::Clusters::IcdManagement::Commands::StayActiveRequest::Type request; |
| 66 | + request.stayActiveDuration = mStayActiveDurationMs; |
| 67 | + return chip::Controller::InvokeCommandRequest(&exchangeMgr, sessionHandle, endpointId, request, onSuccess, onFailure); |
| 68 | +} |
| 69 | + |
| 70 | +CHIP_ERROR StayActiveSender::EstablishSessionToPeer() |
| 71 | +{ |
| 72 | + ChipLogProgress(ICD, "Trying to establish a CASE session to extend the active period for lit icd device"); |
| 73 | + auto * caseSessionManager = mpImEngine->GetCASESessionManager(); |
| 74 | + VerifyOrReturnError(caseSessionManager != nullptr, CHIP_ERROR_INVALID_CASE_PARAMETER); |
| 75 | + caseSessionManager->FindOrEstablishSession(mPeerNode, &mOnConnectedCallback, &mOnConnectionFailureCallback); |
| 76 | + return CHIP_NO_ERROR; |
| 77 | +} |
| 78 | + |
| 79 | +void StayActiveSender::HandleDeviceConnected(void * context, chip::Messaging::ExchangeManager & exchangeMgr, |
| 80 | + const chip::SessionHandle & sessionHandle) |
| 81 | +{ |
| 82 | + StayActiveSender * const _this = static_cast<StayActiveSender *>(context); |
| 83 | + VerifyOrDie(_this != nullptr); |
| 84 | + |
| 85 | + CHIP_ERROR err = _this->SendStayActiveCommand(exchangeMgr, sessionHandle); |
| 86 | + if (CHIP_NO_ERROR != err) |
| 87 | + { |
| 88 | + ChipLogError(ICD, "Failed to send stay active command"); |
| 89 | + chip::Platform::Delete(_this); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +void StayActiveSender::HandleDeviceConnectionFailure(void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR err) |
| 94 | +{ |
| 95 | + StayActiveSender * const _this = static_cast<StayActiveSender *>(context); |
| 96 | + VerifyOrDie(_this != nullptr); |
| 97 | + ChipLogError(ICD, "Failed to establish CASE for stay active command with error '%" CHIP_ERROR_FORMAT "'", err.Format()); |
| 98 | + chip::Platform::Delete(_this); |
| 99 | +} |
0 commit comments