1 /*
2 * Copyright (c) 2021 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_agent.h"
17
18 #include "sensor_agent_proxy.h"
19 #include "sensors_errors.h"
20 #include "sensors_log_domain.h"
21
22 using OHOS::HiviewDFX::HiLog;
23 using OHOS::HiviewDFX::HiLogLabel;
24
25 static const HiLogLabel LABEL = {LOG_CORE, OHOS::SensorsLogDomain::SENSORS_INTERFACE, "SensorNativeAPI"};
26
GetInstance()27 static const OHOS::Sensors::SensorAgentProxy *GetInstance()
28 {
29 HiLog::Info(LABEL, "%{public}s begin", __func__);
30 const OHOS::Sensors::SensorAgentProxy *obj = OHOS::Sensors::SensorAgentProxy::GetSensorsObj();
31 return obj;
32 }
33
GetAllSensors(SensorInfo ** sensorInfo,int32_t * count)34 int32_t GetAllSensors(SensorInfo **sensorInfo, int32_t *count)
35 {
36 HiLog::Info(LABEL, "%{public}s begin", __func__);
37 const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance();
38 if (proxy == nullptr) {
39 HiLog::Error(LABEL, "%s proxy is nullptr", __func__);
40 return OHOS::Sensors::ERROR;
41 }
42 return proxy->GetAllSensors(sensorInfo, count);
43 }
44
ActivateSensor(int32_t sensorId,const SensorUser * user)45 int32_t ActivateSensor(int32_t sensorId, const SensorUser *user)
46 {
47 HiLog::Info(LABEL, "%{public}s begin", __func__);
48 const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance();
49 if (proxy == nullptr) {
50 HiLog::Error(LABEL, "%s proxy is nullptr", __func__);
51 return OHOS::Sensors::ERROR;
52 }
53 return proxy->ActivateSensor(sensorId, user);
54 }
55
DeactivateSensor(int32_t sensorId,const SensorUser * user)56 int32_t DeactivateSensor(int32_t sensorId, const SensorUser *user)
57 {
58 HiLog::Info(LABEL, "%{public}s begin", __func__);
59 const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance();
60 if (proxy == nullptr) {
61 HiLog::Error(LABEL, "%s proxy is nullptr", __func__);
62 return OHOS::Sensors::ERROR;
63 }
64 return proxy->DeactivateSensor(sensorId, user);
65 }
66
SetBatch(int32_t sensorId,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval)67 int32_t SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, int64_t reportInterval)
68 {
69 HiLog::Info(LABEL, "%{public}s begin", __func__);
70 const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance();
71 if (proxy == nullptr) {
72 HiLog::Error(LABEL, "%s proxy is nullptr", __func__);
73 return OHOS::Sensors::ERROR;
74 }
75 return proxy->SetBatch(sensorId, user, samplingInterval, reportInterval);
76 }
77
SubscribeSensor(int32_t sensorId,const SensorUser * user)78 int32_t SubscribeSensor(int32_t sensorId, const SensorUser *user)
79 {
80 HiLog::Info(LABEL, "%{public}s begin", __func__);
81 const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance();
82 if (proxy == nullptr) {
83 HiLog::Error(LABEL, "%s proxy is nullptr", __func__);
84 return OHOS::Sensors::ERROR;
85 }
86 return proxy->SubscribeSensor(sensorId, user);
87 }
88
UnsubscribeSensor(int32_t sensorId,const SensorUser * user)89 int32_t UnsubscribeSensor(int32_t sensorId, const SensorUser *user)
90 {
91 HiLog::Info(LABEL, "%{public}s begin", __func__);
92 const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance();
93 if (proxy == nullptr) {
94 HiLog::Error(LABEL, "%s proxy is nullptr", __func__);
95 return OHOS::Sensors::ERROR;
96 }
97 return proxy->UnsubscribeSensor(sensorId, user);
98 }
99
SetMode(int32_t sensorId,const SensorUser * user,int32_t mode)100 int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode)
101 {
102 HiLog::Info(LABEL, "%{public}s begin", __func__);
103 const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance();
104 if (proxy == nullptr) {
105 HiLog::Error(LABEL, "%s proxy is nullptr", __func__);
106 return OHOS::Sensors::ERROR;
107 }
108 return proxy->SetMode(sensorId, user, mode);
109 }
110
SetOption(int32_t sensorId,const SensorUser * user,int32_t option)111 int32_t SetOption(int32_t sensorId, const SensorUser *user, int32_t option)
112 {
113 HiLog::Info(LABEL, "%{public}s begin", __func__);
114 const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance();
115 if (proxy == nullptr) {
116 HiLog::Error(LABEL, "%s proxy is nullptr", __func__);
117 return OHOS::Sensors::ERROR;
118 }
119 return proxy->SetOption(sensorId, user, option);
120 }