Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2023 Project CHIP Authors
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 : /**
19 : * @file
20 : * This file defines the interface to store subscription information.
21 : */
22 :
23 : #pragma once
24 :
25 : #include <app/ReadClient.h>
26 : #include <lib/core/CHIPCore.h>
27 : #include <lib/support/CommonIterator.h>
28 :
29 : namespace chip {
30 : namespace app {
31 :
32 : /**
33 : * The SubscriptionResumptionStorage interface is used to persist subscriptions when they are established.
34 : */
35 : class SubscriptionResumptionStorage
36 : {
37 : public:
38 : // Structs to hold path param values as is_trivial struct
39 : struct AttributePathParamsValues
40 : {
41 : ClusterId mClusterId;
42 : AttributeId mAttributeId;
43 : EndpointId mEndpointId;
44 0 : void SetValues(const AttributePathParams & params)
45 : {
46 0 : mEndpointId = params.mEndpointId;
47 0 : mClusterId = params.mClusterId;
48 0 : mAttributeId = params.mAttributeId;
49 0 : }
50 0 : AttributePathParams GetParams() { return AttributePathParams(mEndpointId, mClusterId, mAttributeId); }
51 : };
52 : struct EventPathParamsValues
53 : {
54 : ClusterId mClusterId;
55 : EventId mEventId;
56 : EndpointId mEndpointId;
57 : bool mIsUrgentEvent;
58 0 : void SetValues(const EventPathParams & params)
59 : {
60 0 : mEndpointId = params.mEndpointId;
61 0 : mClusterId = params.mClusterId;
62 0 : mEventId = params.mEventId;
63 0 : mIsUrgentEvent = params.mIsUrgentEvent;
64 0 : }
65 0 : EventPathParams GetParams() { return EventPathParams(mEndpointId, mClusterId, mEventId, mIsUrgentEvent); }
66 : };
67 :
68 : /**
69 : * Struct to hold information about subscriptions
70 : */
71 : struct SubscriptionInfo
72 : {
73 : NodeId mNodeId;
74 : FabricIndex mFabricIndex;
75 : SubscriptionId mSubscriptionId;
76 : #if CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
77 : uint32_t mResumptionRetries;
78 : #endif
79 : uint16_t mMinInterval;
80 : uint16_t mMaxInterval;
81 : bool mFabricFiltered;
82 : Platform::ScopedMemoryBufferWithSize<AttributePathParamsValues> mAttributePaths;
83 : Platform::ScopedMemoryBufferWithSize<EventPathParamsValues> mEventPaths;
84 0 : CHIP_ERROR SetAttributePaths(const SingleLinkedListNode<AttributePathParams> * pAttributePathList)
85 : {
86 0 : mAttributePaths.Free();
87 0 : if (!pAttributePathList)
88 : {
89 0 : return CHIP_NO_ERROR;
90 : }
91 0 : const SingleLinkedListNode<AttributePathParams> * attributePath = pAttributePathList;
92 0 : size_t attributePathCount = 0;
93 0 : while (attributePath)
94 : {
95 0 : attributePathCount++;
96 0 : attributePath = attributePath->mpNext;
97 : }
98 0 : VerifyOrReturnError((attributePathCount * sizeof(AttributePathParamsValues)) <= UINT16_MAX, CHIP_ERROR_NO_MEMORY);
99 0 : mAttributePaths.Calloc(attributePathCount);
100 0 : VerifyOrReturnError(mAttributePaths.Get() != nullptr, CHIP_ERROR_NO_MEMORY);
101 0 : attributePath = pAttributePathList;
102 0 : for (size_t i = 0; i < attributePathCount; i++)
103 : {
104 0 : mAttributePaths[i].SetValues(attributePath->mValue);
105 0 : attributePath = attributePath->mpNext;
106 : }
107 0 : return CHIP_NO_ERROR;
108 : }
109 0 : CHIP_ERROR SetEventPaths(const SingleLinkedListNode<EventPathParams> * pEventPathList)
110 : {
111 0 : mEventPaths.Free();
112 0 : if (!pEventPathList)
113 : {
114 0 : return CHIP_NO_ERROR;
115 : }
116 0 : const SingleLinkedListNode<EventPathParams> * eventPath = pEventPathList;
117 0 : size_t eventPathCount = 0;
118 0 : while (eventPath)
119 : {
120 0 : eventPathCount++;
121 0 : eventPath = eventPath->mpNext;
122 : }
123 0 : VerifyOrReturnError((eventPathCount * sizeof(EventPathParamsValues)) <= UINT16_MAX, CHIP_ERROR_NO_MEMORY);
124 0 : mEventPaths.Calloc(eventPathCount);
125 0 : VerifyOrReturnError(mEventPaths.Get() != nullptr, CHIP_ERROR_NO_MEMORY);
126 0 : eventPath = pEventPathList;
127 0 : for (size_t i = 0; i < eventPathCount; i++)
128 : {
129 0 : mEventPaths[i].SetValues(eventPath->mValue);
130 0 : eventPath = eventPath->mpNext;
131 : }
132 0 : return CHIP_NO_ERROR;
133 : }
134 : };
135 :
136 : using SubscriptionInfoIterator = CommonIterator<SubscriptionInfo>;
137 :
138 1 : virtual ~SubscriptionResumptionStorage(){};
139 :
140 : /**
141 : * Iterate through persisted subscriptions
142 : *
143 : * @return A valid iterator on success. Use CommonIterator accessor to retrieve SubscriptionInfo
144 : */
145 : virtual SubscriptionInfoIterator * IterateSubscriptions() = 0;
146 :
147 : /**
148 : * Save subscription resumption information to storage.
149 : *
150 : * @param subscriptionInfo the subscription information to save - caller should expect the passed in value is consumed
151 : */
152 : virtual CHIP_ERROR Save(SubscriptionInfo & subscriptionInfo) = 0;
153 :
154 : /**
155 : * Delete subscription resumption information by node ID, fabric index, and subscription ID.
156 : */
157 : virtual CHIP_ERROR Delete(NodeId nodeId, FabricIndex fabricIndex, SubscriptionId subscriptionId) = 0;
158 :
159 : /**
160 : * Remove all subscription resumption information associated with the specified
161 : * fabric index. If no entries for the fabric index exist, this is a no-op
162 : * and is considered successful.
163 : *
164 : * @param fabricIndex the index of the fabric for which to remove subscription resumption information
165 : */
166 : virtual CHIP_ERROR DeleteAll(FabricIndex fabricIndex) = 0;
167 : };
168 : } // namespace app
169 : } // namespace chip
|