1 /*
2 * Copyright (c) 2021-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 "sensor_agent.h"
17
18 #include "sensor_agent_proxy.h"
19 #include "sensors_errors.h"
20
21 using OHOS::HiviewDFX::HiLog;
22 using OHOS::HiviewDFX::HiLogLabel;
23 using OHOS::Sensors::SensorAgentProxy;
24 using OHOS::Sensors::SERVICE_EXCEPTION;
25 using OHOS::Sensors::PARAMETER_ERROR;
26 using OHOS::Sensors::PERMISSION_DENIED;
27
28 static const HiLogLabel LABEL = {LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorNativeAPI"};
29
NormalizeErrCode(int32_t code)30 static int32_t NormalizeErrCode(int32_t code)
31 {
32 switch (code) {
33 case PERMISSION_DENIED: {
34 return PERMISSION_DENIED;
35 }
36 case PARAMETER_ERROR: {
37 return PARAMETER_ERROR;
38 }
39 default: {
40 return SERVICE_EXCEPTION;
41 }
42 }
43 }
44
GetAllSensors(SensorInfo ** sensorInfo,int32_t * count)45 int32_t GetAllSensors(SensorInfo **sensorInfo, int32_t *count)
46 {
47 int32_t ret = SENSOR_AGENT_IMPL->GetAllSensors(sensorInfo, count);
48 if (ret != OHOS::ERR_OK) {
49 SEN_HILOGE("GetAllSensors failed");
50 return NormalizeErrCode(ret);
51 }
52 return ret;
53 }
54
ActivateSensor(int32_t sensorId,const SensorUser * user)55 int32_t ActivateSensor(int32_t sensorId, const SensorUser *user)
56 {
57 int32_t ret = SENSOR_AGENT_IMPL->ActivateSensor(sensorId, user);
58 if (ret != OHOS::ERR_OK) {
59 SEN_HILOGE("ActivateSensor failed");
60 return NormalizeErrCode(ret);
61 }
62 return ret;
63 }
64
DeactivateSensor(int32_t sensorId,const SensorUser * user)65 int32_t DeactivateSensor(int32_t sensorId, const SensorUser *user)
66 {
67 int32_t ret = SENSOR_AGENT_IMPL->DeactivateSensor(sensorId, user);
68 if (ret != OHOS::ERR_OK) {
69 SEN_HILOGE("DeactivateSensor failed");
70 return NormalizeErrCode(ret);
71 }
72 return ret;
73 }
74
SetBatch(int32_t sensorId,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval)75 int32_t SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, int64_t reportInterval)
76 {
77 int32_t ret = SENSOR_AGENT_IMPL->SetBatch(sensorId, user, samplingInterval, reportInterval);
78 if (ret != OHOS::ERR_OK) {
79 SEN_HILOGE("SetBatch failed");
80 return NormalizeErrCode(ret);
81 }
82 return ret;
83 }
84
SubscribeSensor(int32_t sensorId,const SensorUser * user)85 int32_t SubscribeSensor(int32_t sensorId, const SensorUser *user)
86 {
87 int32_t ret = SENSOR_AGENT_IMPL->SubscribeSensor(sensorId, user);
88 if (ret != OHOS::ERR_OK) {
89 SEN_HILOGE("SubscribeSensor failed");
90 return NormalizeErrCode(ret);
91 }
92 return ret;
93 }
94
UnsubscribeSensor(int32_t sensorId,const SensorUser * user)95 int32_t UnsubscribeSensor(int32_t sensorId, const SensorUser *user)
96 {
97 int32_t ret = SENSOR_AGENT_IMPL->UnsubscribeSensor(sensorId, user);
98 if (ret != OHOS::ERR_OK) {
99 SEN_HILOGE("UnsubscribeSensor failed");
100 return NormalizeErrCode(ret);
101 }
102 return ret;
103 }
104
SetMode(int32_t sensorId,const SensorUser * user,int32_t mode)105 int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode)
106 {
107 return SENSOR_AGENT_IMPL->SetMode(sensorId, user, mode);
108 }
109
SuspendSensors(int32_t pid)110 int32_t SuspendSensors(int32_t pid)
111 {
112 int32_t ret = SENSOR_AGENT_IMPL->SuspendSensors(pid);
113 if (ret != OHOS::ERR_OK) {
114 SEN_HILOGE("Suspend sensors failed, ret:%{public}d", ret);
115 return NormalizeErrCode(ret);
116 }
117 return ret;
118 }
119
ResumeSensors(int32_t pid)120 int32_t ResumeSensors(int32_t pid)
121 {
122 int32_t ret = SENSOR_AGENT_IMPL->ResumeSensors(pid);
123 if (ret != OHOS::ERR_OK) {
124 SEN_HILOGE("Resume sensors failed, ret:%{public}d", ret);
125 return NormalizeErrCode(ret);
126 }
127 return ret;
128 }
129
GetActiveSensorInfos(int32_t pid,SensorActiveInfo ** sensorActiveInfos,int32_t * count)130 int32_t GetActiveSensorInfos(int32_t pid, SensorActiveInfo **sensorActiveInfos, int32_t *count)
131 {
132 CHKPR(sensorActiveInfos, OHOS::Sensors::ERROR);
133 CHKPR(count, OHOS::Sensors::ERROR);
134 int32_t ret = SENSOR_AGENT_IMPL->GetSensorActiveInfos(pid, sensorActiveInfos, count);
135 if (ret != OHOS::ERR_OK) {
136 SEN_HILOGE("Get active sensor infos failed, ret:%{public}d", ret);
137 return NormalizeErrCode(ret);
138 }
139 return ret;
140 }
141
Register(SensorActiveInfoCB callback)142 int32_t Register(SensorActiveInfoCB callback)
143 {
144 int32_t ret = SENSOR_AGENT_IMPL->Register(callback);
145 if (ret != OHOS::ERR_OK) {
146 SEN_HILOGE("Register active sensor infos callback failed, ret:%{public}d", ret);
147 return NormalizeErrCode(ret);
148 }
149 return ret;
150 }
151
Unregister(SensorActiveInfoCB callback)152 int32_t Unregister(SensorActiveInfoCB callback)
153 {
154 int32_t ret = SENSOR_AGENT_IMPL->Unregister(callback);
155 if (ret != OHOS::ERR_OK) {
156 SEN_HILOGE("Unregister active sensor infos callback failed, ret:%{public}d", ret);
157 return NormalizeErrCode(ret);
158 }
159 return ret;
160 }
161
ResetSensors()162 int32_t ResetSensors()
163 {
164 int32_t ret = SENSOR_AGENT_IMPL->ResetSensors();
165 if (ret != OHOS::ERR_OK) {
166 SEN_HILOGE("Reset sensors failed, ret:%{public}d", ret);
167 return NormalizeErrCode(ret);
168 }
169 return ret;
170 }