• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "sensor_manager.h"
17 
18 #include "fi_log.h"
19 
20 namespace OHOS {
21 namespace Msdp {
22 namespace DeviceStatus {
SensorManager(const int32_t sensorTypeId,const int32_t sensorSamplingInterval)23 SensorManager::SensorManager(const int32_t sensorTypeId, const int32_t sensorSamplingInterval)
24 {
25     sensorTypeId_ = sensorTypeId;
26     sensorSamplingInterval_ = sensorSamplingInterval;
27     isRunning_ = false;
28 }
29 
~SensorManager()30 SensorManager::~SensorManager()
31 {
32     StopSensor();
33 }
34 
SetCallback(RecordSensorCallback callback)35 void SensorManager::SetCallback(RecordSensorCallback callback)
36 {
37     sensorUser_.callback = callback;
38 }
39 
StartSensor()40 int32_t SensorManager::StartSensor()
41 {
42     int32_t ret = SubscribeSensor(sensorTypeId_, &sensorUser_);
43     if (ret != 0) {
44         FI_HILOGE("Subscribe sensor failed, ret = %{public}d", ret);
45         return ret;
46     }
47     ret = SetBatch(sensorTypeId_, &sensorUser_, sensorSamplingInterval_, 0);
48     if (ret != 0) {
49         FI_HILOGE("Set Batch failed, ret = %{public}d", ret);
50         return ret;
51     }
52     ret = ActivateSensor(sensorTypeId_, &sensorUser_);
53     if (ret != 0) {
54         FI_HILOGE("Activate sensor failed, ret = %{public}d", ret);
55         return ret;
56     }
57     isRunning_ = true;
58     return ret;
59 }
60 
StopSensor()61 int32_t SensorManager::StopSensor()
62 {
63     int32_t ret = DeactivateSensor(sensorTypeId_, &sensorUser_);
64     if (ret != 0) {
65         FI_HILOGE("Activate sensor failed, ret %{public}d", ret);
66     }
67     ret = UnsubscribeSensor(sensorTypeId_, &sensorUser_);
68     if (ret != 0) {
69         FI_HILOGE("Unsubscribe sensor failed, ret %{public}d", ret);
70     }
71     isRunning_ = false;
72     return ret;
73 }
74 
GetRunningStatus()75 bool SensorManager::GetRunningStatus()
76 {
77     return isRunning_.load();
78 }
79 
IsSupportedSensor(const int32_t sensorTypeId)80 bool SensorManager::IsSupportedSensor(const int32_t sensorTypeId)
81 {
82     SensorInfo *sensorInfos { nullptr };
83     int32_t count { 0 };
84     int32_t ret = GetAllSensors(&sensorInfos, &count);
85     if (ret != 0) {
86         FI_HILOGE("Failed to get all sensors");
87         return false;
88     }
89     for (int32_t i = 0; i < count; i++) {
90         if (sensorTypeId == sensorInfos[i].sensorTypeId) {
91             return true;
92         }
93     }
94     return false;
95 }
96 } // namespace DeviceStatus
97 } // namespace Msdp
98 } // namespace OHOS