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