1 /*
2 * Copyright (c) 2022-2023 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 #include "action_volume.h"
16
17 #include <algorithm>
18 #ifdef HAS_THERMAL_AUDIO_FRAMEWORK_PART
19 #include "audio_system_manager.h"
20 #include "audio_stream_manager.h"
21 #endif
22 #include "file_operation.h"
23 #include "thermal_hisysevent.h"
24 #include "thermal_service.h"
25 #include "securec.h"
26 #include "string_operation.h"
27
28 #ifdef HAS_THERMAL_AUDIO_FRAMEWORK_PART
29 using namespace OHOS::AudioStandard;
30 #endif
31 namespace OHOS {
32 namespace PowerMgr {
33 namespace {
34 constexpr const char* VOLUME_PATH = "/data/service/el0/thermal/config/volume";
35 const int MAX_PATH = 256;
36 std::vector<ActionItem> g_actionInfo;
37 }
38
ActionVolume(const std::string & actionName)39 ActionVolume::ActionVolume(const std::string& actionName)
40 {
41 actionName_ = actionName;
42 }
43
InitParams(const std::string & params)44 void ActionVolume::InitParams(const std::string& params)
45 {
46 (void)params;
47 }
48
SetStrict(bool enable)49 void ActionVolume::SetStrict(bool enable)
50 {
51 isStrict_ = enable;
52 }
53
SetEnableEvent(bool enable)54 void ActionVolume::SetEnableEvent(bool enable)
55 {
56 enableEvent_ = enable;
57 }
58
AddActionValue(uint32_t actionId,std::string value)59 void ActionVolume::AddActionValue(uint32_t actionId, std::string value)
60 {
61 if (value.empty()) {
62 return;
63 }
64 if (actionId > 0) {
65 auto iter = policyActionMap_.find(actionId);
66 if (iter != policyActionMap_.end()) {
67 iter->second.floatDelayValue = static_cast<float>(strtof(value.c_str(), nullptr));
68 }
69 } else {
70 valueList_.push_back(static_cast<float>(strtof(value.c_str(), nullptr)));
71 }
72 }
73
ExecuteInner()74 void ActionVolume::ExecuteInner()
75 {
76 auto tms = ThermalService::GetInstance();
77 THERMAL_RETURN_IF (tms == nullptr);
78 for (auto &policyAction : policyActionMap_) {
79 if (policyAction.second.isCompleted) {
80 valueList_.push_back(policyAction.second.floatDelayValue);
81 }
82 }
83
84 float value = GetActionValue();
85 if (fabs(value - lastValue_) > FLOAT_ACCURACY) {
86 if (!tms->GetSimulationXml()) {
87 VolumeRequest(value);
88 } else {
89 VolumeExecution(value);
90 }
91 WriteActionTriggeredHiSysEventWithRatio(enableEvent_, actionName_, value);
92 tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
93 lastValue_ = value;
94 THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}f}", actionName_.c_str(), lastValue_);
95 }
96 valueList_.clear();
97 }
98
ResetActionValue()99 void ActionVolume::ResetActionValue()
100 {
101 lastValue_ = 0;
102 }
103
GetActionValue()104 float ActionVolume::GetActionValue()
105 {
106 float value = FALLBACK_VALUE_FLOAT;
107 if (!valueList_.empty()) {
108 if (isStrict_) {
109 value = *min_element(valueList_.begin(), valueList_.end());
110 } else {
111 value = *max_element(valueList_.begin(), valueList_.end());
112 }
113 }
114 return value;
115 }
116
VolumeRequest(float volume)117 int32_t ActionVolume::VolumeRequest(float volume)
118 {
119 auto tms = ThermalService::GetInstance();
120 std::string uid;
121 std::vector<std::string> uidList;
122 g_actionInfo = tms->GetActionManagerObj()->GetActionItem();
123 const auto& item = std::find_if(g_actionInfo.begin(), g_actionInfo.end(), [](const auto& info) {
124 return info.name == "volume";
125 });
126 uid = (item != g_actionInfo.end()) ? item->uid : uid;
127
128 StringOperation::SplitString(uid, uidList, ",");
129 #ifdef HAS_THERMAL_AUDIO_FRAMEWORK_PART
130 std::vector<std::shared_ptr<AudioRendererChangeInfo>> audioInfos;
131 auto instance = AudioStreamManager::GetInstance();
132 #endif
133 int32_t ret = -1;
134 #ifdef HAS_THERMAL_AUDIO_FRAMEWORK_PART
135 if (instance == nullptr) {
136 THERMAL_HILOGW(COMP_SVC, "instance is nullptr");
137 return ret;
138 }
139
140 ret = instance->GetCurrentRendererChangeInfos(audioInfos);
141 #endif
142 if (ret < ERR_OK) {
143 return ret;
144 }
145 #ifdef HAS_THERMAL_AUDIO_FRAMEWORK_PART
146 if (audioInfos.size() <= 0) {
147 THERMAL_HILOGD(COMP_SVC, "audioRendererChangeInfos: No Active Streams");
148 return ERR_OK;
149 }
150 for (auto info = audioInfos.begin(); info != audioInfos.end(); ++info) {
151 std::vector<std::string>::iterator it = find(uidList.begin(), uidList.end(),
152 std::to_string(info->get()->clientUID));
153 if (it != uidList.end()) {
154 int32_t streamId = info->get()->sessionId;
155 ret = AudioSystemManager::GetInstance()->SetLowPowerVolume(streamId, volume);
156 if (ret < ERR_OK) {
157 return ret;
158 }
159 }
160 }
161 #endif
162 return ERR_OK;
163 }
164
VolumeExecution(float volume)165 int32_t ActionVolume::VolumeExecution(float volume)
166 {
167 int32_t ret = -1;
168 char buf[MAX_PATH] = {0};
169 ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, VOLUME_PATH);
170 if (ret < ERR_OK) {
171 return ret;
172 }
173 std::string valueString = std::to_string(volume) + "\n";
174 ret = FileOperation::WriteFile(buf, valueString, valueString.length());
175 if (ret != ERR_OK) {
176 return ret;
177 }
178 return ERR_OK;
179 }
180 } // namespace PowerMgr
181 } // namespace OHOS
182