• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 
16 #include "startup_delay_state_collection.h"
17 
18 #include "file_operation.h"
19 #include "securec.h"
20 #include "string_ex.h"
21 #include "string_operation.h"
22 #include "sysparam.h"
23 #include "thermal_service.h"
24 #include "thermal_common.h"
25 
26 namespace OHOS {
27 namespace PowerMgr {
28 
Init()29 bool StartupDelayStateCollection::Init()
30 {
31     THERMAL_HILOGI(COMP_SVC, "StartupDelayStateCollection Init...,start to init startup delay state");
32     if (delayTimerId_ > 0) {
33         StopDelayTimer();
34     }
35     state_ = STARTUP_DELAY_STATE;
36     StartDelayTimer();
37     return true;
38 }
39 
InitParam(std::string & params)40 bool StartupDelayStateCollection::InitParam(std::string& params)
41 {
42     THERMAL_HILOGD(COMP_SVC, "Enter");
43     params_ = params;
44     THERMAL_HILOGI(COMP_SVC, "init power on delay time info");
45     delayTime_ = static_cast<uint32_t>(strtol(params_.c_str(), nullptr, STRTOL_FORMART_DEC));
46     return true;
47 }
48 
GetState()49 std::string StartupDelayStateCollection::GetState()
50 {
51     std::lock_guard<std::mutex> lock(mutex_);
52     return ToString(state_);
53 }
54 
StartDelayTimer()55 bool StartupDelayStateCollection::StartDelayTimer()
56 {
57     auto thermalTimer = std::make_shared<ThermalTimer>();
58     auto timerInfo = std::make_shared<ThermalTimerInfo>();
59     timerInfo->SetType(ThermalTimer::TIMER_TYPE_WAKEUP | ThermalTimer::TIMER_TYPE_EXACT);
60     timerInfo->SetCallbackInfo([this] { ResetState(); });
61 
62     delayTimerId_ = thermalTimer->CreateTimer(timerInfo);
63     int64_t curMsecTimestam = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs();
64 
65     return thermalTimer->StartTimer(delayTimerId_, static_cast<uint64_t>(delayTime_ + curMsecTimestam));
66 }
67 
StopDelayTimer()68 void StartupDelayStateCollection::StopDelayTimer()
69 {
70     if (delayTimerId_ > 0) {
71         auto thermalTimer = std::make_shared<ThermalTimer>();
72         if (!thermalTimer->StopTimer(delayTimerId_)) {
73             THERMAL_HILOGE(COMP_SVC, "StartupDelayStateCollection failed to stop delay timer");
74         }
75         thermalTimer->DestroyTimer(delayTimerId_);
76         delayTimerId_ = 0;
77     }
78 }
79 
SetState(const std::string & stateValue)80 void StartupDelayStateCollection::SetState(const std::string& stateValue)
81 {
82 }
83 
ResetState()84 void StartupDelayStateCollection::ResetState()
85 {
86     std::lock_guard<std::mutex> lock(mutex_);
87     THERMAL_HILOGI(COMP_SVC, "StartupDelayStateCollection ResetState");
88     state_ = NON_STARTUP_DELAY_STATE;
89     StopDelayTimer();
90 }
91 
DecideState(const std::string & value)92 bool StartupDelayStateCollection::DecideState(const std::string& value)
93 {
94     THERMAL_HILOGD(COMP_SVC, "Enter: DecideState the impact of the power-on delay status.");
95     std::lock_guard<std::mutex> lock(mutex_);
96     if ((value == ToString(NON_STARTUP_DELAY_STATE) && state_ == NON_STARTUP_DELAY_STATE) ||
97         (value == ToString(STARTUP_DELAY_STATE) && state_ == STARTUP_DELAY_STATE)) {
98         THERMAL_HILOGD(COMP_SVC, "current power on delay state = %{public}d", state_);
99         return true;
100     }
101     return false;
102 }
103 } // namespace PowerMgr
104 } // namespace OHOS
105