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 : #pragma once
18 :
19 : #include <app/icd/server/ICDServerConfig.h>
20 : #include <lib/core/CHIPError.h>
21 : #include <lib/support/BitFlags.h>
22 :
23 : namespace chip {
24 : namespace app {
25 :
26 : /**
27 : * The ICDManager implements the ICDListener functions and is always subscribed to the ICDNotifier
28 : * This allows other Matter modules to inform the ICDManager that it needs to go and may have to stay in Active Mode,
29 : * outside of its standard ActiveModeDuration and IdleModeDuration, without being tightly coupled the application data model
30 : *
31 : * This implementation also allows other modules to implement an ICDListener and subscribe to ICDNotifier
32 : * to couple behaviours with the ICD cycles. In such cases, ICD_MAX_NOTIFICATION_SUBSCRIBERS need to be adjusted
33 : */
34 :
35 : static_assert(ICD_MAX_NOTIFICATION_SUBSCRIBERS > 0, "At least 1 Subscriber is required for the ICD Manager");
36 :
37 : class ICDListener
38 : {
39 : public:
40 : enum class KeepActiveFlagsValues : uint8_t
41 : {
42 : kCommissioningWindowOpen = 0x01,
43 : kFailSafeArmed = 0x02,
44 : kExchangeContextOpen = 0x04,
45 : kCheckInInProgress = 0x08,
46 : kTestEventTriggerActiveMode = 0x10,
47 : kInvalidFlag = 0x20, // Move up when adding more flags
48 : };
49 :
50 : enum class ICDManagementEvents : uint8_t
51 : {
52 : kTableUpdated = 0x01,
53 : };
54 :
55 : using KeepActiveFlags = BitFlags<KeepActiveFlagsValues>;
56 : using KeepActiveFlag = KeepActiveFlagsValues;
57 :
58 10 : virtual ~ICDListener() {}
59 :
60 : /**
61 : * @brief This function is called for all subscribers of the ICDNotifier when it calls NotifyNetworkActivityNotification.
62 : * It notifies the subscriber that a NetworkActivity occurred. For example, a message sent or received.
63 : */
64 : virtual void OnNetworkActivity() = 0;
65 :
66 : /**
67 : * @brief This function is called for all subscribers of the ICDNotifier when it calls NotifyActiveRequestNotification.
68 : * It informs the subscriber that there is a need to place and keep the ICD in its Active Mode.
69 : *
70 : * @param request : Identity the request source
71 : */
72 : virtual void OnKeepActiveRequest(KeepActiveFlags request) = 0;
73 :
74 : #if CHIP_CONFIG_ENABLE_ICD_DSLS
75 : /**
76 : * @brief This function is called for all subscribers of the ICDNotifier when it calls NotifySITModeRequestNotification.
77 : * It informs the subscriber that the ICD must be kept in SIT mode.
78 : */
79 : virtual void OnSITModeRequest() = 0;
80 :
81 : /**
82 : * @brief This function is called for all subscribers of the ICDNotifier when it calls NotifySITModeRequestWithdrawal.
83 : * It informs the subscriber that a previous request no longer needs ICD to be kept in SIT mode.
84 : */
85 : virtual void OnSITModeRequestWithdrawal() = 0;
86 : #endif // CHIP_CONFIG_ENABLE_ICD_DSLS
87 :
88 : /**
89 : * @brief This function is called for all subscribers of the ICDNotifier when it calls NotifyActiveRequestWithdrawal.
90 : * It informs the subscriber that a previous request no longer needs ICD to maintain its Active Mode.
91 : *
92 : * @param request : The request source
93 : */
94 : virtual void OnActiveRequestWithdrawal(KeepActiveFlags request) = 0;
95 :
96 : /**
97 : * @brief This function is called for all subscribers of the ICDNotifier when it calls NotifyICDManagementEvent.
98 : * It informs the subscriber that an ICD Management action has happened and needs to be processed
99 : *
100 : * @param event : The event type
101 : */
102 : virtual void OnICDManagementServerEvent(ICDManagementEvents event) = 0;
103 :
104 : /**
105 : * @brief This function is called for all subscribers of the ICDNoitifier when it calls NotifySubscriptionReport.
106 : * It informs the subscriber that a subscription report data is being sent.
107 : */
108 : virtual void OnSubscriptionReport() = 0;
109 : };
110 :
111 : class ICDNotifier
112 : {
113 : public:
114 : ~ICDNotifier();
115 : CHIP_ERROR Subscribe(ICDListener * subscriber);
116 : void Unsubscribe(ICDListener * subscriber);
117 :
118 : /**
119 : * The following Broacast* methods triggers all the registered ICDSubscribers related callback
120 : * For thread-safety reason (mostly of the ICDManager, which is a full time subscriber),
121 : * Those functions require to be called from the Chip Task Context, or by holding the chip stack lock.
122 : */
123 : void NotifyNetworkActivityNotification();
124 : void NotifyActiveRequestNotification(ICDListener::KeepActiveFlags request);
125 : void NotifyActiveRequestWithdrawal(ICDListener::KeepActiveFlags request);
126 : #if CHIP_CONFIG_ENABLE_ICD_DSLS
127 : void NotifySITModeRequestNotification();
128 : void NotifySITModeRequestWithdrawal();
129 : #endif // CHIP_CONFIG_ENABLE_ICD_DSLS
130 : void NotifyICDManagementEvent(ICDListener::ICDManagementEvents event);
131 : void NotifySubscriptionReport();
132 :
133 : inline void BroadcastActiveRequest(ICDListener::KeepActiveFlags request, bool notify)
134 : {
135 : (notify) ? NotifyActiveRequestNotification(request) : NotifyActiveRequestWithdrawal(request);
136 : }
137 :
138 26 : static ICDNotifier & GetInstance() { return sICDNotifier; }
139 :
140 : private:
141 : static ICDNotifier sICDNotifier;
142 : ICDListener * mSubscribers[ICD_MAX_NOTIFICATION_SUBSCRIBERS] = {};
143 : };
144 :
145 : } // namespace app
146 : } // namespace chip
|