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