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 #ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_PLUGINS_CONSTRAINTS_INCLUDE_MOTION_SENSOR_CONSTRAINT_H
17 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_PLUGINS_CONSTRAINTS_INCLUDE_MOTION_SENSOR_CONSTRAINT_H
18 
19 #include <memory>
20 #include <map>
21 #include <functional>
22 #include "refbase.h"
23 
24 #include "sensor_agent.h"
25 #include "sensor_agent_type.h"
26 #include "iconstraint_monitor.h"
27 #include "base_state.h"
28 
29 namespace OHOS {
30 namespace DevStandbyMgr {
31 class IStateManagerAdapter;
32 class MotionSensorMonitor : public IConstraintMonitor,
33     public std::enable_shared_from_this<MotionSensorMonitor> {
34 public:
35     MotionSensorMonitor(int32_t detectionTimeOut, int32_t restTimeOut, int32_t totalTimeOut,
36         const ConstraintEvalParam& params);
37     bool Init() override;
38     void StartMonitoring() override;
39     void StopMonitoring() override;
40     static double GetEnergy();
41     static void SetEnergy(double energy);
42     static void AddEnergy(AccelData *accelData);
43     static void MotionSensorCallback(SensorEvent *event);
44     static void AcceleromterCallback(SensorEvent *event);
45     static void RepeatAcceleromterCallback(SensorEvent *event);
46 
47 private:
48     bool InitSensorUserMap(SensorInfo* sensorInfo, int32_t count);
49     void AssignAcclerometerSensorCallBack();
50     void AssignMotionSensorCallBack();
51     ErrCode StartMonitoringInner();
52     void StopMonitoringInner();
53     bool CheckSersorUsable(SensorInfo *sensorInfo, int32_t count, int32_t sensorTypeId);
54     ErrCode StartSensor();
55     void StopSensor();
56     void PeriodlyStartMotionDetection();
57     void StopMotionDetection();
58 
59 private:
60     const int32_t detectionTimeOut_;
61     const int32_t restTimeOut_;
62     const int32_t totalTimeOut_;
63 
64     static double energy_;
65     static bool hasPrevAccelData_;
66     static AccelData previousAccelData_;
67     static AccelData currentAccelData_;
68     std::shared_ptr<AppExecFwk::EventHandler> handler_ {};
69     ConstraintEvalParam params_{};
70     bool isMonitoring_ {false};
71 
72     // key is sensor type, value is sensorUser with callback function pointer
73     std::map<int32_t, SensorUser> sensorUserMap_ {};
74 };
75 } // DevStandbyMgr
76 } // OHOS
77 #endif // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_PLUGINS_CONSTRAINTS_INCLUDE_MOTION_SENSOR_CONSTRAINT_H
78