• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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_airplane.h"
16 
17 #include "file_operation.h"
18 #include "thermal_hisysevent.h"
19 #include "thermal_service.h"
20 #include "securec.h"
21 #include "constants.h"
22 #ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
23 #include "net_conn_client.h"
24 #endif
25 
26 namespace OHOS {
27 namespace PowerMgr {
28 namespace {
29 constexpr const char* AIRPLANE_PATH = "/data/service/el0/thermal/config/airplane";
30 const int MAX_PATH = 256;
31 }
32 
ActionAirplane(const std::string & actionName)33 ActionAirplane::ActionAirplane(const std::string& actionName)
34 {
35     actionName_ = actionName;
36 }
37 
InitParams(const std::string & params)38 void ActionAirplane::InitParams(const std::string& params)
39 {
40     (void)params;
41 }
42 
SetStrict(bool enable)43 void ActionAirplane::SetStrict(bool enable)
44 {
45     isStrict_ = enable;
46 }
47 
SetEnableEvent(bool enable)48 void ActionAirplane::SetEnableEvent(bool enable)
49 {
50     enableEvent_ = enable;
51 }
52 
AddActionValue(uint32_t actionId,std::string value)53 void ActionAirplane::AddActionValue(uint32_t actionId, std::string value)
54 {
55     if (value.empty()) {
56         return;
57     }
58     char *endptr;
59     long int result = strtol(value.c_str(), &endptr, STRTOL_FORMART_DEC);
60     if (*endptr != '\0') {
61         THERMAL_HILOGE(COMP_SVC, "parse airplane value failed");
62         return;
63     }
64     if (actionId > 0) {
65         auto iter = policyActionMap_.find(actionId);
66         if (iter != policyActionMap_.end()) {
67             iter->second.uintDelayValue = static_cast<uint32_t>(result);
68         }
69     } else {
70         valueList_.push_back(static_cast<uint32_t>(result));
71     }
72 }
73 
ExecuteInner()74 void ActionAirplane::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.uintDelayValue);
81         }
82     }
83 
84     uint32_t value = GetActionValue();
85     if (value != lastValue_) {
86         if (!tms->GetSimulationXml()) {
87             AirplaneRequest(value);
88         } else {
89             AirplaneExecution(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}u}", actionName_.c_str(), lastValue_);
95     }
96     valueList_.clear();
97 }
98 
ResetActionValue()99 void ActionAirplane::ResetActionValue()
100 {
101     lastValue_ = 0;
102 }
103 
GetActionValue()104 uint32_t ActionAirplane::GetActionValue()
105 {
106     uint32_t value = FALLBACK_VALUE_UINT_ZERO;
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 
AirplaneRequest(const uint32_t & value)117 int32_t ActionAirplane::AirplaneRequest(const uint32_t& value)
118 {
119 #ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
120     switch (value) {
121         case ActionAirplane::TempStatus::LOWER_TEMP:
122             if (!ThermalService::userAirplaneState_) {
123                 THERMAL_HILOGD(COMP_SVC, "device exit Airplane mode");
124                 ThermalService::isThermalAirplane_ = true;
125                 NetManagerStandard::NetConnClient::GetInstance().SetAirplaneMode(false);
126             } else {
127                 THERMAL_HILOGD(COMP_SVC, "device keep Airplane mode");
128             }
129             break;
130         case ActionAirplane::TempStatus::HIGHER_TEMP:
131             if (!ThermalService::userAirplaneState_) {
132                 THERMAL_HILOGD(COMP_SVC, "device start Airplane mode");
133                 ThermalService::isThermalAirplane_ = true;
134                 NetManagerStandard::NetConnClient::GetInstance().SetAirplaneMode(true);
135             } else {
136                 THERMAL_HILOGD(COMP_SVC, "device already in Airplane mode");
137             }
138             break;
139         default:
140             break;
141     }
142 #endif
143     return ERR_OK;
144 }
145 
AirplaneExecution(const uint32_t & value)146 int32_t ActionAirplane::AirplaneExecution(const uint32_t& value)
147 {
148     int32_t ret = -1;
149     char buf[MAX_PATH] = {0};
150     ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, AIRPLANE_PATH);
151     if (ret < ERR_OK) {
152         THERMAL_HILOGE(COMP_SVC, "snprintf_s airplane value failed");
153         return ret;
154     }
155     std::string valueString = std::to_string(value) + "\n";
156     ret = FileOperation::WriteFile(buf, valueString, valueString.length());
157     if (ret != ERR_OK) {
158         THERMAL_HILOGE(COMP_SVC, "write airplane value failed");
159         return ret;
160     }
161     return ERR_OK;
162 }
163 } // namespace PowerMgr
164 } // namespace OHOS