• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 }
ActionVolume(const std::string & actionName)35 ActionVolume::ActionVolume(const std::string& actionName)
36 {
37     actionName_ = actionName;
38 }
39 
InitParams(const std::string & params)40 void ActionVolume::InitParams(const std::string& params)
41 {
42 }
43 
SetStrict(bool flag)44 void ActionVolume::SetStrict(bool flag)
45 {
46     flag_ = flag;
47 }
48 
SetEnableEvent(bool enable)49 void ActionVolume::SetEnableEvent(bool enable)
50 {
51     enableEvent_ = enable;
52 }
53 
AddActionValue(std::string value)54 void ActionVolume::AddActionValue(std::string value)
55 {
56     if (value.empty()) {
57         return;
58     }
59     valueList_.push_back(atof(value.c_str()));
60 }
61 
Execute()62 void ActionVolume::Execute()
63 {
64     THERMAL_HILOGD(COMP_SVC, "Enter");
65     THERMAL_RETURN_IF (g_service == nullptr);
66     float value;
67     std::string scene = g_service->GetScene();
68     auto iter = g_sceneMap.find(scene);
69     if (iter != g_sceneMap.end()) {
70         value = static_cast<float>(atoi(iter->second.c_str()));
71         if ((value != lastValue_) && (!g_service->GetSimulationXml())) {
72             VolumeRequest(value);
73         } else if (value != lastValue_) {
74             VolumeExecution(value);
75         } else {
76             THERMAL_HILOGD(COMP_SVC, "value is not change");
77         }
78         WriteActionTriggeredHiSysEventWithRatio(enableEvent_, actionName_, value);
79         g_service->GetObserver()->SetDecisionValue(actionName_, iter->second);
80         lastValue_ = value;
81         return;
82     }
83 
84     if (valueList_.empty()) {
85         value = 0.0;
86     } else {
87         if (flag_) {
88             value = *max_element(valueList_.begin(), valueList_.end());
89         } else {
90             value = *min_element(valueList_.begin(), valueList_.end());
91         }
92         valueList_.clear();
93     }
94     if (value != lastValue_) {
95         if (!g_service->GetSimulationXml()) {
96             VolumeRequest(value);
97         } else {
98             VolumeExecution(value);
99         }
100         WriteActionTriggeredHiSysEventWithRatio(enableEvent_, actionName_, value);
101         g_service->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
102         lastValue_ = value;
103     }
104 }
105 
VolumeRequest(float volume)106 int32_t ActionVolume::VolumeRequest(float volume)
107 {
108     THERMAL_HILOGD(COMP_SVC, "Enter");
109     std::string uid;
110     std::vector<std::string> uidList;
111     g_actionInfo = g_service->GetActionManagerObj()->GetActionItem();
112     const auto& item = std::find_if(g_actionInfo.begin(), g_actionInfo.end(), [](auto& info) {
113         return info.name == "volume";
114     });
115     uid = (item != g_actionInfo.end()) ? item->uid : uid;
116 
117     StringOperation::SplitString(uid, uidList, ",");
118     std::vector<std::unique_ptr<AudioRendererChangeInfo>> audioInfos;
119     auto instance = AudioStreamManager::GetInstance();
120     int32_t ret = -1;
121     if (instance == nullptr) {
122         THERMAL_HILOGW(COMP_SVC, "instance is nullptr");
123         return ret;
124     }
125 
126     ret = instance->GetCurrentRendererChangeInfos(audioInfos);
127     if (ret < ERR_OK) {
128         return ret;
129     }
130     if (audioInfos.size() <= 0) {
131         THERMAL_HILOGD(COMP_SVC, "audioRendererChangeInfos: No Active Streams");
132         return ERR_OK;
133     }
134     for (auto info = audioInfos.begin(); info != audioInfos.end(); ++info) {
135         std::vector<std::string>::iterator it = find(uidList.begin(), uidList.end(),
136             std::to_string(info->get()->clientUID));
137         if (it != uidList.end()) {
138             int32_t streamId = info->get()->sessionId;
139             ret = AudioSystemManager::GetInstance()->SetLowPowerVolume(streamId, volume);
140             if (ret < ERR_OK) {
141                 return ret;
142             }
143         }
144     }
145     return ERR_OK;
146 }
147 
VolumeExecution(float volume)148 int32_t ActionVolume::VolumeExecution(float volume)
149 {
150     THERMAL_HILOGD(COMP_SVC, "Enter");
151     int32_t ret = -1;
152     char buf[MAX_PATH] = {0};
153     ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, VOLUME_PATH);
154     if (ret < ERR_OK) {
155         return ret;
156     }
157     std::string valueString = std::to_string(volume) + "\n";
158     ret = FileOperation::WriteFile(buf, valueString, valueString.length());
159     if (ret != ERR_OK) {
160         return ret;
161     }
162     return ERR_OK;
163 }
164 } // namespace PowerMgr
165 } // namespace OHOS
166