-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathApplicationSleepManager.cpp
211 lines (178 loc) · 6.7 KB
/
ApplicationSleepManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*******************************************************************************
* @file ApplicationSleepManager.cpp
* @brief Implementation for the buisness logic around Optimizing Wi-Fi sleep states
*******************************************************************************
* # License
* <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#include "ApplicationSleepManager.h"
#include <lib/core/DataModelTypes.h>
#include <lib/support/logging/CHIPLogging.h>
namespace chip {
namespace app {
namespace Silabs {
namespace {
enum class SpecialCaseVendorID : uint16_t
{
kAppleKeychain = 4996,
};
} // namespace
ApplicationSleepManager ApplicationSleepManager::mInstance;
CHIP_ERROR ApplicationSleepManager::Init()
{
VerifyOrReturnError(mFabricTable != nullptr, CHIP_ERROR_INVALID_ARGUMENT, ChipLogError(AppServer, "FabricTable is null"));
VerifyOrReturnError(mSubscriptionsInfoProvider != nullptr, CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(AppServer, "SubscriptionsInfoProvider is null"));
VerifyOrReturnError(mWifiSleepManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(AppServer, "WifiSleepManager is null"));
ReturnErrorOnFailure(mFabricTable->AddFabricDelegate(this));
// Register WifiSleepManager::ApplicationCallback
mWifiSleepManager->SetApplicationCallback(this);
return CHIP_NO_ERROR;
}
void ApplicationSleepManager::OnCommissioningWindowOpened()
{
mIsCommissionningWindowOpen = true;
#if (defined(SLI_SI917) && SLI_SI917 == 1)
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
#endif // (defined(SLI_SI917) && SLI_SI917 == 1)
}
void ApplicationSleepManager::OnCommissioningWindowClosed()
{
mIsCommissionningWindowOpen = false;
#if (defined(SLI_SI917) && SLI_SI917 == 1)
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
#endif // (defined(SLI_SI917) && SLI_SI917 == 1)
}
void ApplicationSleepManager::OnSubscriptionEstablished(chip::app::ReadHandler & aReadHandler)
{
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
}
void ApplicationSleepManager::OnSubscriptionTerminated(chip::app::ReadHandler & aReadHandler)
{
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
}
CHIP_ERROR ApplicationSleepManager::OnSubscriptionRequested(chip::app::ReadHandler & aReadHandler,
chip::Transport::SecureSession & aSecureSession)
{
// Nothing to execute for the ApplicationSleepManager
return CHIP_NO_ERROR;
}
void ApplicationSleepManager::OnFabricRemoved(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex)
{
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
}
void ApplicationSleepManager::OnFabricCommitted(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex)
{
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
}
bool ApplicationSleepManager::CanGoToDeepSleep()
{
bool canGoToDeepSleep = true;
if (mIsCommissionningWindowOpen)
{
ChipLogProgress(AppServer, "Commissioning Window is Open - Cannot go to Deep sleep");
canGoToDeepSleep = false;
}
return canGoToDeepSleep;
}
bool ApplicationSleepManager::CanGoToLIBasedSleep()
{
bool canGoToLIBasedSleep = true;
if (mIsCommissionningWindowOpen)
{
ChipLogProgress(AppServer, "Commissioning Window is Open - Cannot go to LI based sleep");
canGoToLIBasedSleep = false;
}
else if (mIsInActiveMode)
{
ChipLogProgress(AppServer, "Device is in active mode - Cannot go to LI based sleep");
canGoToLIBasedSleep = false;
}
else
{
for (auto it = mFabricTable->begin(); it != mFabricTable->end(); ++it)
{
if (!mSubscriptionsInfoProvider->FabricHasAtLeastOneActiveSubscription(it->GetFabricIndex()))
{
ChipLogProgress(AppServer, "Fabric index %u has no active subscriptions", it->GetFabricIndex());
canGoToLIBasedSleep = ProcessSpecialVendorIDCase(it->GetVendorId());
if (canGoToLIBasedSleep)
{
ChipLogProgress(AppServer,
"Fabric index %u with vendor ID : %d has an edge case that allows for LI based sleep",
it->GetFabricIndex(), it->GetVendorId());
}
else
{
// Fabric doesn't meet the requirements to allow us to go to LI based sleep
break;
}
}
}
}
return canGoToLIBasedSleep;
}
bool ApplicationSleepManager::ProcessSpecialVendorIDCase(chip::VendorId vendorId)
{
bool hasValidException = false;
switch (to_underlying(vendorId))
{
case to_underlying(SpecialCaseVendorID::kAppleKeychain):
hasValidException = ProcessKeychainEdgeCase();
break;
default:
break;
}
return hasValidException;
}
bool ApplicationSleepManager::ProcessKeychainEdgeCase()
{
bool hasValidException = false;
for (auto it = mFabricTable->begin(); it != mFabricTable->end(); ++it)
{
if ((it->GetVendorId() == chip::VendorId::Apple) &&
mSubscriptionsInfoProvider->FabricHasAtLeastOneActiveSubscription(it->GetFabricIndex()))
{
hasValidException = true;
break;
}
}
return hasValidException;
}
void ApplicationSleepManager::OnEnterActiveMode()
{
mIsInActiveMode = true;
// TEMP-fix: Added SLI_SI917 to delay 9116 NCP sleep till wifi-connection
#if (defined(SLI_SI917) && SLI_SI917 == 1)
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
#endif // (defined(SLI_SI917) && SLI_SI917 == 1)
}
void ApplicationSleepManager::OnEnterIdleMode()
{
mIsInActiveMode = false;
// TEMP-fix: Added SLI_SI917 to delay 9116 NCP sleep till wifi-connection
#if (defined(SLI_SI917) && SLI_SI917 == 1)
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
#endif // (defined(SLI_SI917) && SLI_SI917 == 1)
}
void ApplicationSleepManager::OnTransitionToIdle()
{
// Nothing to do
}
void ApplicationSleepManager::OnICDModeChange()
{
// Nothing to do
}
} // namespace Silabs
} // namespace app
} // namespace chip