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 2329 : static constexpr size_t MaxScopedNodeIdSize() { return TLV::EstimateStructOverhead(sizeof(NodeId), sizeof(FabricIndex)); }
74 :
75 2329 : 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 2329 : TLV::EstimateStructOverhead(
81 2329 : TLV::EstimateStructOverhead(sizeof(uint8_t), sizeof(EndpointId), sizeof(ClusterId), sizeof(AttributeId)) *
82 : // Constant product inside a CHIPConfig.h macro; cannot widen at the use site.
83 : // NOLINTNEXTLINE(bugprone-implicit-widening-of-multiplication-result)
84 2329 : CHIP_IM_SERVER_MAX_NUM_PATH_GROUPS_FOR_SUBSCRIPTIONS);
85 : }
86 :
87 2329 : static constexpr size_t MaxSubscriptionSize()
88 : {
89 : // All the fields added together
90 2329 : return TLV::EstimateStructOverhead(MaxScopedNodeIdSize(), sizeof(SubscriptionId), sizeof(uint16_t), sizeof(uint16_t),
91 2329 : sizeof(bool), MaxSubscriptionPathsSize());
92 : }
93 :
94 : enum class EventPathType : uint8_t
95 : {
96 : kUrgent = 0x1,
97 : kNonUrgent = 0x2,
98 : };
99 :
100 : // Flat list of subscriptions indexed from from 0 to CHIP_IM_SERVER_MAX_NUM_PATH_GROUPS_FOR_SUBSCRIPTIONS-1
101 : //
102 : // Each entry in list is a Subscription TLV structure:
103 : // Structure of: (Subscription info)
104 : // Node ID
105 : // Fabric Index
106 : // Subscription ID
107 : // Min interval
108 : // Max interval
109 : // Fabric filtered boolean
110 : // List of:
111 : // Structure of: (Attribute path)
112 : // Endpoint ID
113 : // Cluster ID
114 : // Attribute ID
115 : // List of:
116 : // Structure of: (Event path)
117 : // Event subscription type (urgent / non-urgent)
118 : // Endpoint ID
119 : // Cluster ID
120 : // Event ID
121 :
122 : static constexpr TLV::Tag kPeerNodeIdTag = TLV::ContextTag(1);
123 : static constexpr TLV::Tag kFabricIndexTag = TLV::ContextTag(2);
124 : static constexpr TLV::Tag kSubscriptionIdTag = TLV::ContextTag(3);
125 : static constexpr TLV::Tag kMinIntervalTag = TLV::ContextTag(4);
126 : static constexpr TLV::Tag kMaxIntervalTag = TLV::ContextTag(5);
127 : static constexpr TLV::Tag kFabricFilteredTag = TLV::ContextTag(6);
128 : static constexpr TLV::Tag kAttributePathsListTag = TLV::ContextTag(7);
129 : static constexpr TLV::Tag kEventPathsListTag = TLV::ContextTag(8);
130 : static constexpr TLV::Tag kAttributePathTag = TLV::ContextTag(9);
131 : static constexpr TLV::Tag kEventPathTag = TLV::ContextTag(10);
132 : static constexpr TLV::Tag kEndpointIdTag = TLV::ContextTag(11);
133 : static constexpr TLV::Tag kClusterIdTag = TLV::ContextTag(12);
134 : static constexpr TLV::Tag kAttributeIdTag = TLV::ContextTag(13);
135 : static constexpr TLV::Tag kEventIdTag = TLV::ContextTag(14);
136 : static constexpr TLV::Tag kEventPathTypeTag = TLV::ContextTag(16);
137 : static constexpr TLV::Tag kResumptionRetriesTag = TLV::ContextTag(17);
138 :
139 : PersistentStorageDelegate * mStorage;
140 : ObjectPool<SimpleSubscriptionInfoIterator, kIteratorsMax> mSubscriptionInfoIterators;
141 : };
142 : } // namespace app
143 : } // namespace chip
|