diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index add28f3a84638b9d2b006d3d05d60f5a1660f734..6a8ca61af3f5fced6e1916e4caa4aa523c8e05ce 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 ce7036e7e7b399eb32e1240d8a9355111ae23dc1..0c3d7c88069bffedadfd6a9589b72e0b647cc427 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 e60040d74130d39538a36334f9250a8ed68f569b..044b7364b90fe7adfbb12c90ef71680a22b9d838 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 2507a8fbaae79a75acd61d3bd11234fb3dcb61bf..47c9f99b1e518b56844f5c3ffb4fe70ac1bd7d58 --- 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 3bdf81c63884cc29692d7b33d383131c32574eee..3d3b8cfe1838298481b89f6d0de2be58870c321e 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 513de8e9a2357b011164ca469d4cc99479c106bd..719e76cc11c12e5e4f0c122e9d2ef2dee7a34aa8 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;