diff --git a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h index d26fe5028f2bf28b84582fdc9cf90c08795be2f0..125326e25b3138f19ad250604ef046b64c7da2d8 100644 --- a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h +++ b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h @@ -27,7 +27,7 @@ namespace Sensors { class HdiServiceImpl : public Singleton { public: HdiServiceImpl() = default; - virtual ~HdiServiceImpl() {} + ~HdiServiceImpl(); int32_t GetSensorList(std::vector &sensorList); int32_t EnableSensor(int32_t sensorId); int32_t DisableSensor(int32_t sensorId); diff --git a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp index 2d3763f50d16adc960c4f13137fe521a506469e5..67714a2d45a952932c47819c0923ea99b8a31bbd 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -62,6 +62,15 @@ int64_t HdiServiceImpl::samplingInterval_ = -1; int64_t HdiServiceImpl::reportInterval_ = -1; std::atomic_bool HdiServiceImpl::isStop_ = false; +HdiServiceImpl::~HdiServiceImpl() +{ + CALL_LOG_ENTER; + isStop_ = true; + if (dataReportThread_.joinable()) { + dataReportThread_.join(); + } +} + void HdiServiceImpl::GenerateEvent() { for (const auto &sensorId : enableSensors_) { @@ -236,6 +245,12 @@ int32_t HdiServiceImpl::SetMode(int32_t sensorId, int32_t mode) int32_t HdiServiceImpl::Register(RecordSensorCallback cb) { CHKPR(cb, ERROR); + for (auto it = callbacks_.begin(); it != callbacks_.end(); it++) { + if (*it == cb) { + SEN_HILOGW("Same recordSensorCallback has been registered"); + return ERR_OK; + } + } callbacks_.push_back(cb); return ERR_OK; } diff --git a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h index f609516c0e53f30da7e1b10d84f178d4818dcf80..0ccb07ef18a2c0b0b6f6757673e7217e41e16c4e 100644 --- a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h @@ -36,6 +36,7 @@ public: int32_t SetMode(int32_t sensorId, int32_t mode) override; int32_t RegisterDataReport(ReportDataCb cb, sptr reportDataCallback) override; int32_t DestroyHdiConnection() override; + int32_t ConnectCompatible(); private: DISALLOW_COPY_AND_MOVE(SensorHdiConnection); diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp index ec893f8916cf0cf91ba4c3124fd6ac343fe1462d..202373f2cf037451bf6a357df640d3b276a06d76 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -57,6 +57,21 @@ int32_t SensorHdiConnection::ConnectHdi() return ERR_OK; } +int32_t SensorHdiConnection::ConnectCompatible() +{ + iSensorHdiConnection_ = std::make_unique(); + int32_t ret = ConnectHdiService(); + if (ret != ERR_OK) { + SEN_HILOGE("Connect compatible connection failed, ret:%{public}d", ret); + return ret; + } + ret = ConnectCompatibleHdi(); + if (ret != ERR_OK) { + SEN_HILOGE("Connect color and sar compatible connection failed, ret:%{public}d", ret); + } + return ret; +} + int32_t SensorHdiConnection::ConnectHdiService() { int32_t ret = iSensorHdiConnection_->ConnectHdi(); diff --git a/services/sensor/include/sensor_data_processer.h b/services/sensor/include/sensor_data_processer.h index bd229494d89ed51d3c0ab1061d7b97d0ffee8c53..e8ec187c0b08e26c773a7290cb0bd1af9720f024 100644 --- a/services/sensor/include/sensor_data_processer.h +++ b/services/sensor/include/sensor_data_processer.h @@ -16,6 +16,7 @@ #ifndef SENSORS_DATA_PROCESSER_H #define SENSORS_DATA_PROCESSER_H +#include #include #include @@ -41,6 +42,7 @@ public: int32_t SendEvents(sptr &channel, SensorData &data); static int DataThread(sptr dataProcesser, sptr dataCallback); int32_t CacheSensorEvent(const SensorData &data, sptr &channel); + void StopThread(); private: DISALLOW_COPY_AND_MOVE(SensorDataProcesser); @@ -61,6 +63,7 @@ private: std::unordered_map>> dataCountMap_; std::mutex sensorMutex_; std::unordered_map sensorMap_; + static std::atomic_bool isStop_; }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/include/sensor_manager.h b/services/sensor/include/sensor_manager.h index 5f8d19056a433d2a088ee15ae54223f18cdffe2d..58913dbede2e0bc6c9cca4092fd8fad1ee4e186e 100644 --- a/services/sensor/include/sensor_manager.h +++ b/services/sensor/include/sensor_manager.h @@ -34,6 +34,7 @@ namespace Sensors { using namespace Security::AccessToken; class SensorManager : public Singleton { public: + ~SensorManager(); #ifdef HDF_DRIVERS_INTERFACE_SENSOR void InitSensorMap(const std::unordered_map &sensorMap, sptr dataProcesser, sptr dataCallback); diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 98a0e9dcade7c95182d9948444c12ac8deb4ad3a..6111029e49197fda8027cebfd7454a0c932963cb 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -50,6 +50,7 @@ public: void OnDump() override; void OnStart() override; void OnStop() override; + void OnStartFuzz(); int Dump(int fd, const std::vector &args) override; ErrCode EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) override; ErrCode DisableSensor(int32_t sensorId) override; @@ -97,7 +98,7 @@ private: std::vector sensors_; std::unordered_map sensorMap_; #ifdef HDF_DRIVERS_INTERFACE_SENSOR - bool InitInterface(); + bool InitInterface(bool status); bool InitDataCallback(); bool InitSensorList(); SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 48258d4ef715eb04ef5d3f68af93a6a8396bc3a6..3ed86daee3d88d185269ef8898f1918a82e3bc5c 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -32,6 +32,7 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorDataProcesser" }; } // namespace +std::atomic_bool SensorDataProcesser::isStop_ = false; SensorDataProcesser::SensorDataProcesser(const std::unordered_map &sensorMap) { @@ -45,6 +46,11 @@ SensorDataProcesser::~SensorDataProcesser() sensorMap_.clear(); } +void SensorDataProcesser::StopThread() +{ + isStop_ = true; +} + void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map &cacheBuf, sptr &channel, SensorData &data, uint64_t periodCount) @@ -296,6 +302,10 @@ int32_t SensorDataProcesser::DataThread(sptr dataProcesser, SEN_HILOGE("Callback cannot be null"); return INVALID_POINTER; } + if (dataProcesser->isStop_) { + SEN_HILOGI("Thread stop"); + return THREAD_STOP; + } } while (1); } } // namespace Sensors diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp index 337c6b1f95546d68e786687f40b4dfb482d97329..f7bae26d6d32be48b82e4b743886f69390be7f53 100644 --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -35,6 +35,21 @@ constexpr uint32_t PROXIMITY_SENSOR_ID = 50331904; constexpr float PROXIMITY_FAR = 5.0; } // namespace +SensorManager::~SensorManager() +{ + CALL_LOG_ENTER; +#ifdef HDF_DRIVERS_INTERFACE_SENSOR + sensorDataProcesser_->StopThread(); + if (dataThread_.joinable()) { + { + std::unique_lock lk(ISensorHdiConnection::dataMutex_); + ISensorHdiConnection::dataCondition_.notify_one(); + } + dataThread_.join(); + } +#endif // HDF_DRIVERS_INTERFACE_SENSOR +} + #ifdef HDF_DRIVERS_INTERFACE_SENSOR void SensorManager::InitSensorMap(const std::unordered_map &sensorMap, sptr dataProcesser, sptr dataCallback) diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 6ada9b6fbc21febc097c8490bd341f19efb29fee..8fc41f8fa019bcb1b7c370c45a46796a69020611 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -66,7 +66,7 @@ void SensorService::OnStart() CHKPV(clientDeathObserver_); } #ifdef HDF_DRIVERS_INTERFACE_SENSOR - if (!InitInterface()) { + if (!InitInterface(true)) { SEN_HILOGE("Init interface error"); return; } @@ -96,10 +96,48 @@ void SensorService::OnStart() state_ = SensorServiceState::STATE_RUNNING; } +void SensorService::OnStartFuzz() +{ + CALL_LOG_ENTER; + if (state_ == SensorServiceState::STATE_RUNNING) { + SEN_HILOGD("SensorService has already started"); + return; + } + if (clientDeathObserver_ == nullptr) { + clientDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); + CHKPV(clientDeathObserver_); + } +#ifdef HDF_DRIVERS_INTERFACE_SENSOR + if (!InitInterface(false)) { + SEN_HILOGE("Init interface error"); + return; + } + if (!InitDataCallback()) { + SEN_HILOGE("Init data callback error"); + return; + } + if (!InitSensorList()) { + SEN_HILOGE("Init sensor list error"); + return; + } + sensorDataProcesser_ = new (std::nothrow) SensorDataProcesser(sensorMap_); + CHKPV(sensorDataProcesser_); +#endif // HDF_DRIVERS_INTERFACE_SENSOR + if (!InitSensorPolicy()) { + SEN_HILOGE("Init sensor policy error"); + } +#ifdef HDF_DRIVERS_INTERFACE_SENSOR + sensorManager_.InitSensorMap(sensorMap_, sensorDataProcesser_, reportDataCallback_); +#else + sensorManager_.InitSensorMap(sensorMap_); +#endif // HDF_DRIVERS_INTERFACE_SENSOR + state_ = SensorServiceState::STATE_RUNNING; +} + #ifdef HDF_DRIVERS_INTERFACE_SENSOR -bool SensorService::InitInterface() +bool SensorService::InitInterface(bool status) { - auto ret = sensorHdiConnection_.ConnectHdi(); + int32_t ret = status ? sensorHdiConnection_.ConnectHdi() : sensorHdiConnection_.ConnectCompatible(); if (ret != ERR_OK) { SEN_HILOGE("Connect hdi failed"); return false; diff --git a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp index 81224c1c827b0f8cbb47b5197a6a6329d1808b59..3693e7d584a1580adc3ff59df58dee0bdd1adc67 100644 --- a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp +++ b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp @@ -78,6 +78,7 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) datas.RewindRead(0); MessageParcel reply; MessageOption option; + g_service->OnStartFuzz(); g_service->OnRemoteRequest(code, datas, reply, option); return true; } diff --git a/utils/common/include/sensor_errors.h b/utils/common/include/sensor_errors.h index f4330c3300f4de3949fa8aa3202ce98810094cda..d12f79412fc9951378d0a2345ae0ba7f7b01380e 100644 --- a/utils/common/include/sensor_errors.h +++ b/utils/common/include/sensor_errors.h @@ -96,6 +96,7 @@ enum { SUSPEND_ERR = REGIST_CALLBACK_ERR + 1, RESUME_ERR = SUSPEND_ERR + 1, RESET_ERR = RESUME_ERR + 1, + THREAD_STOP = RESET_ERR + 1, }; // Error code for Sensor utils