diff --git a/frameworks/native/sensor/BUILD.gn b/frameworks/native/sensor/BUILD.gn index 35fa0fe1b97def4c0ccb9f8217c185c495f8e3f7..6c5824e7a1d592df50495223f6ee9c47e0ed8dce 100755 --- a/frameworks/native/sensor/BUILD.gn +++ b/frameworks/native/sensor/BUILD.gn @@ -39,6 +39,7 @@ ohos_shared_library("libsensor_native") { "//drivers/peripheral/sensor/interfaces/include", "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", "//foundation/appexecfwk/standard/libs/test/moduletest/common/event_handler", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", ] deps = [ "$SUBSYSTEM_DIR/sensor/services/sensor:libsensor_service", diff --git a/frameworks/native/sensor/include/my_file_descriptor_listener.h b/frameworks/native/sensor/include/my_file_descriptor_listener.h index fc7f16d5ae72700e27a53d1a46d55edeb4d46a41..25cc58fd879470b689c5fb0c780e96bf326e5b35 100755 --- a/frameworks/native/sensor/include/my_file_descriptor_listener.h +++ b/frameworks/native/sensor/include/my_file_descriptor_listener.h @@ -46,6 +46,7 @@ public: private: SensorDataChannel* channel_; + struct TransferSensorEvents *receiveDataBuff_ = nullptr; }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/include/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index 9cfe116937958889674ede78a4b241d16180717d..8ff409c3642dc1b7fe3a7968b0e21f226269faba 100755 --- a/frameworks/native/sensor/include/sensor_agent_proxy.h +++ b/frameworks/native/sensor/include/sensor_agent_proxy.h @@ -17,10 +17,14 @@ #define SENSOR_PROXY_H #include -#include "sensor_agent_type.h" +#include + #include "refbase.h" +#include "sensor_agent_type.h" #include "sensor_data_channel.h" +namespace OHOS { +namespace Sensors { struct SensorNativeData; struct SensorIdList; typedef int32_t (*SensorDataCallback)(struct SensorNativeData *events, uint32_t num); @@ -30,7 +34,7 @@ public: SensorAgentProxy(); ~SensorAgentProxy(); static const SensorAgentProxy *GetSensorsObj(); - int32_t CreateSensorDataChannel(const SensorUser *user) const; + int32_t CreateSensorDataChannel() const; int32_t DestroySensorDataChannel() const; int32_t GetSensorId(struct SensorIdList *sensorId, uint32_t sensorGroup, uint32_t sensorType) const; int32_t GetDefaultSensorId(uint32_t sensorGroup, uint32_t sensorType) const; @@ -49,8 +53,15 @@ private: static void HandleSensorData(SensorEvent *events, int32_t num, void *data); static void FillSensorAccuracy(struct SensorNativeData &data, SensorEvent &event); static OHOS::sptr sensorObj_; + static std::mutex subscribeMutex_; + static std::mutex chanelMutex_; OHOS::sptr dataChannel_; - static SensorUser *user_; - static const std::map g_sensorDataLenthMap; + static bool g_isChannelCreated; + static int64_t g_samplingInterval; + static int64_t g_reportInterval; + static std::map g_subscribeMap; + static std::map g_unsubscribeMap; }; +} // namespace Sensors +} // namespace OHOS #endif // endif SENSOR_PROXY_H diff --git a/frameworks/native/sensor/include/sensor_data_channel.h b/frameworks/native/sensor/include/sensor_data_channel.h index 51e2c7a1d4285722ca15df831df8ff4a42e02c55..2902f91427115080abcc84a9f0c92de9cbc46595 100755 --- a/frameworks/native/sensor/include/sensor_data_channel.h +++ b/frameworks/native/sensor/include/sensor_data_channel.h @@ -42,10 +42,7 @@ public: private: static void threadProcessTask(SensorDataChannel *sensorChannel); int32_t InnerSensorDataChannel(); - bool threadStop_ = true; - std::mutex treadMutex_; - std::thread sensorDataThread_; - struct SensorEvent *receiveDataBuff_ = nullptr; + std::mutex eventRunnerMutex_; static std::shared_ptr eventHandler_; static std::shared_ptr eventRunner_; static int32_t receiveFd_; diff --git a/frameworks/native/sensor/src/my_file_descriptor_listener.cpp b/frameworks/native/sensor/src/my_file_descriptor_listener.cpp index 1a778835a8845da7dfab7b6838bbd465f5d9a603..e69ed749a294c52890dcfe38d0213c8b19da2748 100755 --- a/frameworks/native/sensor/src/my_file_descriptor_listener.cpp +++ b/frameworks/native/sensor/src/my_file_descriptor_listener.cpp @@ -31,6 +31,9 @@ constexpr int32_t RECEIVE_DATA_SIZE = 100; } // namespace MyFileDescriptorListener::MyFileDescriptorListener() + :channel_(nullptr), + receiveDataBuff_( + new (std::nothrow) TransferSensorEvents[sizeof(struct TransferSensorEvents) * RECEIVE_DATA_SIZE]) {} void MyFileDescriptorListener::OnReadable(int32_t fileDescriptor) @@ -42,12 +45,14 @@ void MyFileDescriptorListener::OnReadable(int32_t fileDescriptor) } FileDescriptorListener::OnReadable(fileDescriptor); - - struct TransferSensorEvents *receiveDataBuff_ = - new (std::nothrow) TransferSensorEvents[sizeof(struct TransferSensorEvents) * RECEIVE_DATA_SIZE]; - int32_t len = recv(fileDescriptor, receiveDataBuff_, sizeof(struct TransferSensorEvents) * RECEIVE_DATA_SIZE, NULL); + if (receiveDataBuff_ == nullptr) { + receiveDataBuff_ = + new (std::nothrow) TransferSensorEvents[sizeof(struct TransferSensorEvents) * RECEIVE_DATA_SIZE]; + } + int32_t len = + recv(fileDescriptor, receiveDataBuff_, sizeof(struct TransferSensorEvents) * RECEIVE_DATA_SIZE, NULL); + int32_t eventSize = sizeof(struct TransferSensorEvents); while (len > 0) { - int32_t eventSize = sizeof(struct TransferSensorEvents); int32_t num = len / eventSize; for (int i = 0; i < num; i++) { SensorEvent event = { @@ -80,6 +85,9 @@ void MyFileDescriptorListener::OnShutdown(int32_t fileDescriptor) } FileDescriptorListener::OnShutdown(fileDescriptor); + if (receiveDataBuff_ != nullptr) { + delete[] receiveDataBuff_; + } } void MyFileDescriptorListener::OnException(int32_t fileDescriptor) @@ -90,6 +98,9 @@ void MyFileDescriptorListener::OnException(int32_t fileDescriptor) } FileDescriptorListener::OnException(fileDescriptor); + if (receiveDataBuff_ != nullptr) { + delete[] receiveDataBuff_; + } } } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 68a273a71419e415682cb7b2e197f11ee80e168f..b00e7e3d9839b3202f6fe33a92ea715efda6b81c 100755 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -24,6 +24,8 @@ #include "sensors_log_domain.h" using namespace OHOS::HiviewDFX; +namespace OHOS { +namespace Sensors { namespace { constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::SensorsLogDomain::SENSORS_IMPLEMENT, "SensorAgentProxy" }; @@ -65,53 +67,32 @@ using OHOS::Sensors::SensorServiceClient; } // namespace OHOS::sptr SensorAgentProxy::sensorObj_ = nullptr; -static bool g_isChannelCreated = false; -static int64_t g_samplingInterval = -1; -static int64_t g_reportInterval = -1; -static std::map g_subscribeMap; -static std::map g_unsubscribeMap; +bool SensorAgentProxy::g_isChannelCreated; +int64_t SensorAgentProxy::g_samplingInterval; +int64_t SensorAgentProxy::g_reportInterval; +std::mutex SensorAgentProxy::subscribeMutex_; +std::mutex SensorAgentProxy::chanelMutex_; +std::map SensorAgentProxy::g_subscribeMap; +std::map SensorAgentProxy::g_unsubscribeMap; -const std::map SensorAgentProxy::g_sensorDataLenthMap = { - { SENSOR_TYPE_ID_ACCELEROMETER, sizeof(AccelData)}, - { SENSOR_TYPE_ID_LINEAR_ACCELERATION, sizeof(LinearAccelData)}, - { SENSOR_TYPE_ID_GRAVITY, sizeof(GravityData)}, - { SENSOR_TYPE_ID_GYROSCOPE, sizeof(GyroscopeData)}, - { SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, sizeof(AccelUncalibratedData)}, - { SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, sizeof(GyroUncalibratedData)}, - { SENSOR_TYPE_ID_SIGNIFICANT_MOTION, sizeof(SignificantMotionData)}, - { SENSOR_TYPE_ID_PEDOMETER_DETECTION, sizeof(PedometerDetectData)}, - { SENSOR_TYPE_ID_PEDOMETER, sizeof(PedometerData)}, - - { SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, sizeof(AmbientTemperatureData)}, - { SENSOR_TYPE_ID_HUMIDITY, sizeof(HumidityData)}, - { SENSOR_TYPE_ID_MAGNETIC_FIELD, sizeof(MagneticFieldData)}, - { SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, sizeof(MagneticFieldUncalibratedData)}, - { SENSOR_TYPE_ID_BAROMETER, sizeof(BarometerData)}, - - { SENSOR_TYPE_ID_DEVICE_ORIENTATION, sizeof(DeviceOrientationData)}, - { SENSOR_TYPE_ID_ORIENTATION, sizeof(OrientationData)}, - { SENSOR_TYPE_ID_ROTATION_VECTOR, sizeof(RotationVectorData)}, - { SENSOR_TYPE_ID_GAME_ROTATION_VECTOR, sizeof(GameRotationVectorData)}, - { SENSOR_TYPE_ID_GEOMAGNETIC_ROTATION_VECTOR, sizeof(GeomagneticRotaVectorData)}, - - { SENSOR_TYPE_ID_PROXIMITY, sizeof(ProximityData)}, - { SENSOR_TYPE_ID_AMBIENT_LIGHT, sizeof(AmbientLightData)}, - - { SENSOR_TYPE_ID_HEART_RATE, sizeof(HeartRateData)}, - { SENSOR_TYPE_ID_WEAR_DETECTION, sizeof(WearDetectionData)} -}; - -SensorAgentProxy::SensorAgentProxy() : dataChannel_(new (std::nothrow) SensorDataChannel()) +SensorAgentProxy::SensorAgentProxy() + : dataChannel_(new (std::nothrow) SensorDataChannel()) {} SensorAgentProxy::~SensorAgentProxy() -{} +{ + if (sensorObj_ != nullptr) { + delete sensorObj_; + sensorObj_ = nullptr; + } +} const SensorAgentProxy *SensorAgentProxy::GetSensorsObj() { HiLog::Debug(LABEL, "%{public}s", __func__); if (sensorObj_ == nullptr) { + HiLog::Debug(LABEL, "%{public}s sensorObj_ new object", __func__); sensorObj_ = new (std::nothrow) SensorAgentProxy(); } return sensorObj_; @@ -138,9 +119,14 @@ void SensorAgentProxy::HandleSensorData(struct SensorEvent *events, int32_t num, } } -int32_t SensorAgentProxy::CreateSensorDataChannel(const SensorUser *user) const +int32_t SensorAgentProxy::CreateSensorDataChannel() const { HiLog::Debug(LABEL, "%{public}s", __func__); + std::lock_guard chanelLock(chanelMutex_); + if (g_isChannelCreated) { + HiLog::Info(LABEL, "%{public}s the channel has already been created", __func__); + return ERR_OK; + } if (dataChannel_ == nullptr) { HiLog::Error(LABEL, "%{public}s data channel cannot be null", __func__); return INVALID_POINTER; @@ -158,23 +144,40 @@ int32_t SensorAgentProxy::CreateSensorDataChannel(const SensorUser *user) const __func__, ret, destoryRet); return ret; } + g_isChannelCreated = true; return ERR_OK; } int32_t SensorAgentProxy::DestroySensorDataChannel() const { - int32_t ret; - if (dataChannel_ != nullptr) { - ret = dataChannel_->DestroySensorDataChannel(); + HiLog::Debug(LABEL, "%{public}s", __func__); + std::lock_guard chanelLock(chanelMutex_); + if (!g_isChannelCreated) { + HiLog::Info(LABEL, "%{public}s channel has been destroyed", __func__); + return ERR_OK; + } + if (dataChannel_ == nullptr) { + HiLog::Error(LABEL, "%{public}s data channel cannot be null", __func__); + return INVALID_POINTER; + } + int32_t ret = dataChannel_->DestroySensorDataChannel(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s destory data channel failed, ret : %{public}d", __func__, ret); + return ret; } SensorServiceClient &client = SensorServiceClient::GetInstance(); ret = client.DestroyDataChannel(); - return ret; + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s destory service data channel fail, ret : %{public}d", __func__, ret); + return ret; + } + g_isChannelCreated = false; + return ERR_OK; } int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *user) const { - if (user == NULL || sensorId < 0) { + if (user == nullptr || sensorId < 0 || user->callback == nullptr) { HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } @@ -182,6 +185,7 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use HiLog::Error(LABEL, "%{public}s samplingPeroid or g_reportInterval is invalid", __func__); return OHOS::Sensors::ERROR; } + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { HiLog::Error(LABEL, "%{public}s subscribe sensorId first", __func__); return OHOS::Sensors::ERROR; @@ -200,10 +204,11 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *user) const { - if (user == NULL || sensorId < 0) { + if (user == nullptr || sensorId < 0 || user->callback == nullptr) { HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { HiLog::Error(LABEL, "%{public}s subscribe sensorId first", __func__); return OHOS::Sensors::ERROR; @@ -222,7 +227,7 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, int64_t reportInterval) const { - if (user == NULL || sensorId < 0) { + if (user == nullptr || sensorId < 0) { HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } @@ -230,6 +235,7 @@ int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int HiLog::Error(LABEL, "%{public}s samplingInterval or reportInterval is invalid", __func__); return OHOS::Sensors::ERROR; } + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { HiLog::Error(LABEL, "%{public}s subscribe sensorId first", __func__); return OHOS::Sensors::ERROR; @@ -243,14 +249,15 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us { HiLog::Info(LABEL, "%{public}s in, sensorId: %{public}d", __func__, sensorId); if (user == nullptr || sensorId < 0 || user->callback == nullptr) { - HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); + HiLog::Error(LABEL, "%{public}s user or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } - if (!g_isChannelCreated) { - HiLog::Info(LABEL, "%{public}s channel created", __func__); - g_isChannelCreated = true; - CreateSensorDataChannel(user); + int32_t ret = CreateSensorDataChannel(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s create sensor data chanel failed", __func__); + return OHOS::Sensors::ERROR; } + std::lock_guard subscribeLock(subscribeMutex_); g_subscribeMap[sensorId] = user; return OHOS::Sensors::SUCCESS; } @@ -258,31 +265,33 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser *user) const { HiLog::Info(LABEL, "%{public}s in, sensorId: %{public}d", __func__, sensorId); - if (user == NULL || sensorId < 0) { + if (user == nullptr || sensorId < 0 || user->callback == nullptr) { HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } - + std::lock_guard subscribeLock(subscribeMutex_); if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { HiLog::Error(LABEL, "%{public}s deactivate sensorId first", __func__); return OHOS::Sensors::ERROR; } - - g_unsubscribeMap.erase(sensorId); if (g_subscribeMap.empty()) { - DestroySensorDataChannel(); - g_isChannelCreated = false; - HiLog::Info(LABEL, "%{public}s channel has been destroyed", __func__); + int32_t ret = DestroySensorDataChannel(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s destory data channel fail, ret : %{public}d", __func__, ret); + return ret; + } } + g_unsubscribeMap.erase(sensorId); return OHOS::Sensors::SUCCESS; } int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) const { - if (user == NULL || sensorId < 0) { + if (user == nullptr || sensorId < 0 || user->callback == nullptr) { HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { HiLog::Error(LABEL, "%{public}s subscribe sensorId first", __func__); return OHOS::Sensors::ERROR; @@ -292,10 +301,11 @@ int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int3 int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, int32_t option) const { - if (user == NULL || sensorId < 0) { + if (user == nullptr || sensorId < 0 || user->callback == nullptr) { HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { HiLog::Error(LABEL, "%{public}s subscribe sensorId first", __func__); return OHOS::Sensors::ERROR; @@ -305,7 +315,7 @@ int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, in int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) const { - if (sensorInfo == NULL || count == NULL) { + if (sensorInfo == nullptr || count == nullptr) { HiLog::Error(LABEL, "%{public}s sensorInfo or count is null", __func__); return OHOS::Sensors::ERROR; } @@ -347,3 +357,5 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) } return OHOS::Sensors::SUCCESS; } +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/sensor/src/sensor_data_channel.cpp b/frameworks/native/sensor/src/sensor_data_channel.cpp index ca97239207bf35daf8f727454b1d25d7212939b6..2ff8d8b21601a37d4842cfd22353fe3790fa57c7 100755 --- a/frameworks/native/sensor/src/sensor_data_channel.cpp +++ b/frameworks/native/sensor/src/sensor_data_channel.cpp @@ -36,7 +36,6 @@ using namespace OHOS::HiviewDFX; using namespace OHOS::AppExecFwk; std::shared_ptr SensorDataChannel::eventHandler_; std::shared_ptr SensorDataChannel::eventRunner_; -int32_t SensorDataChannel::receiveFd_ = 0; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_NATIVE, "SensorDataChannel" }; @@ -47,15 +46,8 @@ const uint32_t STOP_EVENT_ID = 0; SensorDataChannel::SensorDataChannel() : dataCB_(nullptr), - privateData_(nullptr), - threadStop_(true) -{ - receiveDataBuff_ = new (std::nothrow) SensorEvent[SENSOR_READ_DATA_SIZE]; - if (receiveDataBuff_ == nullptr) { - HiLog::Error(LABEL, "%{public}s receiveDataBuff_ cannot be null", __func__); - return; - } -} + privateData_(nullptr) +{} int32_t SensorDataChannel::CreateSensorDataChannel(DataChannelCB callBack, void *data) { @@ -78,17 +70,12 @@ int32_t SensorDataChannel::RestoreSensorDataChannel() HiLog::Error(LABEL, "%{public}s fd not close", __func__); return SENSOR_CHANNEL_RESTORE_FD_ERR; } - if (sensorDataThread_.joinable()) { - HiLog::Error(LABEL, "%{public}s thread exit", __func__); - return SENSOR_CHANNEL_RESTORE_THREAD_ERR; - } return InnerSensorDataChannel(); } int32_t SensorDataChannel::InnerSensorDataChannel() { - std::lock_guard threadLock(treadMutex_); - + std::lock_guard eventRunnerLock(eventRunnerMutex_); // create basic data channel int32_t ret = CreateSensorBasicChannel(SENSOR_READ_DATA_SIZE, SENSOR_READ_DATA_SIZE); if (ret != ERR_OK) { @@ -108,8 +95,8 @@ int32_t SensorDataChannel::InnerSensorDataChannel() return -1; } - receiveFd_ = GetReceiveDataFd(); - auto inResult = handler->AddFileDescriptorListener(receiveFd_, AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener); + int32_t receiveFd = GetReceiveDataFd(); + auto inResult = handler->AddFileDescriptorListener(receiveFd, AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener); if (inResult != 0) { HiLog::Error(LABEL, "%{public}s AddFileDescriptorListener fail", __func__); return -1; @@ -133,44 +120,23 @@ int32_t SensorDataChannel::InnerSensorDataChannel() int32_t SensorDataChannel::DestroySensorDataChannel() { - std::lock_guard threadLock(treadMutex_); - // send wakeup signal to sensor_fwk_read_data thread - - if (eventHandler_ == nullptr) { - HiLog::Error(LABEL, "%{public}s handler is null", __func__); + std::lock_guard eventRunnerLock(eventRunnerMutex_); + if (eventHandler_ == nullptr || eventRunner_ == nullptr) { + HiLog::Error(LABEL, "%{public}s handler or eventRunner is null", __func__); return -1; } - int32_t fd = GetReceiveDataFd(); - eventHandler_->RemoveFileDescriptorListener(fd); + int32_t receiveFd = GetReceiveDataFd(); + eventHandler_->RemoveFileDescriptorListener(receiveFd); eventHandler_ = nullptr; - if (eventRunner_ != nullptr) { - eventRunner_->Stop(); - eventRunner_ = nullptr; - } - threadStop_ = true; - - if (sensorDataThread_.joinable()) { - sensorDataThread_.join(); - } - // destroy sensor basic channel + eventRunner_->Stop(); + eventRunner_ = nullptr; + // destroy sensor basic channelx return DestroySensorBasicChannel(); } -bool SensorDataChannel::IsThreadExit() -{ - return (!sensorDataThread_.joinable()) && (threadStop_); -} - -bool SensorDataChannel::IsThreadStart() -{ - return (sensorDataThread_.joinable()) && (!threadStop_); -} SensorDataChannel::~SensorDataChannel() { DestroySensorDataChannel(); - if (receiveDataBuff_ != nullptr) { - delete[] receiveDataBuff_; - } } } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/test/BUILD.gn b/frameworks/native/sensor/test/BUILD.gn deleted file mode 100755 index b0ee54618abb4b13b684a686c4475122f638c8b5..0000000000000000000000000000000000000000 --- a/frameworks/native/sensor/test/BUILD.gn +++ /dev/null @@ -1,25 +0,0 @@ -# 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. - -import("//build/test.gni") - -############################################################################### -group("unittest") { - testonly = true - if (is_phone_product || is_wearable_product) { - deps = [ "unittest/common:unittest" ] - } - if (is_phone_product) { - deps += [ "unittest/phone:unittest" ] - } -} diff --git a/frameworks/native/sensor/test/unittest/common/BUILD.gn b/frameworks/native/sensor/test/unittest/common/BUILD.gn deleted file mode 100755 index 5c1464511cc32cac0effb9130f13dee1cb644224..0000000000000000000000000000000000000000 --- a/frameworks/native/sensor/test/unittest/common/BUILD.gn +++ /dev/null @@ -1,96 +0,0 @@ -# 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. - -import("//build/test.gni") - -SUBSYSTEM_DIR = "//base/sensors" -module_output_path = "sensors/sensor/frameworks/sensor" - -####################################################### -ohos_unittest("SensorNativeCommonTest") { - module_out_path = module_output_path - - sources = [ "./sensor_native_test.cpp" ] - - include_dirs = [ - "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", - "$SUBSYSTEM_DIR/sensor/interfaces/native/include", - "$SUBSYSTEM_DIR/sensor/utils/include", - "$SUBSYSTEM_DIR/sensor/services/sensor/include", - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor/include", - "$SUBSYSTEM_DIR/sensor/frameworks/native/common/include", - "//utils/native/base/include", - ] - - deps = [ - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor:libsensor_native", - "$SUBSYSTEM_DIR/sensor/services/sensor:libsensor_service", - "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - "//utils/native/base:utils", - ] - - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", - ] -} - -####################################################### -ohos_unittest("SensorDfxTest") { - module_out_path = module_output_path - - sources = [ "./sensor_dfx_test.cpp" ] - - include_dirs = [ - "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", - "$SUBSYSTEM_DIR/sensor/interfaces/native/include", - "$SUBSYSTEM_DIR/sensor/utils/include", - "$SUBSYSTEM_DIR/sensor/services/sensor/include", - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor/include", - "//utils/native/base/include", - ] - - deps = [ - #"$SUBSYSTEM_DIR/sensor/adapter/frameworks:libsensor_fwk_adapter", - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor:libsensor_native", - "$SUBSYSTEM_DIR/sensor/services/sensor:libsensor_service", - "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - "//utils/native/base:utils", - ] - - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - - #"communication_L2:ipc_core", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", - ] -} - -########################################################################### -group("unittest") { - testonly = true - if (is_phone_product || is_wearable_product) { - deps = [ - ":SensorDfxTest", - ":SensorNativeCommonTest", - ] - } -} diff --git a/frameworks/native/sensor/test/unittest/common/sensor_dfx_test.cpp b/frameworks/native/sensor/test/unittest/common/sensor_dfx_test.cpp deleted file mode 100755 index 8514047ce860a6ff8fde4edffa91166ea1cc56d4..0000000000000000000000000000000000000000 --- a/frameworks/native/sensor/test/unittest/common/sensor_dfx_test.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/* - * 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 -#include -#include - -#include - -#include -#include "sensor_data_channel.h" -#include "sensor_data_event.h" -#include "sensor_service_client.h" -#include "sensors_errors.h" -#include "sensors_log_domain.h" -#include "string_ex.h" - - -namespace OHOS { -namespace Sensors { -using namespace testing::ext; -using namespace OHOS::HiviewDFX; - -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_TEST, "SensorDFXTest" }; -const std::string CMD_LINE = "ps -ef | grep 'hsensors' | grep -v grep | awk '{print $2}'"; -constexpr int32_t BUFFER_SIZE = 8; -constexpr pid_t INVALID_PID = -1; -} // namespace - -class SensorDFXTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); - pid_t GetSensorServicePid(); - static void HandleEvent(struct SensorEvent *events, int32_t num, void *data); - static std::vector g_sensorsList; - static sptr g_sensorDataChannel; - static std::unique_ptr g_sensorServiceClient; - static bool g_dataReport; -}; - -std::vector SensorDFXTest::g_sensorsList; -sptr SensorDFXTest::g_sensorDataChannel = nullptr; -bool SensorDFXTest::g_dataReport = false; -std::unique_ptr SensorDFXTest::g_sensorServiceClient = nullptr; - -pid_t SensorDFXTest::GetSensorServicePid() -{ - pid_t pid = INVALID_PID; - char buf[BUFFER_SIZE] = { 0 }; - FILE *fp = popen(CMD_LINE.c_str(), "r"); - if (fp == nullptr) { - HiLog::Error(LABEL, "get error when getting sensor service process id"); - return pid; - } - - fgets(buf, sizeof(buf) - 1, fp); - pclose(fp); - fp = nullptr; - HiLog::Info(LABEL, "process is : %{public}s", buf); - - std::string pidStr(buf); - pidStr = TrimStr(pidStr, '\n'); - HiLog::Info(LABEL, "pidStr is : %{public}s", pidStr.c_str()); - if (pidStr.empty()) { - return pid; - } - - if (IsNumericStr(pidStr)) { - pid = std::stoi(pidStr); - } - return pid; -} - -void SensorDFXTest::HandleEvent(struct SensorEvent *events, int32_t num, void *data) -{ - HiLog::Info(LABEL, "%{public}s HandleEvent", __func__); - for (int32_t i = 0; i < num; i++) { - g_dataReport = true; - } - return; -} - -void SensorDFXTest::SetUpTestCase() -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - g_sensorDataChannel = new (std::nothrow) SensorDataChannel(); - ASSERT_NE(g_sensorDataChannel, nullptr); - g_sensorServiceClient = std::make_unique(); - ASSERT_NE(g_sensorServiceClient, nullptr); - g_sensorsList = g_sensorServiceClient->GetSensorList(); - ASSERT_NE(g_sensorsList.size(), 0UL); - auto ret = g_sensorDataChannel->CreateSensorDataChannel(HandleEvent, nullptr); - HiLog::Info(LABEL, "CreateSensorDataChannel ret is : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); - ret = g_sensorServiceClient->TransferDataChannel(g_sensorDataChannel); - HiLog::Info(LABEL, "TransferDataChannel ret is : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); - g_dataReport = false; -} - -void SensorDFXTest::TearDownTestCase() -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - g_sensorServiceClient->DestroyDataChannel(); - g_sensorDataChannel->DestroySensorDataChannel(); -} - -void SensorDFXTest::SetUp() -{} - -void SensorDFXTest::TearDown() -{} -} // namespace Sensors -} // namespace OHOS diff --git a/frameworks/native/sensor/test/unittest/common/sensor_native_test.cpp b/frameworks/native/sensor/test/unittest/common/sensor_native_test.cpp deleted file mode 100755 index ff427ccb53869ba085157abd2e35dc4d15045821..0000000000000000000000000000000000000000 --- a/frameworks/native/sensor/test/unittest/common/sensor_native_test.cpp +++ /dev/null @@ -1,185 +0,0 @@ -/* - * 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 -#include -#include - -#include "sensor_agent_type.h" -#include "sensor_data_channel.h" -#include "sensor_service_client.h" -#include "sensors_errors.h" -#include "sensors_log_domain.h" - -namespace OHOS { -namespace Sensors { -using namespace testing::ext; -using namespace OHOS::HiviewDFX; - -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_TEST, "SensorNativeTest" }; -} - -class SensorNativeTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); - static void HandleEvent(struct SensorEvent *events, int32_t num, void *data); - std::vector sensorsList_; - uint32_t sensorId_; - sptr sensorDataChannel_; - std::unique_ptr sensorServiceClient_; - bool dataReport_; -}; -void SensorNativeTest::HandleEvent(struct SensorEvent *events, int32_t num, void *data) -{ - ASSERT_NE(events, nullptr); - HiLog::Info(LABEL, "%{public}s start,num : %{public}d", __func__, num); - SensorNativeTest *test = reinterpret_cast(data); - ASSERT_NE(test, nullptr); - for (int32_t i = 0; i < num; i++) { - if (events[i].sensorTypeId == (int32_t)test->sensorId_) { - test->dataReport_ = true; - } - } - return; -} -void SensorNativeTest::SetUpTestCase() -{} - -void SensorNativeTest::TearDownTestCase() -{} - -void SensorNativeTest::SetUp() -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - sensorDataChannel_ = new (std::nothrow) SensorDataChannel(); - ASSERT_NE(sensorDataChannel_, nullptr); - sensorServiceClient_ = std::make_unique(); - ASSERT_NE(sensorServiceClient_, nullptr); - sensorsList_ = sensorServiceClient_->GetSensorList(); - ASSERT_NE(sensorsList_.size(), 0UL); - sensorId_ = sensorsList_[0].GetSensorId(); - int32_t ret = sensorDataChannel_->CreateSensorDataChannel(HandleEvent, this); - HiLog::Info(LABEL, "CreateSensorDataChannel ret is : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); - sensorServiceClient_->TransferDataChannel(sensorDataChannel_); - dataReport_ = false; -} - -void SensorNativeTest::TearDown() -{ - sensorServiceClient_->DestroyDataChannel(); - sensorDataChannel_->DestroySensorDataChannel(); -} - -/* - * @tc.name: GetSensorList_001 - * @tc.desc: get sensor list - * @tc.type: FUNC - */ -HWTEST_F(SensorNativeTest, GetSensorList_001, TestSize.Level0) -{ - bool ret = sensorsList_.empty(); - HiLog::Info(LABEL, "GetSensorList_001,count : %{public}d!", int{ sensorsList_.size() }); - ASSERT_EQ(ret, false); -} - -/* - * @tc.name: GetSensorList_002 - * @tc.desc: Judge sensor info - * @tc.type: FUNC - */ -HWTEST_F(SensorNativeTest, GetSensorList_002, TestSize.Level0) -{ - for (const auto &it : sensorsList_) { - ASSERT_EQ(it.GetSensorId() == 0, true); - ASSERT_EQ(it.GetName().empty(), false); - ASSERT_EQ(it.GetVendor().empty(), false); - } - HiLog::Info(LABEL, "GetSensorList_002"); -} - -/* - * @tc.name: ClientSensorDataChannel_001 - * @tc.desc: do client sensor send data channel close - * @tc.type: FUNC - */ -HWTEST_F(SensorNativeTest, ClientSensorDataChannel_001, TestSize.Level0) -{ - int32_t ret = sensorDataChannel_->GetSendDataFd(); - HiLog::Info(LABEL, "ClientSensorDataChannel_001,ret : %{public}d!", ret); - ASSERT_EQ(ret, -1); -} - -/* - * @tc.name: ClientSensorDataChannel_002 - * @tc.desc: judge channel fd when create channel - * @tc.type: FUNC - */ -HWTEST_F(SensorNativeTest, ClientSensorDataChannel_002, TestSize.Level0) -{ - int32_t ret = sensorDataChannel_->GetReceiveDataFd(); - ASSERT_EQ((ret >= 0), true); -} - -/* - * @tc.name: ClientSensorDataChannel_003 - * @tc.desc: Judge read thread status when Destroy Channel - * @tc.type: FUNC - */ -HWTEST_F(SensorNativeTest, ClientSensorDataChannel_003, TestSize.Level0) -{ - int32_t ret = sensorDataChannel_->DestroySensorDataChannel(); - ASSERT_EQ(ret, ERR_OK); - bool result = false; - result = sensorDataChannel_->IsThreadExit(); - ASSERT_EQ(result, true); -} - -/* - * @tc.name: ClientSensorDataChannel_004 - * @tc.desc: Destroy ClientSensorDataChannel - * @tc.type: FUNC - */ -HWTEST_F(SensorNativeTest, ClientSensorDataChannel_004, TestSize.Level0) -{ - int32_t ret = sensorDataChannel_->DestroySensorDataChannel(); - ASSERT_EQ(ret, ERR_OK); - ret = sensorDataChannel_->GetSendDataFd(); - ASSERT_EQ(ret, -1); - ret = sensorDataChannel_->GetReceiveDataFd(); - ASSERT_EQ(ret, -1); -} - -/* - * @tc.name: SensorOperation_001 - * @tc.desc: disable sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorNativeTest, SensorOperation_001, TestSize.Level1) -{ - auto sensorId = sensorsList_[0].GetSensorId(); - auto ret = sensorServiceClient_->DisableSensor(sensorId); - HiLog::Info(LABEL, "DisableSensor ret is : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); - dataReport_ = false; - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - ASSERT_EQ(dataReport_, false); -} -} // namespace Sensors -} // namespace OHOS diff --git a/frameworks/native/sensor/test/unittest/phone/BUILD.gn b/frameworks/native/sensor/test/unittest/phone/BUILD.gn deleted file mode 100755 index 8fb67c69b49373a8e7fe95ada574730a3a4fdc26..0000000000000000000000000000000000000000 --- a/frameworks/native/sensor/test/unittest/phone/BUILD.gn +++ /dev/null @@ -1,59 +0,0 @@ -# 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. - -import("//build/test.gni") - -SUBSYSTEM_DIR = "//base/sensors" -module_output_path = "sensors/sensor/frameworks/sensor" - -####################################################### -ohos_unittest("SensorNativeTest") { - module_out_path = module_output_path - - sources = [ "./sensor_native_test.cpp" ] - - include_dirs = [ - "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", - "$SUBSYSTEM_DIR/sensor/interfaces/native/include", - "$SUBSYSTEM_DIR/sensor/utils/include", - "$SUBSYSTEM_DIR/sensor/services/sensor/include", - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor/include", - "$SUBSYSTEM_DIR/sensor/frameworks/native/common/include", - "//utils/native/base/include", - ] - - deps = [ - #"$SUBSYSTEM_DIR/sensor/adapter/frameworks:libsensor_fwk_adapter", - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor:libsensor_native", - "$SUBSYSTEM_DIR/sensor/services/sensor:libsensor_service", - "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - "//utils/native/base:utils", - ] - - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - - #"communication_L2:ipc_core", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", - ] -} - -########################################################################### -group("unittest") { - testonly = true - deps = [ ":SensorNativeTest" ] -} diff --git a/frameworks/native/sensor/test/unittest/phone/sensor_native_test.cpp b/frameworks/native/sensor/test/unittest/phone/sensor_native_test.cpp deleted file mode 100755 index f6621e5d7a1be73b7f8296a7325c0cc83957b011..0000000000000000000000000000000000000000 --- a/frameworks/native/sensor/test/unittest/phone/sensor_native_test.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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 -#include -#include - -#include "sensor_agent_type.h" -#include "sensor_data_channel.h" -#include "sensor_service_client.h" -#include "sensors_errors.h" -#include "sensors_log_domain.h" - -namespace OHOS { -namespace Sensors { -using namespace testing::ext; -using namespace OHOS::HiviewDFX; - -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_TEST, "SensorNativeTest" }; -} - -class SensorNativeTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); - static void HandleEvent(struct SensorEvent *events, int32_t num, void *data); - std::vector sensorsList_; - uint32_t sensorId_; - sptr sensorDataChannel_; - std::unique_ptr sensorServiceClient_; - bool dataReport_; -}; -void SensorNativeTest::HandleEvent(struct SensorEvent *events, int32_t num, void *data) -{ - ASSERT_NE(events, nullptr); - HiLog::Info(LABEL, "%{public}s start,num : %{public}d", __func__, num); - SensorNativeTest *test = reinterpret_cast(data); - ASSERT_NE(test, nullptr); - for (int32_t i = 0; i < num; i++) { - if (events[i].sensorTypeId == (int32_t)test->sensorId_) { - test->dataReport_ = true; - } - } - return; -} -void SensorNativeTest::SetUpTestCase() -{} - -void SensorNativeTest::TearDownTestCase() -{} - -void SensorNativeTest::SetUp() -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - sensorDataChannel_ = new (std::nothrow) SensorDataChannel(); - ASSERT_NE(sensorDataChannel_, nullptr); - sensorServiceClient_ = std::make_unique(); - ASSERT_NE(sensorServiceClient_, nullptr); - sensorsList_ = sensorServiceClient_->GetSensorList(); - ASSERT_NE(sensorsList_.size(), 0UL); - sensorId_ = sensorsList_[0].GetSensorId(); - auto ret = sensorDataChannel_->CreateSensorDataChannel(HandleEvent, this); - HiLog::Info(LABEL, "CreateSensorDataChannel ret is : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); - sensorServiceClient_->TransferDataChannel(sensorDataChannel_); - dataReport_ = false; -} - -void SensorNativeTest::TearDown() -{ - sensorServiceClient_->DestroyDataChannel(); - sensorDataChannel_->DestroySensorDataChannel(); -} - -/* - * @tc.name: SensorOperation_001 - * @tc.desc: enable sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorNativeTest, SensorOperation_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "SensorOperation_001 begin"); - uint64_t samplingPeriodNs = 10000000; - uint64_t maxReportDelayNs = 0; - auto sensorId = sensorsList_[0].GetSensorId(); - auto ret = sensorServiceClient_->EnableSensor(sensorId, samplingPeriodNs, maxReportDelayNs); - HiLog::Info(LABEL, "EnableSensor ret is : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); - dataReport_ = false; - // wait evennt - std::this_thread::sleep_for(std::chrono::milliseconds(200)); - ASSERT_EQ(dataReport_, true); -} -} // namespace Sensors -} // namespace OHOS diff --git a/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn index 8bdb073fab24b6f58defa8b0e6ed24fde28d0af7..d9c5afb0156aa26db7cde9e97b2eecd15dfe3c84 100755 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -37,6 +37,7 @@ ohos_shared_library("sensor_interface_native") { "$SUBSYSTEM_DIR/interfaces/native/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", ] deps = [ "$SUBSYSTEM_DIR/frameworks/native/sensor:libsensor_native", @@ -45,7 +46,10 @@ ohos_shared_library("sensor_interface_native") { "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", "//utils/native/base:utils", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] part_name = "sensor" subsystem_name = "sensors" } diff --git a/interfaces/native/src/sensor_agent.cpp b/interfaces/native/src/sensor_agent.cpp index fe8cf6c6cdcadf191ba51f6699893a15a6e2df86..e7e8b14fc39a6d1c3f1a6d0e0f541997be79c134 100755 --- a/interfaces/native/src/sensor_agent.cpp +++ b/interfaces/native/src/sensor_agent.cpp @@ -24,19 +24,19 @@ using OHOS::HiviewDFX::HiLogLabel; static const HiLogLabel LABEL = {LOG_CORE, OHOS::SensorsLogDomain::SENSORS_INTERFACE, "SensorNativeAPI"}; -static const SensorAgentProxy *GetInstance() +static const OHOS::Sensors::SensorAgentProxy *GetInstance() { HiLog::Info(LABEL, "%{public}s begin", __func__); - const SensorAgentProxy *obj = SensorAgentProxy::GetSensorsObj(); + const OHOS::Sensors::SensorAgentProxy *obj = OHOS::Sensors::SensorAgentProxy::GetSensorsObj(); return obj; } int32_t GetAllSensors(SensorInfo **sensorInfo, int32_t *count) { HiLog::Info(LABEL, "%{public}s begin", __func__); - const SensorAgentProxy *proxy = GetInstance(); - if (proxy == NULL) { - HiLog::Error(LABEL, "%s proxy is null", __func__); + const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + HiLog::Error(LABEL, "%s proxy is nullptr", __func__); return OHOS::Sensors::ERROR; } return proxy->GetAllSensors(sensorInfo, count); @@ -45,9 +45,9 @@ int32_t GetAllSensors(SensorInfo **sensorInfo, int32_t *count) int32_t ActivateSensor(int32_t sensorId, const SensorUser *user) { HiLog::Info(LABEL, "%{public}s begin", __func__); - const SensorAgentProxy *proxy = GetInstance(); - if (proxy == NULL) { - HiLog::Error(LABEL, "%s proxy is null", __func__); + const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + HiLog::Error(LABEL, "%s proxy is nullptr", __func__); return OHOS::Sensors::ERROR; } return proxy->ActivateSensor(sensorId, user); @@ -56,9 +56,9 @@ int32_t ActivateSensor(int32_t sensorId, const SensorUser *user) int32_t DeactivateSensor(int32_t sensorId, const SensorUser *user) { HiLog::Info(LABEL, "%{public}s begin", __func__); - const SensorAgentProxy *proxy = GetInstance(); - if (proxy == NULL) { - HiLog::Error(LABEL, "%s proxy is null", __func__); + const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + HiLog::Error(LABEL, "%s proxy is nullptr", __func__); return OHOS::Sensors::ERROR; } return proxy->DeactivateSensor(sensorId, user); @@ -67,9 +67,9 @@ int32_t DeactivateSensor(int32_t sensorId, const SensorUser *user) int32_t SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, int64_t reportInterval) { HiLog::Info(LABEL, "%{public}s begin", __func__); - const SensorAgentProxy *proxy = GetInstance(); - if (proxy == NULL) { - HiLog::Error(LABEL, "%s proxy is null", __func__); + const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + HiLog::Error(LABEL, "%s proxy is nullptr", __func__); return OHOS::Sensors::ERROR; } return proxy->SetBatch(sensorId, user, samplingInterval, reportInterval); @@ -78,9 +78,9 @@ int32_t SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInter int32_t SubscribeSensor(int32_t sensorId, const SensorUser *user) { HiLog::Info(LABEL, "%{public}s begin", __func__); - const SensorAgentProxy *proxy = GetInstance(); - if (proxy == NULL) { - HiLog::Error(LABEL, "%s proxy is null", __func__); + const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + HiLog::Error(LABEL, "%s proxy is nullptr", __func__); return OHOS::Sensors::ERROR; } return proxy->SubscribeSensor(sensorId, user); @@ -89,9 +89,9 @@ int32_t SubscribeSensor(int32_t sensorId, const SensorUser *user) int32_t UnsubscribeSensor(int32_t sensorId, const SensorUser *user) { HiLog::Info(LABEL, "%{public}s begin", __func__); - const SensorAgentProxy *proxy = GetInstance(); - if (proxy == NULL) { - HiLog::Error(LABEL, "%s proxy is null", __func__); + const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + HiLog::Error(LABEL, "%s proxy is nullptr", __func__); return OHOS::Sensors::ERROR; } return proxy->UnsubscribeSensor(sensorId, user); @@ -100,9 +100,9 @@ int32_t UnsubscribeSensor(int32_t sensorId, const SensorUser *user) int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) { HiLog::Info(LABEL, "%{public}s begin", __func__); - const SensorAgentProxy *proxy = GetInstance(); - if (proxy == NULL) { - HiLog::Error(LABEL, "%s proxy is null", __func__); + const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + HiLog::Error(LABEL, "%s proxy is nullptr", __func__); return OHOS::Sensors::ERROR; } return proxy->SetMode(sensorId, user, mode); @@ -111,9 +111,9 @@ int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) int32_t SetOption(int32_t sensorId, const SensorUser *user, int32_t option) { HiLog::Info(LABEL, "%{public}s begin", __func__); - const SensorAgentProxy *proxy = GetInstance(); - if (proxy == NULL) { - HiLog::Error(LABEL, "%s proxy is null", __func__); + const OHOS::Sensors::SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + HiLog::Error(LABEL, "%s proxy is nullptr", __func__); return OHOS::Sensors::ERROR; } return proxy->SetOption(sensorId, user, option); diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index 338e1b3192f8326031d8cd527f22e687916e7691..d29263e8ccf67d801883429197b97d039026bcc1 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -80,16 +80,18 @@ HWTEST_F(SensorAgentTest, SensorNativeApiTest_001, TestSize.Level1) ret = SetBatch(sensorTypeId, &user, 100000000, 100000000); ASSERT_EQ(ret, 0); + ret = ActivateSensor(sensorTypeId, &user); ASSERT_EQ(ret, 0); std::this_thread::sleep_for(std::chrono::milliseconds(10000)); ASSERT_EQ(ret, 0); - ret = UnsubscribeSensor(sensorTypeId, &user); - ASSERT_EQ(ret, 0); ret = DeactivateSensor(sensorTypeId, &user); ASSERT_EQ(ret, 0); + + ret = UnsubscribeSensor(sensorTypeId, &user); + ASSERT_EQ(ret, 0); } } // namespace Sensors } // namespace OHOS diff --git a/ohos.build b/ohos.build index 09bb335f1fb2632e36cee97d442c404bb925cd53..03d1fc33b37f87dd02c95520f06484dd5b2a2081 100755 --- a/ohos.build +++ b/ohos.build @@ -29,9 +29,7 @@ } ], "test_list": [ - "//base/sensors/sensor/frameworks/native/sensor/test:unittest", "//base/sensors/sensor/interfaces/native/test:unittest", - "//base/sensors/sensor/services/sensor/test:unittest", "//base/sensors/sensor/interfaces/plugin/test/unittest:unittest" ] } diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index c499a4f4acfb635985e78719ca541c7883aa858e..97abc65fc7a52c9d20652cd2ee54077a3cd3e789 100755 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -249,7 +249,7 @@ void SensorDataProcesser::SendRawData(std::unordered_map - -#include "client_info.h" -#include "sensors_errors.h" -#include "sensors_log_domain.h" - -namespace OHOS { -namespace Sensors { -using namespace testing::ext; -using namespace OHOS::HiviewDFX; - -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_TEST, "ClientInfoTest" }; -const uint32_t INVALID_SENSOR_ID = -1; -const uint32_t ACC_SENSOR_ID = (1 << 16) | (1 << 8); -const uint32_t MAG_SENSOR_ID = (1 << 24) | (1 << 16) | (1 << 8); -} // namespace - -class ClientInfoTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); - ClientInfo &clientInfo_ = ClientInfo::GetInstance(); -}; - -void ClientInfoTest::SetUpTestCase() -{} - -void ClientInfoTest::TearDownTestCase() -{} - -void ClientInfoTest::SetUp() -{} - -void ClientInfoTest::TearDown() -{} - -/* - * @tc.name: UpdateSensorInfo_001 - * @tc.desc: update sensor info - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorInfo_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = 1000; - int64_t samplingPeriodNs = 100000000; - int64_t maxReportDelayNs = 0; - SensorBasicInfo sensorInfo; - sensorInfo.SetSamplingPeriodNs(samplingPeriodNs); - sensorInfo.SetMaxReportDelayNs(maxReportDelayNs); - sensorInfo.SetSensorState(SENSOR_ENABLED); - bool ret = clientInfo_.UpdateSensorInfo(ACC_SENSOR_ID, pid, sensorInfo); - ASSERT_TRUE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: UpdateSensorInfo_002 - * @tc.desc: update sensor info - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorInfo_002, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = 1067; - int64_t samplingPeriodNs = 100000000L; - int64_t maxReportDelayNs = 0; - SensorBasicInfo sensorInfo; - sensorInfo.SetSamplingPeriodNs(samplingPeriodNs); - sensorInfo.SetMaxReportDelayNs(maxReportDelayNs); - sensorInfo.SetSensorState(SENSOR_ENABLED); - bool ret = clientInfo_.UpdateSensorInfo(ACC_SENSOR_ID, pid, sensorInfo); - ASSERT_TRUE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: UpdateSensorInfo_003 - * @tc.desc: update sensor info - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorInfo_003, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = 1000; - int64_t samplingPeriodNs = 100000000; - int64_t maxReportDelayNs = 0; - SensorBasicInfo sensorInfo; - sensorInfo.SetSamplingPeriodNs(samplingPeriodNs); - sensorInfo.SetMaxReportDelayNs(maxReportDelayNs); - sensorInfo.SetSensorState(SENSOR_ENABLED); - bool ret = clientInfo_.UpdateSensorInfo(MAG_SENSOR_ID, pid, sensorInfo); - ASSERT_TRUE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: UpdateSensorInfo_004 - * @tc.desc: update sensor info - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorInfo_004, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = 1000; - int64_t samplingPeriodNs = 100000000; - int64_t maxReportDelayNs = 0; - SensorBasicInfo sensorInfo; - sensorInfo.SetSamplingPeriodNs(samplingPeriodNs); - sensorInfo.SetMaxReportDelayNs(maxReportDelayNs); - sensorInfo.SetSensorState(SENSOR_ENABLED); - bool ret = clientInfo_.UpdateSensorInfo(INVALID_SENSOR_ID, pid, sensorInfo); - ASSERT_FALSE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: UpdateSensorInfo_005 - * @tc.desc: update sensor info - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorInfo_005, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = -1; - int64_t samplingPeriodNs = 100000000; - int64_t maxReportDelayNs = 0; - SensorBasicInfo sensorInfo; - sensorInfo.SetSamplingPeriodNs(samplingPeriodNs); - sensorInfo.SetMaxReportDelayNs(maxReportDelayNs); - sensorInfo.SetSensorState(SENSOR_DISABLED); - bool ret = clientInfo_.UpdateSensorInfo(MAG_SENSOR_ID, pid, sensorInfo); - ASSERT_FALSE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: UpdateSensorInfo_006 - * @tc.desc: update sensor info - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorInfo_006, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = 1067; - int64_t samplingPeriodNs = 50000000; - int64_t maxReportDelayNs = 0; - SensorBasicInfo sensorInfo; - sensorInfo.SetSamplingPeriodNs(samplingPeriodNs); - sensorInfo.SetMaxReportDelayNs(maxReportDelayNs); - sensorInfo.SetSensorState(SENSOR_ENABLED); - bool ret = clientInfo_.UpdateSensorInfo(ACC_SENSOR_ID, pid, sensorInfo); - ASSERT_TRUE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: GetSensorState_001 - * @tc.desc: get sensor state - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, GetSensorState_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - SensorState ret = clientInfo_.GetSensorState(ACC_SENSOR_ID); - ASSERT_EQ(ret, SENSOR_ENABLED); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: GetSensorState_002 - * @tc.desc: get sensor state - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, GetSensorState_002, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - SensorState ret = clientInfo_.GetSensorState(MAG_SENSOR_ID); - ASSERT_EQ(ret, SENSOR_ENABLED); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: UpdateSensorChannel_001 - * @tc.desc: update sensor channel - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorChannel_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = 1067; - sptr sensorChannel = new (std::nothrow) SensorBasicDataChannel(); - ASSERT_NE(sensorChannel, nullptr); - auto ret = clientInfo_.UpdateSensorChannel(pid, sensorChannel); - ASSERT_TRUE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: UpdateSensorChannel_002 - * @tc.desc: update sensor channel - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorChannel_002, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = -1; - sptr sensorChannel = new (std::nothrow) SensorBasicDataChannel(); - ASSERT_NE(sensorChannel, nullptr); - auto ret = clientInfo_.UpdateSensorChannel(pid, sensorChannel); - ASSERT_FALSE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: UpdateSensorChannel_003 - * @tc.desc: update sensor channel - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorChannel_003, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = 1000; - sptr sensorChannel = nullptr; - auto ret = clientInfo_.UpdateSensorChannel(pid, sensorChannel); - ASSERT_FALSE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: UpdateSensorChannel_004 - * @tc.desc: update sensor channel - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorChannel_004, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = -1; - sptr sensorChannel = nullptr; - auto ret = clientInfo_.UpdateSensorChannel(pid, sensorChannel); - ASSERT_FALSE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: UpdateSensorChannel_005 - * @tc.desc: update sensor channel - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, UpdateSensorChannel_005, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = 1000; - sptr sensorChannel = new (std::nothrow) SensorBasicDataChannel(); - ASSERT_NE(sensorChannel, nullptr); - auto ret = clientInfo_.UpdateSensorChannel(pid, sensorChannel); - ASSERT_TRUE(ret); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: GetSensorChannel_001 - * @tc.desc: get sensor channel - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, GetSensorChannel_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - auto ret = clientInfo_.GetSensorChannel(ACC_SENSOR_ID); - ASSERT_EQ(ret.size(), 2UL); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: GetSensorChannel_002 - * @tc.desc: get sensor channel - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, GetSensorChannel_002, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - auto ret = clientInfo_.GetSensorChannel(INVALID_SENSOR_ID); - ASSERT_EQ(ret.size(), 0UL); - HiLog::Info(LABEL, "%{public}s end ret.size() : %{public}d", __func__, int32_t{ ret.size() }); -} - -/* - * @tc.name: GetSensorChannel_003 - * @tc.desc: get sensor channel - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, GetSensorChannel_003, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - auto ret = clientInfo_.GetSensorChannel(INVALID_SENSOR_ID); - ASSERT_EQ(ret.size(), 0UL); - HiLog::Info(LABEL, "%{public}s end ret.size() : %{public}d", __func__, int32_t{ ret.size() }); -} - -/* - * @tc.name: GetSensorChannel_001 - * @tc.desc: get sensor channel - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, GetSensorChannel_004, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - auto ret = clientInfo_.GetSensorChannel(MAG_SENSOR_ID); - ASSERT_EQ(ret.size(), 1UL); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -/* - * @tc.name: DestroySensorChannel_001 - * @tc.desc: destroy sensor channel - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, DestroySensorChannel_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t pid = 1067; - - SensorState ret = clientInfo_.GetSensorState(ACC_SENSOR_ID); - ASSERT_EQ(ret, SENSOR_ENABLED); - - auto channelList = clientInfo_.GetSensorChannel(ACC_SENSOR_ID); - ASSERT_EQ(channelList.size(), 2UL); - - auto flag = clientInfo_.DestroySensorChannel(pid); - ASSERT_TRUE(flag); - - ret = clientInfo_.GetSensorState(ACC_SENSOR_ID); - ASSERT_EQ(ret, SENSOR_ENABLED); - - channelList = clientInfo_.GetSensorChannel(ACC_SENSOR_ID); - ASSERT_EQ(channelList.size(), 1UL); - - HiLog::Info(LABEL, "%{public}s end ret.size() : %{public}d", __func__, int32_t{ channelList.size() }); -} - -/* - * @tc.name: ClearSensorInfo_001 - * @tc.desc: clear sensor info - * @tc.type: FUNC - */ -HWTEST_F(ClientInfoTest, ClearSensorInfo_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - - SensorState ret = clientInfo_.GetSensorState(ACC_SENSOR_ID); - ASSERT_EQ(ret, SENSOR_ENABLED); - - auto flag = clientInfo_.ClearSensorInfo(ACC_SENSOR_ID); - ASSERT_TRUE(flag); - - ret = clientInfo_.GetSensorState(ACC_SENSOR_ID); - ASSERT_NE(ret, SENSOR_ENABLED); - - HiLog::Info(LABEL, "%{public}s end", __func__); -} -} // namespace Sensors -} // namespace OHOS diff --git a/services/sensor/test/unittest/phone/BUILD.gn b/services/sensor/test/unittest/phone/BUILD.gn deleted file mode 100755 index b0bcaa952d7a4bcb3bbb379cdbcdb4d2164261d6..0000000000000000000000000000000000000000 --- a/services/sensor/test/unittest/phone/BUILD.gn +++ /dev/null @@ -1,123 +0,0 @@ -# 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. - -import("//build/test.gni") - -SUBSYSTEM_DIR = "//base/sensors" -module_output_path = "sensors/sensor/services/sensor" - -###########################SensorDataProcesserTest########################### -ohos_unittest("SensorDataProcesserTest") { - module_out_path = module_output_path - - sources = [ "./sensor_data_processer_test.cpp" ] - - include_dirs = [ - "//utils/native/base/include", - "$SUBSYSTEM_DIR/sensor/utils/include", - "$SUBSYSTEM_DIR/sensor/services/sensor/include", - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor/include", - "//drivers/peripheral/sensor/interfaces/include", - "$SUBSYSTEM_DIR/sensor/interfaces/native/include", - "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", - ] - - deps = [ - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor:libsensor_native", - "$SUBSYSTEM_DIR/sensor/services/sensor:libsensor_service", - "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", - "//drivers/peripheral/sensor/hal:hdi_sensor", - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - "//utils/native/base:utils", - ] - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "samgr_standard:samgr_proxy", - ] -} - -###########################SensorProxyTest########################### -ohos_unittest("SensorProxyTest") { - module_out_path = module_output_path - - sources = [ "./sensor_proxy_test.cpp" ] - - include_dirs = [ - "//utils/native/base/include", - "//utils/system/safwk/native/include", - "$SUBSYSTEM_DIR/sensor/utils/include", - "$SUBSYSTEM_DIR/sensor/services/sensor/include", - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor/include", - "$SUBSYSTEM_DIR/sensor/interfaces/native/include", - "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", - ] - - deps = [ - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor:libsensor_native", - "$SUBSYSTEM_DIR/sensor/services/sensor:libsensor_service", - "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - "//utils/native/base:utils", - ] - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", - ] -} - -###########################SensorServiceImplTest########################### -ohos_unittest("SensorServiceImplTest") { - module_out_path = module_output_path - - sources = [ "./sensor_service_impl_test.cpp" ] - - include_dirs = [ - "//utils/native/base/include", - "//utils/system/safwk/native/include", - "$SUBSYSTEM_DIR/sensor/utils/include", - "$SUBSYSTEM_DIR/sensor/services/sensor/include", - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor/include", - "//drivers/peripheral/sensor/interfaces/include", - "$SUBSYSTEM_DIR/sensor/interfaces/native/include", - "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", - ] - - deps = [ - "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor:libsensor_native", - "$SUBSYSTEM_DIR/sensor/services/sensor:libsensor_service", - "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", - "//drivers/peripheral/sensor/hal:hdi_sensor", - "//utils/native/base:utils", - ] - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", - ] -} - -###########################end########################### -group("unittest") { - testonly = true - deps = [ - ":SensorDataProcesserTest", - ":SensorProxyTest", - ":SensorServiceImplTest", - ] -} diff --git a/services/sensor/test/unittest/phone/sensor_data_processer_test.cpp b/services/sensor/test/unittest/phone/sensor_data_processer_test.cpp deleted file mode 100755 index 00aa4d78b15332489a20c56091cfccfe37855cfe..0000000000000000000000000000000000000000 --- a/services/sensor/test/unittest/phone/sensor_data_processer_test.cpp +++ /dev/null @@ -1,206 +0,0 @@ -/* - * 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 -#include -#include - -#include "report_data_callback.h" -#include "sensor_data_channel.h" -#include "sensor_service_client.h" -#include "sensors_errors.h" -#include "sensors_log_domain.h" - -namespace OHOS { -namespace Sensors { -using namespace testing::ext; -using namespace OHOS::HiviewDFX; - -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_TEST, "SensorDataProcesserTest" }; -constexpr uint32_t ACC_SENSOR_ID = 0; -constexpr uint32_t WAIT_TIME = 3000; -constexpr uint64_t SAMPLING_PEROID_NS = 200000000; -constexpr uint64_t MAX_REPORT_DELAY_NS = 0; -constexpr uint32_t STEP_COUNTER_SENSORID = 0; -constexpr uint32_t STEP_DETECTOR_SENSORID = 0; -constexpr uint32_t OTHER = 4; -constexpr uint32_t SENSOR_TYPE_FLUSH = 4; -constexpr uint32_t FIRST_INDEX = 1; -constexpr uint32_t SENSOR_INDEX_SHIFT = 8; -constexpr uint32_t SENSOR_TYPE_SHIFT = 16; -constexpr uint32_t SENSOR_CATAGORY_SHIFT = 24; - -constexpr uint32_t FLUSH_COMPLETE_ID = (static_cast(OTHER) << SENSOR_CATAGORY_SHIFT) | - (static_cast(SENSOR_TYPE_FLUSH) << SENSOR_TYPE_SHIFT) | - (static_cast(FIRST_INDEX) << SENSOR_INDEX_SHIFT); -class SensorDataProcesserTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); - static void HandleEvent(struct SensorEvent *events, int32_t num, void *data); - static std::vector g_sensorsList; - static sptr g_dataChannel; - static sptr g_dataCallback; - static std::vector g_eventsBuf; - static std::unique_ptr g_serviceClient; - static int32_t g_dataCount; - static bool g_getFlushComplete; -}; - -struct SensorEvent g_event; -int32_t SensorDataProcesserTest::g_dataCount = 0; -bool SensorDataProcesserTest::g_getFlushComplete = false; -std::vector SensorDataProcesserTest::g_sensorsList; -sptr SensorDataProcesserTest::g_dataChannel; -sptr SensorDataProcesserTest::g_dataCallback; -std::vector SensorDataProcesserTest::g_eventsBuf; -std::unique_ptr SensorDataProcesserTest::g_serviceClient; -} // namespace - -void SensorDataProcesserTest::HandleEvent(struct SensorEvent *events, int32_t num, void *data) -{ - HiLog::Info(LABEL, "HandleEvent begin"); - if (num <= 0) { - HiLog::Error(LABEL, "%{public}s failed, num : %{public}d", __func__, num); - return; - } - g_event.sensorTypeId = events[0].sensorTypeId; - for (int32_t i = 0; i < num; i++) { - if (events[i].sensorTypeId == ACC_SENSOR_ID) { - g_dataCount++; - } - if (events[i].sensorTypeId == FLUSH_COMPLETE_ID) { - g_getFlushComplete = true; - } - } -} - -void SensorDataProcesserTest::SetUpTestCase() -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - g_dataChannel = new (std::nothrow) SensorDataChannel(); - ASSERT_NE(g_dataChannel, nullptr); - g_serviceClient = std::make_unique(); - ASSERT_NE(g_serviceClient, nullptr); - g_dataCallback = new (std::nothrow) ReportDataCallback(); - ASSERT_NE(g_dataCallback, nullptr); - g_sensorsList = g_serviceClient->GetSensorList(); - auto ret = g_dataChannel->CreateSensorDataChannel(HandleEvent, nullptr); - HiLog::Info(LABEL, "CreateSensorDataChannel ret : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); - g_serviceClient->TransferDataChannel(g_dataChannel); -} - -void SensorDataProcesserTest::TearDownTestCase() -{ - g_dataChannel->DestroySensorDataChannel(); - g_serviceClient->DestroyDataChannel(); -} - -void SensorDataProcesserTest::SetUp() -{} - -void SensorDataProcesserTest::TearDown() -{} - -/* - * @tc.name: EnableSensor_001 - * @tc.desc: enable sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorDataProcesserTest, EnableSensor_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "EnableSensor_001 begin"); - uint64_t samplingPeroidNs = SAMPLING_PEROID_NS; - uint64_t maxReportDelayNs = MAX_REPORT_DELAY_NS; - uint32_t sensorId = ACC_SENSOR_ID; - auto ret = g_serviceClient->EnableSensor(sensorId, samplingPeroidNs, maxReportDelayNs); - HiLog::Info(LABEL, "EnableSensor ret : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); - HiLog::Info(LABEL, "EnableSensor_001 end"); -} - -/* - * @tc.name: ReadSensorData_001 - * @tc.desc: read sensor data - * @tc.type: FUNC - */ -HWTEST_F(SensorDataProcesserTest, ReadSensorData_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "ReadSensorData_001 begin"); - g_dataCount = 0; - std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME)); - HiLog::Info(LABEL, "ReadSensorData_001 end"); -} - -/* - * @tc.name: ReportData_001 - * @tc.desc: judge whether the data is valid - * @tc.type: FUNC - */ -HWTEST_F(SensorDataProcesserTest, ReportData_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "ReportData_001 begin"); - ASSERT_EQ(g_event.sensorTypeId, 0); - HiLog::Info(LABEL, "ReportData_001 end, %{public}d", g_event.sensorTypeId); -} - -/* - * @tc.name: EnableStepCounter_001 - * @tc.desc: enable step counter sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorDataProcesserTest, EnableStepCounter_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "EnableStepCounter begin"); - ASSERT_NE(g_serviceClient, nullptr); - const int64_t samplingPeriodNs = 50000000; - const int64_t maxReportDelayNs = 0; - auto ret = g_serviceClient->EnableSensor(STEP_COUNTER_SENSORID, samplingPeriodNs, maxReportDelayNs); - HiLog::Info(LABEL, "Enable step counter, ret : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); -} - -/* - * @tc.name: DisableStepCounter_001 - * @tc.desc: disable step counter sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorDataProcesserTest, DisableStepCounter_001, TestSize.Level1) -{ - ASSERT_NE(g_serviceClient, nullptr); - auto ret = g_serviceClient->DisableSensor(STEP_COUNTER_SENSORID); - HiLog::Info(LABEL, "Disable step counter, ret : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); -} - -/* - * @tc.name: DisableStepDetector_001 - * @tc.desc: disable step detector sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorDataProcesserTest, DisableStepDetector_001, TestSize.Level1) -{ - ASSERT_NE(g_serviceClient, nullptr); - auto ret = g_serviceClient->DisableSensor(STEP_DETECTOR_SENSORID); - HiLog::Info(LABEL, "Disable step detector ret : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); -} -} // namespace Sensors -} // namespace OHOS diff --git a/services/sensor/test/unittest/phone/sensor_proxy_test.cpp b/services/sensor/test/unittest/phone/sensor_proxy_test.cpp deleted file mode 100755 index 22ce909005601b54e52878b73ea3b902b376d0ac..0000000000000000000000000000000000000000 --- a/services/sensor/test/unittest/phone/sensor_proxy_test.cpp +++ /dev/null @@ -1,261 +0,0 @@ -/* - * 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 -#include -#include - -#include - -#include -#include "ipc_skeleton.h" -#include "iservice_registry.h" -#include "sensor_data_event.h" -#include "sensor_service_proxy.h" -#include "sensors_errors.h" -#include "sensors_log_domain.h" -#include "string_ex.h" -#include "system_ability_definition.h" - -namespace OHOS { -namespace Sensors { -using namespace testing::ext; -using namespace OHOS::HiviewDFX; - -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_TEST, "SensorProxyTest" }; -const uint32_t INVALID_SENSOR_ID = -1; -const std::string CMD_LINE = "ps -ef | grep 'hardware.sensors' | grep -v grep | awk '{print $2}'"; -constexpr int32_t BUFFER_SIZE = 8; -constexpr pid_t INVALID_PID = -1; - -class SensorProxyTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); - pid_t GetSensorServicePid(); - sptr sensorProxy_; - std::vector sensors_; - uint32_t sensorId_; -}; -} // namespace - -void SensorProxyTest::SetUpTestCase() -{} - -void SensorProxyTest::TearDownTestCase() -{} - -void SensorProxyTest::SetUp() -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - ASSERT_NE(systemAbilityManager, nullptr); - sensorProxy_ = iface_cast(systemAbilityManager->GetSystemAbility(SENSOR_SERVICE_ABILITY_ID)); - ASSERT_NE(sensorProxy_, nullptr); - sensors_ = sensorProxy_->GetSensorList(); - ASSERT_NE(sensors_.size(), 0UL); - sensorId_ = sensors_[0].GetSensorId(); - ASSERT_NE(sensorId_, INVALID_SENSOR_ID); - HiLog::Info(LABEL, "%{public}s end", __func__); -} - -void SensorProxyTest::TearDown() -{} - -pid_t SensorProxyTest::GetSensorServicePid() -{ - pid_t pid = INVALID_PID; - char buf[BUFFER_SIZE] = { 0 }; - FILE *fp = popen(CMD_LINE.c_str(), "r"); - if (fp == nullptr) { - HiLog::Error(LABEL, "get error when getting sensor service process id"); - return pid; - } - - fgets(buf, sizeof(buf) - 1, fp); - pclose(fp); - fp = nullptr; - HiLog::Info(LABEL, "process is : %{public}s", buf); - - std::string pidStr(buf); - pidStr = TrimStr(pidStr, '\n'); - HiLog::Info(LABEL, "pidStr is : %{public}s", pidStr.c_str()); - if (pidStr.empty()) { - return pid; - } - - if (IsNumericStr(pidStr)) { - pid = std::stoi(pidStr); - } - return pid; -} - -/* - * @tc.name: EnableSensor_001 - * @tc.desc: enable sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorProxyTest, EnableSensor_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "EnableSensor begin"); - ASSERT_NE(sensorProxy_, nullptr); - int64_t samplingPeriodNs = 100000000L; - int64_t maxReportDelayNs = 0L; - auto ret = sensorProxy_->EnableSensor(sensorId_, samplingPeriodNs, maxReportDelayNs); - HiLog::Info(LABEL, "EnableSensor ret is : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); - - samplingPeriodNs = 50000000L; - maxReportDelayNs = 0L; - ret = sensorProxy_->EnableSensor(sensorId_, samplingPeriodNs, maxReportDelayNs); - ASSERT_EQ(ret, ERR_OK); - - samplingPeriodNs = 20000000L; - maxReportDelayNs = 0L; - ret = sensorProxy_->EnableSensor(sensorId_, samplingPeriodNs, maxReportDelayNs); - ASSERT_EQ(ret, ERR_OK); - samplingPeriodNs = 10000000L; - maxReportDelayNs = 0L; - ret = sensorProxy_->EnableSensor(sensorId_, samplingPeriodNs, maxReportDelayNs); - ASSERT_EQ(ret, ERR_OK); - - samplingPeriodNs = 20000000L; - maxReportDelayNs = 0L; - ret = sensorProxy_->EnableSensor(sensorId_, samplingPeriodNs, maxReportDelayNs); - ASSERT_EQ(ret, ERR_OK); -} - -/* - * @tc.name: EnableSensor_002 - * @tc.desc: enable sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorProxyTest, EnableSensor_002, TestSize.Level1) -{ - HiLog::Info(LABEL, "EnableSensor begin"); - ASSERT_NE(sensorProxy_, nullptr); - const int64_t samplingPeriodNs = 50000000; - const int64_t maxReportDelayNs = 0; - auto ret = sensorProxy_->EnableSensor(sensorId_, samplingPeriodNs, maxReportDelayNs); - HiLog::Info(LABEL, "EnableSensor ret is : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); -} - -/* - * @tc.name: EnableSensor_003 - * @tc.desc: enable sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorProxyTest, EnableSensor_003, TestSize.Level1) -{ - HiLog::Info(LABEL, "EnableSensor begin"); - ASSERT_NE(sensorProxy_, nullptr); - const int64_t samplingPeriodNs = 50000000; - const int64_t maxReportDelayNs = 0; - auto ret = sensorProxy_->EnableSensor(INVALID_SENSOR_ID, samplingPeriodNs, maxReportDelayNs); - HiLog::Info(LABEL, "EnableSensor ret is : %{public}d", ret); - ASSERT_NE(ret, ERR_OK); -} - -/* - * @tc.name: RunCommand_001 - * @tc.desc: run command flush - * @tc.type: FUNC - */ -HWTEST_F(SensorProxyTest, RunCommand_001, TestSize.Level1) -{ - ASSERT_NE(sensorProxy_, nullptr); - uint32_t cmdType = 0; // flush cmd - uint32_t params = 1; // flush cmd doesn't need this param, just give a value - - auto ret = sensorProxy_->RunCommand(sensorId_, cmdType, params); - HiLog::Info(LABEL, "RunCommand_001 ret is : %{public}d", ret); - // because the SensorProxyTest haven't calling TransferDataChannel, so RunCommand should return fail. - ASSERT_NE(ret, ERR_OK); -} - -/* - * @tc.name: RunCommand_002 - * @tc.desc: run command unknown - * @tc.type: FUNC - */ -HWTEST_F(SensorProxyTest, RunCommand_002, TestSize.Level1) -{ - ASSERT_NE(sensorProxy_, nullptr); - uint32_t cmdType = 4; // unknown cmd - uint32_t params = 1; - auto ret = sensorProxy_->RunCommand(sensorId_, cmdType, params); - ASSERT_NE(ret, ERR_OK); -} - -/* - * @tc.name: RunCommand_003 - * @tc.desc: run command unknown - * @tc.type: FUNC - */ -HWTEST_F(SensorProxyTest, RunCommand_003, TestSize.Level1) -{ - ASSERT_NE(sensorProxy_, nullptr); - uint32_t cmdType = 4; // unknown cmd - uint32_t params = 1; - auto ret = sensorProxy_->RunCommand(INVALID_SENSOR_ID, cmdType, params); - ASSERT_NE(ret, ERR_OK); -} - -/* - * @tc.name: RunCommand_004 - * @tc.desc: run command unknown - * @tc.type: FUNC - */ -HWTEST_F(SensorProxyTest, RunCommand_004, TestSize.Level1) -{ - ASSERT_NE(sensorProxy_, nullptr); - uint32_t cmdType = 0; // flush cmd - uint32_t params = 1; - auto ret = sensorProxy_->RunCommand(INVALID_SENSOR_ID, cmdType, params); - ASSERT_NE(ret, ERR_OK); -} - -/* - * @tc.name: DisableSensor_001 - * @tc.desc: disable sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorProxyTest, DisableSensor_001, TestSize.Level1) -{ - ASSERT_NE(sensorProxy_, nullptr); - auto ret = sensorProxy_->DisableSensor(sensorId_); - HiLog::Info(LABEL, "DisableSensor ret is : %{public}d", ret); - ASSERT_EQ(ret, ERR_OK); -} - -/* - * @tc.name: DisableSensor_002 - * @tc.desc: disable sensor - * @tc.type: FUNC - */ -HWTEST_F(SensorProxyTest, DisableSensor_002, TestSize.Level1) -{ - ASSERT_NE(sensorProxy_, nullptr); - auto ret = sensorProxy_->DisableSensor(INVALID_SENSOR_ID); - HiLog::Info(LABEL, "DisableSensor ret is : %{public}d", ret); - ASSERT_NE(ret, ERR_OK); -} -} // namespace Sensors -} // namespace OHOS diff --git a/services/sensor/test/unittest/phone/sensor_service_impl_test.cpp b/services/sensor/test/unittest/phone/sensor_service_impl_test.cpp deleted file mode 100755 index 64d9c238fc400c5ac40eed14cdccf05ab61bd0f2..0000000000000000000000000000000000000000 --- a/services/sensor/test/unittest/phone/sensor_service_impl_test.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 -#include -#include - -#include "report_data_callback.h" -#include "sensor_agent_type.h" -#include "sensor_data_channel.h" -#include "sensor_if.h" -#include "sensor_service_client.h" -#include "sensor_service_impl.h" -#include "sensors_errors.h" -#include "sensors_log_domain.h" - -namespace OHOS { -namespace Sensors { -using namespace testing::ext; -using namespace OHOS::HiviewDFX; - -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_TEST, "SensorServiceImplTest" }; -} // namespace - -class SensorServiceImplTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); - SensorServiceImpl &sensorServiceImpl_ = SensorServiceImpl::GetInstance(); -}; - -void SensorServiceImplTest::SetUpTestCase() -{} - -void SensorServiceImplTest::TearDownTestCase() -{} - -void SensorServiceImplTest::SetUp() -{} - -void SensorServiceImplTest::TearDown() -{} - -/* - * @tc.name: RegisteDataReport_001 - * @tc.desc: register data report. - * @tc.type: FUNC - * @tc.require: SR000F5A2Q AR000F8QO2 - * @tc.author: wuzhihui - */ -HWTEST_F(SensorServiceImplTest, RegisteDataReport_001, TestSize.Level1) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - // 获取sensor列表 - sensorServiceImpl_.InitSensorServiceImpl(); - std::vector list = sensorServiceImpl_.GetSensorList(); - for (size_t i=0; i reportDataCallback_; - reportDataCallback_ = new (std::nothrow) ReportDataCallback(); - ASSERT_NE(reportDataCallback_, nullptr); - cb = &ReportDataCallback::ZReportDataCallback; - ret = sensorServiceImpl_.RegisteDataReport(cb, reportDataCallback_); - ASSERT_EQ(ret, 0); - - // 激活 - ret = sensorServiceImpl_.EnableSensor(sensorId); - EXPECT_EQ(0, ret); - std::this_thread::sleep_for(std::chrono::milliseconds(5000)); - - HiLog::Info(LABEL, "%{public}s begin", "--->Unregister1111"); - // 去注册 - ret = sensorServiceImpl_.Unregister(); - ASSERT_EQ(ret, 0); - HiLog::Info(LABEL, "%{public}s begin", "--->Unregister222"); - - // 去激活 - ret = sensorServiceImpl_.DisableSensor(sensorId); - ASSERT_EQ(ret, 0); - HiLog::Info(LABEL, "%{public}s end", __func__); -} -} // namespace Sensors -} // namespace OHOS diff --git a/utils/include/sensor_basic_data_channel.h b/utils/include/sensor_basic_data_channel.h index 52975751b2a2aa1d6aa8a2ed0f6d385441a888b5..2507a8fbaae79a75acd61d3bd11234fb3dcb61bf 100755 --- a/utils/include/sensor_basic_data_channel.h +++ b/utils/include/sensor_basic_data_channel.h @@ -26,6 +26,7 @@ namespace OHOS { namespace Sensors { constexpr int32_t INVALID_FD = -1; +constexpr int32_t SENSOR_MAX_LENGTH = 64; struct TransferSensorEvents { uint32_t sensorTypeId; /**< Sensor type ID */ int32_t version; /**< Sensor algorithm version */ @@ -33,7 +34,7 @@ struct TransferSensorEvents { int32_t option; /**< Sensor data options, including the measurement range and accuracy */ int32_t mode; /**< Sensor data reporting mode (described in {@link SensorMode}) */ uint32_t dataLen; /**< Sensor data length */ - uint8_t data[16]; + uint8_t data[SENSOR_MAX_LENGTH]; }; class SensorBasicDataChannel : public RefBase { public: