-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathWifiSleepManager.cpp
192 lines (155 loc) · 6.22 KB
/
WifiSleepManager.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
/*
*
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <app/icd/server/ICDConfigurationData.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/silabs/wifi/WifiInterface.h>
#include <platform/silabs/wifi/icd/WifiSleepManager.h>
namespace {
// TODO: Once the platform sleep calls are unified, we can removed this ifdef
#if SLI_SI917 // 917 SoC & NCP
/**
* @brief Configures the Wi-Fi Chip to go to LI based sleep.
* Function sets the listen interval the ICD Transort Slow Poll configuration and enables the broadcast filter.
*
* @return CHIP_ERROR CHIP_NO_ERROR if the configuration of the Wi-Fi chip was successful; otherwise CHIP_ERROR_INTERNAL
*/
CHIP_ERROR ConfigureLIBasedSleep()
{
ReturnLogErrorOnFailure(ConfigureBroadcastFilter(true));
// Allowing the device to go to sleep must be the last actions to avoid configuration failures.
ReturnLogErrorOnFailure(ConfigurePowerSave(RSI_SLEEP_MODE_2, ASSOCIATED_POWER_SAVE,
chip::ICDConfigurationData::GetInstance().GetSlowPollingInterval().count()));
return CHIP_NO_ERROR;
}
/**
* @brief Configures the Wi-Fi Chip to go to DTIM based sleep.
* Function sets the listen interval to be synced with the DTIM beacon and disables the broadcast filter.
*
* @return CHIP_ERROR CHIP_NO_ERROR if the configuration of the Wi-Fi chip was successful; otherwise CHIP_ERROR_INTERNAL
*/
CHIP_ERROR ConfigureDTIMBasedSleep()
{
ReturnLogErrorOnFailure(ConfigureBroadcastFilter(false));
// Allowing the device to go to sleep must be the last actions to avoid configuration failures.
ReturnLogErrorOnFailure(ConfigurePowerSave(RSI_SLEEP_MODE_2, ASSOCIATED_POWER_SAVE, 0));
return CHIP_NO_ERROR;
}
/**
* @brief Configures the Wi-Fi chip to go Deep Sleep.
* Function doesn't change the state of the broadcast filter.
*
* @return CHIP_ERROR CHIP_NO_ERROR if the configuration of the Wi-Fi chip was successful; otherwise CHIP_ERROR_INTERNAL
*/
CHIP_ERROR ConfigureDeepSleep()
{
ReturnLogErrorOnFailure(ConfigurePowerSave(RSI_SLEEP_MODE_8, DEEP_SLEEP_WITH_RAM_RETENTION, 0));
return CHIP_NO_ERROR;
}
/**
* @brief Configures the Wi-Fi chip to go to High Performance.
* Function doesn't change the broad cast filter configuration.
*
* @return CHIP_ERROR CHIP_NO_ERROR if the configuration of the Wi-Fi chip was successful; otherwise CHIP_ERROR_INTERNAL
*/
CHIP_ERROR ConfigureHighPerformance()
{
ReturnLogErrorOnFailure(ConfigurePowerSave(RSI_ACTIVE, HIGH_PERFORMANCE, 0));
return CHIP_NO_ERROR;
}
#endif // SLI_SI917
} // namespace
namespace chip {
namespace DeviceLayer {
namespace Silabs {
// Initialize the static instance
WifiSleepManager WifiSleepManager::mInstance;
CHIP_ERROR WifiSleepManager::Init()
{
return VerifyAndTransitionToLowPowerMode(PowerEvent::kGenericEvent);
}
CHIP_ERROR WifiSleepManager::RequestHighPerformance()
{
VerifyOrReturnError(mHighPerformanceRequestCounter < std::numeric_limits<uint8_t>::max(), CHIP_ERROR_INTERNAL,
ChipLogError(DeviceLayer, "High performance request counter overflow"));
mHighPerformanceRequestCounter++;
// We don't do the mHighPerformanceRequestCounter check here; the check is in the VerifyAndTransitionToLowPowerMode function
ReturnErrorOnFailure(VerifyAndTransitionToLowPowerMode(PowerEvent::kGenericEvent));
return CHIP_NO_ERROR;
}
CHIP_ERROR WifiSleepManager::RemoveHighPerformanceRequest()
{
VerifyOrReturnError(mHighPerformanceRequestCounter > 0, CHIP_NO_ERROR,
ChipLogError(DeviceLayer, "Wi-Fi configuration already in low power mode"));
mHighPerformanceRequestCounter--;
// We don't do the mHighPerformanceRequestCounter check here; the check is in the VerifyAndTransitionToLowPowerMode function
ReturnErrorOnFailure(VerifyAndTransitionToLowPowerMode(PowerEvent::kGenericEvent));
return CHIP_NO_ERROR;
}
CHIP_ERROR WifiSleepManager::HandlePowerEvent(PowerEvent event)
{
switch (event)
{
case PowerEvent::kCommissioningComplete:
ChipLogProgress(AppServer, "WifiSleepManager: Handling Commissioning Complete Event");
mIsCommissioningInProgress = false;
// TODO: Remove High Performance Req during commissioning when sleep issues are resolved
WifiSleepManager::GetInstance().RemoveHighPerformanceRequest();
break;
case PowerEvent::kConnectivityChange:
case PowerEvent::kGenericEvent:
// No additional processing needed for these events at the moment
break;
default:
ChipLogError(AppServer, "Unknown Power Event");
return CHIP_ERROR_INVALID_ARGUMENT;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR WifiSleepManager::VerifyAndTransitionToLowPowerMode(PowerEvent event)
{
ReturnErrorOnFailure(HandlePowerEvent(event));
#if SLI_SI917 // 917 SoC & NCP
if (mHighPerformanceRequestCounter > 0)
{
return ConfigureHighPerformance();
}
if (mIsCommissioningInProgress)
{
// During commissioning, don't let the device go to sleep
// This is needed to interrupt the sleep and retry joining the network
return CHIP_NO_ERROR;
}
// TODO: Clean this up when the Wi-Fi interface re-work is finished
wfx_wifi_provision_t wifiConfig;
wfx_get_wifi_provision(&wifiConfig);
if (!(wifiConfig.ssid[0] != 0))
{
return ConfigureDeepSleep();
}
if (mCallback && mCallback->CanGoToLIBasedSleep())
{
return ConfigureLIBasedSleep();
}
return ConfigureDTIMBasedSleep();
#else
ReturnErrorOnFailure(ConfigurePowerSave());
return CHIP_NO_ERROR;
#endif
}
} // namespace Silabs
} // namespace DeviceLayer
} // namespace chip