Line data Source code
1 : /* 2 : * Copyright (c) 2023 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 : /** 19 : * @file 20 : * This file defines a basic implementation of SubscriptionResumptionStorage that 21 : * persists subscriptions in a flat list in TLV. 22 : */ 23 : 24 : #pragma once 25 : 26 : #include <app/SubscriptionResumptionStorage.h> 27 : 28 : #include <lib/core/TLV.h> 29 : #include <lib/support/DefaultStorageKeyAllocator.h> 30 : #include <lib/support/Pool.h> 31 : 32 : namespace chip { 33 : namespace app { 34 : 35 : /** 36 : * An example SubscriptionResumptionStorage using PersistentStorageDelegate as it backend. 37 : */ 38 : class SimpleSubscriptionResumptionStorage : public SubscriptionResumptionStorage 39 : { 40 : public: 41 : static constexpr size_t kIteratorsMax = CHIP_CONFIG_MAX_SUBSCRIPTION_RESUMPTION_STORAGE_CONCURRENT_ITERATORS; 42 : 43 : CHIP_ERROR Init(PersistentStorageDelegate * storage); 44 : 45 : SubscriptionInfoIterator * IterateSubscriptions() override; 46 : 47 : CHIP_ERROR Save(SubscriptionInfo & subscriptionInfo) override; 48 : 49 : CHIP_ERROR Delete(NodeId nodeId, FabricIndex fabricIndex, SubscriptionId subscriptionId) override; 50 : 51 : CHIP_ERROR DeleteAll(FabricIndex fabricIndex) override; 52 : 53 : protected: 54 : CHIP_ERROR Save(TLV::TLVWriter & writer, SubscriptionInfo & subscriptionInfo); 55 : CHIP_ERROR Load(uint16_t subscriptionIndex, SubscriptionInfo & subscriptionInfo); 56 : CHIP_ERROR Delete(uint16_t subscriptionIndex); 57 : uint16_t Count(); 58 : CHIP_ERROR DeleteMaxCount(); 59 : 60 : class SimpleSubscriptionInfoIterator : public SubscriptionInfoIterator 61 : { 62 : public: 63 : SimpleSubscriptionInfoIterator(SimpleSubscriptionResumptionStorage & storage); 64 : size_t Count() override; 65 : bool Next(SubscriptionInfo & output) override; 66 : void Release() override; 67 : 68 : private: 69 : SimpleSubscriptionResumptionStorage & mStorage; 70 : uint16_t mNextIndex; 71 : }; 72 : 73 48 : static constexpr size_t MaxScopedNodeIdSize() { return TLV::EstimateStructOverhead(sizeof(NodeId), sizeof(FabricIndex)); } 74 : 75 48 : static constexpr size_t MaxSubscriptionPathsSize() 76 : { 77 : // IM engine declares an attribute path pool and an event path pool, and each pool 78 : // includes CHIP_IM_SERVER_MAX_NUM_PATH_GROUPS_FOR_SUBSCRIPTIONS for subscriptions 79 : return 2 * 80 48 : TLV::EstimateStructOverhead( 81 48 : TLV::EstimateStructOverhead(sizeof(uint8_t), sizeof(EndpointId), sizeof(ClusterId), sizeof(AttributeId)) * 82 48 : CHIP_IM_SERVER_MAX_NUM_PATH_GROUPS_FOR_SUBSCRIPTIONS); 83 : } 84 : 85 48 : static constexpr size_t MaxSubscriptionSize() 86 : { 87 : // All the fields added together 88 48 : return TLV::EstimateStructOverhead(MaxScopedNodeIdSize(), sizeof(SubscriptionId), sizeof(uint16_t), sizeof(uint16_t), 89 48 : sizeof(bool), MaxSubscriptionPathsSize()); 90 : } 91 : 92 : enum class EventPathType : uint8_t 93 : { 94 : kUrgent = 0x1, 95 : kNonUrgent = 0x2, 96 : }; 97 : 98 : // Flat list of subscriptions indexed from from 0 to CHIP_IM_SERVER_MAX_NUM_PATH_GROUPS_FOR_SUBSCRIPTIONS-1 99 : // 100 : // Each entry in list is a Subscription TLV structure: 101 : // Structure of: (Subscription info) 102 : // Node ID 103 : // Fabric Index 104 : // Subscription ID 105 : // Min interval 106 : // Max interval 107 : // Fabric filtered boolean 108 : // List of: 109 : // Structure of: (Attribute path) 110 : // Endpoint ID 111 : // Cluster ID 112 : // Attribute ID 113 : // List of: 114 : // Structure of: (Event path) 115 : // Event subscription type (urgent / non-urgent) 116 : // Endpoint ID 117 : // Cluster ID 118 : // Event ID 119 : 120 : static constexpr TLV::Tag kPeerNodeIdTag = TLV::ContextTag(1); 121 : static constexpr TLV::Tag kFabricIndexTag = TLV::ContextTag(2); 122 : static constexpr TLV::Tag kSubscriptionIdTag = TLV::ContextTag(3); 123 : static constexpr TLV::Tag kMinIntervalTag = TLV::ContextTag(4); 124 : static constexpr TLV::Tag kMaxIntervalTag = TLV::ContextTag(5); 125 : static constexpr TLV::Tag kFabricFilteredTag = TLV::ContextTag(6); 126 : static constexpr TLV::Tag kAttributePathsListTag = TLV::ContextTag(7); 127 : static constexpr TLV::Tag kEventPathsListTag = TLV::ContextTag(8); 128 : static constexpr TLV::Tag kAttributePathTag = TLV::ContextTag(9); 129 : static constexpr TLV::Tag kEventPathTag = TLV::ContextTag(10); 130 : static constexpr TLV::Tag kEndpointIdTag = TLV::ContextTag(11); 131 : static constexpr TLV::Tag kClusterIdTag = TLV::ContextTag(12); 132 : static constexpr TLV::Tag kAttributeIdTag = TLV::ContextTag(13); 133 : static constexpr TLV::Tag kEventIdTag = TLV::ContextTag(14); 134 : static constexpr TLV::Tag kEventPathTypeTag = TLV::ContextTag(16); 135 : 136 : PersistentStorageDelegate * mStorage; 137 : ObjectPool<SimpleSubscriptionInfoIterator, kIteratorsMax> mSubscriptionInfoIterators; 138 : }; 139 : } // namespace app 140 : } // namespace chip