From 6f960b98617076ed017b441891156e35acbe5663 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 7 Sep 2023 12:31:15 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E5=B0=86=E6=89=93=E6=A1=A9=E4=BC=A0?= =?UTF-8?q?=E6=84=9F=E5=99=A8=E7=94=B1=E6=B0=B8=E4=B9=85=E7=94=9F=E6=95=88?= =?UTF-8?q?=E6=94=B9=E6=88=90=E6=8E=A5=E5=8F=A3=E6=B3=A8=E5=85=A5=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I6d3d1b07e50daf4414cea2060ddf72519b15ffd9 --- .../native/sensor/include/i_sensor_service.h | 2 + .../sensor/include/sensor_agent_proxy.h | 2 + .../sensor/include/sensor_service_client.h | 3 + .../sensor/include/sensor_service_proxy.h | 2 + .../include/sensors_ipc_interface_code.h | 2 + .../native/sensor/src/sensor_agent_proxy.cpp | 32 ++++ .../sensor/src/sensor_service_client.cpp | 35 ++++ .../sensor/src/sensor_service_proxy.cpp | 27 +++ interfaces/native/include/sensor_agent.h | 4 + interfaces/native/src/sensor_agent.cpp | 20 +++ .../hardware/include/hdi_service_impl.h | 1 + .../hardware/src/hdi_service_impl.cpp | 43 ++++- .../interface/include/sensor_hdi_connection.h | 13 +- .../interface/src/sensor_hdi_connection.cpp | 160 +++++++++++++----- services/sensor/include/sensor_service.h | 2 + services/sensor/include/sensor_service_stub.h | 2 + services/sensor/src/sensor_service.cpp | 33 ++++ services/sensor/src/sensor_service_stub.cpp | 13 ++ 18 files changed, 345 insertions(+), 51 deletions(-) diff --git a/frameworks/native/sensor/include/i_sensor_service.h b/frameworks/native/sensor/include/i_sensor_service.h index 5b89bc66..261ab741 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.h @@ -49,6 +49,8 @@ public: virtual ErrCode EnableActiveInfoCB() = 0; virtual ErrCode DisableActiveInfoCB() = 0; virtual ErrCode ResetSensors() = 0; + virtual ErrCode InjectMockSensor(int32_t sensorId) = 0; + virtual ErrCode UninjectMockSensor(int32_t sensorId) = 0; }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/include/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index 912f7141..cd400f73 100644 --- a/frameworks/native/sensor/include/sensor_agent_proxy.h +++ b/frameworks/native/sensor/include/sensor_agent_proxy.h @@ -49,6 +49,8 @@ public: int32_t Unregister(SensorActiveInfoCB callback); void HandleSensorData(SensorEvent *events, int32_t num, void *data); int32_t ResetSensors() const; + int32_t InjectMockSensor(int32_t sensorId); + int32_t UninjectMockSensor(int32_t sensorId); private: int32_t CreateSensorDataChannel(); diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index 5377efc8..f02fa9d2 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -54,6 +54,9 @@ public: void ReceiveMessage(const char *buf, size_t size); void Disconnect(); void HandleNetPacke(NetPacket &pkt); + int32_t InjectMockSensor(int32_t sensorId); + int32_t UninjectMockSensor(int32_t sensorId); + void UpdateSensorList(); private: int32_t InitServiceClient(); diff --git a/frameworks/native/sensor/include/sensor_service_proxy.h b/frameworks/native/sensor/include/sensor_service_proxy.h index 174f7c62..8734ef9c 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.h @@ -43,6 +43,8 @@ public: ErrCode EnableActiveInfoCB() override; ErrCode DisableActiveInfoCB() override; ErrCode ResetSensors() override; + ErrCode InjectMockSensor(int32_t sensorId) override; + ErrCode UninjectMockSensor(int32_t sensorId) override; private: DISALLOW_COPY_AND_MOVE(SensorServiceProxy); diff --git a/frameworks/native/sensor/include/sensors_ipc_interface_code.h b/frameworks/native/sensor/include/sensors_ipc_interface_code.h index 70c1e4e7..1f96ecac 100644 --- a/frameworks/native/sensor/include/sensors_ipc_interface_code.h +++ b/frameworks/native/sensor/include/sensors_ipc_interface_code.h @@ -33,6 +33,8 @@ enum class SensorInterfaceCode { ENABLE_ACTIVE_INFO_CB, DISABLE_ACTIVE_INFO_CB, RESET_SENSORS, + INJECT_MOCK_SENSOR, + UNINJECT_MOCK_SENSOR, }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index f2de18e2..f9a3bd80 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -120,6 +120,7 @@ int32_t SensorAgentProxy::DestroySensorDataChannel() int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *user) { + CALL_LOG_ENTER; CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); if (samplingInterval_ < 0 || reportInterval_ < 0) { @@ -148,6 +149,7 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *user) { + CALL_LOG_ENTER; CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); if (!SenClient.IsValid(sensorId)) { @@ -172,6 +174,7 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, int64_t reportInterval) { + CALL_LOG_ENTER; CHKPR(user, OHOS::Sensors::ERROR); if (!SenClient.IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); @@ -237,6 +240,7 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) { + CALL_LOG_ENTER; CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); if (!SenClient.IsValid(sensorId)) { @@ -398,6 +402,7 @@ int32_t SensorAgentProxy::GetSensorActiveInfos(int32_t pid, int32_t SensorAgentProxy::Register(SensorActiveInfoCB callback) { + CALL_LOG_ENTER; CHKPR(callback, OHOS::Sensors::ERROR); CHKPR(dataChannel_, INVALID_POINTER); int32_t ret = SenClient.Register(callback, dataChannel_); @@ -409,6 +414,7 @@ int32_t SensorAgentProxy::Register(SensorActiveInfoCB callback) int32_t SensorAgentProxy::Unregister(SensorActiveInfoCB callback) { + CALL_LOG_ENTER; CHKPR(callback, OHOS::Sensors::ERROR); int32_t ret = SenClient.Unregister(callback); if (ret != ERR_OK) { @@ -419,11 +425,37 @@ int32_t SensorAgentProxy::Unregister(SensorActiveInfoCB callback) int32_t SensorAgentProxy::ResetSensors() const { + CALL_LOG_ENTER; int32_t ret = SenClient.ResetSensors(); if (ret != ERR_OK) { SEN_HILOGE("Reset sensors failed, ret:%{public}d", ret); } return ret; } + +int32_t SensorAgentProxy::InjectMockSensor(int32_t sensorId) +{ + CALL_LOG_ENTER; + if (SenClient.IsValid(sensorId)) { + // 此时sensor列表中已包含当前sensor,所以不用注入 + SEN_HILOGE("sensorId exist in sensorList, %{public}d", sensorId); + return PARAMETER_ERROR; + } + int32_t ret = SenClient.InjectMockSensor(sensorId); + if (ret != ERR_OK) { + // 注入sensor失败 + SEN_HILOGE("inject sensors failed, ret:%{public}d", ret); + return ret; + } + // 注入sensor成功,更新sensorlist + SenClient.UpdateSensorList(); + return ERR_OK +} + +int32_t SensorAgentProxy::UninjectMockSensor(int32_t sensorId) +{ + CALL_LOG_ENTER; + +} } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index 7a7586b1..ad1d1882 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -476,5 +476,40 @@ int32_t SensorServiceClient::CreateSocketChannel() isConnected_ = true; return ERR_OK; } + +int32_t SensorServiceClient::InjectMockSensor(int32_t sensorId) +{ + CALL_LOG_ENTER; + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); + return ret; + } + CHKPR(sensorServer_, ERROR); + StartTrace(HITRACE_TAG_SENSORS, "InjectMockSensor"); + ret = sensorServer_->InjectMockSensor(sensorId); + FinishTrace(HITRACE_TAG_SENSORS); + return ret; +} + +int32_t SensorServiceClient::UninjectMockSensor(int32_t sensorId) +{ + CALL_LOG_ENTER; +} + +void SensorServiceClient::UpdateSensorList() +{ + CALL_LOG_ENTER; + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); + return; + } + CHKPR(sensorServer_, ERROR); + StartTrace(HITRACE_TAG_SENSORS, "GetSensorList"); + sensorList_.clear(); + sensorList_ = sensorServer_->GetSensorList(); + FinishTrace(HITRACE_TAG_SENSORS); +} } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index ef570263..fe361238 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -364,5 +364,32 @@ ErrCode SensorServiceProxy::ResetSensors() } return static_cast(ret); } + +ErrCode SensorServiceProxy::InjectMockSensor(int32_t sensorId) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { + SEN_HILOGE("Parcel write descriptor failed"); + return WRITE_PARCEL_ERR; + } + WRITEINT32(data, sensorId, WRITE_PARCEL_ERR); + sptr remote = Remote(); + CHKPR(remote, ERROR); + MessageParcel reply; + MessageOption option; + int32_t ret = remote->SendRequest(static_cast(SensorInterfaceCode::INJECT_MOCK_SENSOR), + data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "InjectMockSensor", "ERROR_CODE", ret); + SEN_HILOGE("Failed, ret:%{public}d", ret); + } + return static_cast(ret); +} + +ErrCode SensorServiceProxy::UninjectMockSensor(int32_t sensorId) +{ + +} } // namespace Sensors } // namespace OHOS diff --git a/interfaces/native/include/sensor_agent.h b/interfaces/native/include/sensor_agent.h index f994f56c..184bfbda 100755 --- a/interfaces/native/include/sensor_agent.h +++ b/interfaces/native/include/sensor_agent.h @@ -189,6 +189,10 @@ int32_t Unregister(SensorActiveInfoCB callback); */ int32_t ResetSensors(); +int32_t InjectMockSensor(int32_t sensorTypeId); // 注入打桩sensor + +int32_t UninjectMockSensor(int32_t sensorTypeId); // 删除打桩sensor + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/native/src/sensor_agent.cpp b/interfaces/native/src/sensor_agent.cpp index e4b1daed..940305c8 100755 --- a/interfaces/native/src/sensor_agent.cpp +++ b/interfaces/native/src/sensor_agent.cpp @@ -167,4 +167,24 @@ int32_t ResetSensors() return NormalizeErrCode(ret); } return ret; +} + +int32_t InjectMockSensor(int32_t sensorTypeId) +{ + int32_t ret = SENSOR_AGENT_IMPL->InjectMockSensor(sensorTypeId); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("InjectMockSensor failed"); + return NormalizeErrCode(ret); + } + return ret; +} + +int32_t UninjectMockSensor(int32_t sensorTypeId) +{ + int32_t ret = SENSOR_AGENT_IMPL->UninjectMockSensor(sensorTypeId); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("UninjectMockSensor failed"); + return NormalizeErrCode(ret); + } + return ret; } \ No newline at end of file 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 d26fe502..70a4cd98 100644 --- a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h +++ b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h @@ -43,6 +43,7 @@ private: static void GenerateAccelerometerEvent(); static void GenerateColorEvent(); static void GenerateSarEvent(); + static void GeneratePostureEvent(); static std::vector enableSensors_; std::thread dataReportThread_; static std::vector callbacks_; 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 2d3763f5..cf96eb8a 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -32,17 +32,20 @@ constexpr int64_t SAMPLING_INTERVAL_NS = 200000000; constexpr int32_t CONVERT_MULTIPLES = 1000; constexpr float TARGET_SUM = 9.8F * 9.8F; constexpr float MAX_RANGE = 9999.0F; +constexpr float MAX_ANGLE = 180.0F; std::vector g_sensorInfos = { {"sensor_test", "default", "1.0.0", "1.0.0", 0, 1, 9999.0, 0.000001, 23.0, 100000000, 1000000000}, }; std::vector g_supportSensors = { SENSOR_TYPE_ID_ACCELEROMETER, SENSOR_TYPE_ID_COLOR, - SENSOR_TYPE_ID_SAR + SENSOR_TYPE_ID_SAR, + SENSOR_TYPE_ID_POSTURE }; float g_accData[3]; float g_colorData[2]; float g_sarData[1]; +float g_postureData[7]; SensorEvent g_accEvent = { .sensorTypeId = SENSOR_TYPE_ID_ACCELEROMETER, .dataLen = 12 @@ -55,6 +58,10 @@ SensorEvent g_sarEvent = { .sensorTypeId = SENSOR_TYPE_ID_SAR, .dataLen = 4 }; +SensorEvent g_postureEvent = { + .sensorTypeId = SENSOR_TYPE_ID_POSTURE, + .dataLen = 28 +}; } std::vector HdiServiceImpl::enableSensors_; std::vector HdiServiceImpl::callbacks_; @@ -75,6 +82,9 @@ void HdiServiceImpl::GenerateEvent() case SENSOR_TYPE_ID_SAR: GenerateSarEvent(); break; + case SENSOR_TYPE_ID_POSTURE: + GeneratePostureEvent(); + break; default: SEN_HILOGW("Unknown sensorId:%{public}d", sensorId); break; @@ -124,6 +134,34 @@ void HdiServiceImpl::GenerateSarEvent() g_sarEvent.data = reinterpret_cast(g_sarData); } +void HdiServiceImpl::GeneratePostureEvent() +{ + std::random_device rd; + std::default_random_engine eng(rd()); + std::uniform_real_distribution distr1(0, TARGET_SUM); + float num1 = 0; + float num2 = 0; + while (true) { + num1 = distr1(eng); + num2 = distr1(eng); + if (std::fabs(num1 - num2) > std::numeric_limits::epsilon && num1 > num2) { + float temp = num1; + num1 = num2; + num2 = temp; + break; + } + } + g_postureData[0] = static_cast(sqrt(num1)); + g_postureData[1] = static_cast(sqrt(num2 - num1)); + g_postureData[2] = static_cast(sqrt(TARGET_SUM - num2)); + g_postureData[3] = g_postureData[0]; + g_postureData[4] = g_postureData[1]; + g_postureData[5] = g_postureData[2]; + std::uniform_real_distribution distr2(0, MAX_ANGLE); + g_postureData[6] = distr2(eng); + g_postureEvent.data = reinterpret_cast(g_postureData); +} + int32_t HdiServiceImpl::GetSensorList(std::vector &sensorList) { CALL_LOG_ENTER; @@ -153,6 +191,9 @@ void HdiServiceImpl::DataReportThread() case SENSOR_TYPE_ID_SAR: it(&g_sarEvent); break; + case SENSOR_TYPE_ID_POSTURE: + it(&g_postureEvent); + break; default: SEN_HILOGW("Unknown sensorId:%{public}d", sensorId); break; 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 f609516c..15d0041e 100644 --- a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h @@ -16,7 +16,6 @@ #ifndef SENSOR_HDI_CONNECTION_H #define SENSOR_HDI_CONNECTION_H -#include #include #include "i_sensor_hdi_connection.h" @@ -36,17 +35,23 @@ 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 InjectMockSensor(int32_t sensorId); + int32_t UninjectMockSensor(int32_t sensorId); private: DISALLOW_COPY_AND_MOVE(SensorHdiConnection); std::unique_ptr iSensorHdiConnection_ { nullptr }; std::unique_ptr iSensorCompatibleHdiConnection_ { nullptr }; + std::mutex sensorMutex_; std::vector sensorList_; + std::unordered_set sensorSet_; int32_t ConnectHdiService(); int32_t ConnectCompatibleHdi(); - bool FindTargetSensors(const std::unordered_set &targetSensors); - bool CheckTargetSensors() const; - std::atomic_bool existTargetSensors_ = false; + bool CheckAllInSensorSet(const std::unordered_set &sensors); + bool CheckOneInSensorSet(int32_t sensorId); + bool CheckOneInMockSet(int32_t sensorId); + std::mutex mockMutex_; + std::unordered_set mockSet_; }; } // namespace Sensors } // namespace OHOS 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 ec893f89..17e0fd37 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -30,7 +30,7 @@ constexpr float RESOLITION = 0.000001; constexpr float MIN_SAMPLE_PERIOD_NS = 100000000; constexpr float MAX_SAMPLE_PERIOD_NS = 1000000000; const std::string VERSION_NAME = "1.0.1"; -std::unordered_set g_targetSensors = { SENSOR_TYPE_ID_COLOR, SENSOR_TYPE_ID_SAR }; +std::unordered_set g_supportMockSensors = { SENSOR_TYPE_ID_COLOR, SENSOR_TYPE_ID_SAR, SENSOR_TYPE_ID_POSTURE }; } int32_t SensorHdiConnection::ConnectHdi() @@ -46,11 +46,11 @@ int32_t SensorHdiConnection::ConnectHdi() return ret; } } - if (!FindTargetSensors(g_targetSensors)) { - SEN_HILOGD("SensorList not contain target sensors, connect target sensors compatible connection"); + if (!CheckAllInSensorSet(g_supportMockSensors)) { + SEN_HILOGD("SensorList not contain all mock sensors, connect compatible connection"); ret = ConnectCompatibleHdi(); if (ret != ERR_OK) { - SEN_HILOGE("Connect target sensors compatible connection failed, ret:%{public}d", ret); + SEN_HILOGE("Connect mock sensors compatible connection failed, ret:%{public}d", ret); } return ret; } @@ -64,11 +64,15 @@ int32_t SensorHdiConnection::ConnectHdiService() SEN_HILOGE("Connect hdi service failed"); return CONNECT_SENSOR_HDF_ERR; } + std::lock_guard sensorLock(sensorMutex_); ret = iSensorHdiConnection_->GetSensorList(sensorList_); if (ret != 0) { SEN_HILOGE("Get sensor list failed"); return GET_SENSOR_LIST_ERR; } + for (const auto &sensor : sensorList_) { + sensorSet_.insert(sensor.GetSensorId()); + } return ERR_OK; } @@ -85,58 +89,119 @@ int32_t SensorHdiConnection::ConnectCompatibleHdi() return ERR_OK; } -bool SensorHdiConnection::FindTargetSensors(const std::unordered_set &targetSensors) +bool SensorHdiConnection::CheckAllInSensorSet(const std::unordered_set &sensors) { - std::unordered_set sensorSet; - for (const auto &sensor : sensorList_) { - sensorSet.insert(sensor.GetSensorId()); - } - for (const auto &sensorId : targetSensors) { - if (sensorSet.find(sensorId) == sensorSet.end()) { + std::lock_guard sensorLock(sensorMutex_); + for (const auto &sensorId : sensors) { + if (sensorSet_.find(sensorId) == sensorSet_.end()) { return false; } } - existTargetSensors_ = true; return true; } -bool SensorHdiConnection::CheckTargetSensors() const +bool SensorHdiConnection::CheckOneInSensorSet(int32_t sensorId) +{ + std::lock_guard sensorLock(sensorMutex_); + if (sensorSet_.find(sensorId) == sensorSet_.end()) { + return false; + } + return true; +} + +bool SensorHdiConnection::CheckOneInMockSet(int32_t sensorId) +{ + std::lock_guard mockLock(mockMutex_); + if (mockSet_.find(sensorId) == mockSet_.end()) { + return false; + } + return true; +} + +int32_t SensorHdiConnection::InjectMockSensor(int32_t sensorId) +{ + if (CheckOneInSensorSet(sensorId)) { + SEN_HILOGE("hdi的sensorList中有当前sensor,无需注入"); + return ERROR; + } + if (g_supportMockSensors.find(sensorId) == g_supportMockSensors.end()) { + SEN_HILOGE("打桩代码中没有当前sensor的打桩实现,注入失败"); + return ERROR; + } + std::lock_guard mockLock(mockMutex_); + auto pairRet = mockSet_.insert(sensorId); // 保存注入的打桩传感器 + if (!pairRet.second) { + SEN_HILOGE("mockSet_ insert sensorId fail, sensorId:%{public}d", sensorId); + return ERROR; + } + return ERR_OK; +} + +int32_t SensorHdiConnection::UninjectMockSensor(int32_t sensorId) { - return existTargetSensors_; + std::lock_guard mockLock(mockMutex_); + mockSet_.erase(sensorId); // 删除注入的打桩传感器 + return ERR_OK; } int32_t SensorHdiConnection::GetSensorList(std::vector &sensorList) { - sensorList.assign(sensorList_.begin(), sensorList_.end()); - if (CheckTargetSensors()) { - return ERR_OK; + { + std::lock_guard sensorLock(sensorMutex_); + sensorList.assign(sensorList_.begin(), sensorList_.end()); + } + std::lock_guard mockLock(mockMutex_); + for (const auto &sensorId : mockSet_) { + switch (sensorId) { + case SENSOR_TYPE_ID_COLOR: + Sensor sensorColor; + sensorColor.SetSensorId(SENSOR_TYPE_ID_COLOR); + sensorColor.SetSensorTypeId(SENSOR_TYPE_ID_COLOR); + sensorColor.SetFirmwareVersion(VERSION_NAME); + sensorColor.SetHardwareVersion(VERSION_NAME); + sensorColor.SetMaxRange(MAX_RANGE); + sensorColor.SetSensorName("sensor_color"); + sensorColor.SetVendorName("default_color"); + sensorColor.SetResolution(RESOLITION); + sensorColor.SetPower(POWER); + sensorColor.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); + sensorColor.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); + sensorList.push_back(sensorColor); + break; + case SENSOR_TYPE_ID_SAR: + Sensor sensorSar; + sensorSar.SetSensorId(SENSOR_TYPE_ID_SAR); + sensorSar.SetSensorTypeId(SENSOR_TYPE_ID_SAR); + sensorSar.SetFirmwareVersion(VERSION_NAME); + sensorSar.SetHardwareVersion(VERSION_NAME); + sensorSar.SetMaxRange(MAX_RANGE); + sensorSar.SetSensorName("sensor_sar"); + sensorSar.SetVendorName("default_sar"); + sensorSar.SetResolution(RESOLITION); + sensorSar.SetPower(POWER); + sensorSar.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); + sensorSar.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); + sensorList.push_back(sensorSar); + break; + case SENSOR_TYPE_ID_POSTURE: + Sensor sensorPosture; + sensorPosture.SetSensorId(SENSOR_TYPE_ID_POSTURE); + sensorPosture.SetSensorTypeId(SENSOR_TYPE_ID_POSTURE); + sensorPosture.SetFirmwareVersion(VERSION_NAME); + sensorPosture.SetHardwareVersion(VERSION_NAME); + sensorPosture.SetMaxRange(MAX_RANGE); + sensorPosture.SetSensorName("sensor_posture"); + sensorPosture.SetVendorName("default_posture"); + sensorPosture.SetResolution(RESOLITION); + sensorPosture.SetPower(POWER); + sensorPosture.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); + sensorPosture.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); + sensorList.push_back(sensorPosture); + break; + default: + break; + } } - Sensor sensorColor; - sensorColor.SetSensorId(SENSOR_TYPE_ID_COLOR); - sensorColor.SetSensorTypeId(SENSOR_TYPE_ID_COLOR); - sensorColor.SetFirmwareVersion(VERSION_NAME); - sensorColor.SetHardwareVersion(VERSION_NAME); - sensorColor.SetMaxRange(MAX_RANGE); - sensorColor.SetSensorName("sensor_color"); - sensorColor.SetVendorName("default_color"); - sensorColor.SetResolution(RESOLITION); - sensorColor.SetPower(POWER); - sensorColor.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); - sensorColor.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); - sensorList.push_back(sensorColor); - Sensor sensorSar; - sensorSar.SetSensorId(SENSOR_TYPE_ID_SAR); - sensorSar.SetSensorTypeId(SENSOR_TYPE_ID_SAR); - sensorSar.SetFirmwareVersion(VERSION_NAME); - sensorSar.SetHardwareVersion(VERSION_NAME); - sensorSar.SetMaxRange(MAX_RANGE); - sensorSar.SetSensorName("sensor_sar"); - sensorSar.SetVendorName("default_sar"); - sensorSar.SetResolution(RESOLITION); - sensorSar.SetPower(POWER); - sensorSar.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); - sensorSar.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); - sensorList.push_back(sensorSar); return ERR_OK; } @@ -144,7 +209,8 @@ int32_t SensorHdiConnection::EnableSensor(int32_t sensorId) { StartTrace(HITRACE_TAG_SENSORS, "EnableSensor"); int32_t ret = ENABLE_SENSOR_ERR; - if (!CheckTargetSensors() && g_targetSensors.find(sensorId) != g_targetSensors.end()) { + if (CheckOneInMockSet(sensorId)) { + // 当前sensor在注入打桩的set中,走打桩 CHKPR(iSensorCompatibleHdiConnection_, ENABLE_SENSOR_ERR); ret = iSensorCompatibleHdiConnection_->EnableSensor(sensorId); FinishTrace(HITRACE_TAG_SENSORS); @@ -168,7 +234,8 @@ int32_t SensorHdiConnection::DisableSensor(int32_t sensorId) { StartTrace(HITRACE_TAG_SENSORS, "DisableSensor"); int32_t ret = DISABLE_SENSOR_ERR; - if (!CheckTargetSensors() && g_targetSensors.find(sensorId) != g_targetSensors.end()) { + if (CheckOneInMockSet(sensorId)) { + // 当前sensor在注入打桩的set中,走打桩 CHKPR(iSensorCompatibleHdiConnection_, DISABLE_SENSOR_ERR); ret = iSensorCompatibleHdiConnection_->DisableSensor(sensorId); FinishTrace(HITRACE_TAG_SENSORS); @@ -192,7 +259,8 @@ int32_t SensorHdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval { StartTrace(HITRACE_TAG_SENSORS, "SetBatch"); int32_t ret = SET_SENSOR_CONFIG_ERR; - if (!CheckTargetSensors() && g_targetSensors.find(sensorId) != g_targetSensors.end()) { + if (CheckOneInMockSet(sensorId)) { + // 当前sensor在注入打桩的set中,走打桩 CHKPR(iSensorCompatibleHdiConnection_, SET_SENSOR_CONFIG_ERR); ret = iSensorCompatibleHdiConnection_->SetBatch(sensorId, samplingInterval, reportInterval); FinishTrace(HITRACE_TAG_SENSORS); diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 98a0e9dc..71f7b1ac 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -66,6 +66,8 @@ public: ErrCode EnableActiveInfoCB() override; ErrCode DisableActiveInfoCB() override; ErrCode ResetSensors() override; + ErrCode InjectMockSensor(int32_t sensorId) override; + ErrCode UninjectMockSensor(int32_t sensorId) override; private: DISALLOW_COPY_AND_MOVE(SensorService); diff --git a/services/sensor/include/sensor_service_stub.h b/services/sensor/include/sensor_service_stub.h index 0baa6da6..12965d35 100644 --- a/services/sensor/include/sensor_service_stub.h +++ b/services/sensor/include/sensor_service_stub.h @@ -47,6 +47,8 @@ private: ErrCode EnableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply); ErrCode DisableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply); ErrCode ResetSensorsInner(MessageParcel &data, MessageParcel &reply); + ErrCode InjectMockSensorInner(MessageParcel &data, MessageParcel &reply); + ErrCode UninjectMockSensorInner(MessageParcel &data, MessageParcel &reply); std::unordered_map baseFuncs_; }; } // namespace Sensors diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 6ada9b6f..07894cc8 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -526,6 +526,39 @@ ErrCode SensorService::ResetSensors() return POWER_POLICY.ResetSensors(); } +ErrCode InjectMockSensor(int32_t sensorId) +{ + int32_t ret = sensorHdiConnection_.InjectMockSensor(sensorId); + if (ret != 0) { + // 注入sensor失败 + return ret; + } + std::lock_guard sensorLock(sensorsMutex_); + sensors_.clear(); + ret = sensorHdiConnection_.GetSensorList(sensors_); + if (ret != 0) { + SEN_HILOGE("GetSensorList is failed"); + return ret; + } + { + std::lock_guard sensorMapLock(sensorMapMutex_); + sensorMap_.clear(); + for (const auto &it : sensors_) { + if (!(sensorMap_.insert(std::make_pair(it.GetSensorId(), it)).second)) { + SEN_HILOGW("sensorMap_ insert failed"); + } + } + } + //更新sensorManager_中的sensorMap中的值 + sensorManager_.InitSensorMap(sensorMap_, sensorDataProcesser_, reportDataCallback_); + return ERR_OK; +} + +ErrCode UninjectMockSensor(int32_t sensorId) +{ + +} + void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid) { CALL_LOG_ENTER; diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index c0827673..c9756cfc 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -311,5 +311,18 @@ ErrCode SensorServiceStub::ResetSensorsInner(MessageParcel &data, MessageParcel } return ResetSensors(); } + +ErrCode SensorServiceStub::InjectMockSensorInner(MessageParcel &data, MessageParcel &reply) +{ + (void)reply; + int32_t sensorId; + READINT32(data, sensorId, READ_PARCEL_ERR); + return InjectMockSensor(sensorId); +} + +ErrCode SensorServiceStub::UninjectMockSensorInner(MessageParcel &data, MessageParcel &reply) +{ + +} } // namespace Sensors } // namespace OHOS -- Gitee From 23169a3e45aa876c91b21294194460a46f545f7a Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 8 Sep 2023 03:41:18 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E5=B0=86=E6=89=93=E6=A1=A9=E4=BC=A0?= =?UTF-8?q?=E6=84=9F=E5=99=A8=E7=94=B1=E6=B0=B8=E4=B9=85=E7=94=9F=E6=95=88?= =?UTF-8?q?=E6=94=B9=E6=88=90=E6=8E=A5=E5=8F=A3=E6=B3=A8=E5=85=A5=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I39b9d2293c9315d7b0973d3dbdf167f035a66ac2 --- .../sensor/include/sensor_service_client.h | 1 - .../native/sensor/src/sensor_agent_proxy.cpp | 17 +-- .../sensor/src/sensor_service_client.cpp | 23 ++-- .../sensor/src/sensor_service_proxy.cpp | 19 ++- .../hardware/src/hdi_service_impl.cpp | 2 +- .../interface/include/sensor_hdi_connection.h | 3 + .../interface/src/sensor_hdi_connection.cpp | 126 +++++++++++------- .../sensor/include/sensor_data_processer.h | 3 +- services/sensor/include/sensor_manager.h | 3 +- services/sensor/src/sensor_data_processer.cpp | 15 ++- services/sensor/src/sensor_manager.cpp | 10 +- services/sensor/src/sensor_service.cpp | 59 ++++++-- services/sensor/src/sensor_service_stub.cpp | 9 +- 13 files changed, 199 insertions(+), 91 deletions(-) diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index f02fa9d2..c75783b8 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -56,7 +56,6 @@ public: void HandleNetPacke(NetPacket &pkt); int32_t InjectMockSensor(int32_t sensorId); int32_t UninjectMockSensor(int32_t sensorId); - void UpdateSensorList(); private: int32_t InitServiceClient(); diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index f9a3bd80..eb0684a6 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -436,26 +436,21 @@ int32_t SensorAgentProxy::ResetSensors() const int32_t SensorAgentProxy::InjectMockSensor(int32_t sensorId) { CALL_LOG_ENTER; - if (SenClient.IsValid(sensorId)) { - // 此时sensor列表中已包含当前sensor,所以不用注入 - SEN_HILOGE("sensorId exist in sensorList, %{public}d", sensorId); - return PARAMETER_ERROR; - } int32_t ret = SenClient.InjectMockSensor(sensorId); if (ret != ERR_OK) { - // 注入sensor失败 SEN_HILOGE("inject sensors failed, ret:%{public}d", ret); - return ret; } - // 注入sensor成功,更新sensorlist - SenClient.UpdateSensorList(); - return ERR_OK + return ret; } int32_t SensorAgentProxy::UninjectMockSensor(int32_t sensorId) { CALL_LOG_ENTER; - + int32_t ret = SenClient.UninjectMockSensor(sensorId); + if (ret != ERR_OK) { + SEN_HILOGE("uninject sensors failed, ret:%{public}d", ret); + } + return ret; } } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index ad1d1882..8e5b0adc 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -488,28 +488,33 @@ int32_t SensorServiceClient::InjectMockSensor(int32_t sensorId) CHKPR(sensorServer_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "InjectMockSensor"); ret = sensorServer_->InjectMockSensor(sensorId); + if (ret == ERR_OK) { + // 注入sensor成功,更新sensorList + sensorList_.clear(); + sensorList_ = sensorServer_->GetSensorList(); + } FinishTrace(HITRACE_TAG_SENSORS); return ret; } int32_t SensorServiceClient::UninjectMockSensor(int32_t sensorId) -{ - CALL_LOG_ENTER; -} - -void SensorServiceClient::UpdateSensorList() { CALL_LOG_ENTER; int32_t ret = InitServiceClient(); if (ret != ERR_OK) { SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); - return; + return ret; } CHKPR(sensorServer_, ERROR); - StartTrace(HITRACE_TAG_SENSORS, "GetSensorList"); - sensorList_.clear(); - sensorList_ = sensorServer_->GetSensorList(); + StartTrace(HITRACE_TAG_SENSORS, "UninjectMockSensor"); + ret = sensorServer_->UninjectMockSensor(sensorId); + if (ret == ERR_OK) { + // 删除sensor成功,更新sensorList + sensorList_.clear(); + sensorList_ = sensorServer_->GetSensorList(); + } FinishTrace(HITRACE_TAG_SENSORS); + return ret; } } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index fe361238..e3cf25ae 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -389,7 +389,24 @@ ErrCode SensorServiceProxy::InjectMockSensor(int32_t sensorId) ErrCode SensorServiceProxy::UninjectMockSensor(int32_t sensorId) { - + MessageParcel data; + if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { + SEN_HILOGE("Parcel write descriptor failed"); + return WRITE_PARCEL_ERR; + } + WRITEINT32(data, sensorId, WRITE_PARCEL_ERR); + sptr remote = Remote(); + CHKPR(remote, ERROR); + MessageParcel reply; + MessageOption option; + int32_t ret = remote->SendRequest(static_cast(SensorInterfaceCode::UNINJECT_MOCK_SENSOR), + data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "UninjectMockSensor", "ERROR_CODE", ret); + SEN_HILOGE("Failed, ret:%{public}d", ret); + } + return static_cast(ret); } } // namespace Sensors } // namespace OHOS 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 cf96eb8a..213f41de 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -144,7 +144,7 @@ void HdiServiceImpl::GeneratePostureEvent() while (true) { num1 = distr1(eng); num2 = distr1(eng); - if (std::fabs(num1 - num2) > std::numeric_limits::epsilon && num1 > num2) { + if ((num1 > num2) && (std::fabs(num1 - num2) > std::numeric_limits::epsilon())) { float temp = num1; num1 = num2; num2 = temp; 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 15d0041e..60aaed5a 100644 --- a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h @@ -50,6 +50,9 @@ private: bool CheckAllInSensorSet(const std::unordered_set &sensors); bool CheckOneInSensorSet(int32_t sensorId); bool CheckOneInMockSet(int32_t sensorId); + Sensor GenerateColorSensor(); + Sensor GenerateSarSensor(); + Sensor GeneratePostureSensor(); std::mutex mockMutex_; std::unordered_set mockSet_; }; 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 17e0fd37..c16b3cda 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -121,11 +121,11 @@ bool SensorHdiConnection::CheckOneInMockSet(int32_t sensorId) int32_t SensorHdiConnection::InjectMockSensor(int32_t sensorId) { if (CheckOneInSensorSet(sensorId)) { - SEN_HILOGE("hdi的sensorList中有当前sensor,无需注入"); + SEN_HILOGE("sensorId exist in sensorList, no need inject"); return ERROR; } if (g_supportMockSensors.find(sensorId) == g_supportMockSensors.end()) { - SEN_HILOGE("打桩代码中没有当前sensor的打桩实现,注入失败"); + SEN_HILOGE("no support cur sensor inject"); return ERROR; } std::lock_guard mockLock(mockMutex_); @@ -140,10 +140,66 @@ int32_t SensorHdiConnection::InjectMockSensor(int32_t sensorId) int32_t SensorHdiConnection::UninjectMockSensor(int32_t sensorId) { std::lock_guard mockLock(mockMutex_); - mockSet_.erase(sensorId); // 删除注入的打桩传感器 + auto it = mockSet_.find(sensorId); + if (it == mockSet_.end()) { + SEN_HILOGE("mockSet_ not find sensorId, sensorId:%{public}d", sensorId); + return ERROR; + } + mockSet_.erase(it); // 删除注入的打桩传感器 return ERR_OK; } +Sensor SensorHdiConnection::GenerateColorSensor() +{ + Sensor sensorColor; + sensorColor.SetSensorId(SENSOR_TYPE_ID_COLOR); + sensorColor.SetSensorTypeId(SENSOR_TYPE_ID_COLOR); + sensorColor.SetFirmwareVersion(VERSION_NAME); + sensorColor.SetHardwareVersion(VERSION_NAME); + sensorColor.SetMaxRange(MAX_RANGE); + sensorColor.SetSensorName("sensor_color"); + sensorColor.SetVendorName("default_color"); + sensorColor.SetResolution(RESOLITION); + sensorColor.SetPower(POWER); + sensorColor.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); + sensorColor.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); + return sensorColor; +} + +Sensor SensorHdiConnection::GenerateSarSensor() +{ + Sensor sensorSar; + sensorSar.SetSensorId(SENSOR_TYPE_ID_SAR); + sensorSar.SetSensorTypeId(SENSOR_TYPE_ID_SAR); + sensorSar.SetFirmwareVersion(VERSION_NAME); + sensorSar.SetHardwareVersion(VERSION_NAME); + sensorSar.SetMaxRange(MAX_RANGE); + sensorSar.SetSensorName("sensor_sar"); + sensorSar.SetVendorName("default_sar"); + sensorSar.SetResolution(RESOLITION); + sensorSar.SetPower(POWER); + sensorSar.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); + sensorSar.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); + return sensorSar; +} + +Sensor SensorHdiConnection::GeneratePostureSensor() +{ + Sensor sensorPosture; + sensorPosture.SetSensorId(SENSOR_TYPE_ID_POSTURE); + sensorPosture.SetSensorTypeId(SENSOR_TYPE_ID_POSTURE); + sensorPosture.SetFirmwareVersion(VERSION_NAME); + sensorPosture.SetHardwareVersion(VERSION_NAME); + sensorPosture.SetMaxRange(MAX_RANGE); + sensorPosture.SetSensorName("sensor_posture"); + sensorPosture.SetVendorName("default_posture"); + sensorPosture.SetResolution(RESOLITION); + sensorPosture.SetPower(POWER); + sensorPosture.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); + sensorPosture.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); + return sensorPosture; +} + int32_t SensorHdiConnection::GetSensorList(std::vector &sensorList) { { @@ -154,49 +210,13 @@ int32_t SensorHdiConnection::GetSensorList(std::vector &sensorList) for (const auto &sensorId : mockSet_) { switch (sensorId) { case SENSOR_TYPE_ID_COLOR: - Sensor sensorColor; - sensorColor.SetSensorId(SENSOR_TYPE_ID_COLOR); - sensorColor.SetSensorTypeId(SENSOR_TYPE_ID_COLOR); - sensorColor.SetFirmwareVersion(VERSION_NAME); - sensorColor.SetHardwareVersion(VERSION_NAME); - sensorColor.SetMaxRange(MAX_RANGE); - sensorColor.SetSensorName("sensor_color"); - sensorColor.SetVendorName("default_color"); - sensorColor.SetResolution(RESOLITION); - sensorColor.SetPower(POWER); - sensorColor.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); - sensorColor.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); - sensorList.push_back(sensorColor); + sensorList.push_back(GenerateColorSensor()); break; case SENSOR_TYPE_ID_SAR: - Sensor sensorSar; - sensorSar.SetSensorId(SENSOR_TYPE_ID_SAR); - sensorSar.SetSensorTypeId(SENSOR_TYPE_ID_SAR); - sensorSar.SetFirmwareVersion(VERSION_NAME); - sensorSar.SetHardwareVersion(VERSION_NAME); - sensorSar.SetMaxRange(MAX_RANGE); - sensorSar.SetSensorName("sensor_sar"); - sensorSar.SetVendorName("default_sar"); - sensorSar.SetResolution(RESOLITION); - sensorSar.SetPower(POWER); - sensorSar.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); - sensorSar.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); - sensorList.push_back(sensorSar); + sensorList.push_back(GenerateSarSensor()); break; case SENSOR_TYPE_ID_POSTURE: - Sensor sensorPosture; - sensorPosture.SetSensorId(SENSOR_TYPE_ID_POSTURE); - sensorPosture.SetSensorTypeId(SENSOR_TYPE_ID_POSTURE); - sensorPosture.SetFirmwareVersion(VERSION_NAME); - sensorPosture.SetHardwareVersion(VERSION_NAME); - sensorPosture.SetMaxRange(MAX_RANGE); - sensorPosture.SetSensorName("sensor_posture"); - sensorPosture.SetVendorName("default_posture"); - sensorPosture.SetResolution(RESOLITION); - sensorPosture.SetPower(POWER); - sensorPosture.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); - sensorPosture.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); - sensorList.push_back(sensorPosture); + sensorList.push_back(GeneratePostureSensor()); break; default: break; @@ -223,7 +243,7 @@ int32_t SensorHdiConnection::EnableSensor(int32_t sensorId) CHKPR(iSensorHdiConnection_, ENABLE_SENSOR_ERR); ret = iSensorHdiConnection_->EnableSensor(sensorId); FinishTrace(HITRACE_TAG_SENSORS); - if (ret != 0) { + if (ret != ERR_OK) { SEN_HILOGI("Enable sensor failed, sensorId:%{public}d", sensorId); return ENABLE_SENSOR_ERR; } @@ -248,7 +268,7 @@ int32_t SensorHdiConnection::DisableSensor(int32_t sensorId) CHKPR(iSensorHdiConnection_, DISABLE_SENSOR_ERR); ret = iSensorHdiConnection_->DisableSensor(sensorId); FinishTrace(HITRACE_TAG_SENSORS); - if (ret != 0) { + if (ret != ERR_OK) { SEN_HILOGI("Disable sensor failed, sensorId:%{public}d", sensorId); return DISABLE_SENSOR_ERR; } @@ -273,7 +293,7 @@ int32_t SensorHdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval CHKPR(iSensorHdiConnection_, SET_SENSOR_CONFIG_ERR); ret = iSensorHdiConnection_->SetBatch(sensorId, samplingInterval, reportInterval); FinishTrace(HITRACE_TAG_SENSORS); - if (ret != 0) { + if (ret != ERR_OK) { SEN_HILOGI("Set batch failed, sensorId:%{public}d", sensorId); return SET_SENSOR_CONFIG_ERR; } @@ -283,10 +303,22 @@ int32_t SensorHdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval int32_t SensorHdiConnection::SetMode(int32_t sensorId, int32_t mode) { StartTrace(HITRACE_TAG_SENSORS, "SetMode"); + int32_t ret = SET_SENSOR_MODE_ERR; + if (CheckOneInMockSet(sensorId)) { + // 当前sensor在注入打桩的set中,走打桩 + CHKPR(iSensorCompatibleHdiConnection_, SET_SENSOR_MODE_ERR); + ret = iSensorCompatibleHdiConnection_->SetMode(sensorId, mode); + FinishTrace(HITRACE_TAG_SENSORS); + if (ret != ERR_OK) { + SEN_HILOGI("Set mode failed, sensorId:%{public}d", sensorId); + return SET_SENSOR_MODE_ERR; + } + return ret; + } CHKPR(iSensorHdiConnection_, SET_SENSOR_MODE_ERR); - int32_t ret = iSensorHdiConnection_->SetMode(sensorId, mode); + ret = iSensorHdiConnection_->SetMode(sensorId, mode); FinishTrace(HITRACE_TAG_SENSORS); - if (ret != 0) { + if (ret != ERR_OK) { SEN_HILOGI("Set mode failed, sensorId:%{public}d", sensorId); return SET_SENSOR_MODE_ERR; } diff --git a/services/sensor/include/sensor_data_processer.h b/services/sensor/include/sensor_data_processer.h index bd229494..65ea8764 100644 --- a/services/sensor/include/sensor_data_processer.h +++ b/services/sensor/include/sensor_data_processer.h @@ -37,6 +37,7 @@ class SensorDataProcesser : public RefBase { public: explicit SensorDataProcesser(const std::unordered_map &sensorMap); virtual ~SensorDataProcesser(); + void UpdateSensorMap(const std::unordered_map &sensorMap); int32_t ProcessEvents(sptr dataCallback); int32_t SendEvents(sptr &channel, SensorData &data); static int DataThread(sptr dataProcesser, sptr dataCallback); @@ -59,7 +60,7 @@ private: FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); std::mutex dataCountMutex_; std::unordered_map>> dataCountMap_; - std::mutex sensorMutex_; + std::mutex sensorMapMutex_; std::unordered_map sensorMap_; }; } // namespace Sensors diff --git a/services/sensor/include/sensor_manager.h b/services/sensor/include/sensor_manager.h index 5f8d1905..68d5d647 100644 --- a/services/sensor/include/sensor_manager.h +++ b/services/sensor/include/sensor_manager.h @@ -48,6 +48,7 @@ public: bool IsOtherClientUsingSensor(int32_t sensorId, int32_t clientPid); ErrCode AfterDisableSensor(int32_t sensorId); void GetPackageName(AccessTokenID tokenId, std::string &packageName); + void UpdateSensorMap(const std::unordered_map &sensorMap); private: #ifdef HDF_DRIVERS_INTERFACE_SENSOR @@ -57,8 +58,8 @@ private: sptr reportDataCallback_; #endif // HDF_DRIVERS_INTERFACE_SENSOR ClientInfo &clientInfo_ = ClientInfo::GetInstance(); - std::unordered_map sensorMap_; std::mutex sensorMapMutex_; + std::unordered_map sensorMap_; }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 48258d4e..1c1a9d47 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -35,8 +35,9 @@ constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorDataProcesser SensorDataProcesser::SensorDataProcesser(const std::unordered_map &sensorMap) { - sensorMap_.insert(sensorMap.begin(), sensorMap.end()); - SEN_HILOGD("sensorMap_.size:%{public}d", int32_t { sensorMap_.size() }); + std::lock_guard sensorLock(sensorMapMutex_); + sensorMap_ = sensorMap; + SEN_HILOGD("Begin sensorMap_.size:%{public}zu", sensorMap_.size()); } SensorDataProcesser::~SensorDataProcesser() @@ -45,6 +46,14 @@ SensorDataProcesser::~SensorDataProcesser() sensorMap_.clear(); } +void SensorDataProcesser::UpdateSensorMap(const std::unordered_map &sensorMap) +{ + std::lock_guard sensorLock(sensorMapMutex_); + sensorMap_.clear(); + sensorMap_ = sensorMap; + SEN_HILOGD("Update sensorMap_.size:%{public}zu", sensorMap_.size()); +} + void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map &cacheBuf, sptr &channel, SensorData &data, uint64_t periodCount) @@ -177,7 +186,7 @@ bool SensorDataProcesser::ReportNotContinuousData(std::unordered_map &channel, SensorData &data) { int32_t sensorId = data.sensorTypeId; - std::lock_guard sensorLock(sensorMutex_); + std::lock_guard sensorLock(sensorMapMutex_); auto sensor = sensorMap_.find(sensorId); if (sensor == sensorMap_.end()) { SEN_HILOGE("Data's sensorId is not supported"); diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp index 337c6b1f..adde9802 100644 --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -40,7 +40,7 @@ void SensorManager::InitSensorMap(const std::unordered_map &sen sptr dataProcesser, sptr dataCallback) { std::lock_guard sensorLock(sensorMapMutex_); - sensorMap_.insert(sensorMap.begin(), sensorMap.end()); + sensorMap_ = sensorMap; sensorDataProcesser_ = dataProcesser; reportDataCallback_ = dataCallback; SEN_HILOGD("Begin sensorMap_.size:%{public}zu", sensorMap_.size()); @@ -212,5 +212,13 @@ void SensorManager::GetPackageName(AccessTokenID tokenId, std::string &packageNa } } } + +void SensorManager::UpdateSensorMap(const std::unordered_map &sensorMap) +{ + std::lock_guard sensorLock(sensorMapMutex_); + sensorMap_.clear(); + sensorMap_ = sensorMap; + SEN_HILOGD("Update sensorMap_.size:%{public}zu", sensorMap_.size()); +} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 07894cc8..70a2b758 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -526,37 +526,68 @@ ErrCode SensorService::ResetSensors() return POWER_POLICY.ResetSensors(); } -ErrCode InjectMockSensor(int32_t sensorId) +ErrCode SensorService::InjectMockSensor(int32_t sensorId) { + CALL_LOG_ENTER; int32_t ret = sensorHdiConnection_.InjectMockSensor(sensorId); - if (ret != 0) { + if (ret != ERR_OK) { // 注入sensor失败 return ret; } + // 注入sensor成功 + + //1.更新 SensorService::sensors_ 和 SensorService::sensorMap_ std::lock_guard sensorLock(sensorsMutex_); sensors_.clear(); ret = sensorHdiConnection_.GetSensorList(sensors_); - if (ret != 0) { + if (ret != ERR_OK) { SEN_HILOGE("GetSensorList is failed"); return ret; } - { - std::lock_guard sensorMapLock(sensorMapMutex_); - sensorMap_.clear(); - for (const auto &it : sensors_) { - if (!(sensorMap_.insert(std::make_pair(it.GetSensorId(), it)).second)) { - SEN_HILOGW("sensorMap_ insert failed"); - } - } + std::lock_guard sensorMapLock(sensorMapMutex_); + sensorMap_.clear(); + for (const auto &it : sensors_) { + sensorMap_.insert(std::make_pair(it.GetSensorId(), it)); } - //更新sensorManager_中的sensorMap中的值 - sensorManager_.InitSensorMap(sensorMap_, sensorDataProcesser_, reportDataCallback_); + + //2.更新 SensorDataProcesser::sensorMap_ + sensorDataProcesser_->UpdateSensorMap(sensorMap_); + + //3.更新 SensorManager::sensorMap_ + sensorManager_.UpdateSensorMap(sensorMap_); return ERR_OK; } -ErrCode UninjectMockSensor(int32_t sensorId) +ErrCode SensorService::UninjectMockSensor(int32_t sensorId) { + CALL_LOG_ENTER; + int32_t ret = sensorHdiConnection_.UninjectMockSensor(sensorId); + if (ret != ERR_OK) { + // 删除sensor失败 + return ret; + } + // 删除sensor成功 + + //1.更新 SensorService::sensors_ 和 SensorService::sensorMap_ + std::lock_guard sensorLock(sensorsMutex_); + sensors_.clear(); + ret = sensorHdiConnection_.GetSensorList(sensors_); + if (ret != ERR_OK) { + SEN_HILOGE("GetSensorList is failed"); + return ret; + } + std::lock_guard sensorMapLock(sensorMapMutex_); + sensorMap_.clear(); + for (const auto &it : sensors_) { + sensorMap_.insert(std::make_pair(it.GetSensorId(), it)); + } + //2.更新 SensorDataProcesser::sensorMap_ + sensorDataProcesser_->UpdateSensorMap(sensorMap_); + + //3.更新 SensorManager::sensorMap_ + sensorManager_.UpdateSensorMap(sensorMap_); + return ERR_OK; } void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid) diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index c9756cfc..17dad055 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -64,6 +64,10 @@ SensorServiceStub::SensorServiceStub() &SensorServiceStub::DisableActiveInfoCBInner; baseFuncs_[static_cast(SensorInterfaceCode::RESET_SENSORS)] = &SensorServiceStub::ResetSensorsInner; + baseFuncs_[static_cast(SensorInterfaceCode::INJECT_MOCK_SENSOR)] = + &SensorServiceStub::InjectMockSensorInner; + baseFuncs_[static_cast(SensorInterfaceCode::UNINJECT_MOCK_SENSOR)] = + &SensorServiceStub::UninjectMockSensorInner; } SensorServiceStub::~SensorServiceStub() @@ -322,7 +326,10 @@ ErrCode SensorServiceStub::InjectMockSensorInner(MessageParcel &data, MessagePar ErrCode SensorServiceStub::UninjectMockSensorInner(MessageParcel &data, MessageParcel &reply) { - + (void)reply; + int32_t sensorId; + READINT32(data, sensorId, READ_PARCEL_ERR); + return UninjectMockSensor(sensorId); } } // namespace Sensors } // namespace OHOS -- Gitee From 0fa2d986acfd7df9b6496d82c4e3a0d7c57bf733 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 12 Sep 2023 12:28:54 +0000 Subject: [PATCH 3/3] update Signed-off-by: hui1975 Change-Id: Ic845d6a494d2d9d80712e09f2cdf01fd6b7acc02 --- .../native/sensor/src/sensor_agent_proxy.cpp | 24 ++- interfaces/native/test/BUILD.gn | 25 +++ .../test/unittest/inject_sensor_test.cpp | 175 ++++++++++++++++++ 3 files changed, 217 insertions(+), 7 deletions(-) create mode 100644 interfaces/native/test/unittest/inject_sensor_test.cpp diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index eb0684a6..8506f974 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -313,13 +313,15 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) CHKPR(sensorInfo, OHOS::Sensors::ERROR); CHKPR(count, OHOS::Sensors::ERROR); std::lock_guard listLock(sensorInfoMutex_); - if (sensorInfos_ == nullptr) { - int32_t ret = ConvertSensorInfos(); - if (ret != SUCCESS) { - SEN_HILOGE("Convert sensor lists failed"); - ClearSensorInfos(); - return ERROR; - } + if (sensorInfos_ != nullptr) { + free(sensorInfos_); + sensorInfos_ = nullptr; + } + int32_t ret = ConvertSensorInfos(); + if (ret != SUCCESS) { + SEN_HILOGE("Convert sensor lists failed"); + ClearSensorInfos(); + return ERROR; } *sensorInfo = sensorInfos_; *count = sensorInfoCount_; @@ -436,6 +438,10 @@ int32_t SensorAgentProxy::ResetSensors() const int32_t SensorAgentProxy::InjectMockSensor(int32_t sensorId) { CALL_LOG_ENTER; + if (sensorId < 0) { + SEN_HILOGE("SensorId is invalid, sensorId:%{public}d", sensorId); + return PARAMETER_ERROR; + } int32_t ret = SenClient.InjectMockSensor(sensorId); if (ret != ERR_OK) { SEN_HILOGE("inject sensors failed, ret:%{public}d", ret); @@ -446,6 +452,10 @@ int32_t SensorAgentProxy::InjectMockSensor(int32_t sensorId) int32_t SensorAgentProxy::UninjectMockSensor(int32_t sensorId) { CALL_LOG_ENTER; + if (sensorId < 0) { + SEN_HILOGE("SensorId is invalid, sensorId:%{public}d", sensorId); + return PARAMETER_ERROR; + } int32_t ret = SenClient.UninjectMockSensor(sensorId); if (ret != ERR_OK) { SEN_HILOGE("uninject sensors failed, ret:%{public}d", ret); diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index 727f68b5..14078dcf 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -121,9 +121,34 @@ ohos_unittest("PostureTest") { ] } +ohos_unittest("InjectSensorTest") { + module_out_path = "sensors/sensor/interfaces" + + sources = [ "unittest/inject_sensor_test.cpp" ] + + include_dirs = [ + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/interfaces/native/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_ndk_target", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] +} + group("unittest") { testonly = true deps = [ + ":InjectSensorTest", ":PostureTest", ":SensorAgentTest", ":SensorAlgorithmTest", diff --git a/interfaces/native/test/unittest/inject_sensor_test.cpp b/interfaces/native/test/unittest/inject_sensor_test.cpp new file mode 100644 index 00000000..2ea15b80 --- /dev/null +++ b/interfaces/native/test/unittest/inject_sensor_test.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include + +#include "sensor_agent.h" +#include "sensor_agent_type.h" +#include "sensor_errors.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "InjectSensorTest" }; +constexpr float ANGLE_MAX = 180.0F; +constexpr float ANGLE_MIN = 0.0F; +} // namespace + +class InjectSensorTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void InjectSensorTest::SetUpTestCase(){} + +void InjectSensorTest::TearDownTestCase() {} + +void InjectSensorTest::SetUp() {} + +void InjectSensorTest::TearDown() {} + +void PostureDataCallbackImpl(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("event is nullptr"); + return; + } + if (event[0].data == nullptr) { + SEN_HILOGE("event[0].data is nullptr"); + return; + } + if (event[0].dataLen < sizeof(PostureData)) { + SEN_HILOGE("Event dataLen less than posture data size, event.dataLen:%{public}u", event[0].dataLen); + return; + } + PostureData *postureData = reinterpret_cast(event[0].data); + float angle = postureData->angle; + if ((angle < ANGLE_MIN && std::fabs(angle - ANGLE_MIN) > std::numeric_limits::epsilon()) + || (angle > ANGLE_MAX && std::fabs(angle - ANGLE_MAX) > std::numeric_limits::epsilon())) { + SEN_HILOGE("Invalid posture angle, angle:%{public}f", angle); + return; + } + SEN_HILOGI("sensorId:%{public}d, version:%{public}d, dataLen:%{public}u, gxm:%{public}f, " + "gym:%{public}f, gzm:%{public}f, gxs:%{public}f, gys:%{public}f, gzs:%{public}f, angle:%{public}f", + event[0].sensorTypeId, event[0].version, event[0].dataLen, postureData->gxm, postureData->gym, + postureData->gzm, postureData->gxs, postureData->gys, postureData->gzs, postureData->angle); +} + +HWTEST_F(InjectSensorTest, InjectSensorTest_001, TestSize.Level1) +{ + SEN_HILOGI("InjectSensorTest_001 enter"); + int32_t ret = InjectMockSensor(-1); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(InjectSensorTest, InjectSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("InjectSensorTest_002 enter"); + int32_t ret = UninjectMockSensor(-1); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(InjectSensorTest, InjectSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("InjectSensorTest_003 enter"); + int32_t ret = InjectMockSensor(1); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(InjectSensorTest, InjectSensorTest_004, TestSize.Level1) +{ + SEN_HILOGI("InjectSensorTest_004 enter"); + int32_t ret = UninjectMockSensor(-1); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(InjectSensorTest, InjectSensorTest_005, TestSize.Level1) +{ + SEN_HILOGI("InjectSensorTest_005 enter"); + //查询sensorList + SensorInfo *sensorInfo1 = nullptr; + int32_t count1 = 0; + int32_t ret = GetAllSensors(&sensorInfo1, &count1); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + SEN_HILOGI("ZMH, 1, SensorList size:%{public}d", count1); + for (int32_t i = 0; i < count1; ++i) { + SEN_HILOGI("ZMH, 1, sensorName:%{public}s, sensorId:%{public}d, minSamplePeriod:%{public}" PRId64 + " ns, maxSamplePeriod:%{public}" PRId64 " ns", sensorInfo1[i].sensorName, sensorInfo1[i].sensorId, + sensorInfo1[i].minSamplePeriod, sensorInfo1[i].maxSamplePeriod); + } + + //注入姿态传感器 + ret = InjectMockSensor(SENSOR_TYPE_ID_POSTURE); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + //查询sensorList + SensorInfo *sensorInfo2 = nullptr; + int32_t count2 = 0; + ret = GetAllSensors(&sensorInfo2, &count2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + SEN_HILOGI("ZMH, 2, SensorList size:%{public}d", count2); + for (int32_t i = 0; i < count2; ++i) { + SEN_HILOGI("ZMH, 2, sensorName:%{public}s, sensorId:%{public}d, minSamplePeriod:%{public}" PRId64 + " ns, maxSamplePeriod:%{public}" PRId64 " ns", sensorInfo2[i].sensorName, sensorInfo2[i].sensorId, + sensorInfo2[i].minSamplePeriod, sensorInfo2[i].maxSamplePeriod); + } + + //订阅姿态传感器 + SensorUser user; + user.callback = PostureDataCallbackImpl; + ret = SubscribeSensor(SENSOR_TYPE_ID_POSTURE, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = SetBatch(SENSOR_TYPE_ID_POSTURE, &user, 10000000, 10000000); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = ActivateSensor(SENSOR_TYPE_ID_POSTURE, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + //取消订阅姿态传感器 + ret = DeactivateSensor(SENSOR_TYPE_ID_POSTURE, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = UnsubscribeSensor(SENSOR_TYPE_ID_POSTURE, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + //删除注入姿态传感器 + ret = UninjectMockSensor(SENSOR_TYPE_ID_POSTURE); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + //查询sensorList + SensorInfo *sensorInfo3 = nullptr; + int32_t count3 = 0; + ret = GetAllSensors(&sensorInfo3, &count3); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + SEN_HILOGI("ZMH, 3, SensorList size:%{public}d", count3); + for (int32_t i = 0; i < count3; ++i) { + SEN_HILOGI("ZMH, 3, sensorName:%{public}s, sensorId:%{public}d, minSamplePeriod:%{public}" PRId64 + " ns, maxSamplePeriod:%{public}" PRId64 " ns", sensorInfo3[i].sensorName, sensorInfo3[i].sensorId, + sensorInfo3[i].minSamplePeriod, sensorInfo3[i].maxSamplePeriod); + } + //尝试订阅姿态传感器 + ret = SubscribeSensor(SENSOR_TYPE_ID_POSTURE, &user); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} +} // namespace Sensors +} // namespace OHOS -- Gitee