From 79e5c2b54cc27faec07abbf21c7233a3f94d0afc Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Fri, 4 Mar 2022 15:32:42 +0800 Subject: [PATCH] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- .../adapter/src/compatible_connection.cpp | 6 ++---- .../adapter/src/sensor_event_callback.cpp | 20 +++++++++++-------- services/sensor/src/sensor_data_processer.cpp | 3 +-- utils/include/sensor_basic_data_channel.h | 12 +++++------ utils/src/permission_util.cpp | 10 +++++++++- utils/src/report_data_callback.cpp | 7 ++++++- 6 files changed, 36 insertions(+), 22 deletions(-) mode change 100755 => 100644 utils/include/sensor_basic_data_channel.h diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index add28f3a..6a8ca61a 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -123,13 +123,11 @@ int32_t CompatibleConnection::SetOption(int32_t sensorId, int32_t option) int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *event) { - HiLog::Debug(LABEL, "%{public}s begin", __func__); if ((event == nullptr) || (event->dataLen == 0)) { HiLog::Error(LABEL, "%{public}s event is NULL", __func__); return ERR_INVALID_VALUE; } - - if (reportDataCb_ == nullptr) { + if (reportDataCb_ == nullptr || reportDataCallback_) { HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); return ERR_NO_INIT; } @@ -146,9 +144,9 @@ int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *even errno_t ret = memcpy_s(sensorEvent.data, event->dataLen, event->data, event->dataLen); if (ret != EOK) { HiLog::Error(LABEL, "%{public}s copy data failed", __func__); + delete[] sensorEvent.data; return COPY_ERR; } - (void)(reportDataCallback_->*reportDataCb_)(&sensorEvent, reportDataCallback_); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; diff --git a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp index ce7036e7..0c3d7c88 100644 --- a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp +++ b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp @@ -27,7 +27,17 @@ std::unique_ptr HdiConnection_ = std::make_unique( } int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) { - HiLog::Debug(LABEL, "%{public}s begin", __func__); + ZReportDataCb reportDataCb_ = HdiConnection_->getReportDataCb(); + sptr reportDataCallback_ = HdiConnection_->getReportDataCallback(); + if (reportDataCb_ == nullptr || reportDataCallback_ == nullptr) { + HiLog::Error(LABEL, "%{public}s reportDataCb_ or reportDataCallback_ cannot be null", __func__); + return ERR_NO_INIT; + } + int32_t dataSize = static_cast(event.data.size()); + if (dataSize == 0) { + HiLog::Error(LABEL, "%{public}s data is empty", __func__); + return ERR_INVALID_VALUE; + } struct SensorEvent sensorEvent = { .sensorTypeId = event.sensorId, .version = event.version, @@ -37,15 +47,9 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) .dataLen = event.dataLen }; sensorEvent.data = new uint8_t[SENSOR_DATA_LENGHT]; - for (int32_t i = 0; i < static_cast(event.data.size()); i++) { + for (int32_t i = 0; i < static_cast(dataSize); i++) { sensorEvent.data[i] = event.data[i]; } - ZReportDataCb reportDataCb_ = HdiConnection_->getReportDataCb(); - sptr reportDataCallback_ = HdiConnection_->getReportDataCallback(); - if (reportDataCb_ == nullptr || reportDataCallback_ == nullptr) { - HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); - return ERR_NO_INIT; - } (void)(reportDataCallback_->*(reportDataCb_))(&sensorEvent, reportDataCallback_); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index e60040d7..044b7364 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -373,8 +373,7 @@ int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback int32_t eventNum = eventsBuf.eventNum; for (int32_t i = 0; i < eventNum; i++) { EventFilter(eventsBuf); - delete eventsBuf.circularBuf[eventsBuf.readPosition].data; - eventsBuf.circularBuf[eventsBuf.readPosition].data = nullptr; + delete[] eventsBuf.circularBuf[eventsBuf.readPosition].data; eventsBuf.readPosition++; if (eventsBuf.readPosition == CIRCULAR_BUF_LEN) { eventsBuf.readPosition = 0; diff --git a/utils/include/sensor_basic_data_channel.h b/utils/include/sensor_basic_data_channel.h old mode 100755 new mode 100644 index 2507a8fb..47c9f99b --- a/utils/include/sensor_basic_data_channel.h +++ b/utils/include/sensor_basic_data_channel.h @@ -28,12 +28,12 @@ namespace Sensors { constexpr int32_t INVALID_FD = -1; constexpr int32_t SENSOR_MAX_LENGTH = 64; struct TransferSensorEvents { - uint32_t sensorTypeId; /**< Sensor type ID */ - int32_t version; /**< Sensor algorithm version */ - int64_t timestamp; /**< Time when sensor data was reported */ - int32_t option; /**< Sensor data options, including the measurement range and accuracy */ - int32_t mode; /**< Sensor data reporting mode (described in {@link SensorMode}) */ - uint32_t dataLen; /**< Sensor data length */ + uint32_t sensorTypeId; + int32_t version; + int64_t timestamp; + int32_t option; + int32_t mode; + uint32_t dataLen; uint8_t data[SENSOR_MAX_LENGTH]; }; class SensorBasicDataChannel : public RefBase { diff --git a/utils/src/permission_util.cpp b/utils/src/permission_util.cpp index 3bdf81c6..3d3b8cfe 100644 --- a/utils/src/permission_util.cpp +++ b/utils/src/permission_util.cpp @@ -48,8 +48,16 @@ bool PermissionUtil::CheckSensorPermission(AccessTokenID callerToken, int32_t se if (sensorPermissions_.find(sensorTypeId) == sensorPermissions_.end()) { return true; } + int32_t result; std::string permissionName = sensorPermissions_[sensorTypeId]; - int32_t result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName); + if (AccessTokenKit::GetTokenTypeFlag(callerToken) == TOKEN_NATIVE) { + result = AccessTokenKit::VerifyNativeToken(callerToken, permissionName); + } else if (AccessTokenKit::GetTokenTypeFlag(callerToken) == TOKEN_HAP) { + result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName); + } else { + HiLog::Error(LABEL, "%{public}s tokenId invalid", __func__); + return false; + } if (result != PERMISSION_GRANTED) { HiLog::Error(LABEL, "%{public}s sensorId: %{public}d grant failed, result: %{public}d", __func__, sensorTypeId, result); diff --git a/utils/src/report_data_callback.cpp b/utils/src/report_data_callback.cpp index 513de8e9..719e76cc 100644 --- a/utils/src/report_data_callback.cpp +++ b/utils/src/report_data_callback.cpp @@ -53,8 +53,13 @@ ReportDataCallback::~ReportDataCallback() int32_t ReportDataCallback::ZReportDataCallback(const struct SensorEvent* event, sptr cb) { - if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr || event == nullptr) { + if (event == nullptr) { + HiLog::Error(LABEL, "%{public}s sensor data is null", __func__); + return ERROR; + } + if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr) { HiLog::Error(LABEL, "%{public}s callback or circularBuf or event cannot be null", __func__); + delete[] event->data; return ERROR; } int32_t leftSize = CIRCULAR_BUF_LEN - cb->eventsBuf_.eventNum; -- Gitee