diff --git a/services/sensor/hdi_connection/adapter/include/compatible_connection.h b/services/sensor/hdi_connection/adapter/include/compatible_connection.h index cf5d1900fd92450398bb76d67ab724e0f4b147eb..958fb80d3d64ecdc2408ae50c1cba0079f355e9f 100644 --- a/services/sensor/hdi_connection/adapter/include/compatible_connection.h +++ b/services/sensor/hdi_connection/adapter/include/compatible_connection.h @@ -31,17 +31,17 @@ public: int32_t GetSensorList(std::vector& sensorList) override; - int32_t EnableSensor(uint32_t sensorId) override; + int32_t EnableSensor(int32_t sensorId) override; - int32_t DisableSensor(uint32_t sensorId) override; + int32_t DisableSensor(int32_t sensorId) override; int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; int32_t SetMode(int32_t sensorId, int32_t mode) override; - int32_t SetOption(int32_t sensorId, uint32_t option) override; + int32_t SetOption(int32_t sensorId, int32_t option) override; - int32_t RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) override; + int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) override; int32_t RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) override; diff --git a/services/sensor/hdi_connection/adapter/include/hdi_connection.h b/services/sensor/hdi_connection/adapter/include/hdi_connection.h index 8444072c0016ccd887b8903937f28eaf37620ee0..b5f26cb5b213d037c2a3ba96707af4f35d4d2458 100644 --- a/services/sensor/hdi_connection/adapter/include/hdi_connection.h +++ b/services/sensor/hdi_connection/adapter/include/hdi_connection.h @@ -16,7 +16,9 @@ #ifndef HDI_CONNECTION_H #define HDI_CONNECTION_H +#include "death_recipient_template.h" #include "i_sensor_hdi_connection.h" +#include "sensor_basic_info.h" namespace OHOS { namespace Sensors { @@ -30,17 +32,17 @@ public: int32_t GetSensorList(std::vector& sensorList) override; - int32_t EnableSensor(uint32_t sensorId) override; + int32_t EnableSensor(int32_t sensorId) override; - int32_t DisableSensor(uint32_t sensorId) override; + int32_t DisableSensor(int32_t sensorId) override; int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; int32_t SetMode(int32_t sensorId, int32_t mode) override; - int32_t SetOption(int32_t sensorId, uint32_t option) override; + int32_t SetOption(int32_t sensorId, int32_t option) override; - int32_t RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) override; + int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) override; int32_t RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) override; @@ -50,10 +52,18 @@ public: sptr getReportDataCallback(); + void ProcessDeathObserver(const wptr &object); private: DISALLOW_COPY_AND_MOVE(HdiConnection); static ZReportDataCb reportDataCb_; static sptr reportDataCallback_; + sptr hdiDeathObserver_; + void RegisterHdiDeathRecipient(); + void UnregisterHdiDeathRecipient(); + void reconnect(); + void updateSensorBasicInfo(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); + void setSensorBasicInfoState(int32_t sensorId, SensorState state); + void deleteSensorBasicInfoState(int32_t sensorId); }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index 25d5d0250a04027ce384288306060b825052f0a6..ac93b115d2d1639a024075c9cc00aab0cda56824 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -59,7 +59,7 @@ int32_t CompatibleConnection::GetSensorList(std::vector& sensorList) return ERR_OK; } -int32_t CompatibleConnection::EnableSensor(uint32_t sensorId) +int32_t CompatibleConnection::EnableSensor(int32_t sensorId) { int32_t ret = hdiServiceImpl_.EnableSensor(sensorId); if (ret < 0) { @@ -69,7 +69,7 @@ int32_t CompatibleConnection::EnableSensor(uint32_t sensorId) return ERR_OK; }; -int32_t CompatibleConnection::DisableSensor(uint32_t sensorId) +int32_t CompatibleConnection::DisableSensor(int32_t sensorId) { int32_t ret = hdiServiceImpl_.DisableSensor(sensorId); if (ret < 0) { @@ -99,12 +99,12 @@ int32_t CompatibleConnection::SetMode(int32_t sensorId, int32_t mode) return ERR_OK; } -int32_t CompatibleConnection::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) +int32_t CompatibleConnection::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) { return ERR_OK; } -int32_t CompatibleConnection::SetOption(int32_t sensorId, uint32_t option) +int32_t CompatibleConnection::SetOption(int32_t sensorId, int32_t option) { int32_t ret = hdiServiceImpl_.SetOption(sensorId, option); if (ret != 0) { diff --git a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp index e3b6907182cdcbaebfab3963feb5476a44f0764a..7e892ca5835190b94274211060c36915298337e5 100644 --- a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp @@ -14,8 +14,12 @@ */ #include "hdi_connection.h" -#include "sensor_interface_proxy.h" +#include +#include +#include + #include "sensor_event_callback.h" +#include "sensor_interface_proxy.h" #include "sensors_errors.h" #include "sensors_log_domain.h" @@ -28,7 +32,11 @@ using hdi::sensor::v1_0::HdfSensorInformation; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "HdiConnection" }; sptr sensorInterface_ = nullptr; -sptr eventCallback_ = new SensorEventCallback(); +sptr eventCallback_ = nullptr; +std::map sensorBasicInfoMap_; +std::mutex sensorBasicInfoMutex_; +constexpr int32_t GET_HDI_SERVICE_COUNT = 10; +constexpr uint32_t WAIT_MS = 100; } ZReportDataCb HdiConnection::reportDataCb_ = nullptr; @@ -36,17 +44,35 @@ sptr HdiConnection::reportDataCallback_ = nullptr; int32_t HdiConnection::ConnectHdi() { - sensorInterface_ = ISensorInterface::Get(); - if (sensorInterface_ == nullptr) { - HiLog::Error(LABEL, "%{public}s connect v1_0 hdi failed", __func__); - return ERR_NO_INIT; + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t retry = 0; + while (retry < GET_HDI_SERVICE_COUNT) { + sensorInterface_ = ISensorInterface::Get(); + if (sensorInterface_ != nullptr) { + HiLog::Info(LABEL, "%{public}s connect v1_0 hdi success", __func__); + eventCallback_ = new (std::nothrow) SensorEventCallback(); + if (eventCallback_ == nullptr) { + HiLog::Error(LABEL, "%{public}s failed to initialize eventCallback", __func__); + return ERR_NO_INIT; + } + RegisterHdiDeathRecipient(); + return ERR_OK; + } + retry++; + HiLog::Warn(LABEL, "%{public}s connect hdi service failed, retry : %{public}d", __func__, retry); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS)); } - return ERR_OK; + HiLog::Error(LABEL, "%{public}s connect v1_0 hdi failed", __func__); + return ERR_NO_INIT; } int32_t HdiConnection::GetSensorList(std::vector& sensorList) { - HiLog::Info(LABEL, "%{public}s in", __func__); + HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (sensorInterface_ == nullptr) { + HiLog::Error(LABEL, "%{public}s connect v1_0 hdi failed", __func__); + return ERR_NO_INIT; + } std::vector sensorInfos; int32_t ret = sensorInterface_->GetAllSensorInfo(sensorInfos); if (ret != 0) { @@ -68,38 +94,58 @@ int32_t HdiConnection::GetSensorList(std::vector& sensorList) return ERR_OK; } -int32_t HdiConnection::EnableSensor(uint32_t sensorId) +int32_t HdiConnection::EnableSensor(int32_t sensorId) { + if (sensorInterface_ == nullptr) { + HiLog::Error(LABEL, "%{public}s connect v1_0 hdi failed", __func__); + return ERR_NO_INIT; + } int32_t ret = sensorInterface_->Enable(sensorId); if (ret < 0) { HiLog::Error(LABEL, "%{public}s is failed", __func__); return ret; } + setSensorBasicInfoState(sensorId, SENSOR_ENABLED); return ERR_OK; } -int32_t HdiConnection::DisableSensor(uint32_t sensorId) +int32_t HdiConnection::DisableSensor(int32_t sensorId) { + if (sensorInterface_ == nullptr) { + HiLog::Error(LABEL, "%{public}s connect v1_0 hdi failed", __func__); + return ERR_NO_INIT; + } int32_t ret = sensorInterface_->Disable(sensorId); if (ret < 0) { HiLog::Error(LABEL, "%{public}s is failed", __func__); return ret; } + deleteSensorBasicInfoState(sensorId); return ERR_OK; } int32_t HdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) { + if (sensorInterface_ == nullptr) { + HiLog::Error(LABEL, "%{public}s connect v1_0 hdi failed", __func__); + return ERR_NO_INIT; + } int32_t ret = sensorInterface_->SetBatch(sensorId, samplingInterval, reportInterval); if (ret < 0) { HiLog::Error(LABEL, "%{public}s failed", __func__); return ret; } + updateSensorBasicInfo(sensorId, samplingInterval, reportInterval); return ERR_OK; } int32_t HdiConnection::SetMode(int32_t sensorId, int32_t mode) { + HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (sensorInterface_ == nullptr) { + HiLog::Error(LABEL, "%{public}s connect v1_0 hdi failed", __func__); + return ERR_NO_INIT; + } int32_t ret = sensorInterface_->SetMode(sensorId, mode); if (ret < 0) { HiLog::Error(LABEL, "%{public}s is failed", __func__); @@ -108,8 +154,13 @@ int32_t HdiConnection::SetMode(int32_t sensorId, int32_t mode) return ERR_OK; } -int32_t HdiConnection::SetOption(int32_t sensorId, uint32_t option) +int32_t HdiConnection::SetOption(int32_t sensorId, int32_t option) { + HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (sensorInterface_ == nullptr) { + HiLog::Error(LABEL, "%{public}s connect v1_0 hdi failed", __func__); + return ERR_NO_INIT; + } int32_t ret = sensorInterface_->SetOption(sensorId, option); if (ret < 0) { HiLog::Error(LABEL, "%{public}s is failed", __func__); @@ -120,8 +171,9 @@ int32_t HdiConnection::SetOption(int32_t sensorId, uint32_t option) int32_t HdiConnection::RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) { - if (reportDataCallback == nullptr) { - HiLog::Error(LABEL, "%{public}s failed, reportDataCallback cannot be null", __func__); + HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (reportDataCallback == nullptr || sensorInterface_ == nullptr) { + HiLog::Error(LABEL, "%{public}s failed, reportDataCallback or sensorInterface_ cannot be null", __func__); return ERR_NO_INIT; } int32_t ret = sensorInterface_->Register(0, eventCallback_); @@ -136,21 +188,29 @@ int32_t HdiConnection::RegisteDataReport(ZReportDataCb cb, sptrUnregister(0, eventCallback_); if (ret < 0) { HiLog::Error(LABEL, "%{public}s failed", __func__); return ret; } + eventCallback_ = nullptr; + UnregisterHdiDeathRecipient(); return ERR_OK; } -int32_t HdiConnection::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) +int32_t HdiConnection::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) { return 0; } ZReportDataCb HdiConnection::getReportDataCb() { + HiLog::Debug(LABEL, "%{public}s begin", __func__); if (reportDataCb_ == nullptr) { HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); } @@ -159,10 +219,113 @@ ZReportDataCb HdiConnection::getReportDataCb() sptr HdiConnection::getReportDataCallback() { + HiLog::Debug(LABEL, "%{public}s begin", __func__); if (reportDataCallback_ == nullptr) { HiLog::Error(LABEL, "%{public}s reportDataCallback_ cannot be null", __func__); } return reportDataCallback_; } + +void HdiConnection::updateSensorBasicInfo(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) +{ + std::lock_guard sensorInfoLock(sensorBasicInfoMutex_); + SensorBasicInfo sensorBasicInfo; + sensorBasicInfo.SetSamplingPeriodNs(samplingPeriodNs); + sensorBasicInfo.SetMaxReportDelayNs(maxReportDelayNs); + sensorBasicInfoMap_[sensorId] = sensorBasicInfo; +} + +void HdiConnection::setSensorBasicInfoState(int32_t sensorId, SensorState state) +{ + std::lock_guard sensorInfoLock(sensorBasicInfoMutex_); + auto it = sensorBasicInfoMap_.find(sensorId); + if (it == sensorBasicInfoMap_.end()) { + HiLog::Warn(LABEL, "%{public}s should set batch first", __func__); + } + sensorBasicInfoMap_[sensorId].SetSensorState(state); +} + +void HdiConnection::deleteSensorBasicInfoState(int32_t sensorId) +{ + std::lock_guard sensorInfoLock(sensorBasicInfoMutex_); + auto it = sensorBasicInfoMap_.find(sensorId); + if (it != sensorBasicInfoMap_.end()) { + sensorBasicInfoMap_.erase(sensorId); + } +} + +void HdiConnection::RegisterHdiDeathRecipient() +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (sensorInterface_ == nullptr) { + HiLog::Error(LABEL, "%{public}s connect v1_0 hdi failed", __func__); + return; + } + hdiDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); + if (hdiDeathObserver_ == nullptr) { + HiLog::Error(LABEL, "%{public}s hdiDeathObserver_ cannot be null", __func__); + return; + } + sensorInterface_->AsObject()->AddDeathRecipient(hdiDeathObserver_); +} + +void HdiConnection::UnregisterHdiDeathRecipient() +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (sensorInterface_ == nullptr || hdiDeathObserver_ == nullptr) { + HiLog::Error(LABEL, "%{public}s sensorInterface_ or hdiDeathObserver_ is null", __func__); + return; + } + sensorInterface_->AsObject()->RemoveDeathRecipient(hdiDeathObserver_); +} + +void HdiConnection::ProcessDeathObserver(const wptr &object) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + sptr hdiService = object.promote(); + if (hdiService == nullptr) { + HiLog::Error(LABEL, "%{public}s invalid remote object", __func__); + return; + } + hdiService->RemoveDeathRecipient(hdiDeathObserver_); + eventCallback_ = nullptr; + reconnect(); +} + +void HdiConnection::reconnect() +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = ConnectHdi(); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s failed to get an instance of hdi service", __func__); + return; + } + ret = sensorInterface_->Register(0, eventCallback_); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s register callback fail", __func__); + return; + } + std::vector sensorList; + ret = GetSensorList(sensorList); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s get sensor list fail", __func__); + return; + } + std::lock_guard sensorInfoLock(sensorBasicInfoMutex_); + for (const auto &sensorInfo: sensorBasicInfoMap_) { + int32_t sensorTypeId = sensorInfo.first; + ret = SetBatch(sensorTypeId, sensorInfo.second.GetSamplingPeriodNs(), + sensorInfo.second.GetMaxReportDelayNs()); + if (ret < 0 || sensorInfo.second.GetSensorState() != SENSOR_ENABLED) { + HiLog::Error(LABEL, "%{public}s sensorTypeId: %{public}d set batch fail or not need enable sensor", + __func__, sensorTypeId); + continue; + } + ret = EnableSensor(sensorTypeId); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s enable sensor fail, sensorTypeId: %{public}d", __func__, sensorTypeId); + } + } +} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h b/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h index 85ef4c1c7914e4f9cb99757d29e07b356e07bf7b..d3cc38aae07209fb4e3763587206df8033d82f33 100644 --- a/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h @@ -33,17 +33,17 @@ public: virtual int32_t GetSensorList(std::vector& sensorList) = 0; - virtual int32_t EnableSensor(uint32_t sensorId) = 0; + virtual int32_t EnableSensor(int32_t sensorId) = 0; - virtual int32_t DisableSensor(uint32_t sensorId) = 0; + virtual int32_t DisableSensor(int32_t sensorId) = 0; virtual int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) = 0; virtual int32_t SetMode(int32_t sensorId, int32_t mode) = 0; - virtual int32_t SetOption(int32_t sensorId, uint32_t option) = 0; + virtual int32_t SetOption(int32_t sensorId, int32_t option) = 0; - virtual int32_t RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) = 0; + virtual int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) = 0; virtual int32_t RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) = 0; 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 a4a30a4dafb368b1206704f3a33cc2947d2a08b9..809d83e5a6658d7036f75e7b0732cfdbeddbb8c8 100644 --- a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h @@ -31,17 +31,17 @@ public: int32_t GetSensorList(std::vector& sensorList) override; - int32_t EnableSensor(uint32_t sensorId) override; + int32_t EnableSensor(int32_t sensorId) override; - int32_t DisableSensor(uint32_t sensorId) override; + int32_t DisableSensor(int32_t sensorId) override; int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; int32_t SetMode(int32_t sensorId, int32_t mode) override; - int32_t SetOption(int32_t sensorId, uint32_t option) override; + int32_t SetOption(int32_t sensorId, int32_t option) override; - int32_t RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) override; + int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) override; int32_t RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) override; 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 2b9e59ae7efb7b5d2e3cabc186d9e37556bf8989..2384713d86dd68e8c9dd599d7466605f7f6c59bc 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -64,7 +64,7 @@ int32_t SensorHdiConnection::GetSensorList(std::vector& sensorList) return ERR_OK; } -int32_t SensorHdiConnection::EnableSensor(uint32_t sensorId) +int32_t SensorHdiConnection::EnableSensor(int32_t sensorId) { int32_t ret = iSensorHdiConnection_->EnableSensor(sensorId); if (ret != 0) { @@ -74,7 +74,7 @@ int32_t SensorHdiConnection::EnableSensor(uint32_t sensorId) return ret; }; -int32_t SensorHdiConnection::DisableSensor(uint32_t sensorId) +int32_t SensorHdiConnection::DisableSensor(int32_t sensorId) { int32_t ret = iSensorHdiConnection_->DisableSensor(sensorId); if (ret != 0) { @@ -104,7 +104,7 @@ int32_t SensorHdiConnection::SetMode(int32_t sensorId, int32_t mode) return ret; } -int32_t SensorHdiConnection::SetOption(int32_t sensorId, uint32_t option) +int32_t SensorHdiConnection::SetOption(int32_t sensorId, int32_t option) { int32_t ret = iSensorHdiConnection_->SetOption(sensorId, option); if (ret != 0) { @@ -114,7 +114,7 @@ int32_t SensorHdiConnection::SetOption(int32_t sensorId, uint32_t option) return ret; } -int32_t SensorHdiConnection::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) +int32_t SensorHdiConnection::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) { int32_t ret = iSensorHdiConnection_->RunCommand(sensorId, cmd, params); if (ret != 0) { diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index d2fffa4998d7b9c6766cf93f9240bf7b307593c7..a7e736a7facc607b2d73f21d05cf2cf60d8e1576 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -61,7 +61,7 @@ void SensorService::OnDump() void SensorService::OnStart() { - HiLog::Info(LABEL, "%{public}s begin", __func__); + HiLog::Debug(LABEL, "%{public}s begin", __func__); if (state_ == SensorServiceState::STATE_RUNNING) { HiLog::Warn(LABEL, "%{public}s SensorService has already started", __func__); return; @@ -147,11 +147,16 @@ bool SensorService::InitSensorPolicy() void SensorService::OnStop() { + HiLog::Debug(LABEL, "%{public}s begin", __func__); if (state_ == SensorServiceState::STATE_STOPPED) { HiLog::Warn(LABEL, "%{public}s already stopped", __func__); return; } state_ = SensorServiceState::STATE_STOPPED; + int32_t ret = sensorHdiConnection_.DestroyHdiConnection(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s destroy hdi connect fail", __func__); + } } void SensorService::ReportSensorUsedInfo(uint32_t sensorId, bool enable)