diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 5429d0510fa0fe1ecd31f21f92ab35ec6cda4498..07621bd2b6f9a0c866c6e586556220d10634270f 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -91,6 +91,7 @@ typedef enum SensorTypeId { SENSOR_TYPE_ID_PEDOMETER_DETECTION = 265, /**< Pedometer detection sensor */ SENSOR_TYPE_ID_PEDOMETER = 266, /**< Pedometer sensor */ SENSOR_TYPE_ID_POSTURE = 267, /**< Posture sensor */ + SENSOR_TYPE_ID_HEADPOSTURE = 268, /**< Head posture sensor */ SENSOR_TYPE_ID_GEOMAGNETIC_ROTATION_VECTOR = 277, /**< Geomagnetic rotation vector sensor */ SENSOR_TYPE_ID_HEART_RATE = 278, /**< Heart rate sensor */ SENSOR_TYPE_ID_DEVICE_ORIENTATION = 279, /**< Device orientation sensor */ @@ -472,6 +473,17 @@ typedef struct PostureData { float angle = 0.0; /**< The angle between two screens. The angle ranges from 0 to 180 degrees. */ } PostureData; +/** + * @brief Defines the struct of the data reported by the head posture sensor. + * This sensor measures the head posture of user. + */ +typedef struct HeadPostureData { + float w = 0.0; + float x = 0.0; + float y = 0.0; + float z = 0.0; +} HeadPostureData; + typedef struct SensorActiveInfo { int32_t pid = -1; /**< PID */ int32_t sensorId = -1; /**< Sensor ID */ diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index 727f68b5e000ff510be2143ce1429715d3c2bba6..e5dc08e9de64e0d4814fa0fc0ea27ab1a6c9c595 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -121,9 +121,34 @@ ohos_unittest("PostureTest") { ] } +ohos_unittest("HeadPostureTest") { + module_out_path = "sensors/sensor/interfaces" + + sources = [ "unittest/head_posture_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 = [ + ":HeadPostureTest", ":PostureTest", ":SensorAgentTest", ":SensorAlgorithmTest", diff --git a/interfaces/native/test/unittest/head_posture_test.cpp b/interfaces/native/test/unittest/head_posture_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..463f8f987891bb7d288cc5a10359fc985bb8c7c3 --- /dev/null +++ b/interfaces/native/test/unittest/head_posture_test.cpp @@ -0,0 +1,306 @@ +/* + * 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 "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, "HeadPostureTest" }; +std::atomic_bool g_existHeadPosture = false; +} // namespace + +class HeadPostureTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void HeadPostureTest::SetUpTestCase() +{ + SensorInfo *sensorInfo = nullptr; + int32_t count = 0; + int32_t ret = GetAllSensors(&sensorInfo, &count); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + if (sensorInfo == nullptr || count == 0) { + SEN_HILOGE("sensorInfo is nullptr or count is 0"); + return; + } + for (int32_t i = 0; i < count; ++i) { + if (sensorInfo[i].sensorId == SENSOR_TYPE_ID_HEADPOSTURE) { + g_existHeadPosture = true; + SEN_HILOGD("Exist head posture sensor"); + break; + } + } + SEN_HILOGD("Not exist head posture sensor"); +} + +void HeadPostureTest::TearDownTestCase() {} + +void HeadPostureTest::SetUp() {} + +void HeadPostureTest::TearDown() {} + +void HeadPostureDataCallbackImpl(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(HeadPostureData)) { + SEN_HILOGE("Event dataLen less than head posture data size, event.dataLen:%{public}u", event[0].dataLen); + return; + } + HeadPostureData *headPostureData = reinterpret_cast(event[0].data); + if (headPostureData == nullptr) { + SEN_HILOGE("headPostureData is nullptr"); + return; + } + SEN_HILOGD("sensorId:%{public}d, version:%{public}d, dataLen:%{public}u, " + "w:%{public}f, x:%{public}f, y:%{public}f, z:%{public}f", event[0].sensorTypeId, event[0].version, + event[0].dataLen, headPostureData->w, headPostureData->x, headPostureData->y, headPostureData->z); +} + +void HeadPostureDataCallbackImpl2(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(HeadPostureData)) { + SEN_HILOGE("Event dataLen less than head posture data size, event.dataLen:%{public}u", event[0].dataLen); + return; + } + HeadPostureData *headPostureData = reinterpret_cast(event[0].data); + if (headPostureData == nullptr) { + SEN_HILOGE("headPostureData is nullptr"); + return; + } + SEN_HILOGD("sensorId:%{public}d, version:%{public}d, dataLen:%{public}u, " + "w:%{public}f, x:%{public}f, y:%{public}f, z:%{public}f", event[0].sensorTypeId, event[0].version, + event[0].dataLen, headPostureData->w, headPostureData->x, headPostureData->y, headPostureData->z); +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_001, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_001 enter"); + if (g_existHeadPosture) { + ASSERT_NE(ActivateSensor(SENSOR_TYPE_ID_HEADPOSTURE, nullptr), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_002, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_002 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = HeadPostureDataCallbackImpl; + ASSERT_NE(ActivateSensor(-1, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_003, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_003 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = nullptr; + ASSERT_NE(ActivateSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_004, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_004 enter"); + if (g_existHeadPosture) { + ASSERT_NE(DeactivateSensor(SENSOR_TYPE_ID_HEADPOSTURE, nullptr), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_005, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_005 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = HeadPostureDataCallbackImpl; + ASSERT_NE(DeactivateSensor(-1, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_006, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_006 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = nullptr; + ASSERT_NE(DeactivateSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_007, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_007 enter"); + if (g_existHeadPosture) { + ASSERT_NE(SetBatch(SENSOR_TYPE_ID_HEADPOSTURE, nullptr, 10000000, 10000000), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_008, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_008 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = HeadPostureDataCallbackImpl; + ASSERT_NE(SetBatch(-1, &user, 10000000, 10000000), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_009, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_009 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = nullptr; + ASSERT_NE(SetBatch(SENSOR_TYPE_ID_HEADPOSTURE, &user, 10000000, 10000000), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_010, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_010 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = HeadPostureDataCallbackImpl; + ASSERT_NE(SetBatch(SENSOR_TYPE_ID_HEADPOSTURE, &user, -1, -1), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_011, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_011 enter"); + if (g_existHeadPosture) { + ASSERT_NE(SubscribeSensor(SENSOR_TYPE_ID_HEADPOSTURE, nullptr), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_012, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_012 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = HeadPostureDataCallbackImpl; + ASSERT_NE(SubscribeSensor(-1, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_013, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_013 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = nullptr; + ASSERT_NE(SubscribeSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_014, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_014 enter"); + if (g_existHeadPosture) { + ASSERT_NE(UnsubscribeSensor(SENSOR_TYPE_ID_HEADPOSTURE, nullptr), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_015, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_015 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = HeadPostureDataCallbackImpl; + ASSERT_NE(UnsubscribeSensor(-1, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_016, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_016 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = nullptr; + ASSERT_NE(UnsubscribeSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_017, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_017 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = HeadPostureDataCallbackImpl; + ASSERT_EQ(SubscribeSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + ASSERT_EQ(SetBatch(SENSOR_TYPE_ID_HEADPOSTURE, &user, 10000000, 10000000), OHOS::Sensors::SUCCESS); + ASSERT_EQ(ActivateSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + ASSERT_EQ(DeactivateSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + ASSERT_EQ(UnsubscribeSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + } +} + +HWTEST_F(HeadPostureTest, HeadPostureTest_018, TestSize.Level1) +{ + SEN_HILOGI("HeadPostureTest_018 enter"); + if (g_existHeadPosture) { + SensorUser user; + user.callback = HeadPostureDataCallbackImpl; + ASSERT_EQ(SubscribeSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + ASSERT_EQ(SetBatch(SENSOR_TYPE_ID_HEADPOSTURE, &user, 10000000, 10000000), OHOS::Sensors::SUCCESS); + ASSERT_EQ(ActivateSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + ASSERT_EQ(DeactivateSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + ASSERT_EQ(UnsubscribeSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user), OHOS::Sensors::SUCCESS); + + SensorUser user2; + user2.callback = HeadPostureDataCallbackImpl2; + ASSERT_EQ(SubscribeSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user2), OHOS::Sensors::SUCCESS); + ASSERT_EQ(SetBatch(SENSOR_TYPE_ID_HEADPOSTURE, &user2, 20000000, 20000000), OHOS::Sensors::SUCCESS); + ASSERT_EQ(ActivateSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user2), OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + ASSERT_EQ(DeactivateSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user2), OHOS::Sensors::SUCCESS); + ASSERT_EQ(UnsubscribeSensor(SENSOR_TYPE_ID_HEADPOSTURE, &user2), OHOS::Sensors::SUCCESS); + } +} +} // namespace Sensors +} // namespace OHOS 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..5bd18569f3a0f6f904f1a15eb8393fa4d75183df 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 GenerateHeadPostureEvent(); 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 657166dea0b0430cce284072cbac71356a6c9891..e3eb8e451b657aee73abea18748ee0d751447694 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -32,16 +32,18 @@ constexpr int64_t SAMPLING_INTERVAL_NS = 200000000; constexpr float TARGET_SUM = 9.8F * 9.8F; constexpr float MAX_RANGE = 9999.0F; std::vector g_sensorInfos = { - {"sensor_test", "default", "1.0.0", "1.0.0", 0, 1, 9999.0, 0.000001, 23.0, 100000000, 1000000000}, + {"sensor_test", "default", "1.0.0", "1.0.0", 1, 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_HEADPOSTURE }; float g_accData[3]; float g_colorData[2]; float g_sarData[1]; +float g_headPostureData[4]; SensorEvent g_accEvent = { .sensorTypeId = SENSOR_TYPE_ID_ACCELEROMETER, .option = 3, @@ -57,6 +59,11 @@ SensorEvent g_sarEvent = { .option = 3, .dataLen = 4 }; +SensorEvent g_headPostureEvent = { + .sensorTypeId = SENSOR_TYPE_ID_HEADPOSTURE, + .option = 3, + .dataLen = 16 +}; } std::vector HdiServiceImpl::enableSensors_; std::vector HdiServiceImpl::callbacks_; @@ -77,6 +84,9 @@ void HdiServiceImpl::GenerateEvent() case SENSOR_TYPE_ID_SAR: GenerateSarEvent(); break; + case SENSOR_TYPE_ID_HEADPOSTURE: + GenerateHeadPostureEvent(); + break; default: SEN_HILOGW("Unknown sensorId:%{public}d", sensorId); break; @@ -88,9 +98,9 @@ void HdiServiceImpl::GenerateAccelerometerEvent() { std::random_device rd; std::default_random_engine eng(rd()); - std::uniform_real_distribution distr(0, TARGET_SUM); - float num1 = 0; - float num2 = 0; + std::uniform_real_distribution distr(0.0, TARGET_SUM); + float num1 = 0.0; + float num2 = 0.0; while (true) { num1 = distr(eng); num2 = distr(eng); @@ -111,7 +121,7 @@ void HdiServiceImpl::GenerateColorEvent() { std::random_device rd; std::default_random_engine eng(rd()); - std::uniform_real_distribution distr(0, MAX_RANGE); + std::uniform_real_distribution distr(0.0, MAX_RANGE); g_colorData[0] = distr(eng); g_colorData[1] = distr(eng); g_colorEvent.data = reinterpret_cast(g_colorData); @@ -121,11 +131,34 @@ void HdiServiceImpl::GenerateSarEvent() { std::random_device rd; std::default_random_engine eng(rd()); - std::uniform_real_distribution distr(0, MAX_RANGE); + std::uniform_real_distribution distr(0.0, MAX_RANGE); g_sarData[0] = distr(eng); g_sarEvent.data = reinterpret_cast(g_sarData); } +void HdiServiceImpl::GenerateHeadPostureEvent() +{ + std::random_device rd; + std::default_random_engine eng(rd()); + std::uniform_real_distribution distr(0.0, 1.0); + std::vector nums(3); + while (true) { + nums[0] = distr(eng); + nums[1] = distr(eng); + nums[2] = distr(eng); + sort(nums.begin(), nums.end()); + if ((std::fabs(nums[1] - nums[0]) > std::numeric_limits::epsilon()) && + (std::fabs(nums[2] - nums[1]) > std::numeric_limits::epsilon())) { + break; + } + } + g_headPostureData[0] = static_cast(sqrt(nums[0])); + g_headPostureData[1] = static_cast(sqrt(nums[1] - nums[0])); + g_headPostureData[2] = static_cast(sqrt(nums[2] - nums[1])); + g_headPostureData[3] = static_cast(sqrt(1.0 - nums[2])); + g_headPostureEvent.data = reinterpret_cast(g_headPostureData); +} + int32_t HdiServiceImpl::GetSensorList(std::vector &sensorList) { CALL_LOG_ENTER; @@ -155,6 +188,9 @@ void HdiServiceImpl::DataReportThread() case SENSOR_TYPE_ID_SAR: it(&g_sarEvent); break; + case SENSOR_TYPE_ID_HEADPOSTURE: + it(&g_headPostureEvent); + 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 f609516c0e53f30da7e1b10d84f178d4818dcf80..838b814c1c5559f59188273575afd19fdb5fd010 100644 --- a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h @@ -41,12 +41,18 @@ 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_; + std::unordered_set mockSet_; int32_t ConnectHdiService(); int32_t ConnectCompatibleHdi(); - bool FindTargetSensors(const std::unordered_set &targetSensors); - bool CheckTargetSensors() const; - std::atomic_bool existTargetSensors_ = false; + bool FindAllInSensorSet(const std::unordered_set &sensors); + bool FindOneInMockSet(int32_t sensorId); + Sensor GenerateColorSensor(); + Sensor GenerateSarSensor(); + Sensor GenerateHeadPostureSensor(); + std::atomic_bool hdiConnectionStatus_ = false; }; } // 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 e46078da0a8f309beb891d26168abd1333e2266e..d4337a9cafef430a848047867e4a298d8769a2bc 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,11 @@ 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_HEADPOSTURE +}; } int32_t SensorHdiConnection::ConnectHdi() @@ -45,12 +49,15 @@ int32_t SensorHdiConnection::ConnectHdi() SEN_HILOGE("Connect compatible connection failed, ret:%{public}d", ret); return ret; } + hdiConnectionStatus_ = false; + } else { + hdiConnectionStatus_ = true; } - if (!FindTargetSensors(g_targetSensors)) { - SEN_HILOGD("SensorList not contain target sensors, connect target sensors compatible connection"); + if (hdiConnectionStatus_ && !FindAllInSensorSet(g_supportMockSensors)) { + SEN_HILOGD("SensorList not contain all mock sensors, connect mock sensors 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; } @@ -60,15 +67,19 @@ int32_t SensorHdiConnection::ConnectHdi() int32_t SensorHdiConnection::ConnectHdiService() { int32_t ret = iSensorHdiConnection_->ConnectHdi(); - if (ret != 0) { + if (ret != ERR_OK) { SEN_HILOGE("Connect hdi service failed"); return CONNECT_SENSOR_HDF_ERR; } + std::lock_guard sensorLock(sensorMutex_); ret = iSensorHdiConnection_->GetSensorList(sensorList_); - if (ret != 0) { + if (ret != ERR_OK) { SEN_HILOGE("Get sensor list failed"); return GET_SENSOR_LIST_ERR; } + for (const auto &sensor : sensorList_) { + sensorSet_.insert(sensor.GetSensorId()); + } return ERR_OK; } @@ -85,33 +96,27 @@ int32_t SensorHdiConnection::ConnectCompatibleHdi() return ERR_OK; } -bool SensorHdiConnection::FindTargetSensors(const std::unordered_set &targetSensors) +bool SensorHdiConnection::FindAllInSensorSet(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()) { - return false; + int32_t count = 0; + std::lock_guard sensorLock(sensorMutex_); + for (const auto &sensorId : sensors) { + if (sensorSet_.find(sensorId) == sensorSet_.end()) { + mockSet_.insert(sensorId); + count++; } } - existTargetSensors_ = true; - return true; + return count == 0 ? true : false; } -bool SensorHdiConnection::CheckTargetSensors() const +bool SensorHdiConnection::FindOneInMockSet(int32_t sensorId) { - return existTargetSensors_; + std::lock_guard sensorLock(sensorMutex_); + return mockSet_.find(sensorId) != mockSet_.end(); } -int32_t SensorHdiConnection::GetSensorList(std::vector &sensorList) +Sensor SensorHdiConnection::GenerateColorSensor() { - CHKPR(iSensorHdiConnection_, GET_SENSOR_LIST_ERR); - sensorList.assign(sensorList_.begin(), sensorList_.end()); - if (CheckTargetSensors()) { - return ERR_OK; - } Sensor sensorColor; sensorColor.SetSensorId(SENSOR_TYPE_ID_COLOR); sensorColor.SetSensorTypeId(SENSOR_TYPE_ID_COLOR); @@ -124,7 +129,11 @@ int32_t SensorHdiConnection::GetSensorList(std::vector &sensorList) sensorColor.SetPower(POWER); sensorColor.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); sensorColor.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); - sensorList.push_back(sensorColor); + return sensorColor; +} + +Sensor SensorHdiConnection::GenerateSarSensor() +{ Sensor sensorSar; sensorSar.SetSensorId(SENSOR_TYPE_ID_SAR); sensorSar.SetSensorTypeId(SENSOR_TYPE_ID_SAR); @@ -137,7 +146,49 @@ int32_t SensorHdiConnection::GetSensorList(std::vector &sensorList) sensorSar.SetPower(POWER); sensorSar.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); sensorSar.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); - sensorList.push_back(sensorSar); + return sensorSar; +} + +Sensor SensorHdiConnection::GenerateHeadPostureSensor() +{ + Sensor sensorHeadPosture; + sensorHeadPosture.SetSensorId(SENSOR_TYPE_ID_HEADPOSTURE); + sensorHeadPosture.SetSensorTypeId(SENSOR_TYPE_ID_HEADPOSTURE); + sensorHeadPosture.SetFirmwareVersion(VERSION_NAME); + sensorHeadPosture.SetHardwareVersion(VERSION_NAME); + sensorHeadPosture.SetMaxRange(MAX_RANGE); + sensorHeadPosture.SetSensorName("sensor_headPosture"); + sensorHeadPosture.SetVendorName("default_headPosture"); + sensorHeadPosture.SetResolution(RESOLITION); + sensorHeadPosture.SetPower(POWER); + sensorHeadPosture.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS); + sensorHeadPosture.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS); + return sensorHeadPosture; +} + +int32_t SensorHdiConnection::GetSensorList(std::vector &sensorList) +{ + CHKPR(iSensorHdiConnection_, GET_SENSOR_LIST_ERR); + std::lock_guard sensorLock(sensorMutex_); + sensorList.assign(sensorList_.begin(), sensorList_.end()); + if (!hdiConnectionStatus_) { + return ERR_OK; + } + for (const auto &sensorId : mockSet_) { + switch (sensorId) { + case SENSOR_TYPE_ID_COLOR: + sensorList.push_back(GenerateColorSensor()); + break; + case SENSOR_TYPE_ID_SAR: + sensorList.push_back(GenerateSarSensor()); + break; + case SENSOR_TYPE_ID_HEADPOSTURE: + sensorList.push_back(GenerateHeadPostureSensor()); + break; + default: + break; + } + } return ERR_OK; } @@ -145,7 +196,7 @@ 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 (FindOneInMockSet(sensorId)) { CHKPR(iSensorCompatibleHdiConnection_, ENABLE_SENSOR_ERR); ret = iSensorCompatibleHdiConnection_->EnableSensor(sensorId); FinishTrace(HITRACE_TAG_SENSORS); @@ -158,7 +209,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; } @@ -169,7 +220,7 @@ 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 (FindOneInMockSet(sensorId)) { CHKPR(iSensorCompatibleHdiConnection_, DISABLE_SENSOR_ERR); ret = iSensorCompatibleHdiConnection_->DisableSensor(sensorId); FinishTrace(HITRACE_TAG_SENSORS); @@ -182,7 +233,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; } @@ -193,7 +244,7 @@ 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 (FindOneInMockSet(sensorId)) { CHKPR(iSensorCompatibleHdiConnection_, SET_SENSOR_CONFIG_ERR); ret = iSensorCompatibleHdiConnection_->SetBatch(sensorId, samplingInterval, reportInterval); FinishTrace(HITRACE_TAG_SENSORS); @@ -206,7 +257,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; } @@ -216,10 +267,21 @@ 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 (FindOneInMockSet(sensorId)) { + 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; } @@ -231,16 +293,18 @@ int32_t SensorHdiConnection::RegisterDataReport(ReportDataCb cb, sptrRegisterDataReport(cb, reportDataCallback); - CHKPR(iSensorCompatibleHdiConnection_, REGIST_CALLBACK_ERR); - int32_t compatibleRet = iSensorCompatibleHdiConnection_->RegisterDataReport(cb, reportDataCallback); - if (compatibleRet != ERR_OK) { - SEN_HILOGE("Registe dataReport failed in compatible"); - } - FinishTrace(HITRACE_TAG_SENSORS); - if (ret != 0) { - SEN_HILOGI("Registe dataReport failed"); + if (ret != ERR_OK) { + SEN_HILOGE("Registe dataReport failed"); return REGIST_CALLBACK_ERR; } + if (iSensorCompatibleHdiConnection_ != nullptr) { + ret = iSensorCompatibleHdiConnection_->RegisterDataReport(cb, reportDataCallback); + if (ret != ERR_OK) { + SEN_HILOGE("Registe dataReport failed in compatible"); + return REGIST_CALLBACK_ERR; + } + } + FinishTrace(HITRACE_TAG_SENSORS); return ret; } @@ -248,14 +312,16 @@ int32_t SensorHdiConnection::DestroyHdiConnection() { CHKPR(iSensorHdiConnection_, DEVICE_ERR); int32_t ret = iSensorHdiConnection_->DestroyHdiConnection(); - if (ret != 0) { - SEN_HILOGI("Destroy hdi connection failed"); + if (ret != ERR_OK) { + SEN_HILOGE("Destroy hdi connection failed"); return DEVICE_ERR; } - CHKPR(iSensorCompatibleHdiConnection_, DEVICE_ERR); - int32_t compatibleRet = iSensorCompatibleHdiConnection_->DestroyHdiConnection(); - if (compatibleRet != ERR_OK) { - SEN_HILOGE("Destroy hdi connection failed in compatible"); + if (iSensorCompatibleHdiConnection_ != nullptr) { + ret = iSensorCompatibleHdiConnection_->DestroyHdiConnection(); + if (ret != ERR_OK) { + SEN_HILOGE("Destroy hdi connection failed in compatible"); + } + return DEVICE_ERR; } return ret; } diff --git a/services/sensor/src/sensor_dump.cpp b/services/sensor/src/sensor_dump.cpp index 5d75db93440c2428cf5004acbbc2cdf5e12cd822..8d8f6610bef8f67df9eb0aff1032a21c70be2f55 100644 --- a/services/sensor/src/sensor_dump.cpp +++ b/services/sensor/src/sensor_dump.cpp @@ -78,6 +78,7 @@ std::unordered_map SensorDump::sensorMap_ = { { SENSOR_TYPE_ID_COLOR, "COLOR" }, { SENSOR_TYPE_ID_SAR, "SAR" }, { SENSOR_TYPE_ID_POSTURE, "POSTURE" }, + { SENSOR_TYPE_ID_HEADPOSTURE, "HEAD POSTURE" }, }; void SensorDump::ParseCommand(int32_t fd, const std::vector &args, const std::vector &sensors, @@ -287,6 +288,7 @@ int32_t SensorDump::GetDataDimension(int32_t sensorId) case SENSOR_TYPE_ID_COLOR: return TWO_DIMENSION; case SENSOR_TYPE_ID_ROTATION_VECTOR: + case SENSOR_TYPE_ID_HEADPOSTURE: return VECTOR_DIMENSION; case SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED: case SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED: