• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 constexpr int32_t DEFAULT_SENSOR_ID = 0;
29 constexpr int32_t DEFAULT_LOCATION = 1;
30 constexpr int32_t DEFAULT_DEVICE_ID = -1;
31 
NormalizeErrCode(int32_t code)32 static int32_t NormalizeErrCode(int32_t code)
33 {
34     switch (code) {
35         case PERMISSION_DENIED: {
36             return PERMISSION_DENIED;
37         }
38         case PARAMETER_ERROR: {
39             return PARAMETER_ERROR;
40         }
41         case NON_SYSTEM_API: {
42             return NON_SYSTEM_API;
43         }
44         default: {
45             return SERVICE_EXCEPTION;
46         }
47     }
48 }
49 
GetAllSensors(SensorInfo ** sensorInfo,int32_t * count)50 int32_t GetAllSensors(SensorInfo **sensorInfo, int32_t *count)
51 {
52     int32_t ret = SENSOR_AGENT_IMPL->GetAllSensors(sensorInfo, count);
53     if (ret != OHOS::ERR_OK) {
54         SEN_HILOGE("GetAllSensors failed");
55         return NormalizeErrCode(ret);
56     }
57     return ret;
58 }
59 
GetDeviceSensors(int32_t deviceId,SensorInfo ** sensorInfo,int32_t * count)60 int32_t GetDeviceSensors(int32_t deviceId, SensorInfo **sensorInfo, int32_t *count)
61 {
62     int32_t ret = SENSOR_AGENT_IMPL->GetDeviceSensors(deviceId, sensorInfo, count);
63     if (ret != OHOS::ERR_OK) {
64         SEN_HILOGE("GetDeviceSensors failed");
65         return NormalizeErrCode(ret);
66     }
67     return ret;
68 }
69 
ActivateSensor(int32_t sensorId,const SensorUser * user)70 int32_t ActivateSensor(int32_t sensorId, const SensorUser *user)
71 {
72     int32_t deviceId;
73     if (SENSOR_AGENT_IMPL->GetLocalDeviceId(deviceId) != OHOS::ERR_OK) {
74         SEN_HILOGW("The local deviceId cannot be found");
75         deviceId = DEFAULT_DEVICE_ID;
76     }
77     int32_t ret = SENSOR_AGENT_IMPL->ActivateSensor({deviceId, sensorId, DEFAULT_SENSOR_ID, DEFAULT_LOCATION}, user);
78     if (ret != OHOS::ERR_OK) {
79         SEN_HILOGE("ActivateSensor failed");
80         return NormalizeErrCode(ret);
81     }
82     return ret;
83 }
84 
DeactivateSensor(int32_t sensorId,const SensorUser * user)85 int32_t DeactivateSensor(int32_t sensorId, const SensorUser *user)
86 {
87     int32_t deviceId;
88     if (SENSOR_AGENT_IMPL->GetLocalDeviceId(deviceId) != OHOS::ERR_OK) {
89         SEN_HILOGW("The local deviceId cannot be found");
90         deviceId = DEFAULT_DEVICE_ID;
91     }
92     int32_t ret = SENSOR_AGENT_IMPL->DeactivateSensor({deviceId, sensorId, DEFAULT_SENSOR_ID, DEFAULT_LOCATION}, user);
93     if (ret != OHOS::ERR_OK) {
94         SEN_HILOGE("DeactivateSensor failed");
95         return NormalizeErrCode(ret);
96     }
97     return ret;
98 }
99 
SetBatch(int32_t sensorId,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval)100 int32_t SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, int64_t reportInterval)
101 {
102     int32_t deviceId;
103     if (SENSOR_AGENT_IMPL->GetLocalDeviceId(deviceId) != OHOS::ERR_OK) {
104         SEN_HILOGW("The local deviceId cannot be found");
105         deviceId = DEFAULT_DEVICE_ID;
106     }
107     int32_t ret = SENSOR_AGENT_IMPL->SetBatch({deviceId, sensorId, DEFAULT_SENSOR_ID, DEFAULT_LOCATION},
108         user, samplingInterval, reportInterval);
109     if (ret != OHOS::ERR_OK) {
110         SEN_HILOGE("SetBatch failed");
111         return NormalizeErrCode(ret);
112     }
113     return ret;
114 }
115 
SubscribeSensor(int32_t sensorId,const SensorUser * user)116 int32_t SubscribeSensor(int32_t sensorId, const SensorUser *user)
117 {
118     int32_t deviceId;
119     if (SENSOR_AGENT_IMPL->GetLocalDeviceId(deviceId) != OHOS::ERR_OK) {
120         SEN_HILOGW("The local deviceId cannot be found");
121         deviceId = DEFAULT_DEVICE_ID;
122     }
123     int32_t ret = SENSOR_AGENT_IMPL->SubscribeSensor({deviceId, sensorId, DEFAULT_SENSOR_ID, DEFAULT_LOCATION}, user);
124     if (ret != OHOS::ERR_OK) {
125         SEN_HILOGE("SubscribeSensor failed");
126         return NormalizeErrCode(ret);
127     }
128     return ret;
129 }
130 
UnsubscribeSensor(int32_t sensorId,const SensorUser * user)131 int32_t UnsubscribeSensor(int32_t sensorId, const SensorUser *user)
132 {
133     int32_t deviceId;
134     if (SENSOR_AGENT_IMPL->GetLocalDeviceId(deviceId) != OHOS::ERR_OK) {
135         SEN_HILOGW("The local deviceId cannot be found");
136         deviceId = DEFAULT_DEVICE_ID;
137     }
138     int32_t ret = SENSOR_AGENT_IMPL->UnsubscribeSensor(
139         {deviceId, sensorId, DEFAULT_SENSOR_ID, DEFAULT_LOCATION}, user);
140     if (ret != OHOS::ERR_OK) {
141         SEN_HILOGE("UnsubscribeSensor failed");
142         return NormalizeErrCode(ret);
143     }
144     return ret;
145 }
146 
SetMode(int32_t sensorId,const SensorUser * user,int32_t mode)147 int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode)
148 {
149     int32_t deviceId;
150     if (SENSOR_AGENT_IMPL->GetLocalDeviceId(deviceId) != OHOS::ERR_OK) {
151         SEN_HILOGW("The local deviceId cannot be found");
152         deviceId = DEFAULT_DEVICE_ID;
153     }
154     return SENSOR_AGENT_IMPL->SetMode({deviceId, sensorId, DEFAULT_SENSOR_ID, DEFAULT_LOCATION}, user, mode);
155 }
156 
SuspendSensors(int32_t pid)157 int32_t SuspendSensors(int32_t pid)
158 {
159     int32_t ret = SENSOR_AGENT_IMPL->SuspendSensors(pid);
160     if (ret != OHOS::ERR_OK) {
161         SEN_HILOGD("Suspend sensors failed, ret:%{public}d", ret);
162         return NormalizeErrCode(ret);
163     }
164     return ret;
165 }
166 
ResumeSensors(int32_t pid)167 int32_t ResumeSensors(int32_t pid)
168 {
169     int32_t ret = SENSOR_AGENT_IMPL->ResumeSensors(pid);
170     if (ret != OHOS::ERR_OK) {
171         SEN_HILOGD("Resume sensors failed, ret:%{public}d", ret);
172         return NormalizeErrCode(ret);
173     }
174     return ret;
175 }
176 
GetActiveSensorInfos(int32_t pid,SensorActiveInfo ** sensorActiveInfos,int32_t * count)177 int32_t GetActiveSensorInfos(int32_t pid, SensorActiveInfo **sensorActiveInfos, int32_t *count)
178 {
179     CHKPR(sensorActiveInfos, OHOS::Sensors::ERROR);
180     CHKPR(count, OHOS::Sensors::ERROR);
181     int32_t ret = SENSOR_AGENT_IMPL->GetSensorActiveInfos(pid, sensorActiveInfos, count);
182     if (ret != OHOS::ERR_OK) {
183         SEN_HILOGE("Get active sensor infos failed, ret:%{public}d", ret);
184         return NormalizeErrCode(ret);
185     }
186     return ret;
187 }
188 
Register(SensorActiveInfoCB callback)189 int32_t Register(SensorActiveInfoCB callback)
190 {
191     int32_t ret = SENSOR_AGENT_IMPL->Register(callback);
192     if (ret != OHOS::ERR_OK) {
193         SEN_HILOGE("Register active sensor infos callback failed, ret:%{public}d", ret);
194         return NormalizeErrCode(ret);
195     }
196     return ret;
197 }
198 
Unregister(SensorActiveInfoCB callback)199 int32_t Unregister(SensorActiveInfoCB callback)
200 {
201     int32_t ret = SENSOR_AGENT_IMPL->Unregister(callback);
202     if (ret != OHOS::ERR_OK) {
203         SEN_HILOGE("Unregister active sensor infos callback failed, ret:%{public}d", ret);
204         return NormalizeErrCode(ret);
205     }
206     return ret;
207 }
208 
ResetSensors()209 int32_t ResetSensors()
210 {
211     int32_t ret = SENSOR_AGENT_IMPL->ResetSensors();
212     if (ret != OHOS::ERR_OK) {
213         SEN_HILOGE("Reset sensors failed, ret:%{public}d", ret);
214         return NormalizeErrCode(ret);
215     }
216     return ret;
217 }
218 
SetDeviceStatus(uint32_t deviceStatus)219 void SetDeviceStatus(uint32_t deviceStatus)
220 {
221     SENSOR_AGENT_IMPL->SetDeviceStatus(deviceStatus);
222 }
223 
ActivateSensorEnhanced(const SensorIdentifier & sensorIdentifier,const SensorUser * user)224 int32_t ActivateSensorEnhanced(const SensorIdentifier &sensorIdentifier, const SensorUser *user)
225 {
226     int32_t ret = SENSOR_AGENT_IMPL->ActivateSensor({sensorIdentifier.deviceId, sensorIdentifier.sensorType,
227         sensorIdentifier.sensorId, sensorIdentifier.location}, user);
228     if (ret != OHOS::ERR_OK) {
229         SEN_HILOGE("ActivateSensor failed");
230         return NormalizeErrCode(ret);
231     }
232     return ret;
233 }
234 
DeactivateSensorEnhanced(const SensorIdentifier & sensorIdentifier,const SensorUser * user)235 int32_t DeactivateSensorEnhanced(const SensorIdentifier &sensorIdentifier, const SensorUser *user)
236 {
237     int32_t ret = SENSOR_AGENT_IMPL->DeactivateSensor({sensorIdentifier.deviceId, sensorIdentifier.sensorType,
238         sensorIdentifier.sensorId, sensorIdentifier.location}, user);
239     if (ret != OHOS::ERR_OK) {
240         SEN_HILOGE("DeactivateSensor failed");
241         return NormalizeErrCode(ret);
242     }
243     return ret;
244 }
245 
SetBatchEnhanced(const SensorIdentifier & sensorIdentifier,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval)246 int32_t SetBatchEnhanced(const SensorIdentifier &sensorIdentifier, const SensorUser *user, int64_t samplingInterval,
247     int64_t reportInterval)
248 {
249     int32_t ret = SENSOR_AGENT_IMPL->SetBatch({sensorIdentifier.deviceId, sensorIdentifier.sensorType,
250         sensorIdentifier.sensorId, sensorIdentifier.location}, user, samplingInterval, reportInterval);
251     if (ret != OHOS::ERR_OK) {
252         SEN_HILOGE("SetBatch failed");
253         return NormalizeErrCode(ret);
254     }
255     return ret;
256 }
257 
SubscribeSensorEnhanced(const SensorIdentifier & sensorIdentifier,const SensorUser * user)258 int32_t SubscribeSensorEnhanced(const SensorIdentifier &sensorIdentifier, const SensorUser *user)
259 {
260     int32_t ret = SENSOR_AGENT_IMPL->SubscribeSensor({sensorIdentifier.deviceId, sensorIdentifier.sensorType,
261         sensorIdentifier.sensorId, sensorIdentifier.location}, user);
262     if (ret != OHOS::ERR_OK) {
263         SEN_HILOGE("SubscribeSensor failed");
264         return NormalizeErrCode(ret);
265     }
266     return ret;
267 }
268 
UnsubscribeSensorEnhanced(const SensorIdentifier & sensorIdentifier,const SensorUser * user)269 int32_t UnsubscribeSensorEnhanced(const SensorIdentifier &sensorIdentifier, const SensorUser *user)
270 {
271     int32_t ret = SENSOR_AGENT_IMPL->UnsubscribeSensor({sensorIdentifier.deviceId, sensorIdentifier.sensorType,
272         sensorIdentifier.sensorId, sensorIdentifier.location}, user);
273     if (ret != OHOS::ERR_OK) {
274         SEN_HILOGE("UnsubscribeSensor failed");
275         return NormalizeErrCode(ret);
276     }
277     return ret;
278 }
279 
SetModeEnhanced(const SensorIdentifier & sensorIdentifier,const SensorUser * user,int32_t mode)280 int32_t SetModeEnhanced(const SensorIdentifier &sensorIdentifier, const SensorUser *user, int32_t mode)
281 {
282     return SENSOR_AGENT_IMPL->SetMode({sensorIdentifier.deviceId, sensorIdentifier.sensorType,
283         sensorIdentifier.sensorId, sensorIdentifier.location}, user, mode);
284 }
285 
SubscribeSensorPlug(const SensorUser * user)286 int32_t SubscribeSensorPlug(const SensorUser *user)
287 {
288     int32_t ret = SENSOR_AGENT_IMPL->SubscribeSensorPlug(user);
289     if (ret != OHOS::ERR_OK) {
290         SEN_HILOGE("SubscribeSensorPlug failed");
291         return NormalizeErrCode(ret);
292     }
293     return ret;
294 }
295 
UnsubscribeSensorPlug(const SensorUser * user)296 int32_t UnsubscribeSensorPlug(const SensorUser *user)
297 {
298     int32_t ret = SENSOR_AGENT_IMPL->UnsubscribeSensorPlug(user);
299     if (ret != OHOS::ERR_OK) {
300         SEN_HILOGE("UnSubscribeSensorPlug failed");
301         return NormalizeErrCode(ret);
302     }
303     return ret;
304 }