forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubscriptionResumptionSessionEstablisher.cpp
164 lines (148 loc) · 7.81 KB
/
SubscriptionResumptionSessionEstablisher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* 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 <app/InteractionModelEngine.h>
#include <app/SubscriptionResumptionSessionEstablisher.h>
namespace chip {
namespace app {
class AutoDeleteEstablisher
{
public:
AutoDeleteEstablisher(SubscriptionResumptionSessionEstablisher * sessionEstablisher) : mSessionEstablisher(sessionEstablisher)
{}
~AutoDeleteEstablisher() { chip::Platform::Delete(mSessionEstablisher); }
SubscriptionResumptionSessionEstablisher * operator->() const { return mSessionEstablisher; }
SubscriptionResumptionSessionEstablisher & operator*() const { return *mSessionEstablisher; }
private:
SubscriptionResumptionSessionEstablisher * mSessionEstablisher;
};
SubscriptionResumptionSessionEstablisher::SubscriptionResumptionSessionEstablisher() :
mOnConnectedCallback(HandleDeviceConnected, this), mOnConnectionFailureCallback(HandleDeviceConnectionFailure, this)
{}
CHIP_ERROR
SubscriptionResumptionSessionEstablisher::ResumeSubscription(
CASESessionManager & caseSessionManager, const SubscriptionResumptionStorage::SubscriptionInfo & subscriptionInfo)
{
mSubscriptionInfo.mNodeId = subscriptionInfo.mNodeId;
mSubscriptionInfo.mFabricIndex = subscriptionInfo.mFabricIndex;
mSubscriptionInfo.mSubscriptionId = subscriptionInfo.mSubscriptionId;
mSubscriptionInfo.mMinInterval = subscriptionInfo.mMinInterval;
mSubscriptionInfo.mMaxInterval = subscriptionInfo.mMaxInterval;
mSubscriptionInfo.mFabricFiltered = subscriptionInfo.mFabricFiltered;
#if CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
mSubscriptionInfo.mResumptionRetries = subscriptionInfo.mResumptionRetries;
#endif
// Copy the Attribute Paths and Event Paths
if (subscriptionInfo.mAttributePaths.AllocatedSize() > 0)
{
mSubscriptionInfo.mAttributePaths.Alloc(subscriptionInfo.mAttributePaths.AllocatedSize());
if (!mSubscriptionInfo.mAttributePaths.Get())
{
return CHIP_ERROR_NO_MEMORY;
}
for (size_t i = 0; i < mSubscriptionInfo.mAttributePaths.AllocatedSize(); ++i)
{
mSubscriptionInfo.mAttributePaths[i] = subscriptionInfo.mAttributePaths[i];
}
}
if (subscriptionInfo.mEventPaths.AllocatedSize() > 0)
{
mSubscriptionInfo.mEventPaths.Alloc(subscriptionInfo.mEventPaths.AllocatedSize());
if (!mSubscriptionInfo.mEventPaths.Get())
{
return CHIP_ERROR_NO_MEMORY;
}
for (size_t i = 0; i < mSubscriptionInfo.mEventPaths.AllocatedSize(); ++i)
{
mSubscriptionInfo.mEventPaths[i] = subscriptionInfo.mEventPaths[i];
}
}
ScopedNodeId peerNode = ScopedNodeId(mSubscriptionInfo.mNodeId, mSubscriptionInfo.mFabricIndex);
caseSessionManager.FindOrEstablishSession(peerNode, &mOnConnectedCallback, &mOnConnectionFailureCallback);
return CHIP_NO_ERROR;
}
void SubscriptionResumptionSessionEstablisher::HandleDeviceConnected(void * context, Messaging::ExchangeManager & exchangeMgr,
const SessionHandle & sessionHandle)
{
AutoDeleteEstablisher establisher(static_cast<SubscriptionResumptionSessionEstablisher *>(context));
SubscriptionResumptionStorage::SubscriptionInfo & subscriptionInfo = establisher->mSubscriptionInfo;
InteractionModelEngine * imEngine = InteractionModelEngine::GetInstance();
// Decrement the number of subscriptions to resume since we have completed our retry attempt for a given subscription.
// We do this before the readHandler creation since we do not care if the subscription has successfully been resumed or
// not. Counter only tracks the number of individual subscriptions we will try to resume.
imEngine->DecrementNumSubscriptionsToResume();
if (!imEngine->EnsureResourceForSubscription(subscriptionInfo.mFabricIndex, subscriptionInfo.mAttributePaths.AllocatedSize(),
subscriptionInfo.mEventPaths.AllocatedSize()))
{
// TODO - Should we keep the subscription here?
ChipLogProgress(InteractionModel, "no resource for subscription resumption");
return;
}
ReadHandler * readHandler = imEngine->mReadHandlers.CreateObject(*imEngine, imEngine->GetReportScheduler());
if (readHandler == nullptr)
{
// TODO - Should we keep the subscription here?
ChipLogProgress(InteractionModel, "no resource for ReadHandler creation");
return;
}
readHandler->OnSubscriptionResumed(sessionHandle, *establisher);
#if CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
// Reset the resumption retries to 0 if subscription is resumed
subscriptionInfo.mResumptionRetries = 0;
auto * subscriptionResumptionStorage = InteractionModelEngine::GetInstance()->GetSubscriptionResumptionStorage();
if (subscriptionResumptionStorage)
{
subscriptionResumptionStorage->Save(subscriptionInfo);
}
#endif // CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
}
void SubscriptionResumptionSessionEstablisher::HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId,
CHIP_ERROR error)
{
AutoDeleteEstablisher establisher(static_cast<SubscriptionResumptionSessionEstablisher *>(context));
InteractionModelEngine * imEngine = InteractionModelEngine::GetInstance();
SubscriptionResumptionStorage::SubscriptionInfo & subscriptionInfo = establisher->mSubscriptionInfo;
ChipLogError(DataManagement, "Failed to establish CASE for subscription-resumption with error '%" CHIP_ERROR_FORMAT "'",
error.Format());
// Decrement the number of subscriptions to resume since we have completed our retry attempt for a given subscription.
// We do this here since we were not able to connect to the subscriber thus we have completed our resumption attempt.
// Counter only tracks the number of individual subscriptions we will try to resume.
imEngine->DecrementNumSubscriptionsToResume();
auto * subscriptionResumptionStorage = imEngine->GetSubscriptionResumptionStorage();
if (!subscriptionResumptionStorage)
{
ChipLogError(DataManagement, "Failed to get subscription resumption storage");
return;
}
#if CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
if (subscriptionInfo.mResumptionRetries <= CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION_MAX_FIBONACCI_STEP_INDEX)
{
InteractionModelEngine::GetInstance()->TryToResumeSubscriptions();
subscriptionInfo.mResumptionRetries++;
subscriptionResumptionStorage->Save(subscriptionInfo);
}
else
#endif // CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
{
// If the device fails to establish the session several times, the subscriber might be offline and its subscription
// read client will be deleted when the device reconnects to the subscriber. This subscription will be never used again.
// Clean up the persistent subscription information storage.
subscriptionResumptionStorage->Delete(subscriptionInfo.mNodeId, subscriptionInfo.mFabricIndex,
subscriptionInfo.mSubscriptionId);
}
}
} // namespace app
} // namespace chip