1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include "sensor_controller.h" 17094332d3Sopenharmony_ci#include <fcntl.h> 18094332d3Sopenharmony_ci#include <inttypes.h> 19094332d3Sopenharmony_ci#include <securec.h> 20094332d3Sopenharmony_ci#include "osal_mem.h" 21094332d3Sopenharmony_ci#include "osal_mutex.h" 22094332d3Sopenharmony_ci#include "sensor_channel.h" 23094332d3Sopenharmony_ci#include "sensor_common.h" 24094332d3Sopenharmony_ci#include "sensor_dump.h" 25094332d3Sopenharmony_ci#include "sensor_if.h" 26094332d3Sopenharmony_ci#include "sensor_manager.h" 27094332d3Sopenharmony_ci 28094332d3Sopenharmony_ci#define HDF_LOG_TAG uhdf_sensor_service 29094332d3Sopenharmony_ci 30094332d3Sopenharmony_ci#define HDF_SENSOR_INFO_MAX_SIZE (4 * 1024) // 4kB 31094332d3Sopenharmony_ci#define SENSOR_STATUS_ENABLE 1 32094332d3Sopenharmony_ci#define SENSOR_STATUS_DISENABLE 0 33094332d3Sopenharmony_ci#define HDF_SENSOR_EVENT_MAX_SIZE (4 * 1024) // 4kB 34094332d3Sopenharmony_ci 35094332d3Sopenharmony_cistatic int32_t sensorStatusList[SENSOR_TYPE_MAX] = { 0 }; 36094332d3Sopenharmony_ciint32_t *GetSensorStatus(void) 37094332d3Sopenharmony_ci{ 38094332d3Sopenharmony_ci return sensorStatusList; 39094332d3Sopenharmony_ci} 40094332d3Sopenharmony_ci 41094332d3Sopenharmony_civoid ReleaseAllSensorInfo(void) 42094332d3Sopenharmony_ci{ 43094332d3Sopenharmony_ci struct SensorDevManager *manager = GetSensorDevManager(); 44094332d3Sopenharmony_ci struct SensorIdListNode *pos = NULL; 45094332d3Sopenharmony_ci struct SensorIdListNode *tmp = NULL; 46094332d3Sopenharmony_ci 47094332d3Sopenharmony_ci DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &manager->sensorIdListHead, struct SensorIdListNode, node) { 48094332d3Sopenharmony_ci DListRemove(&(pos->node)); 49094332d3Sopenharmony_ci OsalMemFree(pos); 50094332d3Sopenharmony_ci } 51094332d3Sopenharmony_ci if (manager->sensorInfoEntry != NULL) { 52094332d3Sopenharmony_ci OsalMemFree(manager->sensorInfoEntry); 53094332d3Sopenharmony_ci manager->sensorInfoEntry = NULL; 54094332d3Sopenharmony_ci } 55094332d3Sopenharmony_ci manager->sensorSum = 0; 56094332d3Sopenharmony_ci} 57094332d3Sopenharmony_ci 58094332d3Sopenharmony_cistatic int32_t SetSensorIdClassification(void) 59094332d3Sopenharmony_ci{ 60094332d3Sopenharmony_ci struct SensorDevManager *manager = GetSensorDevManager(); 61094332d3Sopenharmony_ci struct SensorManagerNode *pos = NULL; 62094332d3Sopenharmony_ci int32_t begin = 0; 63094332d3Sopenharmony_ci int32_t end; 64094332d3Sopenharmony_ci struct SensorIdListNode *sensorIdNode = NULL; 65094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(manager->sensorInfoEntry, SENSOR_NULL_PTR); 66094332d3Sopenharmony_ci 67094332d3Sopenharmony_ci DLIST_FOR_EACH_ENTRY(pos, &manager->managerHead, struct SensorManagerNode, node) { 68094332d3Sopenharmony_ci end = begin + pos->sensorCount; 69094332d3Sopenharmony_ci if (end > manager->sensorSum) { 70094332d3Sopenharmony_ci break; 71094332d3Sopenharmony_ci } 72094332d3Sopenharmony_ci 73094332d3Sopenharmony_ci for (int i = begin; i < end; i++) { 74094332d3Sopenharmony_ci sensorIdNode = (struct SensorIdListNode*)OsalMemCalloc(sizeof(*sensorIdNode)); 75094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(sensorIdNode, SENSOR_NULL_PTR); 76094332d3Sopenharmony_ci sensorIdNode->ioService = pos->ioService; 77094332d3Sopenharmony_ci sensorIdNode->sensorId = manager->sensorInfoEntry[i].sensorId; 78094332d3Sopenharmony_ci SetSensorIdBySensorType(manager->sensorInfoEntry[i].sensorTypeId, manager->sensorInfoEntry[i].sensorId); 79094332d3Sopenharmony_ci if (sensorIdNode->sensorId == SENSOR_TYPE_ACCELEROMETER) { 80094332d3Sopenharmony_ci manager->sensorInfoEntry[i].maxRange = manager->sensorInfoEntry[i].maxRange * HDI_SENSOR_GRAVITY; 81094332d3Sopenharmony_ci manager->sensorInfoEntry[i].accuracy = HDI_SENSOR_GRAVITY / HDI_SENSOR_ACCEL_LSB / HDI_SENSOR_UNITS; 82094332d3Sopenharmony_ci manager->sensorInfoEntry[i].power = manager->sensorInfoEntry[i].power / HDI_SENSOR_UNITS; 83094332d3Sopenharmony_ci } 84094332d3Sopenharmony_ci DListInsertTail(&sensorIdNode->node, &manager->sensorIdListHead); 85094332d3Sopenharmony_ci } 86094332d3Sopenharmony_ci begin = end; 87094332d3Sopenharmony_ci } 88094332d3Sopenharmony_ci 89094332d3Sopenharmony_ci return HDF_SUCCESS; 90094332d3Sopenharmony_ci} 91094332d3Sopenharmony_ci 92094332d3Sopenharmony_cistatic int32_t GetSensorInfoFromReply(struct HdfSBuf *reply) 93094332d3Sopenharmony_ci{ 94094332d3Sopenharmony_ci struct SensorDevManager *manager = GetSensorDevManager(); 95094332d3Sopenharmony_ci struct SensorInformation *pos = NULL; 96094332d3Sopenharmony_ci struct SensorBasicInformation *buf = NULL; 97094332d3Sopenharmony_ci int32_t count = manager->sensorSum; 98094332d3Sopenharmony_ci uint32_t len; 99094332d3Sopenharmony_ci 100094332d3Sopenharmony_ci if (manager->sensorInfoEntry != NULL) { 101094332d3Sopenharmony_ci OsalMemFree(manager->sensorInfoEntry); 102094332d3Sopenharmony_ci manager->sensorInfoEntry = NULL; 103094332d3Sopenharmony_ci } 104094332d3Sopenharmony_ci 105094332d3Sopenharmony_ci manager->sensorInfoEntry = (struct SensorInformation *)OsalMemCalloc(sizeof(*manager->sensorInfoEntry) * count); 106094332d3Sopenharmony_ci if (manager->sensorInfoEntry == NULL) { 107094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor info malloc failed", __func__); 108094332d3Sopenharmony_ci return SENSOR_FAILURE; 109094332d3Sopenharmony_ci } 110094332d3Sopenharmony_ci 111094332d3Sopenharmony_ci pos = manager->sensorInfoEntry; 112094332d3Sopenharmony_ci size_t preLen = sizeof(*manager->sensorInfoEntry) - 113094332d3Sopenharmony_ci (sizeof(pos->maxRange) + sizeof(pos->accuracy) + sizeof(pos->power)); 114094332d3Sopenharmony_ci 115094332d3Sopenharmony_ci for (int32_t i = 0; i < count; i++) { 116094332d3Sopenharmony_ci if (!HdfSbufReadBuffer(reply, (const void **)&buf, &len) || buf == NULL) { 117094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor read reply info failed", __func__); 118094332d3Sopenharmony_ci break; 119094332d3Sopenharmony_ci } 120094332d3Sopenharmony_ci 121094332d3Sopenharmony_ci if (memcpy_s(pos, sizeof(*pos), (void *)buf, preLen) != EOK) { 122094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor copy reply info failed", __func__); 123094332d3Sopenharmony_ci goto ERROR; 124094332d3Sopenharmony_ci } 125094332d3Sopenharmony_ci pos->maxRange = (float)(buf->maxRange); 126094332d3Sopenharmony_ci pos->accuracy = (float)(buf->accuracy); 127094332d3Sopenharmony_ci pos->power = (float)(buf->power); 128094332d3Sopenharmony_ci pos->minDelay = (int64_t)(buf->minDelay); 129094332d3Sopenharmony_ci pos->maxDelay = (int64_t)(buf->maxDelay); 130094332d3Sopenharmony_ci pos++; 131094332d3Sopenharmony_ci } 132094332d3Sopenharmony_ci 133094332d3Sopenharmony_ci if (SetSensorIdClassification() != SENSOR_SUCCESS) { 134094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor id Classification failed", __func__); 135094332d3Sopenharmony_ci goto ERROR; 136094332d3Sopenharmony_ci } 137094332d3Sopenharmony_ci return SENSOR_SUCCESS; 138094332d3Sopenharmony_ci 139094332d3Sopenharmony_ciERROR: 140094332d3Sopenharmony_ci ReleaseAllSensorInfo(); 141094332d3Sopenharmony_ci return SENSOR_FAILURE; 142094332d3Sopenharmony_ci} 143094332d3Sopenharmony_ci 144094332d3Sopenharmony_cistatic int32_t GetSensorNumByManagerType(struct HdfSBuf *reply) 145094332d3Sopenharmony_ci{ 146094332d3Sopenharmony_ci struct SensorDevManager *manager = GetSensorDevManager(); 147094332d3Sopenharmony_ci int32_t count = (int32_t)(HdfSbufGetDataSize(reply) / sizeof(struct SensorBasicInformation)); 148094332d3Sopenharmony_ci 149094332d3Sopenharmony_ci return ((count > manager->sensorSum) ? (count - manager->sensorSum) : 0); 150094332d3Sopenharmony_ci} 151094332d3Sopenharmony_ci 152094332d3Sopenharmony_cistatic int32_t GetSensorInfo(struct SensorInformation **sensor, int32_t *count) 153094332d3Sopenharmony_ci{ 154094332d3Sopenharmony_ci struct SensorDevManager *manager = GetSensorDevManager(); 155094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(sensor, SENSOR_NULL_PTR); 156094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(count, SENSOR_NULL_PTR); 157094332d3Sopenharmony_ci 158094332d3Sopenharmony_ci if (manager->sensorSum > 0) { 159094332d3Sopenharmony_ci *count = manager->sensorSum; 160094332d3Sopenharmony_ci *sensor = manager->sensorInfoEntry; 161094332d3Sopenharmony_ci return SENSOR_SUCCESS; 162094332d3Sopenharmony_ci } 163094332d3Sopenharmony_ci 164094332d3Sopenharmony_ci struct HdfSBuf *reply = HdfSbufObtain(HDF_SENSOR_INFO_MAX_SIZE); 165094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(reply, SENSOR_NULL_PTR); 166094332d3Sopenharmony_ci 167094332d3Sopenharmony_ci (void)OsalMutexLock(&manager->mutex); 168094332d3Sopenharmony_ci manager->sensorSum = 0; 169094332d3Sopenharmony_ci struct SensorManagerNode *pos = NULL; 170094332d3Sopenharmony_ci DLIST_FOR_EACH_ENTRY(pos, &manager->managerHead, struct SensorManagerNode, node) { 171094332d3Sopenharmony_ci if (manager->sensorSum >= SENSOR_TYPE_MAX) { 172094332d3Sopenharmony_ci break; 173094332d3Sopenharmony_ci } 174094332d3Sopenharmony_ci pos->sensorCount = 0; 175094332d3Sopenharmony_ci if ((pos->ioService == NULL) || (pos->ioService->dispatcher == NULL) || 176094332d3Sopenharmony_ci (pos->ioService->dispatcher->Dispatch == NULL)) { 177094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor pos para failed", __func__); 178094332d3Sopenharmony_ci continue; 179094332d3Sopenharmony_ci } 180094332d3Sopenharmony_ci 181094332d3Sopenharmony_ci int32_t ret = pos->ioService->dispatcher->Dispatch(&pos->ioService->object, 182094332d3Sopenharmony_ci SENSOR_IO_CMD_GET_INFO_LIST, NULL, reply); 183094332d3Sopenharmony_ci if (ret != SENSOR_SUCCESS) { 184094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor dispatch info failed[%{public}d]", __func__, ret); 185094332d3Sopenharmony_ci break; 186094332d3Sopenharmony_ci } 187094332d3Sopenharmony_ci 188094332d3Sopenharmony_ci pos->sensorCount = GetSensorNumByManagerType(reply); 189094332d3Sopenharmony_ci manager->sensorSum += pos->sensorCount; 190094332d3Sopenharmony_ci } 191094332d3Sopenharmony_ci 192094332d3Sopenharmony_ci if (manager->sensorSum == 0) { 193094332d3Sopenharmony_ci HdfSbufRecycle(reply); 194094332d3Sopenharmony_ci (void)OsalMutexUnlock(&manager->mutex); 195094332d3Sopenharmony_ci return SENSOR_FAILURE; 196094332d3Sopenharmony_ci } 197094332d3Sopenharmony_ci 198094332d3Sopenharmony_ci if (GetSensorInfoFromReply(reply) != SENSOR_SUCCESS) { 199094332d3Sopenharmony_ci HdfSbufRecycle(reply); 200094332d3Sopenharmony_ci (void)OsalMutexUnlock(&manager->mutex); 201094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor get info from reply failed", __func__); 202094332d3Sopenharmony_ci return SENSOR_FAILURE; 203094332d3Sopenharmony_ci } 204094332d3Sopenharmony_ci 205094332d3Sopenharmony_ci HdfSbufRecycle(reply); 206094332d3Sopenharmony_ci (void)OsalMutexUnlock(&manager->mutex); 207094332d3Sopenharmony_ci 208094332d3Sopenharmony_ci *count = manager->sensorSum; 209094332d3Sopenharmony_ci *sensor = manager->sensorInfoEntry; 210094332d3Sopenharmony_ci return SENSOR_SUCCESS; 211094332d3Sopenharmony_ci} 212094332d3Sopenharmony_ci 213094332d3Sopenharmony_cistatic struct HdfIoService *GetSensorServiceBySensorId(int32_t sensorId) 214094332d3Sopenharmony_ci{ 215094332d3Sopenharmony_ci struct SensorDevManager *manager = GetSensorDevManager(); 216094332d3Sopenharmony_ci struct SensorIdListNode *sensorIdPos = NULL; 217094332d3Sopenharmony_ci 218094332d3Sopenharmony_ci DLIST_FOR_EACH_ENTRY(sensorIdPos, &manager->sensorIdListHead, struct SensorIdListNode, node) { 219094332d3Sopenharmony_ci if (sensorIdPos->sensorId == sensorId) { 220094332d3Sopenharmony_ci return sensorIdPos->ioService; 221094332d3Sopenharmony_ci } 222094332d3Sopenharmony_ci } 223094332d3Sopenharmony_ci return NULL; 224094332d3Sopenharmony_ci} 225094332d3Sopenharmony_ci 226094332d3Sopenharmony_cistatic int32_t SendSensorMsg(int32_t sensorId, struct HdfSBuf *msg, struct HdfSBuf *reply) 227094332d3Sopenharmony_ci{ 228094332d3Sopenharmony_ci struct HdfIoService *ioService = NULL; 229094332d3Sopenharmony_ci 230094332d3Sopenharmony_ci ioService = GetSensorServiceBySensorId(sensorId); 231094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(ioService, SENSOR_NOT_SUPPORT); 232094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(ioService->dispatcher, SENSOR_NULL_PTR); 233094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(ioService->dispatcher->Dispatch, SENSOR_NULL_PTR); 234094332d3Sopenharmony_ci 235094332d3Sopenharmony_ci int32_t ret = ioService->dispatcher->Dispatch(&ioService->object, SENSOR_IO_CMD_OPS, msg, reply); 236094332d3Sopenharmony_ci if (ret != SENSOR_SUCCESS) { 237094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor dispatch failed", __func__); 238094332d3Sopenharmony_ci return ret; 239094332d3Sopenharmony_ci } 240094332d3Sopenharmony_ci return SENSOR_SUCCESS; 241094332d3Sopenharmony_ci} 242094332d3Sopenharmony_ci 243094332d3Sopenharmony_cistatic int32_t EnableSensor(int32_t sensorId) 244094332d3Sopenharmony_ci{ 245094332d3Sopenharmony_ci struct HdfSBuf *msg = HdfSbufObtainDefaultSize(); 246094332d3Sopenharmony_ci if (msg == NULL) { 247094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Failed to obtain sBuf size", __func__); 248094332d3Sopenharmony_ci return SENSOR_FAILURE; 249094332d3Sopenharmony_ci } 250094332d3Sopenharmony_ci 251094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, sensorId)) { 252094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write id failed", __func__); 253094332d3Sopenharmony_ci HdfSbufRecycle(msg); 254094332d3Sopenharmony_ci return SENSOR_FAILURE; 255094332d3Sopenharmony_ci } 256094332d3Sopenharmony_ci 257094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_ENABLE)) { 258094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write enable failed", __func__); 259094332d3Sopenharmony_ci HdfSbufRecycle(msg); 260094332d3Sopenharmony_ci return SENSOR_FAILURE; 261094332d3Sopenharmony_ci } 262094332d3Sopenharmony_ci 263094332d3Sopenharmony_ci int32_t ret = SendSensorMsg(sensorId, msg, NULL); 264094332d3Sopenharmony_ci if (ret != SENSOR_SUCCESS) { 265094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor enable failed, ret[%{public}d]", __func__, ret); 266094332d3Sopenharmony_ci } 267094332d3Sopenharmony_ci HdfSbufRecycle(msg); 268094332d3Sopenharmony_ci 269094332d3Sopenharmony_ci if (sensorId < SENSOR_TYPE_MAX && sensorId >= 0) { 270094332d3Sopenharmony_ci sensorStatusList[sensorId] = SENSOR_STATUS_ENABLE; 271094332d3Sopenharmony_ci } 272094332d3Sopenharmony_ci 273094332d3Sopenharmony_ci return ret; 274094332d3Sopenharmony_ci} 275094332d3Sopenharmony_ci 276094332d3Sopenharmony_cistatic int32_t DisableSensor(int32_t sensorId) 277094332d3Sopenharmony_ci{ 278094332d3Sopenharmony_ci struct HdfSBuf *msg = HdfSbufObtainDefaultSize(); 279094332d3Sopenharmony_ci if (msg == NULL) { 280094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Failed to obtain sBuf", __func__); 281094332d3Sopenharmony_ci return SENSOR_FAILURE; 282094332d3Sopenharmony_ci } 283094332d3Sopenharmony_ci 284094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, sensorId)) { 285094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write id failed", __func__); 286094332d3Sopenharmony_ci HdfSbufRecycle(msg); 287094332d3Sopenharmony_ci return SENSOR_FAILURE; 288094332d3Sopenharmony_ci } 289094332d3Sopenharmony_ci 290094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_DISABLE)) { 291094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write disable failed", __func__); 292094332d3Sopenharmony_ci HdfSbufRecycle(msg); 293094332d3Sopenharmony_ci return SENSOR_FAILURE; 294094332d3Sopenharmony_ci } 295094332d3Sopenharmony_ci 296094332d3Sopenharmony_ci int32_t ret = SendSensorMsg(sensorId, msg, NULL); 297094332d3Sopenharmony_ci if (ret != SENSOR_SUCCESS) { 298094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor disable failed, ret[%{public}d]", __func__, ret); 299094332d3Sopenharmony_ci } 300094332d3Sopenharmony_ci HdfSbufRecycle(msg); 301094332d3Sopenharmony_ci 302094332d3Sopenharmony_ci if (sensorId < SENSOR_TYPE_MAX && sensorId >= 0) { 303094332d3Sopenharmony_ci sensorStatusList[sensorId] = SENSOR_STATUS_DISENABLE; 304094332d3Sopenharmony_ci } 305094332d3Sopenharmony_ci 306094332d3Sopenharmony_ci return ret; 307094332d3Sopenharmony_ci} 308094332d3Sopenharmony_ci 309094332d3Sopenharmony_cistatic int32_t SetSensorBatch(int32_t sensorId, int64_t samplingInterval, int64_t interval) 310094332d3Sopenharmony_ci{ 311094332d3Sopenharmony_ci if (interval < 0) { 312094332d3Sopenharmony_ci HDF_LOGE("%{public}s: invalid param , interval is [%{public}" PRId64 "]", __func__, interval); 313094332d3Sopenharmony_ci return SENSOR_INVALID_PARAM; 314094332d3Sopenharmony_ci } 315094332d3Sopenharmony_ci 316094332d3Sopenharmony_ci if (samplingInterval < 0) { 317094332d3Sopenharmony_ci HDF_LOGE("%{public}s: invalid param , samplingInterval is [%{public}" PRId64 "]", __func__, samplingInterval); 318094332d3Sopenharmony_ci return SENSOR_INVALID_PARAM; 319094332d3Sopenharmony_ci } 320094332d3Sopenharmony_ci 321094332d3Sopenharmony_ci struct HdfSBuf *msg = HdfSbufObtainDefaultSize(); 322094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(msg, SENSOR_FAILURE); 323094332d3Sopenharmony_ci 324094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, sensorId)) { 325094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write id failed", __func__); 326094332d3Sopenharmony_ci HdfSbufRecycle(msg); 327094332d3Sopenharmony_ci return SENSOR_FAILURE; 328094332d3Sopenharmony_ci } 329094332d3Sopenharmony_ci 330094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_SET_BATCH) || !HdfSbufWriteInt64(msg, samplingInterval) || 331094332d3Sopenharmony_ci !HdfSbufWriteInt64(msg, interval)) { 332094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write failed", __func__); 333094332d3Sopenharmony_ci HdfSbufRecycle(msg); 334094332d3Sopenharmony_ci return SENSOR_FAILURE; 335094332d3Sopenharmony_ci } 336094332d3Sopenharmony_ci 337094332d3Sopenharmony_ci int32_t ret = SendSensorMsg(sensorId, msg, NULL); 338094332d3Sopenharmony_ci if (ret != SENSOR_SUCCESS) { 339094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor set batch failed, ret[%{public}d]", __func__, ret); 340094332d3Sopenharmony_ci } 341094332d3Sopenharmony_ci HdfSbufRecycle(msg); 342094332d3Sopenharmony_ci 343094332d3Sopenharmony_ci return ret; 344094332d3Sopenharmony_ci} 345094332d3Sopenharmony_ci 346094332d3Sopenharmony_cistatic int32_t SetSensorMode(int32_t sensorId, int32_t mode) 347094332d3Sopenharmony_ci{ 348094332d3Sopenharmony_ci struct HdfSBuf *msg = HdfSbufObtainDefaultSize(); 349094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(msg, SENSOR_FAILURE); 350094332d3Sopenharmony_ci 351094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, sensorId)) { 352094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write id failed", __func__); 353094332d3Sopenharmony_ci HdfSbufRecycle(msg); 354094332d3Sopenharmony_ci return SENSOR_FAILURE; 355094332d3Sopenharmony_ci } 356094332d3Sopenharmony_ci 357094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_SET_MODE) || !HdfSbufWriteInt32(msg, mode)) { 358094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write failed", __func__); 359094332d3Sopenharmony_ci HdfSbufRecycle(msg); 360094332d3Sopenharmony_ci return SENSOR_FAILURE; 361094332d3Sopenharmony_ci } 362094332d3Sopenharmony_ci 363094332d3Sopenharmony_ci int32_t ret = SendSensorMsg(sensorId, msg, NULL); 364094332d3Sopenharmony_ci if (ret != SENSOR_SUCCESS) { 365094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor set mode failed, ret[%{public}d]", __func__, ret); 366094332d3Sopenharmony_ci } 367094332d3Sopenharmony_ci HdfSbufRecycle(msg); 368094332d3Sopenharmony_ci 369094332d3Sopenharmony_ci return ret; 370094332d3Sopenharmony_ci} 371094332d3Sopenharmony_ci 372094332d3Sopenharmony_cistatic int32_t SetSensorOption(int32_t sensorId, uint32_t option) 373094332d3Sopenharmony_ci{ 374094332d3Sopenharmony_ci struct HdfSBuf *msg = HdfSbufObtainDefaultSize(); 375094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(msg, SENSOR_FAILURE); 376094332d3Sopenharmony_ci 377094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, sensorId)) { 378094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write id failed", __func__); 379094332d3Sopenharmony_ci HdfSbufRecycle(msg); 380094332d3Sopenharmony_ci return SENSOR_FAILURE; 381094332d3Sopenharmony_ci } 382094332d3Sopenharmony_ci 383094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_SET_OPTION) || !HdfSbufWriteUint32(msg, option)) { 384094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write failed", __func__); 385094332d3Sopenharmony_ci HdfSbufRecycle(msg); 386094332d3Sopenharmony_ci return SENSOR_FAILURE; 387094332d3Sopenharmony_ci } 388094332d3Sopenharmony_ci 389094332d3Sopenharmony_ci int32_t ret = SendSensorMsg(sensorId, msg, NULL); 390094332d3Sopenharmony_ci if (ret != SENSOR_SUCCESS) { 391094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor set option failed, ret[%{public}d]", __func__, ret); 392094332d3Sopenharmony_ci } 393094332d3Sopenharmony_ci HdfSbufRecycle(msg); 394094332d3Sopenharmony_ci 395094332d3Sopenharmony_ci return ret; 396094332d3Sopenharmony_ci} 397094332d3Sopenharmony_ci 398094332d3Sopenharmony_cistatic int32_t GetSensorEvent(struct HdfSBuf *reply, struct SensorEvents *sensorEvent) 399094332d3Sopenharmony_ci{ 400094332d3Sopenharmony_ci struct SensorEvents *events = NULL; 401094332d3Sopenharmony_ci uint8_t *buf = NULL; 402094332d3Sopenharmony_ci uint32_t len = 0; 403094332d3Sopenharmony_ci uint32_t length = 0; 404094332d3Sopenharmony_ci 405094332d3Sopenharmony_ci if (!HdfSbufReadBuffer(reply, (const void **)&events, &len) || sensorEvent == NULL) { 406094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Read sensor event fail!", __func__); 407094332d3Sopenharmony_ci return SENSOR_FAILURE; 408094332d3Sopenharmony_ci } 409094332d3Sopenharmony_ci 410094332d3Sopenharmony_ci if (!HdfSbufReadBuffer(reply, (const void **)&buf, &len) || buf == NULL) { 411094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Read sensor data fail!", __func__); 412094332d3Sopenharmony_ci return SENSOR_FAILURE; 413094332d3Sopenharmony_ci } 414094332d3Sopenharmony_ci 415094332d3Sopenharmony_ci length = sensorEvent->dataLen > len ? len : sensorEvent->dataLen; 416094332d3Sopenharmony_ci if (memcpy_s(sensorEvent->data, sensorEvent->dataLen, buf, length) != EOK) { 417094332d3Sopenharmony_ci HDF_LOGE("%{public}s: sensor memcpy data fail!", __func__); 418094332d3Sopenharmony_ci return SENSOR_FAILURE; 419094332d3Sopenharmony_ci } 420094332d3Sopenharmony_ci 421094332d3Sopenharmony_ci sensorEvent->dataLen = length; 422094332d3Sopenharmony_ci sensorEvent->sensorId = events->sensorId; 423094332d3Sopenharmony_ci sensorEvent->version = events->version; 424094332d3Sopenharmony_ci sensorEvent->timestamp = events->timestamp; 425094332d3Sopenharmony_ci sensorEvent->option = events->option; 426094332d3Sopenharmony_ci sensorEvent->mode = events->mode; 427094332d3Sopenharmony_ci 428094332d3Sopenharmony_ci ConvertSensorData(sensorEvent); 429094332d3Sopenharmony_ci return SENSOR_SUCCESS; 430094332d3Sopenharmony_ci} 431094332d3Sopenharmony_ci 432094332d3Sopenharmony_cistatic int32_t ReadData(int32_t sensorId, struct SensorEvents *event) 433094332d3Sopenharmony_ci{ 434094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(event, SENSOR_NULL_PTR); 435094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(event->data, SENSOR_NULL_PTR); 436094332d3Sopenharmony_ci struct HdfSBuf *msg = HdfSbufObtainDefaultSize(); 437094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(msg, SENSOR_NULL_PTR); 438094332d3Sopenharmony_ci struct HdfSBuf *reply = HdfSbufObtain(HDF_SENSOR_EVENT_MAX_SIZE); 439094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(reply, SENSOR_NULL_PTR); 440094332d3Sopenharmony_ci 441094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, sensorId)) { 442094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write id failed", __func__); 443094332d3Sopenharmony_ci goto ERROR; 444094332d3Sopenharmony_ci } 445094332d3Sopenharmony_ci 446094332d3Sopenharmony_ci if (!HdfSbufWriteInt32(msg, SENSOR_OPS_IO_CMD_READ_DATA)) { 447094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor write readData failed", __func__); 448094332d3Sopenharmony_ci goto ERROR; 449094332d3Sopenharmony_ci } 450094332d3Sopenharmony_ci 451094332d3Sopenharmony_ci int32_t ret = SendSensorMsg(sensorId, msg, reply); 452094332d3Sopenharmony_ci if (ret != SENSOR_SUCCESS) { 453094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor read data failed, ret[%{public}d]", __func__, ret); 454094332d3Sopenharmony_ci HdfSbufRecycle(reply); 455094332d3Sopenharmony_ci HdfSbufRecycle(msg); 456094332d3Sopenharmony_ci return ret; 457094332d3Sopenharmony_ci } 458094332d3Sopenharmony_ci 459094332d3Sopenharmony_ci ret = GetSensorEvent(reply, event); 460094332d3Sopenharmony_ci if (ret != SENSOR_SUCCESS) { 461094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Sensor get data from reply failed", __func__); 462094332d3Sopenharmony_ci goto ERROR; 463094332d3Sopenharmony_ci } 464094332d3Sopenharmony_ci 465094332d3Sopenharmony_ci HdfSbufRecycle(reply); 466094332d3Sopenharmony_ci HdfSbufRecycle(msg); 467094332d3Sopenharmony_ci return HDF_SUCCESS; 468094332d3Sopenharmony_ci 469094332d3Sopenharmony_ciERROR: 470094332d3Sopenharmony_ci HdfSbufRecycle(reply); 471094332d3Sopenharmony_ci HdfSbufRecycle(msg); 472094332d3Sopenharmony_ci return HDF_FAILURE; 473094332d3Sopenharmony_ci} 474094332d3Sopenharmony_ci 475094332d3Sopenharmony_cistatic int32_t GetSdcSensorInfo(struct SdcSensorInfo sdcSensorInfo[]) 476094332d3Sopenharmony_ci{ 477094332d3Sopenharmony_ci (void)sdcSensorInfo; 478094332d3Sopenharmony_ci return HDF_SUCCESS; 479094332d3Sopenharmony_ci} 480094332d3Sopenharmony_ci 481094332d3Sopenharmony_civoid GetSensorDeviceMethods(struct SensorInterface *device) 482094332d3Sopenharmony_ci{ 483094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN(device); 484094332d3Sopenharmony_ci device->GetAllSensors = GetSensorInfo; 485094332d3Sopenharmony_ci device->Enable = EnableSensor; 486094332d3Sopenharmony_ci device->Disable = DisableSensor; 487094332d3Sopenharmony_ci device->SetBatch = SetSensorBatch; 488094332d3Sopenharmony_ci device->SetMode = SetSensorMode; 489094332d3Sopenharmony_ci device->SetOption = SetSensorOption; 490094332d3Sopenharmony_ci device->ReadData = ReadData; 491094332d3Sopenharmony_ci device->Register = Register; 492094332d3Sopenharmony_ci device->Unregister = Unregister; 493094332d3Sopenharmony_ci device->GetSdcSensorInfo = GetSdcSensorInfo; 494094332d3Sopenharmony_ci} 495