diff --git a/frameworks/native/sensor/include/i_sensor_service.h b/frameworks/native/sensor/include/i_sensor_service.h index e89fc433c266687a58f6ed0e1115df622f6a0e8d..e4400ba5a5e14f6e8ec31349bc7a29174256f6f5 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.h @@ -35,8 +35,6 @@ public: virtual ErrCode EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) = 0; virtual ErrCode DisableSensor(uint32_t sensorId) = 0; - virtual int32_t GetSensorState(uint32_t sensorId) = 0; - virtual ErrCode RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params) = 0; virtual std::vector GetSensorList() = 0; virtual ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) = 0; diff --git a/frameworks/native/sensor/include/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index 3fc3e01420c5e7cbd00ed466aa54caed6f6fb582..a14ea60d025a305583ed80e6abbfe291b4e07a40 100644 --- a/frameworks/native/sensor/include/sensor_agent_proxy.h +++ b/frameworks/native/sensor/include/sensor_agent_proxy.h @@ -51,7 +51,7 @@ private: int32_t ConvertSensorInfos() const; void ClearSensorInfos() const; static OHOS::sptr sensorObj_; - static std::mutex subscribeMutex_; + static std::recursive_mutex subscribeMutex_; static std::mutex chanelMutex_; OHOS::sptr dataChannel_; static bool g_isChannelCreated; diff --git a/frameworks/native/sensor/include/sensor_file_descriptor_listener.h b/frameworks/native/sensor/include/sensor_file_descriptor_listener.h index 165ba763029db259ad4121f83d8f151b02cedae0..f403490b23db8cbe9a43292043c030aa779dcf51 100644 --- a/frameworks/native/sensor/include/sensor_file_descriptor_listener.h +++ b/frameworks/native/sensor/include/sensor_file_descriptor_listener.h @@ -25,16 +25,15 @@ namespace OHOS { namespace Sensors { class SensorFileDescriptorListener : public AppExecFwk::FileDescriptorListener { public: - explicit SensorFileDescriptorListener(); + SensorFileDescriptorListener(); ~SensorFileDescriptorListener(); void OnReadable(int32_t fileDescriptor) override; - void OnWritable(int32_t fileDescriptor) override; void OnShutdown(int32_t fileDescriptor) override; void OnException(int32_t fileDescriptor) override; void SetChannel(SensorDataChannel* channel); private: - SensorDataChannel* channel_; + SensorDataChannel* channel_ = nullptr; TransferSensorEvents *receiveDataBuff_ = nullptr; }; } // namespace Sensors diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index 38c29496254fc7b8bf22ea06e62ea6ca6654caad..586bb05da46db59296c8e8f6d6e31332878a36fc 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -38,7 +38,6 @@ public: std::vector GetSensorList(); int32_t EnableSensor(uint32_t sensorId, int64_t samplingPeroid, int64_t maxReportDelay); int32_t DisableSensor(uint32_t sensorId); - int32_t RunCommand(uint32_t sensorId, int32_t cmdType, int32_t parms); int32_t TransferDataChannel(sptr sensorDataChannel); int32_t DestroyDataChannel(); void ProcessDeathObserver(const wptr &object); diff --git a/frameworks/native/sensor/include/sensor_service_proxy.h b/frameworks/native/sensor/include/sensor_service_proxy.h index 2894fbc81f383f01c1788a791d96cf290b316955..70a43ecaa55676cd114765aadbbdd5fe2695abbe 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.h @@ -31,8 +31,6 @@ public: virtual ~SensorServiceProxy() = default; ErrCode EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) override; ErrCode DisableSensor(uint32_t sensorId) override; - int32_t GetSensorState(uint32_t sensorId) override; - ErrCode RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params) override; std::vector GetSensorList() override; ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) override; diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 7b28161cdaf8042a64f53dd307d59d4e68446dbd..18c38c93dc38f8fea8a622012be46219dc7f77af 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -35,7 +35,7 @@ OHOS::sptr SensorAgentProxy::sensorObj_ = nullptr; bool SensorAgentProxy::g_isChannelCreated; int64_t SensorAgentProxy::g_samplingInterval; int64_t SensorAgentProxy::g_reportInterval; -std::mutex SensorAgentProxy::subscribeMutex_; +std::recursive_mutex SensorAgentProxy::subscribeMutex_; std::mutex SensorAgentProxy::chanelMutex_; std::mutex sensorInfoMutex_; SensorInfo *sensorInfos_ = nullptr; @@ -73,12 +73,15 @@ void SensorAgentProxy::HandleSensorData(SensorEvent *events, int32_t num, void * SensorEvent eventStream; for (int32_t i = 0; i < num; ++i) { eventStream = events[i]; - if (g_subscribeMap.find(eventStream.sensorTypeId) == g_subscribeMap.end()) { - SEN_HILOGE("sensorTypeId not in g_subscribeMap"); + std::lock_guard subscribeLock(subscribeMutex_); + auto iter = g_subscribeMap.find(eventStream.sensorTypeId); + if (iter == g_subscribeMap.end()) { + SEN_HILOGE("sensor is not subscribed"); return; } - CHKPV(g_subscribeMap[eventStream.sensorTypeId]); - g_subscribeMap[eventStream.sensorTypeId]->callback(&eventStream); + const SensorUser *user = iter->second; + CHKPV(user); + user->callback(&eventStream); } } @@ -133,7 +136,6 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use { CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - std::lock_guard subscribeLock(subscribeMutex_); if (g_samplingInterval < 0 || g_reportInterval < 0) { SEN_HILOGE("samplingPeroid or g_reportInterval is invalid"); return ERROR; @@ -142,6 +144,7 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return PARAMETER_ERROR; } + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { SEN_HILOGE("subscribe sensorId first"); return ERROR; @@ -165,7 +168,7 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return PARAMETER_ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { SEN_HILOGE("subscribe sensorId first"); return OHOS::Sensors::ERROR; @@ -192,7 +195,7 @@ int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int SEN_HILOGE("samplingInterval or reportInterval is invalid"); return OHOS::Sensors::ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { SEN_HILOGE("subscribe sensorId first"); return OHOS::Sensors::ERROR; @@ -216,7 +219,7 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us SEN_HILOGE("create sensor data chanel failed"); return OHOS::Sensors::ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); g_subscribeMap[sensorId] = user; return OHOS::Sensors::SUCCESS; } @@ -230,7 +233,7 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return PARAMETER_ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { SEN_HILOGE("deactivate sensorId first"); return OHOS::Sensors::ERROR; @@ -254,23 +257,7 @@ int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int3 SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); - if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { - SEN_HILOGE("subscribe sensorId first"); - return OHOS::Sensors::ERROR; - } - return OHOS::Sensors::SUCCESS; -} - -int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, int32_t option) const -{ - CHKPR(user, OHOS::Sensors::ERROR); - CHKPR(user->callback, OHOS::Sensors::ERROR); - if (!SenClient.IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); - return ERROR; - } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { SEN_HILOGE("subscribe sensorId first"); return OHOS::Sensors::ERROR; diff --git a/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp b/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp index 8dc136149f60440b9ee3d3a40b76c5cc23767360..9ae412c720fadb7c272cde462f87aaa55eff4558 100644 --- a/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp +++ b/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp @@ -31,9 +31,7 @@ constexpr int32_t RECEIVE_DATA_SIZE = 100; SensorFileDescriptorListener::SensorFileDescriptorListener() { - channel_ = nullptr; - receiveDataBuff_ = - new (std::nothrow) TransferSensorEvents[sizeof(TransferSensorEvents) * RECEIVE_DATA_SIZE]; + receiveDataBuff_ = new (std::nothrow) TransferSensorEvents[RECEIVE_DATA_SIZE]; CHKPL(receiveDataBuff_); } @@ -52,8 +50,9 @@ void SensorFileDescriptorListener::OnReadable(int32_t fileDescriptor) SEN_HILOGE("fileDescriptor:%{public}d", fileDescriptor); return; } - FileDescriptorListener::OnReadable(fileDescriptor); + CHKPV(channel_); if (receiveDataBuff_ == nullptr) { + SEN_HILOGE("Receive data buff_ is null"); return; } int32_t len = @@ -77,8 +76,6 @@ void SensorFileDescriptorListener::OnReadable(int32_t fileDescriptor) } } -void SensorFileDescriptorListener::OnWritable(int32_t fileDescriptor) {} - void SensorFileDescriptorListener::SetChannel(SensorDataChannel* channel) { channel_ = channel; @@ -87,26 +84,27 @@ void SensorFileDescriptorListener::SetChannel(SensorDataChannel* channel) void SensorFileDescriptorListener::OnShutdown(int32_t fileDescriptor) { if (fileDescriptor < 0) { - SEN_HILOGE("param is error:%{public}d", fileDescriptor); + SEN_HILOGE("Invalid fd:%{public}d", fileDescriptor); } - FileDescriptorListener::OnShutdown(fileDescriptor); if (receiveDataBuff_ != nullptr) { delete[] receiveDataBuff_; receiveDataBuff_ = nullptr; } + CHKPV(channel_); + channel_->DestroySensorDataChannel(); } void SensorFileDescriptorListener::OnException(int32_t fileDescriptor) { if (fileDescriptor < 0) { - SEN_HILOGE("param is error:%{public}d", fileDescriptor); - return; + SEN_HILOGE("Invalid fd::%{public}d", fileDescriptor); } - FileDescriptorListener::OnException(fileDescriptor); if (receiveDataBuff_ != nullptr) { delete[] receiveDataBuff_; receiveDataBuff_ = nullptr; } + CHKPV(channel_); + channel_->DestroySensorDataChannel(); } } // 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 df26d269e3bcd3edfb2659d1a6bcbc1cd6d55779..98e90ea96c49f9a01d4b218c32d0685691c3ac6f 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -95,10 +95,6 @@ bool SensorServiceClient::IsValid(uint32_t sensorId) int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay) { CALL_LOG_ENTER; - if (!IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid"); - return PARAMETER_ERROR; - } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); @@ -117,10 +113,6 @@ int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPer int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) { CALL_LOG_ENTER; - if (!IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid"); - return PARAMETER_ERROR; - } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); @@ -136,29 +128,6 @@ int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) return ret; } -int32_t SensorServiceClient::RunCommand(uint32_t sensorId, int32_t cmdType, int32_t params) -{ - CALL_LOG_ENTER; - if (!IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid"); - return PARAMETER_ERROR; - } - 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, "RunCommand"); - ret = sensorServer_->RunCommand(sensorId, cmdType, params); - FinishTrace(HITRACE_TAG_SENSORS); - if (ret != ERR_OK) { - SEN_HILOGE("RunCommand failed"); - return ret; - } - return ret; -} - std::vector SensorServiceClient::GetSensorList() { CALL_LOG_ENTER; diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index b0406b7a1ab6b066779538011780cffac1b1a41c..fa826efd9cc4daf7da033fba6057783ae2e134ea 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -95,66 +95,6 @@ ErrCode SensorServiceProxy::DisableSensor(uint32_t sensorId) return static_cast(ret); } -int32_t SensorServiceProxy::GetSensorState(uint32_t sensorId) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { - SEN_HILOGE("write descriptor failed"); - return WRITE_MSG_ERR; - } - if (!data.WriteUint32(sensorId)) { - SEN_HILOGE("write sensorId failed"); - return WRITE_MSG_ERR; - } - sptr remote = Remote(); - CHKPR(remote, ERROR); - int32_t ret = remote->SendRequest(ISensorService::GET_SENSOR_STATE, data, reply, option); - if (ret != NO_ERROR) { - HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "PKG_NAME", "GetSensorState", "ERROR_CODE", ret); - SEN_HILOGE("failed, ret:%{public}d", ret); - } - return static_cast(ret); -} - -ErrCode SensorServiceProxy::RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params) -{ - if (cmdType > RESERVED) { - SEN_HILOGE("failed, cmdType:%{public}u", cmdType); - return CMD_TYPE_ERR; - } - MessageParcel data; - MessageParcel reply; - MessageOption option; - if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { - SEN_HILOGE("write descriptor failed"); - return WRITE_MSG_ERR; - } - if (!data.WriteUint32(sensorId)) { - SEN_HILOGE("write sensorId failed"); - return WRITE_MSG_ERR; - } - if (!data.WriteUint32(cmdType)) { - SEN_HILOGE("write cmdType failed"); - return WRITE_MSG_ERR; - } - if (!data.WriteUint32(params)) { - SEN_HILOGE("write params failed"); - return WRITE_MSG_ERR; - } - sptr remote = Remote(); - CHKPR(remote, ERROR); - int32_t ret = remote->SendRequest(ISensorService::RUN_COMMAND, data, reply, option); - if (ret != NO_ERROR) { - HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "PKG_NAME", "RunCommand", "ERROR_CODE", ret); - SEN_HILOGE("failed, ret:%{public}d", ret); - } - return static_cast(ret); -} - std::vector SensorServiceProxy::GetSensorList() { MessageParcel data; diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 85531a585d586719192e43621f5fc0bf4775ef02..96a24d6ff94d0e8c8583e15767f71a92bf586e1d 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -47,7 +47,7 @@ extern "C" { /** Maximum length of the sensor name */ #ifndef NAME_MAX_LEN -#define NAME_MAX_LEN 48 +#define NAME_MAX_LEN 128 #endif /* NAME_MAX_LEN */ /** Size of sensor data */ #ifndef SENSOR_USER_DATA_SIZE diff --git a/interfaces/native/src/sensor_agent.cpp b/interfaces/native/src/sensor_agent.cpp index c370db5c883e696976013b21cf7d5794ac330f19..b09a25315e1f1e065dce55d780d0e78fdbc1634a 100755 --- a/interfaces/native/src/sensor_agent.cpp +++ b/interfaces/native/src/sensor_agent.cpp @@ -147,13 +147,3 @@ int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) } return proxy->SetMode(sensorId, user, mode); } - -int32_t SetOption(int32_t sensorId, const SensorUser *user, int32_t option) -{ - const SensorAgentProxy *proxy = GetInstance(); - if (proxy == nullptr) { - SEN_HILOGE("proxy is nullptr"); - return SERVICE_EXCEPTION; - } - return proxy->SetOption(sensorId, user, option); -} \ No newline at end of file diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index a9913aa7b100b0e9198fbff30e4de9c19fe1ebea..5a175be21419441dd1c75a3cef2b693b9dfae6c5 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -48,8 +48,36 @@ ohos_unittest("SensorAgentTest") { ] } +ohos_unittest("SensorAlgorithmTest") { + module_out_path = module_output_path + + sources = [ "unittest/sensor_algorithm_test.cpp" ] + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "$SUBSYSTEM_DIR/sensor/utils/include", + "$SUBSYSTEM_DIR/sensor/interfaces/native/include", + "$SUBSYSTEM_DIR/sensor/test/unittest/common/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/sensor/interfaces/native:sensor_ndk_target", + "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + ###########################end########################### group("unittest") { testonly = true - deps = [ ":SensorAgentTest" ] + deps = [ + ":SensorAgentTest", + ":SensorAlgorithmTest", + ] } diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index 4dc06b25735c79aa8be106cecacbd2711b45246d..1227168397885b28bbb0448407617aee4a557e57 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -117,6 +117,17 @@ void SensorDataCallbackImpl(SensorEvent *event) event[0].sensorTypeId, event[0].version, event[0].dataLen, *(sensorData)); } +void SensorDataCallbackImpl2(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("SensorEvent is null"); + return; + } + float *sensorData = (float *)event[0].data; + SEN_HILOGI("SensorId:%{public}d, version:%{public}d,dataLen:%{public}d,data:%{public}f", + event[0].sensorTypeId, event[0].version, event[0].dataLen, *(sensorData)); +} + HWTEST_F(SensorAgentTest, GetAllSensorsTest_001, TestSize.Level1) { SEN_HILOGI("GetAllSensorsTest_001 in"); @@ -385,5 +396,67 @@ HWTEST_F(SensorAgentTest, SensorListTest_001, TestSize.Level1) sensorInfo[i].minSamplePeriod, sensorInfo[i].maxSamplePeriod); } } + +/* + * Feature: sensor + * Function: SubscribeSensor + * FunctionPoints: Check the interface function + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the sensor service framework process. + */ +HWTEST_F(SensorAgentTest, SensorNativeApiTest_002, TestSize.Level1) +{ + SEN_HILOGI("SensorNativeApiTest_002 in"); + + SensorUser user; + user.callback = SensorDataCallbackImpl; + + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = SetBatch(sensorId, &user, 100000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + SensorUser user2; + user2.callback = SensorDataCallbackImpl2; + + ret = SubscribeSensor(sensorId, &user2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = SetBatch(sensorId, &user2, 200000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = ActivateSensor(sensorId, &user2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + ret = DeactivateSensor(sensorId, &user2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = UnsubscribeSensor(sensorId, &user2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorAgentTest, SensorNativeApiTest_003, TestSize.Level1) +{ + SEN_HILOGI("SensorNativeApiTest_003 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = DeactivateSensor(sensorId, &user); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorAgentTest, SensorNativeApiTest_004, TestSize.Level1) +{ + SEN_HILOGI("SensorNativeApiTest_004 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = SetMode(sensorId, &user, SENSOR_DEFAULT_MODE); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} } // namespace Sensors } // namespace OHOS diff --git a/interfaces/native/test/unittest/sensor_algorithm_test.cpp b/interfaces/native/test/unittest/sensor_algorithm_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f4004861dd04e15b6496a4fdec550b33b0252643 --- /dev/null +++ b/interfaces/native/test/unittest/sensor_algorithm_test.cpp @@ -0,0 +1,342 @@ +/* + * Copyright (c) 2021 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 "geomagnetic_field.h" +#include "sensor_algorithm.h" +#include "sensors_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, "SensorAlgorithmTest" }; +constexpr int32_t QUATERNION_LENGTH = 4; +constexpr int32_t ROTATION_VECTOR_LENGTH = 3; +constexpr int32_t THREE_DIMENSIONAL_MATRIX_LENGTH = 9; +} // namespace + +class SensorAlgorithmTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void SensorAlgorithmTest::SetUpTestCase() {} + +void SensorAlgorithmTest::TearDownTestCase() {} + +void SensorAlgorithmTest::SetUp() {} + +void SensorAlgorithmTest::TearDown() {} + +SensorAlgorithm sensorAlgorithm; + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_001, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_001 in"); + std::vector rotationVector = {0.52, -0.336, -0.251}; + std::vector quaternion(QUATERNION_LENGTH); + int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ASSERT_EQ(quaternion.size(), QUATERNION_LENGTH); + std::vector result = {0.7441122531890869, 0.5199999809265137, -0.335999995470047, -0.25099998712539673}; + for (size_t i = 0; i < QUATERNION_LENGTH; ++i) { + ASSERT_EQ(quaternion[i], result[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_002, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_002 in"); + std::vector rotationVector = {}; + std::vector quaternion(QUATERNION_LENGTH); + int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_003, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_003 in"); + std::vector rotationVector = {0.52, -0.336, -0.251}; + std::vector quaternion(ROTATION_VECTOR_LENGTH - 1); + int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_004, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_004 in"); + std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, 2, outRotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::vector result = + {1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000}; + ASSERT_EQ(outRotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); + for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { + ASSERT_EQ(outRotationMatrix[i], result[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_005, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_005 in"); + std::vector inRotationMatrix(3); + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, 2, outRotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_006, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_006 in"); + std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, -1, outRotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_007, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_007 in"); + std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, -1, 1, outRotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_008, TestSize.Level1) +{ + float altitude = -1.0; + int32_t ret = sensorAlgorithm.GetAltitude(5.0, 0.0, &altitude); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ASSERT_EQ(altitude, 44330.0); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_009, TestSize.Level1) +{ + int32_t ret = sensorAlgorithm.GetAltitude(5.0, 0.0, nullptr); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_010, TestSize.Level1) +{ + float geomagneticDip = -1.0; + std::vector inclinationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ASSERT_EQ(geomagneticDip, 0.8760581016540527); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_011, TestSize.Level1) +{ + std::vector inclinationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, nullptr); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_012, TestSize.Level1) +{ + std::vector inclinationMatrix(3); + float geomagneticDip = -1.0; + int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_013, TestSize.Level1) +{ + std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector angleChange(ROTATION_VECTOR_LENGTH); + int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ASSERT_EQ(angleChange.size(), ROTATION_VECTOR_LENGTH); + std::vector result = {0.0, -0.0, -0.0}; + for (size_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) { + ASSERT_EQ(angleChange[i], result[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_014, TestSize.Level1) +{ + std::vector currotationMatrix(3); + std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector angleChange(ROTATION_VECTOR_LENGTH); + int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_015, TestSize.Level1) +{ + std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector preRotationMatrix(ROTATION_VECTOR_LENGTH); + std::vector angleChange(ROTATION_VECTOR_LENGTH); + int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_016, TestSize.Level1) +{ + std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector angleChange(ROTATION_VECTOR_LENGTH - 1); + int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_017, TestSize.Level1) +{ + std::vector rotationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + std::vector rotationAngle(ROTATION_VECTOR_LENGTH); + int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::vector result = {0.38050639629364014, -0.9783217310905457, -0.6610431671142578}; + ASSERT_EQ(rotationAngle.size(), ROTATION_VECTOR_LENGTH); + for (size_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) { + ASSERT_EQ(rotationAngle[i], result[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_018, TestSize.Level1) +{ + std::vector rotationMatrix(5); + std::vector rotationAngle(ROTATION_VECTOR_LENGTH); + int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_019, TestSize.Level1) +{ + std::vector rotationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + std::vector rotationAngle(ROTATION_VECTOR_LENGTH - 1); + int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_020, TestSize.Level1) +{ + std::vector rotationVector = {0.0, 0.0, 0.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::vector result = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; + ASSERT_EQ(rotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); + for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { + ASSERT_EQ(rotationMatrix[i], result[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_021, TestSize.Level1) +{ + std::vector rotationVector(ROTATION_VECTOR_LENGTH - 1); + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_022, TestSize.Level1) +{ + std::vector rotationVector = {0.0, 0.0, 0.0}; + std::vector rotationMatrix(ROTATION_VECTOR_LENGTH - 1); + int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_023, TestSize.Level1) +{ + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ASSERT_EQ(rotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); + ASSERT_EQ(inclinationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector rotationMatrixResult = + {-0.7980074882507324, 0.5486301183700562, 0.24937734007835388, -0.17277367413043976, + -0.6047078967094421, 0.7774815559387207, 0.5773502588272095, 0.5773502588272095, 0.5773502588272095}; + std::vector inclinationMatrixResult = {1.0, 0.0, 0.0, 0.0, 0.20444221794605255, + 0.9788785576820374, 0, -0.9788785576820374, 0.20444221794605255}; + for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { + ASSERT_EQ(rotationMatrix[i], rotationMatrixResult[i]); + ASSERT_EQ(inclinationMatrix[i], inclinationMatrixResult[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_024, TestSize.Level1) +{ + std::vector gravity(ROTATION_VECTOR_LENGTH - 1); + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_025, TestSize.Level1) +{ + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic(ROTATION_VECTOR_LENGTH - 1); + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_026, TestSize.Level1) +{ + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(ROTATION_VECTOR_LENGTH - 1); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_027, TestSize.Level1) +{ + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(ROTATION_VECTOR_LENGTH - 1); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_028, TestSize.Level1) +{ + GeomagneticField geomagneticField(80.0, 0.0, 0.0, 1580486400000); + ASSERT_EQ(geomagneticField.ObtainX(), 6570.3935546875); + ASSERT_EQ(geomagneticField.ObtainY(), -146.3289337158203); + ASSERT_EQ(geomagneticField.ObtainZ(), 54606.0078125); + ASSERT_EQ(geomagneticField.ObtainGeomagneticDip(), 83.13726043701172); + ASSERT_EQ(geomagneticField.ObtainDeflectionAngle(), -1.2758207321166992); + ASSERT_EQ(geomagneticField.ObtainLevelIntensity(), 6572.02294921875); + ASSERT_EQ(geomagneticField.ObtainTotalIntensity(), 55000.0703125); +} +} // namespace Sensors +} // namespace OHOS diff --git a/interfaces/plugin/include/async_callback_info.h b/interfaces/plugin/include/async_callback_info.h index 2d515f213513d9b470ce0ae31ae4a4c4ab3d56db..a506bae737209b2296aa16ca3915d540e11e054c 100644 --- a/interfaces/plugin/include/async_callback_info.h +++ b/interfaces/plugin/include/async_callback_info.h @@ -111,14 +111,16 @@ public: ~AsyncCallbackInfo() { CALL_LOG_ENTER; - if (asyncWork != nullptr) { - SEN_HILOGD("Delete async work"); - napi_delete_async_work(env, asyncWork); - } - for (int32_t i = 0; i < CALLBACK_NUM; ++i) { - if (callback[i] != nullptr) { - SEN_HILOGD("Delete reference, i:%{public}d", i); - napi_delete_reference(env, callback[i]); + if (type != ONCE_CALLBACK) { + if (asyncWork != nullptr) { + SEN_HILOGD("Delete async work"); + napi_delete_async_work(env, asyncWork); + } + for (int32_t i = 0; i < CALLBACK_NUM; ++i) { + if (callback[i] != nullptr) { + SEN_HILOGD("Delete reference, i:%{public}d", i); + napi_delete_reference(env, callback[i]); + } } } if (work != nullptr) { diff --git a/interfaces/plugin/include/sensor_napi_utils.h b/interfaces/plugin/include/sensor_napi_utils.h index 6adfdb42aa2fcd82ffcf48357c0c27a22ccd4ec8..ca4549ea1caf10c8353047b56cb35fd5f7f1f7af 100644 --- a/interfaces/plugin/include/sensor_napi_utils.h +++ b/interfaces/plugin/include/sensor_napi_utils.h @@ -57,6 +57,8 @@ bool CreateFailMessage(CallbackDataType type, int32_t code, string message, sptr &asyncCallbackInfo); bool ConvertToBodyData(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); bool ConvertToCompass(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); +void ReleaseCallback(sptr asyncCallbackInfo); + #define CHKNCF(env, cond, message) \ do { \ diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index c87bb402b5c270b25c7cc5f51f19247e3c2ae99d..d94619d041869bf5e604d711d4019b82fb639566 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -137,14 +137,17 @@ static void EmitOnceCallback(SensorEvent *event) if (iter == g_onceCallbackInfos.end()) { return; } - for (auto &onceCallbackInfo : iter->second) { + auto& onceCallbackInfos = iter->second; + while (!onceCallbackInfos.empty()) { + auto onceCallbackInfo = onceCallbackInfos.front(); + auto beginIter = onceCallbackInfos.begin(); + onceCallbackInfos.erase(beginIter); if (!copySensorData(onceCallbackInfo, event)) { SEN_HILOGE("Copy sensor data failed"); continue; } - EmitUvEventLoop(onceCallbackInfo); + EmitUvEventLoop(std::move(onceCallbackInfo)); } - g_onceCallbackInfos[sensorTypeId].clear(); g_onceCallbackInfos.erase(sensorTypeId); CHKCV((!CheckSubscribe(sensorTypeId)), "Has client subscribe, not need cancel subscribe"); @@ -349,12 +352,26 @@ static napi_value On(napi_env env, napi_callback_info info) return nullptr; } -static void RemoveAllCallback(napi_env env, int32_t sensorTypeId) +static int32_t RemoveAllCallback(napi_env env, int32_t sensorTypeId) { CALL_LOG_ENTER; std::lock_guard onCallbackLock(onMutex_); - g_onCallbackInfos[sensorTypeId].clear(); - g_onCallbackInfos.erase(sensorTypeId); + std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; + for (auto iter = callbackInfos.begin(); iter != callbackInfos.end();) { + CHKPC(*iter); + if ((*iter)->env != env) { + ++iter; + continue; + } + iter = callbackInfos.erase(iter); + } + if (callbackInfos.empty()) { + SEN_HILOGD("No subscription to change sensor data"); + g_onCallbackInfos.erase(sensorTypeId); + return 0; + } + g_onCallbackInfos[sensorTypeId] = callbackInfos; + return callbackInfos.size(); } static int32_t RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value callback) @@ -364,6 +381,9 @@ static int32_t RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value cal std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; for (auto iter = callbackInfos.begin(); iter != callbackInfos.end(); ++iter) { CHKPC(*iter); + if ((*iter)->env != env) { + continue; + } napi_value sensorCallback = nullptr; if (napi_get_reference_value(env, (*iter)->callback[0], &sensorCallback) != napi_ok) { SEN_HILOGE("napi_get_reference_value fail"); @@ -400,17 +420,17 @@ static napi_value Off(napi_env env, napi_callback_info info) ThrowErr(env, PARAMETER_ERROR, "Wrong argument type or get number fail"); return nullptr; } + int32_t subscribeSize = -1; if (argc == 1) { - RemoveAllCallback(env, sensorTypeId); + subscribeSize = RemoveAllCallback(env, sensorTypeId); } else { if (!IsMatchType(env, args[1], napi_function)) { ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function"); return nullptr; } - CHKCP((RemoveCallback(env, sensorTypeId, args[1]) == 0), - "There are other client subscribe as well, not need unsubscribe"); + subscribeSize = RemoveCallback(env, sensorTypeId, args[1]); } - if (CheckSystemSubscribe(sensorTypeId)) { + if (CheckSystemSubscribe(sensorTypeId) || (subscribeSize > 0)) { SEN_HILOGW("There are other client subscribe system js api as well, not need unsubscribe"); return nullptr; } @@ -501,6 +521,16 @@ static napi_value GetGeomagneticField(napi_env env, napi_callback_info info) return nullptr; } +static napi_value GetAxisX(napi_env env, napi_value value) +{ + return GetNamedProperty(env, value, "x"); +} + +static napi_value GetAxisY(napi_env env, napi_value value) +{ + return GetNamedProperty(env, value, "y"); +} + static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info info) { CALL_LOG_ENTER; @@ -526,7 +556,7 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf ThrowErr(env, PARAMETER_ERROR, "Wrong inRotationVector length"); return nullptr; } - napi_value napiAxisX = GetNamedProperty(env, args[1], "axisX"); + napi_value napiAxisX = GetAxisX(env, args[1]); if (napiAxisX == nullptr) { ThrowErr(env, PARAMETER_ERROR, "napiAxisX is null"); return nullptr; @@ -536,9 +566,9 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf ThrowErr(env, PARAMETER_ERROR, "Get axisY fail"); return nullptr; } - napi_value napiAxisY = GetNamedProperty(env, args[1], "axisY"); + napi_value napiAxisY = GetAxisY(env, args[1]); if (napiAxisY == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiAxisX is null"); + ThrowErr(env, PARAMETER_ERROR, "napiAxisY is null"); return nullptr; } int32_t axisY = 0; diff --git a/interfaces/plugin/src/sensor_napi_error.cpp b/interfaces/plugin/src/sensor_napi_error.cpp index 1b2cc5e89636e1f396a42c73141eb7009a482bac..9ff41bb714f806c6379405ae4a595b04615de547 100755 --- a/interfaces/plugin/src/sensor_napi_error.cpp +++ b/interfaces/plugin/src/sensor_napi_error.cpp @@ -51,8 +51,11 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri SEN_HILOGE("errCode: %{public}d is invalid", errCode); return; } + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); napi_value error = CreateBusinessError(env, errCode, msg.value()); napi_throw(env, error); + napi_close_handle_scope(env, scope); } } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 4cfecb6827b2522fc6282b6e9610e95323881325..ab3e5fb8b17aaa41137af6ab9ef4d20c6bb1ed67 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -418,6 +418,19 @@ bool CreateNapiArray(const napi_env &env, float data[], int32_t dataLength, napi return true; } +void ReleaseCallback(sptr asyncCallbackInfo) +{ + CHKPV(asyncCallbackInfo); + if (asyncCallbackInfo->type == ONCE_CALLBACK) { + napi_env env = asyncCallbackInfo->env; + CHKPV(env); + napi_ref callback = asyncCallbackInfo->callback[0]; + if (callback != nullptr) { + napi_delete_reference(env, callback); + } + } +} + void EmitAsyncCallbackWork(sptr asyncCallbackInfo) { CALL_LOG_ENTER; @@ -504,6 +517,7 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) napi_open_handle_scope(asyncCallbackInfo->env, &scope); if (scope == nullptr) { SEN_HILOGE("napi_handle_scope is nullptr"); + ReleaseCallback(asyncCallbackInfo); return; } napi_env env = asyncCallbackInfo->env; @@ -511,6 +525,7 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) if (napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback) != napi_ok) { SEN_HILOGE("napi_get_reference_value fail"); napi_throw_error(env, nullptr, "napi_get_reference_value fail"); + ReleaseCallback(asyncCallbackInfo); napi_close_handle_scope(asyncCallbackInfo->env, scope); return; } @@ -519,6 +534,7 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) if (!(g_convertfuncList.find(asyncCallbackInfo->type) != g_convertfuncList.end())) { SEN_HILOGE("asyncCallbackInfo type is invalid"); napi_throw_error(env, nullptr, "asyncCallbackInfo type is invalid"); + ReleaseCallback(asyncCallbackInfo); napi_close_handle_scope(asyncCallbackInfo->env, scope); return; } @@ -526,9 +542,11 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) if (napi_call_function(env, nullptr, callback, 1, &result[1], &callResult) != napi_ok) { SEN_HILOGE("napi_call_function callback fail"); napi_throw_error(env, nullptr, "napi_call_function callback fail"); + ReleaseCallback(asyncCallbackInfo); napi_close_handle_scope(asyncCallbackInfo->env, scope); return; } + ReleaseCallback(asyncCallbackInfo); napi_close_handle_scope(asyncCallbackInfo->env, scope); asyncCallbackInfo->work = nullptr; freeWork(work); diff --git a/interfaces/plugin/test/unittest/ExampleJsunit.test.js b/interfaces/plugin/test/unittest/ExampleJsunit.test.js index 3ab3807c9233f15f752c173507ef308428fa2a46..18dad8b95dddc76deb0c1de0cc1a8a44b89f67fe 100755 --- a/interfaces/plugin/test/unittest/ExampleJsunit.test.js +++ b/interfaces/plugin/test/unittest/ExampleJsunit.test.js @@ -3460,7 +3460,7 @@ describe("SensorJsTest", function () { */ it('Sensor_TransformCoordinateSystem_001', 0, async function (done) { console.info("---------------------------Sensor_TransformCoordinateSystem_001----------------------------------"); - sensor.transformRotationMatrix([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], {'axisX':1, 'axisY':2}, (error, data) => { + sensor.transformRotationMatrix([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], {'x':1, 'y':2}, (error, data) => { if (error) { console.info('Sensor_TransformCoordinateSystem_001 failed'); expect(false).assertTrue(); @@ -3480,7 +3480,7 @@ describe("SensorJsTest", function () { */ it('Sensor_TransformCoordinateSystem_002', 0, async function (done) { console.info("---------------------------Sensor_TransformCoordinateSystem_002----------------------------------"); - sensor.transformRotationMatrix([3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38], {'axisX':1, 'axisY':2}, (error, data) => { + sensor.transformRotationMatrix([3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38], {'x':1, 'y':2}, (error, data) => { if (error) { console.info('Sensor_TransformCoordinateSystem_002 failed'); expect(false).assertTrue(); @@ -3500,7 +3500,7 @@ describe("SensorJsTest", function () { */ it("Sensor_TransformCoordinateSystem_003", 0, async function (done) { console.info("---------------------------Sensor_TransformCoordinateSystem_003----------------------------------"); - sensor.transformRotationMatrix([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], {'axisX':1, 'axisY':2}).then((data) => { + sensor.transformRotationMatrix([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], {'x':1, 'y':2}).then((data) => { for (var i = 0; i < data.length; i++) { console.info("Sensor_TransformCoordinateSystem_003 data[ " + i + "] = " + data[i]); expect(data[i]).assertEqual(transformCoordinateSystemResult[0][i]); @@ -3521,7 +3521,7 @@ describe("SensorJsTest", function () { */ it("Sensor_TransformCoordinateSystem_004", 0, async function (done) { console.info("---------------------------Sensor_TransformCoordinateSystem_004----------------------------------"); - sensor.transformRotationMatrix([3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39], {'axisX':1, 'axisY':3}).then((data) => { + sensor.transformRotationMatrix([3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39], {'x':1, 'y':3}).then((data) => { for (var i = 0; i < data.length; i++) { console.info("Sensor_TransformCoordinateSystem_004 data[ " + i + "] = " + data[i]); expect(data[i]).assertEqual(transformCoordinateSystemResult[2][i]); @@ -3642,7 +3642,7 @@ describe("SensorJsTest", function () { it('Sensor_TransformCoordinateSystem_009', 0, async function (done) { console.info('Sensor_TransformCoordinateSystem_008 start') try { - sensor.transformRotationMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], {'axisX':1, 'axisY':1}).then((data)=>{ + sensor.transformRotationMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], {'x':1, 'y':1}).then((data)=>{ console.info("Sensor_TransformCoordinateSystem_009" + data) expect(true).assertfalse() done() @@ -3668,7 +3668,7 @@ describe("SensorJsTest", function () { it('Sensor_TransformCoordinateSystem_010', 0, async function (done) { console.info('Sensor_TransformCoordinateSystem_010 start') try { - sensor.transformRotationMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], {'axisX':1, 'axisY':1}, (error, data) => { + sensor.transformRotationMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], {'x':1, 'y':1}, (error, data) => { if (error) { console.info('Sensor_TransformCoordinateSystem_010 failed'); expect(false).assertTrue(); diff --git a/interfaces/plugin/test/unittest/config.json b/interfaces/plugin/test/unittest/config.json index b4bc5993bb2744aa49d7b454fbb54ded36840da8..026e069a5396ecb6b29de7b5bad6d427758ce528 100755 --- a/interfaces/plugin/test/unittest/config.json +++ b/interfaces/plugin/test/unittest/config.json @@ -28,7 +28,8 @@ "package": "com.example.myapplication", "name": ".MyApplication", "deviceType": [ - "phone" + "default", + "tablet" ], "distro": { "deliveryWithInstall": true, diff --git a/services/sensor/hdi_connection/adapter/include/compatible_connection.h b/services/sensor/hdi_connection/adapter/include/compatible_connection.h index 1dad247a2c4b259afa37e8e02308472973612a32..3ce78807265a79e0d06e5b76ee41c262f31528db 100644 --- a/services/sensor/hdi_connection/adapter/include/compatible_connection.h +++ b/services/sensor/hdi_connection/adapter/include/compatible_connection.h @@ -31,8 +31,6 @@ public: 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, int32_t option) override; - int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) override; int32_t RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) override; int32_t DestroyHdiConnection() override; diff --git a/services/sensor/hdi_connection/adapter/include/hdi_connection.h b/services/sensor/hdi_connection/adapter/include/hdi_connection.h index f93de5be9677edd436b108d44e0d9fd6c86f1744..53436867a49b2cb1051c53fd4127b1069c0bfcfa 100644 --- a/services/sensor/hdi_connection/adapter/include/hdi_connection.h +++ b/services/sensor/hdi_connection/adapter/include/hdi_connection.h @@ -32,8 +32,6 @@ public: 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, int32_t option) override; - int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) override; int32_t RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) override; int32_t DestroyHdiConnection() override; ReportDataCb GetReportDataCb(); diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index 1863ffd63f61687771c48168caa34188248daba0..821504b0df3b5dc1f1af10e97c7a65740a432a31 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -109,21 +109,6 @@ int32_t CompatibleConnection::SetMode(int32_t sensorId, int32_t mode) return ERR_OK; } -int32_t CompatibleConnection::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) -{ - return ERR_OK; -} - -int32_t CompatibleConnection::SetOption(int32_t sensorId, int32_t option) -{ - int32_t ret = hdiServiceImpl_.SetOption(sensorId, option); - if (ret != 0) { - SEN_HILOGI("set option failed, sensorId:%{public}d", sensorId); - return ret; - } - return ERR_OK; -} - int32_t CompatibleConnection::SensorDataCallback(const SensorEvents *event) { CHKPR(event, ERR_INVALID_VALUE); diff --git a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp index 524d0e4190494b416421011219b9b0652f641cd6..1727f29685de71ed5d9af9406f79079628e26e2f 100644 --- a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp @@ -152,20 +152,6 @@ int32_t HdiConnection::SetMode(int32_t sensorId, int32_t mode) return ERR_OK; } -int32_t HdiConnection::SetOption(int32_t sensorId, int32_t option) -{ - CALL_LOG_ENTER; - CHKPR(sensorInterface_, ERR_NO_INIT); - int32_t ret = sensorInterface_->SetOption(sensorId, option); - if (ret != 0) { - HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "PKG_NAME", "SetOption", "ERROR_CODE", ret); - SEN_HILOGE("SetOption is failed"); - return ret; - } - return ERR_OK; -} - int32_t HdiConnection::RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) { CALL_LOG_ENTER; @@ -199,11 +185,6 @@ int32_t HdiConnection::DestroyHdiConnection() return ERR_OK; } -int32_t HdiConnection::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) -{ - return 0; -} - ReportDataCb HdiConnection::GetReportDataCb() { if (reportDataCb_ == nullptr) { 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 8056a67cd6e456194857ef2699ff8008c34d01ee..26095217594046e2e1461b5dda2a475a78498bf7 100644 --- a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h +++ b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h @@ -34,8 +34,6 @@ public: int32_t DisableSensor(int32_t sensorId); int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval); int32_t SetMode(int32_t sensorId, int32_t mode); - int32_t SetOption(int32_t sensorId, int32_t option); - int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params); int32_t Register(RecordDataCallback cb); int32_t Unregister(); 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 6bec1fae81ef10ab655d9654e27d7133be2a721a..6a67a16c5c1a0415c4487cee90f68f0e5664ca1e 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -129,16 +129,6 @@ int32_t HdiServiceImpl::SetMode(int32_t sensorId, int32_t mode) return ERR_OK; } -int32_t HdiServiceImpl::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) -{ - return ERR_OK; -} - -int32_t HdiServiceImpl::SetOption(int32_t sensorId, int32_t option) -{ - return ERR_OK; -} - int32_t HdiServiceImpl::Register(RecordDataCallback cb) { CHKPR(cb, ERROR); 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 c0890f9ccea9e25ebec633059affa33034369cd6..c3b28f153f2f964c2ec8b2154391eac62cb1c609 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,8 +33,6 @@ public: 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, int32_t option) = 0; - virtual int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) = 0; virtual int32_t RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) = 0; virtual int32_t DestroyHdiConnection() = 0; static std::mutex dataMutex_; 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 6c67144db9f1e593757962b3b30ce0613125878c..f027c46072eb27cd22a577d44b6c58948e5c4a86 100644 --- a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h @@ -31,8 +31,6 @@ public: 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, int32_t option) override; - int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) override; int32_t RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) override; int32_t DestroyHdiConnection() 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 7f1ca5e6ec05f7cab4ee50810438a21a59a6b593..9b59e9f7de145fb4ba9814c6cfd23f84ad77dc3c 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -111,30 +111,6 @@ int32_t SensorHdiConnection::SetMode(int32_t sensorId, int32_t mode) return ret; } -int32_t SensorHdiConnection::SetOption(int32_t sensorId, int32_t option) -{ - StartTrace(HITRACE_TAG_SENSORS, "SetOption"); - int32_t ret = iSensorHdiConnection_->SetOption(sensorId, option); - FinishTrace(HITRACE_TAG_SENSORS); - if (ret != 0) { - SEN_HILOGI("set option failed, sensorId:%{public}d", sensorId); - return SET_SENSOR_OPTION_ERR; - } - return ret; -} - -int32_t SensorHdiConnection::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) -{ - StartTrace(HITRACE_TAG_SENSORS, "RunCommand"); - int32_t ret = iSensorHdiConnection_->RunCommand(sensorId, cmd, params); - FinishTrace(HITRACE_TAG_SENSORS); - if (ret != 0) { - SEN_HILOGI("run command failed, sensorId:%{public}d", sensorId); - return RUN_COMMAND_ERR; - } - return ret; -} - int32_t SensorHdiConnection::RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) { StartTrace(HITRACE_TAG_SENSORS, "RegisteDataReport"); diff --git a/services/sensor/include/fifo_cache_data.h b/services/sensor/include/fifo_cache_data.h index 7a1713b59ec8a572aa9083f6e4ccb1f3624d062e..6361e06fddb263dc4cf597709a470f87e2ed2ad1 100644 --- a/services/sensor/include/fifo_cache_data.h +++ b/services/sensor/include/fifo_cache_data.h @@ -40,7 +40,7 @@ public: private: DISALLOW_COPY_AND_MOVE(FifoCacheData); uint64_t periodCount_; - sptr channel_; + wptr channel_; std::vector fifoCacheData_; }; } // namespace Sensors diff --git a/services/sensor/include/flush_info_record.h b/services/sensor/include/flush_info_record.h index fe100f7e65664ca7a35831753d15363f21686328..d2c02a8aef37ba823e8c05c7cab4da67591cfe1f 100644 --- a/services/sensor/include/flush_info_record.h +++ b/services/sensor/include/flush_info_record.h @@ -33,7 +33,7 @@ namespace OHOS { namespace Sensors { struct FlushInfo { - sptr flushChannel; + wptr flushChannel; bool flushFromEnable; FlushInfo(const sptr &channel, bool enableFlush) : flushChannel(channel), flushFromEnable(enableFlush){}; @@ -57,7 +57,6 @@ public: private: DISALLOW_COPY_AND_MOVE(FlushInfoRecord); - SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); // sensorId, channel pointer for pending flush. std::unordered_map> flushInfo_; diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index a436cf89adc7c5377f994b9f4f67c6af91248997..d28a6681926f1b3ce0f67a986ad8b6cbb1c8dbac 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -49,8 +49,6 @@ public: int Dump(int fd, const std::vector &args) override; ErrCode EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) override; ErrCode DisableSensor(uint32_t sensorId) override; - int32_t GetSensorState(uint32_t sensorId) override; - ErrCode RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params) override; std::vector GetSensorList() override; ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) override; @@ -77,7 +75,6 @@ private: SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); - FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); sptr sensorDataProcesser_; sptr reportDataCallback_; std::mutex uidLock_; diff --git a/services/sensor/include/sensor_service_stub.h b/services/sensor/include/sensor_service_stub.h index c816b42509a67ab2fc266a716404d6e278dd5533..9259e793f11101593e9d19465052a4a08266169e 100644 --- a/services/sensor/include/sensor_service_stub.h +++ b/services/sensor/include/sensor_service_stub.h @@ -36,8 +36,6 @@ private: using SensorBaseFunc = ErrCode (SensorServiceStub::*)(MessageParcel &data, MessageParcel &reply); ErrCode SensorEnableInner(MessageParcel &data, MessageParcel &reply); ErrCode SensorDisableInner(MessageParcel &data, MessageParcel &reply); - ErrCode GetSensorStateInner(MessageParcel &data, MessageParcel &reply); - ErrCode RunCommandInner(MessageParcel &data, MessageParcel &reply); ErrCode GetAllSensorsInner(MessageParcel &data, MessageParcel &reply); ErrCode CreateDataChannelInner(MessageParcel &data, MessageParcel &reply); ErrCode DestroyDataChannelInner(MessageParcel &data, MessageParcel &reply); diff --git a/services/sensor/src/fifo_cache_data.cpp b/services/sensor/src/fifo_cache_data.cpp index b3aa44c816f6b84cf95153cd0af666a581d45c24..45bf5ef415bf88bd940967c7a48449f8825bc750 100644 --- a/services/sensor/src/fifo_cache_data.cpp +++ b/services/sensor/src/fifo_cache_data.cpp @@ -14,9 +14,9 @@ */ #include "fifo_cache_data.h" - namespace OHOS { namespace Sensors { + FifoCacheData::FifoCacheData() : periodCount_(0), channel_(nullptr) {} @@ -55,9 +55,10 @@ void FifoCacheData::SetChannel(const sptr &channel) { channel_ = channel; } + sptr FifoCacheData::GetChannel() const { - return channel_; + return channel_.promote(); } } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/flush_info_record.cpp b/services/sensor/src/flush_info_record.cpp index 6e1ea29dbf74cb6dc4fa7da65132cc772502236b..25e158e0ceba900276a82e732b175aec75930f36 100644 --- a/services/sensor/src/flush_info_record.cpp +++ b/services/sensor/src/flush_info_record.cpp @@ -69,7 +69,6 @@ bool FlushInfoRecord::IsFlushChannelValid(const std::vector(currChannelList.size())); for (const auto &channel : currChannelList) { - SEN_HILOGD("channel:%{public}p,flushchannel:%{public}p", channel.GetRefPtr(), flushChannel.GetRefPtr()); if (channel == flushChannel) { return true; } @@ -81,7 +80,7 @@ int32_t FlushInfoRecord::GetFlushChannelIndex(const std::vector &flus const sptr &channel) { for (size_t i = 0; i < flushInfoList.size(); i++) { - if (flushInfoList[i].flushChannel == channel) { + if (flushInfoList[i].flushChannel.promote() == channel) { return i; } } @@ -91,14 +90,9 @@ int32_t FlushInfoRecord::GetFlushChannelIndex(const std::vector &flus ErrCode FlushInfoRecord::FlushProcess(const uint32_t sensorId, const uint32_t flag, const int32_t pid, const bool isEnableFlush) { - auto ret = sensorHdiConnection_.RunCommand(sensorId, FLUSH, 0); - if (ret != ERR_OK) { - SEN_HILOGE("flush command failed"); - return ret; - } sptr channel = clientInfo_.GetSensorChannelByPid(pid); CHKPR(channel, ERROR); - ret = SetFlushInfo(sensorId, channel, false); + int32_t ret = SetFlushInfo(sensorId, channel, false); if (ret != ERR_OK) { SEN_HILOGE("set flush info failed"); return ret; diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 5dc6a707c8a61735d35ecd833284d7033c6d8602..967839b8982d527523ae579155767cf88ce0aef8 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -68,9 +68,6 @@ void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map dataCountLock(dataCountMutex_); sendEvents.push_back(event); uint32_t sensorId = static_cast(event.sensorTypeId); - if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); - } auto dataCountIt = dataCountMap_.find(sensorId); if (dataCountIt == dataCountMap_.end()) { std::vector> channelFifoList; @@ -83,8 +80,16 @@ void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_mapsecond) { - if (fifoCacheData->GetChannel() != channel) { + for (auto fifoIt = dataCountIt->second.begin(); fifoIt != dataCountIt->second.end();) { + auto fifoCacheData = *fifoIt; + CHKPC(fifoCacheData); + auto fifoChannel = fifoCacheData->GetChannel(); + if (fifoChannel == nullptr) { + fifoIt = dataCountIt->second.erase(fifoIt); + continue; + } + ++fifoIt; + if (fifoChannel != channel) { continue; } channelExist = true; @@ -112,9 +117,6 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_map(event.sensorTypeId); - if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); - } std::lock_guard dataCountLock(dataCountMutex_); auto dataCountIt = dataCountMap_.find(sensorId); // there is no channelFifoList @@ -129,8 +131,16 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_mapsecond) { - if (fifoData->GetChannel() != channel) { + for (auto fifoIt = dataCountIt->second.begin(); fifoIt != dataCountIt->second.end();) { + auto fifoData = *fifoIt; + CHKPC(fifoData); + auto fifoChannel = fifoData->GetChannel(); + if (fifoChannel == nullptr) { + fifoIt = dataCountIt->second.erase(fifoIt); + continue; + } + ++fifoIt; + if (fifoChannel != channel) { continue; } channelExist = true; @@ -295,8 +305,9 @@ void SensorDataProcesser::EventFilter(CircularEventBuf &eventsBuf) if (it != flushInfo.end()) { flushVec = it->second; for (auto &channel : flushVec) { - if (flushInfo_.IsFlushChannelValid(channelList, channel.flushChannel)) { - SendEvents(channel.flushChannel, eventsBuf.circularBuf[eventsBuf.readPos]); + auto flushChannel = channel.flushChannel.promote(); + if (flushInfo_.IsFlushChannelValid(channelList, flushChannel)) { + SendEvents(flushChannel, eventsBuf.circularBuf[eventsBuf.readPos]); flushInfo_.ClearFlushInfoItem(realSensorId); break; } else { diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 563427c50c359bf0b08ee5df1d543a68ab09ef0e..227044ed454f64d9ead97348fd3178971684be6e 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -227,8 +227,6 @@ ErrCode SensorService::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, return ret; } ReportSensorSysEvent(sensorId, true, pid); - uint32_t flag = sensorManager_.GetSensorFlag(sensorId); - ret = flushInfo_.FlushProcess(sensorId, flag, pid, true); if (ret != ERR_OK) { SEN_HILOGE("ret : %{public}d", ret); } @@ -284,43 +282,6 @@ ErrCode SensorService::DisableSensor(uint32_t sensorId) return DisableSensor(sensorId, GetCallingPid()); } -int32_t SensorService::GetSensorState(uint32_t sensorId) -{ - if (sensorId == INVALID_SENSOR_ID) { - SEN_HILOGE("sensorId is 0"); - return ERR_NO_INIT; - } - auto state = clientInfo_.GetSensorState(sensorId); - return static_cast(state); -} - -ErrCode SensorService::RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params) -{ - CALL_LOG_ENTER; - if (sensorId == INVALID_SENSOR_ID || ((cmdType != FLUSH) && (cmdType != SET_MODE))) { - SEN_HILOGE("sensorId or cmd is invalid"); - return ERR_NO_INIT; - } - std::lock_guard serviceLock(serviceLock_); - uint32_t flag = sensorManager_.GetSensorFlag(sensorId); - if (cmdType == FLUSH) { - int32_t pid = this->GetCallingPid(); - SEN_HILOGI("sensorId:%{public}u,flag:%{public}u", sensorId, flag); - auto retFlush = flushInfo_.FlushProcess(sensorId, flag, pid, false); - if (retFlush != ERR_OK) { - SEN_HILOGE("ret:%{public}d", retFlush); - } - return retFlush; - } - if (sensorHdiConnection_.RunCommand(sensorId, cmdType, params) != ERR_OK) { - SEN_HILOGE("RunCommand is failed"); - return RUN_COMMAND_ERR; - } - auto uid = GetCallingUid(); - clientInfo_.UpdateCmd(sensorId, uid, cmdType); - return ERR_OK; -} - std::vector SensorService::GetSensorList() { std::lock_guard sensorLock(sensorsMutex_); diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index bed7a011e211b46817073be3ed2477cebf20ca01..167e8c4be7aaa7e9e2621606e81d45f7da8e60a2 100644 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -40,8 +40,6 @@ SensorServiceStub::SensorServiceStub() CALL_LOG_ENTER; baseFuncs_[ENABLE_SENSOR] = &SensorServiceStub::SensorEnableInner; baseFuncs_[DISABLE_SENSOR] = &SensorServiceStub::SensorDisableInner; - baseFuncs_[GET_SENSOR_STATE] = &SensorServiceStub::GetSensorStateInner; - baseFuncs_[RUN_COMMAND] = &SensorServiceStub::RunCommandInner; baseFuncs_[GET_SENSOR_LIST] = &SensorServiceStub::GetAllSensorsInner; baseFuncs_[TRANSFER_DATA_CHANNEL] = &SensorServiceStub::CreateDataChannelInner; baseFuncs_[DESTROY_SENSOR_CHANNEL] = &SensorServiceStub::DestroyDataChannelInner; @@ -104,36 +102,6 @@ ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel return DisableSensor(sensorId); } -ErrCode SensorServiceStub::GetSensorStateInner(MessageParcel &data, MessageParcel &reply) -{ - (void)reply; - uint32_t sensorId = data.ReadUint32(); - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); - if (ret != PERMISSION_GRANTED) { - HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL", - HiSysEvent::EventType::SECURITY, "PKG_NAME", "GetSensorStateInner", "ERROR_CODE", ret); - SEN_HILOGE("sensorId:%{public}u grant failed, result:%{public}d", sensorId, ret); - return PERMISSION_DENIED; - } - return GetSensorState(sensorId); -} - -ErrCode SensorServiceStub::RunCommandInner(MessageParcel &data, MessageParcel &reply) -{ - (void)reply; - uint32_t sensorId = data.ReadUint32(); - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); - if (ret != PERMISSION_GRANTED) { - HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL", - HiSysEvent::EventType::SECURITY, "PKG_NAME", "RunCommandInner", "ERROR_CODE", ret); - SEN_HILOGE("sensorId:%{public}u grant failed,result:%{public}d", sensorId, ret); - return PERMISSION_DENIED; - } - return RunCommand(sensorId, data.ReadUint32(), data.ReadUint32()); -} - ErrCode SensorServiceStub::GetAllSensorsInner(MessageParcel &data, MessageParcel &reply) { (void)data; diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h index 7c20589372b5d7b240138e20a6b3bf177a422ae7..e6a01dc1644e6d76b463ddea6c2bcb932113958f 100644 --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -99,8 +99,8 @@ enum { SENSOR_CHANNEL_SOCKET_CREATE_ERR = SENSOR_UTILS_ERR_OFFSET, SENSOR_CHANNEL_SENDFD_ERR = SENSOR_CHANNEL_SOCKET_CREATE_ERR + 1, SENSOR_CHANNEL_WRITE_DESCRIPTOR_ERR = SENSOR_CHANNEL_SENDFD_ERR + 1, - SENSOR_CHANNEL_DUP_ERR = SENSOR_CHANNEL_WRITE_DESCRIPTOR_ERR + 1, - SENSOR_CHANNEL_BASIC_CHANNEL_NOT_INIT = SENSOR_CHANNEL_DUP_ERR + 1, + SENSOR_CHANNEL_READ_DESCRIPTOR_ERR = SENSOR_CHANNEL_WRITE_DESCRIPTOR_ERR + 1, + SENSOR_CHANNEL_BASIC_CHANNEL_NOT_INIT = SENSOR_CHANNEL_READ_DESCRIPTOR_ERR + 1, SENSOR_CHANNEL_SEND_ADDR_ERR = SENSOR_CHANNEL_BASIC_CHANNEL_NOT_INIT + 1, SENSOR_CHANNEL_SEND_DATA_ERR = SENSOR_CHANNEL_SEND_ADDR_ERR + 1, SENSOR_CHANNEL_RECEIVE_DATA_ERR = SENSOR_CHANNEL_SEND_DATA_ERR + 1, diff --git a/utils/src/sensor_basic_data_channel.cpp b/utils/src/sensor_basic_data_channel.cpp index 0e7ef0777eaf5c1609e4b19e50d3a17995c80444..7e98aeef7b2569ff1bde0a48237fd5b239e469c4 100755 --- a/utils/src/sensor_basic_data_channel.cpp +++ b/utils/src/sensor_basic_data_channel.cpp @@ -100,17 +100,11 @@ int32_t SensorBasicDataChannel::CreateSensorBasicChannel(MessageParcel &data) SEN_HILOGD("already create socketpair"); return ERR_OK; } - int32_t tmpFd = data.ReadFileDescriptor(); - if (tmpFd < 0) { - SEN_HILOGE("ReadFileDescriptor is failed"); - sendFd_ = -1; - return SENSOR_CHANNEL_DUP_ERR; - } - sendFd_ = dup(tmpFd); + sendFd_ = data.ReadFileDescriptor(); if (sendFd_ < 0) { - SEN_HILOGE("dup FileDescriptor is failed"); + SEN_HILOGE("ReadFileDescriptor is failed"); sendFd_ = -1; - return SENSOR_CHANNEL_DUP_ERR; + return SENSOR_CHANNEL_READ_DESCRIPTOR_ERR; } return ERR_OK; }