1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "power_spec.h"
17 
18 namespace OHOS {
19 namespace bluetooth {
20 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_AG = {
21     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
22     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::LEVEL_MID, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
23     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
24     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
25     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
26 };
27 
28 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_HF = {
29     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
30     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::LEVEL_MID, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
31     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
32     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
33     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
34 };
35 
36 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_A2DP_SRC = {
37     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
38     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
39     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
40     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
41     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
42 };
43 
44 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_A2DP_SINK = {
45     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_5000_MS)},
46     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
47     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
48     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_3000_MS)},
49     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
50 };
51 
52 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_AVRCP_CT = {
53     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_5000_MS)},
54     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
55     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
56     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_5000_MS)},
57     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
58 };
59 
60 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_MAP_MSE = {
61     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
62     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
63     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
64     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
65     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
66 };
67 
68 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_PBAP_PCE = {
69     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_5000_MS)},
70     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
71     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
72     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_1000_MS)},
73     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
74 };
75 
76 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_PBAP_PSE = {
77     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
78     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
79     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
80     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_7000_MS)},
81     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
82 };
83 
84 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_SPP = {
85     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, SNIFF_DELAYSET_TIMEOUT_5000_MS)},
86     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
87     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
88     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_5000_MS)},
89     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
90 };
91 
92 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_GATT_CLIENT = {
93     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_10000_MS)},
94     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
95     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
96     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_10000_MS)},
97     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
98 };
99 
100 const std::map<RequestStatus, PowerInfo> PowerSpec::MODE_SPEC_GATT_SERVER = {
101     {RequestStatus::CONNECT_ON, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_5000_MS)},
102     {RequestStatus::SCO_ON, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
103     {RequestStatus::SCO_OFF, PowerInfo(PowerModeLevel::NO_ACTION, 0)},
104     {RequestStatus::IDLE, PowerInfo(PowerModeLevel::LEVEL_LOW, SNIFF_DELAYSET_TIMEOUT_5000_MS)},
105     {RequestStatus::BUSY, PowerInfo(PowerModeLevel::LEVEL_ACTIVE, 0)},
106 };
107 
108 const std::map<std::string, std::map<RequestStatus, PowerInfo>> PowerSpec::POWER_MODE_SPEC = {
109     {PROFILE_NAME_HFP_AG, MODE_SPEC_AG},
110     {PROFILE_NAME_HFP_HF, MODE_SPEC_HF},
111     {PROFILE_NAME_A2DP_SRC, MODE_SPEC_A2DP_SRC},
112     {PROFILE_NAME_A2DP_SINK, MODE_SPEC_A2DP_SINK},
113     {PROFILE_NAME_AVRCP_CT, MODE_SPEC_AVRCP_CT},
114     {PROFILE_NAME_MAP_MSE, MODE_SPEC_MAP_MSE},
115     {PROFILE_NAME_PBAP_PCE, MODE_SPEC_PBAP_PCE},
116     {PROFILE_NAME_PBAP_PSE, MODE_SPEC_PBAP_PSE},
117     {PROFILE_NAME_SPP, MODE_SPEC_SPP},
118     {PROFILE_NAME_GATT_CLIENT, MODE_SPEC_GATT_CLIENT},
119     {PROFILE_NAME_GATT_SERVER, MODE_SPEC_GATT_SERVER},
120 };
121 
122 const std::map<PowerModeLevel, PowerParam> PowerSpec::POWER_PARAM = {
123     {PowerModeLevel::LEVEL_LOW,
124         PowerParam(SNIFF_LEVEL_LOW_MAX_INTERVAL, SNIFF_LEVEL_LOW_MIN_INTERVAL, SNIFF_LEVEL_LOW_ATTEMPT,
125             SNIFF_LEVEL_LOW_TIMEOUT)},
126     {PowerModeLevel::LEVEL_MID,
127         PowerParam(SNIFF_LEVEL_MID_MAX_INTERVAL, SNIFF_LEVEL_MID_MIN_INTERVAL, SNIFF_LEVEL_MID_ATTEMPT,
128             SNIFF_LEVEL_MID_TIMEOUT)},
129     {PowerModeLevel::LEVEL_HIG,
130         PowerParam(SNIFF_LEVEL_HIG_MAX_INTERVAL, SNIFF_LEVEL_HIG_MIN_INTERVAL, SNIFF_LEVEL_HIG_ATTEMPT,
131             SNIFF_LEVEL_HIG_TIMEOUT)},
132 };
133 
134 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_AG = {
135     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
136     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
137     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
138 };
139 
140 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_HF = {
141     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
142     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
143     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
144 };
145 
146 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_A2DP_SRC = {
147     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
148     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
149     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
150     {RequestStatus::IDLE, PowerSsrLevel::SSR2},
151 };
152 
153 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_A2DP_SINK = {
154     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
155     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
156     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
157 };
158 
159 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_AVRCP_CT = {
160     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
161     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
162     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
163 };
164 
165 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_MAP_MSE = {
166     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
167     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
168     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
169 };
170 
171 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_PBAP_PCE = {
172     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
173     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
174     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
175 };
176 
177 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_PBAP_PSE = {
178     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
179     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
180     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
181 };
182 
183 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_SPP = {
184     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
185     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
186     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
187 };
188 
189 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_GATT_CLIENT = {
190     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
191     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
192     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
193 };
194 
195 const std::map<RequestStatus, PowerSsrLevel> PowerSpec::SSR_SPEC_GATT_SERVER = {
196     {RequestStatus::CONNECT_ON, PowerSsrLevel::SSR2},
197     {RequestStatus::SCO_ON, PowerSsrLevel::SSR0},
198     {RequestStatus::SCO_OFF, PowerSsrLevel::SSR2},
199 };
200 
201 const std::map<std::string, std::map<RequestStatus, PowerSsrLevel>> PowerSpec::SSR_SPEC = {
202     {PROFILE_NAME_HFP_AG, SSR_SPEC_AG},
203     {PROFILE_NAME_HFP_HF, SSR_SPEC_HF},
204     {PROFILE_NAME_A2DP_SRC, SSR_SPEC_A2DP_SRC},
205     {PROFILE_NAME_A2DP_SINK, SSR_SPEC_A2DP_SINK},
206     {PROFILE_NAME_AVRCP_CT, SSR_SPEC_AVRCP_CT},
207     {PROFILE_NAME_MAP_MSE, SSR_SPEC_MAP_MSE},
208     {PROFILE_NAME_PBAP_PCE, SSR_SPEC_PBAP_PCE},
209     {PROFILE_NAME_PBAP_PSE, SSR_SPEC_PBAP_PSE},
210     {PROFILE_NAME_SPP, SSR_SPEC_SPP},
211     {PROFILE_NAME_GATT_CLIENT, SSR_SPEC_GATT_CLIENT},
212     {PROFILE_NAME_GATT_SERVER, SSR_SPEC_GATT_SERVER},
213 };
214 
215 const std::map<PowerSsrLevel, PowerSsrParam> PowerSpec::POWER_SSR_PARAMS = {
216     {PowerSsrLevel::SSR0, PowerSsrParam(SSR0_MAX_LATENCY, SSR0_MIN_REMOTE_TIMEOUT, SSR0_MIN_LOCAL_TIMEOUT)},
217     {PowerSsrLevel::SSR1, PowerSsrParam(SSR1_MAX_LATENCY, SSR1_MIN_REMOTE_TIMEOUT, SSR1_MIN_LOCAL_TIMEOUT)},
218     {PowerSsrLevel::SSR2, PowerSsrParam(SSR2_MAX_LATENCY, SSR2_MIN_REMOTE_TIMEOUT, SSR2_MIN_LOCAL_TIMEOUT)},
219     {PowerSsrLevel::SSR3, PowerSsrParam(SSR3_MAX_LATENCY, SSR3_MIN_REMOTE_TIMEOUT, SSR3_MIN_LOCAL_TIMEOUT)},
220 };
221 
GetPowerSpec(const std::string & profileName,RequestStatus status)222 PowerInfo PowerSpec::GetPowerSpec(const std::string &profileName, RequestStatus status)
223 {
224     auto profileIts = POWER_MODE_SPEC.find(profileName);
225     if (profileIts != POWER_MODE_SPEC.end()) {
226         auto its = profileIts->second.find(status);
227         if (its != profileIts->second.end()) {
228             return its->second;
229         }
230     }
231     return PowerInfo(PowerModeLevel::NO_ACTION, 0);
232 }
233 
GetPowerParam(PowerModeLevel level)234 PowerParam PowerSpec::GetPowerParam(PowerModeLevel level)
235 {
236     auto its = POWER_PARAM.find(level);
237     if (its != POWER_PARAM.end()) {
238         return its->second;
239     }
240     return PowerParam(0, 0, 0, 0);
241 }
242 
GetPowerSsrLevel(const std::string & profileName,RequestStatus status)243 PowerSsrLevel PowerSpec::GetPowerSsrLevel(const std::string &profileName, RequestStatus status)
244 {
245     auto profileIts = SSR_SPEC.find(profileName);
246     if (profileIts != SSR_SPEC.end()) {
247         auto its = profileIts->second.find(status);
248         if (its != profileIts->second.end()) {
249             return its->second;
250         }
251     }
252     return PowerSsrLevel::NO_ACTION;
253 }
254 
GetPowerSsrParam(PowerSsrLevel level)255 PowerSsrParam PowerSpec::GetPowerSsrParam(PowerSsrLevel level)
256 {
257     auto iter = POWER_SSR_PARAMS.find(level);
258     if (iter != POWER_SSR_PARAMS.end()) {
259         return iter->second;
260     }
261     return PowerSsrParam(0, 0, 0);
262 }
263 }  // namespace bluetooth
264 }  // namespace OHOS