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_controller.h"
17 #include <fcntl.h>
18 #include <inttypes.h>
19 #include <securec.h>
20 #include "osal_mem.h"
21 #include "osal_mutex.h"
22 #include "sensor_channel.h"
23 #include "sensor_common.h"
24 #include "sensor_if.h"
25 #include "sensor_manager.h"
26
27 #define HDF_LOG_TAG uhdf_sensor
28
29 #define HDF_SENSOR_INFO_MAX_SIZE (4 * 1024) // 4kB
30
ReleaseAllSensorInfo(void)31 void ReleaseAllSensorInfo(void)
32 {
33 struct SensorDevManager *manager = GetSensorDevManager();
34 struct SensorIdListNode *pos = NULL;
35 struct SensorIdListNode *tmp = NULL;
36
37 DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &manager->sensorIdListHead, struct SensorIdListNode, node) {
38 DListRemove(&(pos->node));
39 OsalMemFree(pos);
40 }
41 if (manager->sensorInfoEntry != NULL) {
42 OsalMemFree(manager->sensorInfoEntry);
43 manager->sensorInfoEntry = NULL;
44 }
45 manager->sensorSum = 0;
46 }
47
SetSensorIdClassification(void)48 static int32_t SetSensorIdClassification(void)
49 {
50 struct SensorDevManager *manager = GetSensorDevManager();
51 struct SensorManagerNode *pos = NULL;
52 int32_t begin = 0;
53 int32_t end;
54 struct SensorIdListNode *sensorIdNode = NULL;
55 CHECK_NULL_PTR_RETURN_VALUE(manager->sensorInfoEntry, SENSOR_NULL_PTR);
56
57 DLIST_FOR_EACH_ENTRY(pos, &manager->managerHead, struct SensorManagerNode, node) {
58 end = begin + pos->sensorCount;
59 if (end > manager->sensorSum) {
60 break;
61 }
62
63 for (int i = begin; i < end; i++) {
64 sensorIdNode = (struct SensorIdListNode*)OsalMemCalloc(sizeof(*sensorIdNode));
65 CHECK_NULL_PTR_RETURN_VALUE(sensorIdNode, SENSOR_NULL_PTR);
66 sensorIdNode->ioService = pos->ioService;
67 sensorIdNode->sensorId = manager->sensorInfoEntry[i].sensorId;
68 SetSensorIdBySensorType(manager->sensorInfoEntry[i].sensorTypeId, manager->sensorInfoEntry[i].sensorId);
69 if (sensorIdNode->sensorId == SENSOR_TYPE_ACCELEROMETER) {
70 manager->sensorInfoEntry[i].maxRange = manager->sensorInfoEntry[i].maxRange * HDI_SENSOR_GRAVITY;
71 manager->sensorInfoEntry[i].accuracy = HDI_SENSOR_GRAVITY / HDI_SENSOR_ACCEL_LSB / HDI_SENSOR_UNITS;
72 manager->sensorInfoEntry[i].power = manager->sensorInfoEntry[i].power / HDI_SENSOR_UNITS;
73 }
74 DListInsertTail(&sensorIdNode->node, &manager->sensorIdListHead);
75 }
76 begin = end;
77 }
78
79 return HDF_SUCCESS;
80 }
81
GetSensorInfoFromReply(struct HdfSBuf * reply)82 static int32_t GetSensorInfoFromReply(struct HdfSBuf *reply)
83 {
84 struct SensorDevManager *manager = GetSensorDevManager();
85 struct SensorInformation *pos = NULL;
86 struct SensorBasicInformation *buf = NULL;
87 int32_t count = manager->sensorSum;
88 uint32_t len;
89
90 if (manager->sensorInfoEntry != NULL) {
91 OsalMemFree(manager->sensorInfoEntry);
92 manager->sensorInfoEntry = NULL;
93 }
94
95 manager->sensorInfoEntry = (struct SensorInformation *)OsalMemCalloc(sizeof(*manager->sensorInfoEntry) * count);
96 if (manager->sensorInfoEntry == NULL) {
97 HDF_LOGE("%{public}s: Sensor info malloc failed", __func__);
98 return SENSOR_FAILURE;
99 }
100
101 pos = manager->sensorInfoEntry;
102 size_t preLen = sizeof(*manager->sensorInfoEntry) -
103 (sizeof(pos->maxRange) + sizeof(pos->accuracy) + sizeof(pos->power));
104
105 for (int32_t i = 0; i < count; i++) {
106 if (!HdfSbufReadBuffer(reply, (const void **)&buf, &len) || buf == NULL) {
107 HDF_LOGE("%{public}s: Sensor read reply info failed", __func__);
108 break;
109 }
110
111 if (memcpy_s(pos, sizeof(*pos), (void *)buf, preLen) != EOK) {
112 HDF_LOGE("%{public}s: Sensor copy reply info failed", __func__);
113 goto ERROR;
114 }
115 pos->maxRange = (float)(buf->maxRange);
116 pos->accuracy = (float)(buf->accuracy);
117 pos->power = (float)(buf->power);
118 pos->minDelay = (int64_t)(buf->minDelay);
119 pos->maxDelay = (int64_t)(buf->maxDelay);
120 pos++;
121 }
122
123 if (SetSensorIdClassification() != SENSOR_SUCCESS) {
124 HDF_LOGE("%{public}s: Sensor id Classification failed", __func__);
125 goto ERROR;
126 }
127 return SENSOR_SUCCESS;
128
129 ERROR:
130 ReleaseAllSensorInfo();
131 return SENSOR_FAILURE;
132 }
133
GetSensorNumByManagerType(struct HdfSBuf * reply)134 static int32_t GetSensorNumByManagerType(struct HdfSBuf *reply)
135 {
136 struct SensorDevManager *manager = GetSensorDevManager();
137 int32_t count = (int32_t)(HdfSbufGetDataSize(reply) / sizeof(struct SensorBasicInformation));
138
139 return ((count > manager->sensorSum) ? (count - manager->sensorSum) : 0);
140 }
141
GetSensorInfo(struct SensorInformation ** sensor,int32_t * count)142 static int32_t GetSensorInfo(struct SensorInformation **sensor, int32_t *count)
143 {
144 struct SensorDevManager *manager = GetSensorDevManager();
145 CHECK_NULL_PTR_RETURN_VALUE(sensor, SENSOR_NULL_PTR);
146 CHECK_NULL_PTR_RETURN_VALUE(count, SENSOR_NULL_PTR);
147
148 if (manager->sensorSum > 0) {
149 *count = manager->sensorSum;
150 *sensor = manager->sensorInfoEntry;
151 return SENSOR_SUCCESS;
152 }
153
154 struct HdfSBuf *reply = HdfSbufObtain(HDF_SENSOR_INFO_MAX_SIZE);
155 CHECK_NULL_PTR_RETURN_VALUE(reply, SENSOR_NULL_PTR);
156
157 (void)OsalMutexLock(&manager->mutex);
158 manager->sensorSum = 0;
159 struct SensorManagerNode *pos = NULL;
160 DLIST_FOR_EACH_ENTRY(pos, &manager->managerHead, struct SensorManagerNode, node) {
161 if (manager->sensorSum >= SENSOR_TYPE_MAX) {
162 break;
163 }
164 pos->sensorCount = 0;
165 if ((pos->ioService == NULL) || (pos->ioService->dispatcher == NULL) ||
166 (pos->ioService->dispatcher->Dispatch == NULL)) {
167 HDF_LOGE("%{public}s: Sensor pos para failed", __func__);
168 continue;
169 }
170
171 int32_t ret = pos->ioService->dispatcher->Dispatch(&pos->ioService->object,
172 SENSOR_IO_CMD_GET_INFO_LIST, NULL, reply);
173 if (ret != SENSOR_SUCCESS) {
174 HDF_LOGE("%{public}s: Sensor dispatch info failed[%{public}d]", __func__, ret);
175 break;
176 }
177
178 pos->sensorCount = GetSensorNumByManagerType(reply);
179 manager->sensorSum += pos->sensorCount;
180 }
181
182 if (manager->sensorSum == 0) {
183 HdfSbufRecycle(reply);
184 (void)OsalMutexUnlock(&manager->mutex);
185 return SENSOR_FAILURE;
186 }
187
188 if (GetSensorInfoFromReply(reply) != SENSOR_SUCCESS) {
189 HdfSbufRecycle(reply);
190 (void)OsalMutexUnlock(&manager->mutex);
191 HDF_LOGE("%{public}s: Sensor get info from reply failed", __func__);
192 return SENSOR_FAILURE;
193 }
194
195 HdfSbufRecycle(reply);
196 (void)OsalMutexUnlock(&manager->mutex);
197
198 *count = manager->sensorSum;
199 *sensor = manager->sensorInfoEntry;
200 return SENSOR_SUCCESS;
201 }
202
GetSensorServiceBySensorId(int32_t sensorId)203 static struct HdfIoService *GetSensorServiceBySensorId(int32_t sensorId)
204 {
205 struct SensorDevManager *manager = GetSensorDevManager();
206 struct SensorIdListNode *sensorIdPos = NULL;
207
208 DLIST_FOR_EACH_ENTRY(sensorIdPos, &manager->sensorIdListHead, struct SensorIdListNode, node) {
209 if (sensorIdPos->sensorId == sensorId) {
210 return sensorIdPos->ioService;
211 }
212 }
213 return NULL;
214 }
215
SendSensorMsg(int32_t sensorId,struct HdfSBuf * msg,struct HdfSBuf * reply)216 static int32_t SendSensorMsg(int32_t sensorId, struct HdfSBuf *msg, struct HdfSBuf *reply)
217 {
218 struct HdfIoService *ioService = NULL;
219
220 ioService = GetSensorServiceBySensorId(sensorId);
221 CHECK_NULL_PTR_RETURN_VALUE(ioService, SENSOR_NOT_SUPPORT);
222 CHECK_NULL_PTR_RETURN_VALUE(ioService->dispatcher, SENSOR_NULL_PTR);
223 CHECK_NULL_PTR_RETURN_VALUE(ioService->dispatcher->Dispatch, SENSOR_NULL_PTR);
224
225 int32_t ret = ioService->dispatcher->Dispatch(&ioService->object, SENSOR_IO_CMD_OPS, msg, reply);
226 if (ret != SENSOR_SUCCESS) {
227 HDF_LOGE("%{public}s: Sensor dispatch failed", __func__);
228 return ret;
229 }
230 return SENSOR_SUCCESS;
231 }
232
EnableSensor(int32_t sensorId)233 static int32_t EnableSensor(int32_t sensorId)
234 {
235 struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
236 if (msg == NULL) {
237 HDF_LOGE("%{public}s: Failed to obtain sBuf size", __func__);
238 return SENSOR_FAILURE;
239 }
240
241 if (!HdfSbufWriteInt32(msg, sensorId)) {
242 HDF_LOGE("%{public}s: Sensor write id failed", __func__);
243 HdfSbufRecycle(msg);
244 return SENSOR_FAILURE;
245 }
246
247 if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_ENABLE)) {
248 HDF_LOGE("%{public}s: Sensor write enable failed", __func__);
249 HdfSbufRecycle(msg);
250 return SENSOR_FAILURE;
251 }
252
253 int32_t ret = SendSensorMsg(sensorId, msg, NULL);
254 if (ret != SENSOR_SUCCESS) {
255 HDF_LOGE("%{public}s: Sensor enable failed, ret[%{public}d]", __func__, ret);
256 }
257 HdfSbufRecycle(msg);
258
259 return ret;
260 }
261
DisableSensor(int32_t sensorId)262 static int32_t DisableSensor(int32_t sensorId)
263 {
264 struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
265 if (msg == NULL) {
266 HDF_LOGE("%{public}s: Failed to obtain sBuf", __func__);
267 return SENSOR_FAILURE;
268 }
269
270 if (!HdfSbufWriteInt32(msg, sensorId)) {
271 HDF_LOGE("%{public}s: Sensor write id failed", __func__);
272 HdfSbufRecycle(msg);
273 return SENSOR_FAILURE;
274 }
275
276 if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_DISABLE)) {
277 HDF_LOGE("%{public}s: Sensor write disable failed", __func__);
278 HdfSbufRecycle(msg);
279 return SENSOR_FAILURE;
280 }
281
282 int32_t ret = SendSensorMsg(sensorId, msg, NULL);
283 if (ret != SENSOR_SUCCESS) {
284 HDF_LOGE("%{public}s: Sensor disable failed, ret[%{public}d]", __func__, ret);
285 }
286 HdfSbufRecycle(msg);
287
288 return ret;
289 }
290
SetSensorBatch(int32_t sensorId,int64_t samplingInterval,int64_t interval)291 static int32_t SetSensorBatch(int32_t sensorId, int64_t samplingInterval, int64_t interval)
292 {
293 if (interval < 0) {
294 HDF_LOGE("%{public}s: invalid param , interval is [%{public}" PRId64 "]", __func__, interval);
295 return SENSOR_INVALID_PARAM;
296 }
297
298 if (samplingInterval < 0) {
299 HDF_LOGE("%{public}s: invalid param , samplingInterval is [%{public}" PRId64 "]", __func__, samplingInterval);
300 return SENSOR_INVALID_PARAM;
301 }
302
303 struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
304 CHECK_NULL_PTR_RETURN_VALUE(msg, SENSOR_FAILURE);
305
306 if (!HdfSbufWriteInt32(msg, sensorId)) {
307 HDF_LOGE("%{public}s: Sensor write id failed", __func__);
308 HdfSbufRecycle(msg);
309 return SENSOR_FAILURE;
310 }
311
312 if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_SET_BATCH) || !HdfSbufWriteInt64(msg, samplingInterval) ||
313 !HdfSbufWriteInt64(msg, interval)) {
314 HDF_LOGE("%{public}s: Sensor write failed", __func__);
315 HdfSbufRecycle(msg);
316 return SENSOR_FAILURE;
317 }
318
319 int32_t ret = SendSensorMsg(sensorId, msg, NULL);
320 if (ret != SENSOR_SUCCESS) {
321 HDF_LOGE("%{public}s: Sensor set batch failed, ret[%{public}d]", __func__, ret);
322 }
323 HdfSbufRecycle(msg);
324
325 return ret;
326 }
327
SetSensorMode(int32_t sensorId,int32_t mode)328 static int32_t SetSensorMode(int32_t sensorId, int32_t mode)
329 {
330 struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
331 CHECK_NULL_PTR_RETURN_VALUE(msg, SENSOR_FAILURE);
332
333 if (!HdfSbufWriteInt32(msg, sensorId)) {
334 HDF_LOGE("%{public}s: Sensor write id failed", __func__);
335 HdfSbufRecycle(msg);
336 return SENSOR_FAILURE;
337 }
338
339 if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_SET_MODE) || !HdfSbufWriteInt32(msg, mode)) {
340 HDF_LOGE("%{public}s: Sensor write failed", __func__);
341 HdfSbufRecycle(msg);
342 return SENSOR_FAILURE;
343 }
344
345 int32_t ret = SendSensorMsg(sensorId, msg, NULL);
346 if (ret != SENSOR_SUCCESS) {
347 HDF_LOGE("%{public}s: Sensor set mode failed, ret[%{public}d]", __func__, ret);
348 }
349 HdfSbufRecycle(msg);
350
351 return ret;
352 }
353
SetSensorOption(int32_t sensorId,uint32_t option)354 static int32_t SetSensorOption(int32_t sensorId, uint32_t option)
355 {
356 struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
357 CHECK_NULL_PTR_RETURN_VALUE(msg, SENSOR_FAILURE);
358
359 if (!HdfSbufWriteInt32(msg, sensorId)) {
360 HDF_LOGE("%{public}s: Sensor write id failed", __func__);
361 HdfSbufRecycle(msg);
362 return SENSOR_FAILURE;
363 }
364
365 if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_SET_OPTION) || !HdfSbufWriteUint32(msg, option)) {
366 HDF_LOGE("%{public}s: Sensor write failed", __func__);
367 HdfSbufRecycle(msg);
368 return SENSOR_FAILURE;
369 }
370
371 int32_t ret = SendSensorMsg(sensorId, msg, NULL);
372 if (ret != SENSOR_SUCCESS) {
373 HDF_LOGE("%{public}s: Sensor set option failed, ret[%{public}d]", __func__, ret);
374 }
375 HdfSbufRecycle(msg);
376
377 return ret;
378 }
379
GetSensorDeviceMethods(struct SensorInterface * device)380 void GetSensorDeviceMethods(struct SensorInterface *device)
381 {
382 CHECK_NULL_PTR_RETURN(device);
383 device->GetAllSensors = GetSensorInfo;
384 device->Enable = EnableSensor;
385 device->Disable = DisableSensor;
386 device->SetBatch = SetSensorBatch;
387 device->SetMode = SetSensorMode;
388 device->SetOption = SetSensorOption;
389 device->Register = Register;
390 device->Unregister = Unregister;
391 }
392