1 /*
2 * Copyright (c) 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
16 #include "constraint_manager_adapter.h"
17 #include "standby_service_impl.h"
18 #include "standby_service_log.h"
19 #include "standby_config_manager.h"
20 #include "motion_sensor_monitor.h"
21 #include "charge_state_monitor.h"
22 #include "base_state.h"
23
24 namespace OHOS {
25 namespace DevStandbyMgr {
Init()26 bool ConstraintManagerAdapter::Init()
27 {
28 stateManager_ = StandbyServiceImpl::GetInstance()->GetStateManager();
29 if (stateManager_.expired()) {
30 STANDBYSERVICE_LOGI("constraint manager plugin initialization failed");
31 return false;
32 }
33 constraintMonitorList_.emplace_back(std::make_shared<ChargeStateMonitor>());
34 if (StandbyConfigManager::GetInstance()->GetStandbySwitch(DETECT_MOTION_CONFIG)) {
35 ConstraintEvalParam motionDetectParams{StandbyState::NAP, NapStatePhase::END, StandbyState::SLEEP,
36 SleepStatePhase::SYS_RES_DEEP};
37 motionConstraint_ = std::make_shared<MotionSensorMonitor>(MOTION_DETECTION_TIMEOUT, REST_TIMEOUT,
38 TOTAL_TIMEOUT, motionDetectParams);
39 constraintMonitorList_.emplace_back(motionConstraint_);
40
41 ConstraintEvalParam repeatedMotionParams{StandbyState::SLEEP, SleepStatePhase::END, StandbyState::SLEEP,
42 SleepStatePhase::END};
43 repeatedMotionParams.isRepeatedDetection_ = true;
44 repeatedMotionConstraint_ = std::make_shared<MotionSensorMonitor>(PERIODLY_TASK_DECTION_TIMEOUT,
45 PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
46 constraintMonitorList_.emplace_back(repeatedMotionConstraint_);
47 }
48 for (const auto& constraintMonitor : constraintMonitorList_) {
49 constraintMonitor->Init();
50 }
51 STANDBYSERVICE_LOGI("constraint manager plugin initialization succeed");
52 return true;
53 }
54
UnInit()55 bool ConstraintManagerAdapter::UnInit()
56 {
57 constraintMonitorList_.clear();
58 constraintMap_.clear();
59 stateManager_.reset();
60 curMonitor_.reset();
61 isEvaluation_ = false;
62 repeatedMotionConstraint_.reset();
63 motionConstraint_.reset();
64 return true;
65 }
66
StartEvalution(const ConstraintEvalParam & params)67 ErrCode ConstraintManagerAdapter::StartEvalution(const ConstraintEvalParam& params)
68 {
69 if (isEvaluation_) {
70 STANDBYSERVICE_LOGE("can not start evalution repeatedly");
71 return ERR_STANDBY_STATE_TIMING_SEQ_ERROR;
72 }
73 STANDBYSERVICE_LOGD("start constraint evalution");
74 isEvaluation_ = true;
75 uint32_t evalutionHash = params.GetHashValue();
76 auto iter = constraintMap_.find(evalutionHash);
77 if (iter == constraintMap_.end()) {
78 STANDBYSERVICE_LOGD("constraint evalution is nullptr, pass");
79 curMonitor_ = nullptr;
80 auto stateManagerPtr = stateManager_.lock();
81 if (!stateManagerPtr) {
82 STANDBYSERVICE_LOGW("state manager is nullptr, can not end evalution");
83 return ERR_STATE_MANAGER_IS_NULLPTR;
84 } else {
85 stateManagerPtr->EndEvalCurrentState(true);
86 }
87 } else {
88 curMonitor_ = iter->second;
89 if (curMonitor_) {
90 curMonitor_->StartMonitoring();
91 }
92 }
93 return ERR_OK;
94 }
95
StopEvalution()96 ErrCode ConstraintManagerAdapter::StopEvalution()
97 {
98 if (!isEvaluation_) {
99 STANDBYSERVICE_LOGE("can not stop unused evalution");
100 return ERR_STANDBY_STATE_TIMING_SEQ_ERROR;
101 }
102 isEvaluation_ = false;
103 if (!curMonitor_) {
104 STANDBYSERVICE_LOGW("current monitor is nullptr");
105 return ERR_OK;
106 }
107 curMonitor_->StopMonitoring();
108 curMonitor_ = nullptr;
109 return ERR_OK;
110 }
111
RegisterConstraintCallback(const ConstraintEvalParam & params,const std::shared_ptr<IConstraintMonitor> & monitor)112 void ConstraintManagerAdapter::RegisterConstraintCallback(const ConstraintEvalParam& params,
113 const std::shared_ptr<IConstraintMonitor>& monitor)
114 {
115 constraintMap_.emplace(params.GetHashValue(), monitor);
116 }
117
ShellDump(const std::vector<std::string> & argsInStr,std::string & result)118 void ConstraintManagerAdapter::ShellDump(const std::vector<std::string>& argsInStr, std::string& result)
119 {
120 }
121 } // namespace DevStandbyMgr
122 } // namespace OHOS
123