/src/connectedhomeip/examples/all-clusters-app/all-clusters-common/include/tls-client-management-instance.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2025 Project CHIP Authors |
4 | | * All rights reserved. |
5 | | * |
6 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | * you may not use this file except in compliance with the License. |
8 | | * You may obtain a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, software |
13 | | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | * See the License for the specific language governing permissions and |
16 | | * limitations under the License. |
17 | | */ |
18 | | |
19 | | #pragma once |
20 | | |
21 | | #include <app/clusters/tls-client-management-server/TLSClientManagementCluster.h> |
22 | | #include <app/storage/FabricTableImpl.h> |
23 | | #include <vector> |
24 | | |
25 | | namespace chip { |
26 | | namespace app { |
27 | | namespace Clusters { |
28 | | |
29 | | /// @brief struct used to identify a TLS Endpoint |
30 | | inline constexpr uint16_t kUndefinedTlsEndpointId = 0xffff; |
31 | | static constexpr uint8_t kMaxProvisionedEndpoints = 254; |
32 | | |
33 | | struct TlsEndpointId |
34 | | { |
35 | | uint16_t mEndpointId = kUndefinedTlsEndpointId; |
36 | | |
37 | 0 | TlsEndpointId() = default; |
38 | 0 | TlsEndpointId(uint16_t id) : mEndpointId(id) {} |
39 | | |
40 | 0 | uint16_t & Value() { return mEndpointId; } |
41 | 0 | const uint16_t & Value() const { return mEndpointId; } |
42 | | |
43 | 0 | void Clear() { mEndpointId = kUndefinedTlsEndpointId; } |
44 | | |
45 | 0 | bool IsValid() { return (mEndpointId != kUndefinedTlsEndpointId); } |
46 | | |
47 | 0 | bool operator==(const TlsEndpointId & other) const { return (mEndpointId == other.mEndpointId); } |
48 | | }; |
49 | | |
50 | | class EndpointTable : public app::Storage::FabricTableImpl<TlsEndpointId, TLSClientManagementDelegate::EndpointStructType> |
51 | | { |
52 | | public: |
53 | | using Super = app::Storage::FabricTableImpl<TlsEndpointId, TLSClientManagementDelegate::EndpointStructType>; |
54 | | |
55 | 0 | EndpointTable() : Super(kMaxProvisionedEndpoints, UINT16_MAX) {} |
56 | 0 | ~EndpointTable() { Finish(); }; |
57 | | }; |
58 | | |
59 | | /** |
60 | | * The application delegate to define the options & implement commands. |
61 | | */ |
62 | | class TlsClientManagementCommandDelegate : public TLSClientManagementDelegate |
63 | | { |
64 | | struct Provisioned |
65 | | { |
66 | | FabricIndex fabric; |
67 | | EndpointStructType payload; |
68 | | }; |
69 | | |
70 | | static TlsClientManagementCommandDelegate instance; |
71 | | EndpointTable mProvisioned; |
72 | | PersistentStorageDelegate * mStorage = nullptr; |
73 | | |
74 | | CHIP_ERROR GetEndpointId(FabricIndex fabric, uint16_t & id); |
75 | | |
76 | | public: |
77 | 0 | TlsClientManagementCommandDelegate() {} |
78 | 0 | ~TlsClientManagementCommandDelegate() = default; |
79 | | |
80 | | CHIP_ERROR Init(PersistentStorageDelegate & storage) override; |
81 | | |
82 | | CHIP_ERROR ForEachEndpoint(EndpointId matterEndpoint, FabricIndex fabric, LoadedEndpointCallback callback) override; |
83 | | |
84 | | Protocols::InteractionModel::ClusterStatusCode |
85 | | ProvisionEndpoint(EndpointId matterEndpoint, FabricIndex fabric, |
86 | | const TlsClientManagement::Commands::ProvisionEndpoint::DecodableType & provisionReq, |
87 | | uint16_t & endpointID) override; |
88 | | |
89 | | CHIP_ERROR FindProvisionedEndpointByID(EndpointId matterEndpoint, FabricIndex fabric, uint16_t endpointID, |
90 | | LoadedEndpointCallback callback) override; |
91 | | |
92 | | Protocols::InteractionModel::Status RemoveProvisionedEndpointByID(EndpointId matterEndpoint, FabricIndex fabric, |
93 | | uint16_t endpointID) override; |
94 | | |
95 | | CHIP_ERROR RootCertCanBeRemoved(EndpointId matterEndpoint, FabricIndex fabric, Tls::TLSCAID id) override; |
96 | | CHIP_ERROR ClientCertCanBeRemoved(EndpointId matterEndpoint, FabricIndex fabric, Tls::TLSCCDID id) override; |
97 | | |
98 | | void RemoveFabric(FabricIndex fabric) override; |
99 | | |
100 | | CHIP_ERROR MutateEndpointReferenceCount(EndpointId matterEndpoint, FabricIndex fabric, uint16_t endpointID, |
101 | | int8_t delta) override; |
102 | | |
103 | 0 | static inline TlsClientManagementCommandDelegate & GetInstance() { return instance; } |
104 | | }; |
105 | | |
106 | | /** |
107 | | * Initialize the TLS Client Management cluster with application-specific delegate and certificate table. |
108 | | * MUST be called before server initialization (e.g. in ApplicationInit()). |
109 | | */ |
110 | | void InitializeTlsClientManagement(); |
111 | | |
112 | | } // namespace Clusters |
113 | | } // namespace app |
114 | | } // namespace chip |