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