1 /*
2 * Copyright (c) 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 "oh_sensor.h"
17
18 #include "i_sensor_service.h"
19 #include "native_sensor_impl.h"
20 #include "securec.h"
21 #include "sensor_agent.h"
22 #include "sensor_errors.h"
23
24 #undef LOG_TAG
25 #define LOG_TAG "SensorCapiAPI"
26
27 namespace {
28 const uint32_t FLOAT_SIZE = 4;
29 }
30
OH_Sensor_GetInfos(Sensor_Info ** sensors,uint32_t * count)31 Sensor_Result OH_Sensor_GetInfos(Sensor_Info **sensors, uint32_t *count)
32 {
33 if (count == nullptr) {
34 SEN_HILOGE("Parameter error");
35 return SENSOR_PARAMETER_ERROR;
36 }
37 SensorInfo *sensorInfo = nullptr;
38 int32_t sensorCount = -1;
39 int32_t ret = GetAllSensors(&sensorInfo, &sensorCount);
40 if (ret != SENSOR_SUCCESS || sensorCount < 0) {
41 SEN_HILOGE("GetAllSensors fail");
42 return SENSOR_SERVICE_EXCEPTION;
43 }
44 if (sensors == nullptr) {
45 *count = static_cast<uint32_t>(sensorCount);
46 SEN_HILOGD("Sensor count: %{public}d", *count);
47 return SENSOR_SUCCESS;
48 }
49 if (static_cast<uint32_t>(sensorCount) != *count) {
50 SEN_HILOGE("Count:%{public}d is invalid, should be:%{public}d", *count, sensorCount);
51 return SENSOR_PARAMETER_ERROR;
52 }
53 for (int32_t i = 0; i < sensorCount; ++i) {
54 if (sensors[i] == nullptr) {
55 SEN_HILOGE("Sensor is null, i:%{public}d", i);
56 return SENSOR_PARAMETER_ERROR;
57 }
58 errno_t result = strcpy_s(sensors[i]->sensorName, NAME_MAX_LEN, sensorInfo[i].sensorName);
59 CHKCR(result == EOK, SENSOR_SERVICE_EXCEPTION);
60 result = strcpy_s(sensors[i]->vendorName, NAME_MAX_LEN, sensorInfo[i].vendorName);
61 CHKCR(result == EOK, SENSOR_SERVICE_EXCEPTION);
62 result = strcpy_s(sensors[i]->firmwareVersion, VERSION_MAX_LEN, sensorInfo[i].firmwareVersion);
63 CHKCR(result == EOK, SENSOR_SERVICE_EXCEPTION);
64 result = strcpy_s(sensors[i]->hardwareVersion, VERSION_MAX_LEN, sensorInfo[i].hardwareVersion);
65 CHKCR(result == EOK, SENSOR_SERVICE_EXCEPTION);
66 sensors[i]->sensorTypeId = sensorInfo[i].sensorTypeId;
67 sensors[i]->sensorId = sensorInfo[i].sensorId;
68 sensors[i]->maxRange = sensorInfo[i].maxRange;
69 sensors[i]->precision = sensorInfo[i].precision;
70 sensors[i]->power = sensorInfo[i].power;
71 sensors[i]->minSamplePeriod = sensorInfo[i].minSamplePeriod;
72 sensors[i]->maxSamplePeriod = sensorInfo[i].maxSamplePeriod;
73 }
74 return SENSOR_SUCCESS;
75 }
76
OH_Sensor_CreateInfos(uint32_t count)77 Sensor_Info **OH_Sensor_CreateInfos(uint32_t count)
78 {
79 auto sensors = new Sensor_Info *[count];
80 for (uint32_t i = 0; i < count; ++i) {
81 sensors[i] = new Sensor_Info();
82 }
83 return sensors;
84 }
85
OH_Sensor_DestroyInfos(Sensor_Info ** sensors,uint32_t count)86 int32_t OH_Sensor_DestroyInfos(Sensor_Info **sensors, uint32_t count)
87 {
88 for (uint32_t i = 0; i < count; ++i) {
89 if (sensors[i] != nullptr) {
90 delete sensors[i];
91 sensors[i] = nullptr;
92 }
93 }
94 if (sensors != nullptr) {
95 delete[] sensors;
96 sensors = nullptr;
97 }
98 return SENSOR_SUCCESS;
99 }
100
OH_SensorInfo_GetName(Sensor_Info * sensor,char * sensorName,uint32_t * length)101 int32_t OH_SensorInfo_GetName(Sensor_Info* sensor, char *sensorName, uint32_t *length)
102 {
103 if (sensor == nullptr || sensorName == nullptr || length == nullptr) {
104 SEN_HILOGE("Parameter error");
105 return SENSOR_PARAMETER_ERROR;
106 }
107 uint32_t nameLen = strlen(sensor->sensorName);
108 if (nameLen == 0 || *length <= nameLen) {
109 SEN_HILOGE("Parameter error, length:%{public}d is small, should big than:%{public}d", *length, nameLen);
110 return SENSOR_PARAMETER_ERROR;
111 }
112 errno_t result = strcpy_s(sensorName, *length, sensor->sensorName);
113 if (result != EOK) {
114 SEN_HILOGE("strcpy_s failed, result is %{public}d", result);
115 return SENSOR_SERVICE_EXCEPTION;
116 }
117 return SENSOR_SUCCESS;
118 }
119
OH_SensorInfo_GetVendorName(Sensor_Info * sensor,char * vendorName,uint32_t * length)120 int32_t OH_SensorInfo_GetVendorName(Sensor_Info* sensor, char *vendorName, uint32_t *length)
121 {
122 if (sensor == nullptr || vendorName == nullptr || length == nullptr) {
123 SEN_HILOGE("Parameter error");
124 return SENSOR_PARAMETER_ERROR;
125 }
126 uint32_t nameLen = strlen(sensor->vendorName);
127 if (nameLen == 0 || *length <= nameLen) {
128 SEN_HILOGE("Parameter error, length:%{public}d is small, should big than:%{public}d", *length, nameLen);
129 return SENSOR_PARAMETER_ERROR;
130 }
131 errno_t result = strcpy_s(vendorName, *length, sensor->vendorName);
132 if (result != EOK) {
133 SEN_HILOGE("strcpy_s failed, result is %{public}d", result);
134 return SENSOR_SERVICE_EXCEPTION;
135 }
136 return SENSOR_SUCCESS;
137 }
138
OH_SensorInfo_GetType(Sensor_Info * sensor,Sensor_Type * sensorType)139 int32_t OH_SensorInfo_GetType(Sensor_Info* sensor, Sensor_Type *sensorType)
140 {
141 if (sensor == nullptr || sensorType == nullptr) {
142 SEN_HILOGE("Parameter error");
143 return SENSOR_PARAMETER_ERROR;
144 }
145 *sensorType = static_cast<Sensor_Type>(sensor->sensorTypeId);
146 return SENSOR_SUCCESS;
147 }
148
OH_SensorInfo_GetResolution(Sensor_Info * sensor,float * resolution)149 int32_t OH_SensorInfo_GetResolution(Sensor_Info* sensor, float *resolution)
150 {
151 if (sensor == nullptr || resolution == nullptr) {
152 SEN_HILOGE("Parameter error");
153 return SENSOR_PARAMETER_ERROR;
154 }
155 *resolution = sensor->precision;
156 return SENSOR_SUCCESS;
157 }
158
OH_SensorInfo_GetMinSamplingInterval(Sensor_Info * sensor,int64_t * minSamplePeriod)159 int32_t OH_SensorInfo_GetMinSamplingInterval(Sensor_Info* sensor, int64_t *minSamplePeriod)
160 {
161 if (sensor == nullptr || minSamplePeriod == nullptr) {
162 SEN_HILOGE("Parameter error");
163 return SENSOR_PARAMETER_ERROR;
164 }
165 *minSamplePeriod = sensor->minSamplePeriod;
166 return SENSOR_SUCCESS;
167 }
168
OH_SensorInfo_GetMaxSamplingInterval(Sensor_Info * sensor,int64_t * maxSamplePeriod)169 int32_t OH_SensorInfo_GetMaxSamplingInterval(Sensor_Info* sensor, int64_t *maxSamplePeriod)
170 {
171 if (sensor == nullptr || maxSamplePeriod == nullptr) {
172 SEN_HILOGE("Parameter error");
173 return SENSOR_PARAMETER_ERROR;
174 }
175 *maxSamplePeriod = sensor->maxSamplePeriod;
176 return SENSOR_SUCCESS;
177 }
178
OH_Sensor_Subscribe(const Sensor_SubscriptionId * id,const Sensor_SubscriptionAttribute * attribute,const Sensor_Subscriber * user)179 Sensor_Result OH_Sensor_Subscribe(const Sensor_SubscriptionId *id,
180 const Sensor_SubscriptionAttribute *attribute, const Sensor_Subscriber *user)
181 {
182 if (id == nullptr || attribute == nullptr || user == nullptr) {
183 SEN_HILOGE("Parameter error");
184 return SENSOR_PARAMETER_ERROR;
185 }
186 const SensorUser *sensorUser = reinterpret_cast<const SensorUser *>(user);
187 int32_t sensorType = id->sensorType;
188 int32_t ret = SubscribeSensor(sensorType, sensorUser);
189 if (ret != SENSOR_SUCCESS) {
190 SEN_HILOGE("SubscribeSensor failed, %{public}d", ret);
191 return SENSOR_SERVICE_EXCEPTION;
192 }
193 int64_t samplingInterval = attribute->samplingInterval;
194 ret = SetBatch(sensorType, sensorUser, samplingInterval, samplingInterval);
195 if (ret != SENSOR_SUCCESS) {
196 SEN_HILOGE("SetBatch failed, %{public}d", ret);
197 return SENSOR_SERVICE_EXCEPTION;
198 }
199 return static_cast<Sensor_Result>(ActivateSensor(sensorType, sensorUser));
200 }
201
OH_Sensor_Unsubscribe(const Sensor_SubscriptionId * id,const Sensor_Subscriber * user)202 Sensor_Result OH_Sensor_Unsubscribe(const Sensor_SubscriptionId *id,
203 const Sensor_Subscriber *user)
204 {
205 if (id == nullptr || user == nullptr) {
206 SEN_HILOGE("Parameter error");
207 return SENSOR_PARAMETER_ERROR;
208 }
209 const SensorUser *sensorUser = reinterpret_cast<const SensorUser *>(user);
210 int32_t sensorType = id->sensorType;
211 int32_t ret = DeactivateSensor(sensorType, sensorUser);
212 if (ret != SENSOR_SUCCESS) {
213 SEN_HILOGE("SetBatch failed, %{public}d", ret);
214 return SENSOR_SERVICE_EXCEPTION;
215 }
216 return static_cast<Sensor_Result>(UnsubscribeSensor(sensorType, sensorUser));
217 }
218
OH_SensorEvent_GetType(Sensor_Event * sensorEvent,Sensor_Type * sensorType)219 int32_t OH_SensorEvent_GetType(Sensor_Event* sensorEvent, Sensor_Type *sensorType)
220 {
221 if (sensorEvent == nullptr || sensorType == nullptr) {
222 SEN_HILOGE("Parameter error");
223 return SENSOR_PARAMETER_ERROR;
224 }
225 *sensorType = static_cast<Sensor_Type>(sensorEvent->sensorTypeId);
226 return SENSOR_SUCCESS;
227 }
228
OH_SensorEvent_GetTimestamp(Sensor_Event * sensorEvent,int64_t * timestamp)229 int32_t OH_SensorEvent_GetTimestamp(Sensor_Event* sensorEvent, int64_t *timestamp)
230 {
231 if (sensorEvent == nullptr || timestamp == nullptr) {
232 SEN_HILOGE("Parameter error");
233 return SENSOR_PARAMETER_ERROR;
234 }
235 *timestamp = sensorEvent->timestamp;
236 return SENSOR_SUCCESS;
237 }
238
OH_SensorEvent_GetAccuracy(Sensor_Event * sensorEvent,Sensor_Accuracy * accuracy)239 int32_t OH_SensorEvent_GetAccuracy(Sensor_Event* sensorEvent, Sensor_Accuracy *accuracy)
240 {
241 if (sensorEvent == nullptr || accuracy == nullptr) {
242 SEN_HILOGE("Parameter error");
243 return SENSOR_PARAMETER_ERROR;
244 }
245 *accuracy = static_cast<Sensor_Accuracy>(sensorEvent->option);
246 return SENSOR_SUCCESS;
247 }
248
OH_SensorEvent_GetData(Sensor_Event * sensorEvent,float ** data,uint32_t * length)249 int32_t OH_SensorEvent_GetData(Sensor_Event* sensorEvent, float **data, uint32_t *length)
250 {
251 if (sensorEvent == nullptr || data == nullptr || length == nullptr) {
252 SEN_HILOGE("Parameter error");
253 return SENSOR_PARAMETER_ERROR;
254 }
255 *data = reinterpret_cast<float *>(sensorEvent->data);
256 *length = sensorEvent->dataLen / FLOAT_SIZE;
257 return SENSOR_SUCCESS;
258 }
259
OH_SensorSubscriptionId_GetType(Sensor_SubscriptionId * id,Sensor_Type * sensorType)260 int32_t OH_SensorSubscriptionId_GetType(Sensor_SubscriptionId* id, Sensor_Type *sensorType)
261 {
262 if (id == nullptr || sensorType == nullptr) {
263 SEN_HILOGE("Parameter error");
264 return SENSOR_PARAMETER_ERROR;
265 }
266 *sensorType = static_cast<Sensor_Type>(id->sensorType);
267 return SENSOR_SUCCESS;
268 }
269
OH_SensorSubscriptionId_SetType(Sensor_SubscriptionId * id,const Sensor_Type sensorType)270 int32_t OH_SensorSubscriptionId_SetType(Sensor_SubscriptionId* id, const Sensor_Type sensorType)
271 {
272 if (id == nullptr) {
273 SEN_HILOGE("Parameter error");
274 return SENSOR_PARAMETER_ERROR;
275 }
276 id->sensorType = sensorType;
277 return SENSOR_SUCCESS;
278 }
279
OH_SensorSubscriptionAttribute_SetSamplingInterval(Sensor_SubscriptionAttribute * attribute,const int64_t samplingInterval)280 int32_t OH_SensorSubscriptionAttribute_SetSamplingInterval(Sensor_SubscriptionAttribute* attribute,
281 const int64_t samplingInterval)
282 {
283 if (attribute == nullptr || samplingInterval < 0) {
284 SEN_HILOGE("Parameter error");
285 return SENSOR_PARAMETER_ERROR;
286 }
287 attribute->samplingInterval = samplingInterval;
288 return SENSOR_SUCCESS;
289 }
290
OH_SensorSubscriptionAttribute_GetSamplingInterval(Sensor_SubscriptionAttribute * attribute,int64_t * samplingInterval)291 int32_t OH_SensorSubscriptionAttribute_GetSamplingInterval(Sensor_SubscriptionAttribute* attribute,
292 int64_t *samplingInterval)
293 {
294 if (attribute == nullptr || samplingInterval == nullptr) {
295 SEN_HILOGE("Parameter error");
296 return SENSOR_PARAMETER_ERROR;
297 }
298 *samplingInterval = attribute->samplingInterval;
299 return SENSOR_SUCCESS;
300 }
301
OH_SensorSubscriber_SetCallback(Sensor_Subscriber * user,const Sensor_EventCallback callback)302 int32_t OH_SensorSubscriber_SetCallback(Sensor_Subscriber* user, const Sensor_EventCallback callback)
303 {
304 if (user == nullptr || callback == nullptr) {
305 SEN_HILOGE("Parameter error");
306 return SENSOR_PARAMETER_ERROR;
307 }
308 user->callback = callback;
309 return SENSOR_SUCCESS;
310 }
311
OH_SensorSubscriber_GetCallback(Sensor_Subscriber * user,Sensor_EventCallback * callback)312 int32_t OH_SensorSubscriber_GetCallback(Sensor_Subscriber* user, Sensor_EventCallback *callback)
313 {
314 if (user == nullptr || callback == nullptr) {
315 SEN_HILOGE("Parameter error");
316 return SENSOR_PARAMETER_ERROR;
317 }
318 *callback = user->callback;
319 return SENSOR_SUCCESS;
320 }
321
OH_Sensor_CreateSubscriptionId()322 Sensor_SubscriptionId *OH_Sensor_CreateSubscriptionId()
323 {
324 return new (std::nothrow) Sensor_SubscriptionId();
325 }
326
OH_Sensor_DestroySubscriptionId(Sensor_SubscriptionId * id)327 int32_t OH_Sensor_DestroySubscriptionId(Sensor_SubscriptionId *id)
328 {
329 if (id == nullptr) {
330 SEN_HILOGE("Parameter error");
331 return SENSOR_PARAMETER_ERROR;
332 }
333 delete id;
334 id = nullptr;
335 return SENSOR_SUCCESS;
336 }
337
OH_Sensor_CreateSubscriptionAttribute()338 Sensor_SubscriptionAttribute *OH_Sensor_CreateSubscriptionAttribute()
339 {
340 return new (std::nothrow) Sensor_SubscriptionAttribute();
341 }
342
OH_Sensor_DestroySubscriptionAttribute(Sensor_SubscriptionAttribute * attribute)343 int32_t OH_Sensor_DestroySubscriptionAttribute(Sensor_SubscriptionAttribute *attribute)
344 {
345 if (attribute == nullptr) {
346 SEN_HILOGE("Parameter error");
347 return SENSOR_PARAMETER_ERROR;
348 }
349 delete attribute;
350 attribute = nullptr;
351 return SENSOR_SUCCESS;
352 }
353
OH_Sensor_CreateSubscriber()354 Sensor_Subscriber *OH_Sensor_CreateSubscriber()
355 {
356 return new (std::nothrow) Sensor_Subscriber();
357 }
358
OH_Sensor_DestroySubscriber(Sensor_Subscriber * user)359 int32_t OH_Sensor_DestroySubscriber(Sensor_Subscriber *user)
360 {
361 if (user == nullptr) {
362 SEN_HILOGE("Parameter error");
363 return SENSOR_PARAMETER_ERROR;
364 }
365 delete user;
366 user = nullptr;
367 return SENSOR_SUCCESS;
368 }