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 : uint16_t mMinInterval; 77 : uint16_t mMaxInterval; 78 : bool mFabricFiltered; 79 : Platform::ScopedMemoryBufferWithSize<AttributePathParamsValues> mAttributePaths; 80 : Platform::ScopedMemoryBufferWithSize<EventPathParamsValues> mEventPaths; 81 0 : CHIP_ERROR SetAttributePaths(const ObjectList<AttributePathParams> * pAttributePathList) 82 : { 83 0 : mAttributePaths.Free(); 84 0 : if (!pAttributePathList) 85 : { 86 0 : return CHIP_NO_ERROR; 87 : } 88 0 : const ObjectList<AttributePathParams> * attributePath = pAttributePathList; 89 0 : size_t attributePathCount = 0; 90 0 : while (attributePath) 91 : { 92 0 : attributePathCount++; 93 0 : attributePath = attributePath->mpNext; 94 : } 95 0 : ReturnErrorCodeIf((attributePathCount * sizeof(AttributePathParamsValues)) > UINT16_MAX, CHIP_ERROR_NO_MEMORY); 96 0 : mAttributePaths.Calloc(attributePathCount); 97 0 : ReturnErrorCodeIf(mAttributePaths.Get() == nullptr, CHIP_ERROR_NO_MEMORY); 98 0 : attributePath = pAttributePathList; 99 0 : for (size_t i = 0; i < attributePathCount; i++) 100 : { 101 0 : mAttributePaths[i].SetValues(attributePath->mValue); 102 0 : attributePath = attributePath->mpNext; 103 : } 104 0 : return CHIP_NO_ERROR; 105 : } 106 0 : CHIP_ERROR SetEventPaths(const ObjectList<EventPathParams> * pEventPathList) 107 : { 108 0 : mEventPaths.Free(); 109 0 : if (!pEventPathList) 110 : { 111 0 : return CHIP_NO_ERROR; 112 : } 113 0 : const ObjectList<EventPathParams> * eventPath = pEventPathList; 114 0 : size_t eventPathCount = 0; 115 0 : while (eventPath) 116 : { 117 0 : eventPathCount++; 118 0 : eventPath = eventPath->mpNext; 119 : } 120 0 : ReturnErrorCodeIf((eventPathCount * sizeof(EventPathParamsValues)) > UINT16_MAX, CHIP_ERROR_NO_MEMORY); 121 0 : mEventPaths.Calloc(eventPathCount); 122 0 : ReturnErrorCodeIf(mEventPaths.Get() == nullptr, CHIP_ERROR_NO_MEMORY); 123 0 : eventPath = pEventPathList; 124 0 : for (size_t i = 0; i < eventPathCount; i++) 125 : { 126 0 : mEventPaths[i].SetValues(eventPath->mValue); 127 0 : eventPath = eventPath->mpNext; 128 : } 129 0 : return CHIP_NO_ERROR; 130 : } 131 : }; 132 : 133 : using SubscriptionInfoIterator = CommonIterator<SubscriptionInfo>; 134 : 135 1 : virtual ~SubscriptionResumptionStorage(){}; 136 : 137 : /** 138 : * Iterate through persisted subscriptions 139 : * 140 : * @return A valid iterator on success. Use CommonIterator accessor to retrieve SubscriptionInfo 141 : */ 142 : virtual SubscriptionInfoIterator * IterateSubscriptions() = 0; 143 : 144 : /** 145 : * Save subscription resumption information to storage. 146 : * 147 : * @param subscriptionInfo the subscription information to save - caller should expect the passed in value is consumed 148 : */ 149 : virtual CHIP_ERROR Save(SubscriptionInfo & subscriptionInfo) = 0; 150 : 151 : /** 152 : * Delete subscription resumption information by node ID, fabric index, and subscription ID. 153 : */ 154 : virtual CHIP_ERROR Delete(NodeId nodeId, FabricIndex fabricIndex, SubscriptionId subscriptionId) = 0; 155 : 156 : /** 157 : * Remove all subscription resumption information associated with the specified 158 : * fabric index. If no entries for the fabric index exist, this is a no-op 159 : * and is considered successful. 160 : * 161 : * @param fabricIndex the index of the fabric for which to remove subscription resumption information 162 : */ 163 : virtual CHIP_ERROR DeleteAll(FabricIndex fabricIndex) = 0; 164 : }; 165 : } // namespace app 166 : } // namespace chip