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(std::string value)59 void ActionVolume::AddActionValue(std::string value)
60 {
61     if (value.empty()) {
62         return;
63     }
64     valueList_.push_back(static_cast<float>(strtof(value.c_str(), nullptr)));
65 }
66 
Execute()67 void ActionVolume::Execute()
68 {
69     auto tms = ThermalService::GetInstance();
70     THERMAL_RETURN_IF (tms == nullptr);
71     float value = GetActionValue();
72     if (fabs(value - lastValue_) > FLOAT_ACCURACY) {
73         if (!tms->GetSimulationXml()) {
74             VolumeRequest(value);
75         } else {
76             VolumeExecution(value);
77         }
78         WriteActionTriggeredHiSysEventWithRatio(enableEvent_, actionName_, value);
79         tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
80         lastValue_ = value;
81         THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}f}", actionName_.c_str(), lastValue_);
82     }
83     valueList_.clear();
84 }
85 
GetActionValue()86 float ActionVolume::GetActionValue()
87 {
88     float value = FALLBACK_VALUE_FLOAT;
89     if (!valueList_.empty()) {
90         if (isStrict_) {
91             value = *min_element(valueList_.begin(), valueList_.end());
92         } else {
93             value = *max_element(valueList_.begin(), valueList_.end());
94         }
95     }
96     return value;
97 }
98 
VolumeRequest(float volume)99 int32_t ActionVolume::VolumeRequest(float volume)
100 {
101     auto tms = ThermalService::GetInstance();
102     std::string uid;
103     std::vector<std::string> uidList;
104     g_actionInfo = tms->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 #ifdef HAS_THERMAL_AUDIO_FRAMEWORK_PART
112     std::vector<std::unique_ptr<AudioRendererChangeInfo>> audioInfos;
113     auto instance = AudioStreamManager::GetInstance();
114 #endif
115     int32_t ret = -1;
116 #ifdef HAS_THERMAL_AUDIO_FRAMEWORK_PART
117     if (instance == nullptr) {
118         THERMAL_HILOGW(COMP_SVC, "instance is nullptr");
119         return ret;
120     }
121 
122     ret = instance->GetCurrentRendererChangeInfos(audioInfos);
123 #endif
124     if (ret < ERR_OK) {
125         return ret;
126     }
127 #ifdef HAS_THERMAL_AUDIO_FRAMEWORK_PART
128     if (audioInfos.size() <= 0) {
129         THERMAL_HILOGD(COMP_SVC, "audioRendererChangeInfos: No Active Streams");
130         return ERR_OK;
131     }
132     for (auto info = audioInfos.begin(); info != audioInfos.end(); ++info) {
133         std::vector<std::string>::iterator it = find(uidList.begin(), uidList.end(),
134             std::to_string(info->get()->clientUID));
135         if (it != uidList.end()) {
136             int32_t streamId = info->get()->sessionId;
137             ret = AudioSystemManager::GetInstance()->SetLowPowerVolume(streamId, volume);
138             if (ret < ERR_OK) {
139                 return ret;
140             }
141         }
142     }
143 #endif
144     return ERR_OK;
145 }
146 
VolumeExecution(float volume)147 int32_t ActionVolume::VolumeExecution(float volume)
148 {
149     int32_t ret = -1;
150     char buf[MAX_PATH] = {0};
151     ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, VOLUME_PATH);
152     if (ret < ERR_OK) {
153         return ret;
154     }
155     std::string valueString = std::to_string(volume) + "\n";
156     ret = FileOperation::WriteFile(buf, valueString, valueString.length());
157     if (ret != ERR_OK) {
158         return ret;
159     }
160     return ERR_OK;
161 }
162 } // namespace PowerMgr
163 } // namespace OHOS
164