From 5f2daeff8cd37ea265134799b758741f56c72483 Mon Sep 17 00:00:00 2001 From: hhh2 Date: Mon, 30 May 2022 13:17:31 +0000 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E9=80=80=20'Pull=20Request=20!159=20:?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9get=20sensorlist=E5=AE=9E=E7=8E=B0'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sensor/include/sensor_agent_proxy.h | 5 +- .../sensor/include/sensor_service_client.h | 2 +- .../native/sensor/src/sensor_agent_proxy.cpp | 201 ++++++++++-------- .../sensor/src/sensor_service_client.cpp | 13 +- interfaces/plugin/src/sensor_js.cpp | 4 + utils/include/sensors_errors.h | 8 - 6 files changed, 117 insertions(+), 116 deletions(-) diff --git a/frameworks/native/sensor/include/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index 2393cf4d..9ca8a8e6 100644 --- a/frameworks/native/sensor/include/sensor_agent_proxy.h +++ b/frameworks/native/sensor/include/sensor_agent_proxy.h @@ -33,7 +33,7 @@ typedef int32_t (*SensorDataCallback)(struct SensorNativeData *events, uint32_t struct SensorAgentProxy : public OHOS::RefBase { public: SensorAgentProxy(); - ~SensorAgentProxy(); + ~SensorAgentProxy() = default; static const SensorAgentProxy *GetSensorsObj(); int32_t ActivateSensor(int32_t sensorId, const SensorUser *user) const; int32_t DeactivateSensor(int32_t sensorId, const SensorUser *user) const; @@ -48,9 +48,6 @@ private: int32_t CreateSensorDataChannel() const; int32_t DestroySensorDataChannel() const; static void HandleSensorData(SensorEvent *events, int32_t num, void *data); - int32_t ConvertSensorInfos() const; - void ClearSensorInfos() const; - static OHOS::sptr sensorObj_; static std::mutex subscribeMutex_; static std::mutex chanelMutex_; diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index 38c29496..57a07166 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -42,12 +42,12 @@ public: int32_t TransferDataChannel(sptr sensorDataChannel); int32_t DestroyDataChannel(); void ProcessDeathObserver(const wptr &object); - bool IsValid(uint32_t sensorId); private: int32_t InitServiceClient(); void UpdateSensorInfoMap(uint32_t sensorId, int64_t samplingPeroid, int64_t maxReportDelay); void DeleteSensorInfoItem(uint32_t sensorId); + bool IsValidSensorId(uint32_t sensorId); std::mutex clientMutex_; sptr serviceDeathObserver_; sptr sensorServer_; diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index bb65c746..a87511f4 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -28,19 +28,50 @@ namespace OHOS { namespace Sensors { namespace { constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::SensorsLogDomain::SENSORS_IMPLEMENT, "SensorAgentProxy" }; -constexpr uint32_t MAX_SENSOR_LIST_SIZE = 0Xffff; + +using OHOS::ERR_OK; +using OHOS::Sensors::BODY; +using OHOS::Sensors::DEVICE_MOTION; +using OHOS::Sensors::ENVIRONMENT; +using OHOS::Sensors::INVALID_POINTER; +using OHOS::Sensors::LIGHT; +using OHOS::Sensors::ORIENTATION; +using OHOS::Sensors::OTHER; +using OHOS::Sensors::SENSOR_TYPE_6DOF_ATTITUDE; +using OHOS::Sensors::SENSOR_TYPE_ACCELEROMETER; +using OHOS::Sensors::SENSOR_TYPE_ACCELEROMETER_UNCALIBRATED; +using OHOS::Sensors::SENSOR_TYPE_AMBIENT_LIGHT; +using OHOS::Sensors::SENSOR_TYPE_AMBIENT_TEMPERATURE; +using OHOS::Sensors::SENSOR_TYPE_BAROMETER; +using OHOS::Sensors::SENSOR_TYPE_DEVICE_ORIENTATION; +using OHOS::Sensors::SENSOR_TYPE_GAME_ROTATION_VECTOR; +using OHOS::Sensors::SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR; +using OHOS::Sensors::SENSOR_TYPE_GRAVITY; +using OHOS::Sensors::SENSOR_TYPE_GYROSCOPE; +using OHOS::Sensors::SENSOR_TYPE_GYROSCOPE_UNCALIBRATED; +using OHOS::Sensors::SENSOR_TYPE_HEART_RATE_DETECTOR; +using OHOS::Sensors::SENSOR_TYPE_HUMIDITY; +using OHOS::Sensors::SENSOR_TYPE_LINEAR_ACCELERATION; +using OHOS::Sensors::SENSOR_TYPE_MAGNETIC_FIELD; +using OHOS::Sensors::SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED; +using OHOS::Sensors::SENSOR_TYPE_ORIENTATION; +using OHOS::Sensors::SENSOR_TYPE_PRESSURE_DETECTOR; +using OHOS::Sensors::SENSOR_TYPE_PROXIMITY; +using OHOS::Sensors::SENSOR_TYPE_ROTATION_VECTOR; +using OHOS::Sensors::SENSOR_TYPE_SIGNIFICANT_MOTION; +using OHOS::Sensors::SENSOR_TYPE_STEP_COUNTER; +using OHOS::Sensors::SENSOR_TYPE_STEP_DETECTOR; +using OHOS::Sensors::SENSOR_TYPE_WEAR_DETECTOR; +using OHOS::Sensors::SensorDataChannel; +using OHOS::Sensors::SensorServiceClient; } // namespace -#define SenClient SensorServiceClient::GetInstance() OHOS::sptr SensorAgentProxy::sensorObj_ = nullptr; bool SensorAgentProxy::g_isChannelCreated; int64_t SensorAgentProxy::g_samplingInterval; int64_t SensorAgentProxy::g_reportInterval; std::mutex SensorAgentProxy::subscribeMutex_; std::mutex SensorAgentProxy::chanelMutex_; -std::mutex sensorInfoMutex_; -SensorInfo *sensorInfos_ = nullptr; -int32_t sensorInfoCount_ = 0; std::map SensorAgentProxy::g_subscribeMap; std::map SensorAgentProxy::g_unsubscribeMap; @@ -48,15 +79,10 @@ SensorAgentProxy::SensorAgentProxy() : dataChannel_(new (std::nothrow) SensorDataChannel()) {} -SensorAgentProxy::~SensorAgentProxy() -{ - CALL_LOG_ENTER; - ClearSensorInfos(); -} - const SensorAgentProxy *SensorAgentProxy::GetSensorsObj() { CALL_LOG_ENTER; + if (sensorObj_ == nullptr) { SEN_HILOGD("sensorObj_ new object"); sensorObj_ = new (std::nothrow) SensorAgentProxy(); @@ -97,7 +123,8 @@ int32_t SensorAgentProxy::CreateSensorDataChannel() const SEN_HILOGE("create data channel failed, ret: %{public}d", ret); return ret; } - ret = SenClient.TransferDataChannel(dataChannel_); + auto &client = SensorServiceClient::GetInstance(); + ret = client.TransferDataChannel(dataChannel_); if (ret != ERR_OK) { auto destoryRet = dataChannel_->DestroySensorDataChannel(); SEN_HILOGE("transfer data channel failed, ret : %{public}d, destoryRet : %{public}d", ret, destoryRet); @@ -121,7 +148,8 @@ int32_t SensorAgentProxy::DestroySensorDataChannel() const SEN_HILOGE("destory data channel failed, ret : %{public}d", ret); return ret; } - ret = SenClient.DestroyDataChannel(); + SensorServiceClient &client = SensorServiceClient::GetInstance(); + ret = client.DestroyDataChannel(); if (ret != ERR_OK) { SEN_HILOGE("destory service data channel fail, ret : %{public}d", ret); return ret; @@ -134,20 +162,21 @@ 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"); + if (sensorId < 0) { + SEN_HILOGE("user is null or sensorId is invalid"); return ERROR; } - if (!SenClient.IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); + if (g_samplingInterval < 0 || g_reportInterval < 0) { + SEN_HILOGE("samplingPeroid or g_reportInterval is invalid"); return 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; } - int32_t ret = SenClient.EnableSensor(sensorId, g_samplingInterval, g_reportInterval); + SensorServiceClient &client = SensorServiceClient::GetInstance(); + int32_t ret = client.EnableSensor(sensorId, g_samplingInterval, g_reportInterval); g_samplingInterval = -1; g_reportInterval = -1; if (ret != 0) { @@ -163,9 +192,9 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u { 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; + if (sensorId < 0) { + SEN_HILOGE("user is null or sensorId is invalid"); + return OHOS::Sensors::ERROR; } std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { @@ -174,7 +203,8 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u } g_subscribeMap.erase(sensorId); g_unsubscribeMap[sensorId] = user; - int32_t ret = SenClient.DisableSensor(sensorId); + SensorServiceClient &client = SensorServiceClient::GetInstance(); + int32_t ret = client.DisableSensor(sensorId); if (ret != 0) { SEN_HILOGE("disable sensor failed, ret: %{public}d", ret); return OHOS::Sensors::ERROR; @@ -186,9 +216,9 @@ int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int int64_t reportInterval) const { CHKPR(user, OHOS::Sensors::ERROR); - if (!SenClient.IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); - return ERROR; + if (sensorId < 0) { + SEN_HILOGE("user is null or sensorId is invalid"); + return OHOS::Sensors::ERROR; } if (samplingInterval < 0 || reportInterval < 0) { SEN_HILOGE("samplingInterval or reportInterval is invalid"); @@ -209,9 +239,9 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us SEN_HILOGI("in, sensorId: %{public}d", sensorId); 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; + if (sensorId < 0) { + SEN_HILOGE("user or sensorId is invalid"); + return OHOS::Sensors::ERROR; } int32_t ret = CreateSensorDataChannel(); if (ret != ERR_OK) { @@ -228,9 +258,9 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * SEN_HILOGI("in, sensorId: %{public}d", sensorId); 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; + if (sensorId < 0) { + SEN_HILOGE("user is null or sensorId is invalid"); + return OHOS::Sensors::ERROR; } std::lock_guard subscribeLock(subscribeMutex_); if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { @@ -252,9 +282,9 @@ int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int3 { 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; + if (sensorId < 0) { + SEN_HILOGE("user is null or sensorId is invalid"); + return OHOS::Sensors::ERROR; } std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { @@ -268,9 +298,9 @@ int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, in { 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; + if (sensorId < 0) { + SEN_HILOGE("user is null or sensorId is invalid"); + return OHOS::Sensors::ERROR; } std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { @@ -280,68 +310,51 @@ int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, in return OHOS::Sensors::SUCCESS; } -void SensorAgentProxy::ClearSensorInfos() const -{ - CHKPV(sensorInfos_); - free(sensorInfos_); - sensorInfos_ = nullptr; -} - -int32_t SensorAgentProxy::ConvertSensorInfos() const -{ - CALL_LOG_ENTER; - std::vector sensorList = SenClient.GetSensorList(); - if (sensorList.empty()) { - SEN_HILOGE("get sensor lists failed"); - return ERROR; - } - size_t count = sensorList.size(); - if (count > MAX_SENSOR_LIST_SIZE) { - SEN_HILOGE("The number of sensors exceeds the maximum value"); - return ERROR; - } - sensorInfos_ = (SensorInfo *)malloc(sizeof(SensorInfo) * count); - CHKPR(sensorInfos_, ERROR); - for (size_t i = 0; i < count; ++i) { - errno_t ret = strcpy_s((sensorInfos_ + i)->sensorName, NAME_MAX_LEN, - sensorList[i].GetSensorName().c_str()); - CHKCR(ret == EOK, ERROR); - ret = strcpy_s((sensorInfos_ + i)->vendorName, NAME_MAX_LEN, - sensorList[i].GetVendorName().c_str()); - CHKCR(ret == EOK, ERROR); - ret = strcpy_s((sensorInfos_ + i)->hardwareVersion, VERSION_MAX_LEN, - sensorList[i].GetHardwareVersion().c_str()); - CHKCR(ret == EOK, ERROR); - ret = strcpy_s((sensorInfos_ + i)->firmwareVersion, VERSION_MAX_LEN, - sensorList[i].GetFirmwareVersion().c_str()); - CHKCR(ret == EOK, ERROR); - (sensorInfos_ + i)->sensorId = static_cast(sensorList[i].GetSensorId()); - (sensorInfos_ + i)->sensorTypeId = static_cast(sensorList[i].GetSensorTypeId()); - (sensorInfos_ + i)->maxRange = sensorList[i].GetMaxRange(); - (sensorInfos_ + i)->precision = sensorList[i].GetResolution(); - (sensorInfos_ + i)->power = sensorList[i].GetPower(); - } - sensorInfoCount_ = count; - return SUCCESS; -} - int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) const { - CALL_LOG_ENTER; CHKPR(sensorInfo, OHOS::Sensors::ERROR); CHKPR(count, OHOS::Sensors::ERROR); - std::lock_guard listLock(sensorInfoMutex_); - if (sensorInfos_ == nullptr) { - int32_t ret = ConvertSensorInfos(); - if (ret != SUCCESS) { - SEN_HILOGE("convert sensor lists failed"); - ClearSensorInfos(); - return ERROR; + SensorServiceClient &client = SensorServiceClient::GetInstance(); + std::vector sensorList_ = client.GetSensorList(); + if (sensorList_.empty()) { + SEN_HILOGE("get sensor lists failed"); + return OHOS::Sensors::ERROR; + } + *count = sensorList_.size(); + *sensorInfo = (SensorInfo *)malloc(sizeof(SensorInfo) * (*count)); + CHKPR(*sensorInfo, ERROR); + for (int32_t index = 0; index < *count; ++index) { + errno_t ret = strcpy_s((*sensorInfo + index)->sensorName, NAME_MAX_LEN, + sensorList_[index].GetSensorName().c_str()); + if (ret != EOK) { + SEN_HILOGE("strcpy sensorName failed"); + return OHOS::Sensors::ERROR; + } + ret = strcpy_s((*sensorInfo + index)->vendorName, NAME_MAX_LEN, + sensorList_[index].GetVendorName().c_str()); + if (ret != EOK) { + SEN_HILOGE("strcpy vendorName failed"); + return OHOS::Sensors::ERROR; + } + ret = strcpy_s((*sensorInfo + index)->hardwareVersion, VERSION_MAX_LEN, + sensorList_[index].GetHardwareVersion().c_str()); + if (ret != EOK) { + SEN_HILOGE("strcpy hardwareVersion failed"); + return OHOS::Sensors::ERROR; } + ret = strcpy_s((*sensorInfo + index)->firmwareVersion, VERSION_MAX_LEN, + sensorList_[index].GetFirmwareVersion().c_str()); + if (ret != EOK) { + SEN_HILOGE("strcpy hardwareVersion failed"); + return OHOS::Sensors::ERROR; + } + (*sensorInfo + index)->sensorId = static_cast(sensorList_[index].GetSensorId()); + (*sensorInfo + index)->sensorTypeId = static_cast(sensorList_[index].GetSensorTypeId()); + (*sensorInfo + index)->maxRange = sensorList_[index].GetMaxRange(); + (*sensorInfo + index)->precision = sensorList_[index].GetResolution(); + (*sensorInfo + index)->power = sensorList_[index].GetPower(); } - *sensorInfo = sensorInfos_; - *count = sensorInfoCount_; - return SUCCESS; + return OHOS::Sensors::SUCCESS; } } // 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 84f2b2a5..af3883aa 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -72,13 +72,8 @@ int32_t SensorServiceClient::InitServiceClient() return SENSOR_NATIVE_GET_SERVICE_ERR; } -bool SensorServiceClient::IsValid(uint32_t sensorId) +bool SensorServiceClient::IsValidSensorId(uint32_t sensorId) { - int32_t ret = InitServiceClient(); - if (ret != ERR_OK) { - SEN_HILOGE("InitServiceClient failed, ret : %{public}d", ret); - return false; - } if (sensorList_.empty()) { SEN_HILOGE("sensorList_ cannot be empty"); return false; @@ -94,7 +89,7 @@ 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)) { + if (!IsValidSensorId(sensorId)) { SEN_HILOGE("sensorId is invalid"); return SENSOR_NATIVE_SAM_ERR; } @@ -113,7 +108,7 @@ int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPer int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) { CALL_LOG_ENTER; - if (!IsValid(sensorId)) { + if (!IsValidSensorId(sensorId)) { SEN_HILOGE("sensorId is invalid"); return SENSOR_NATIVE_SAM_ERR; } @@ -132,7 +127,7 @@ int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) int32_t SensorServiceClient::RunCommand(uint32_t sensorId, int32_t cmdType, int32_t params) { CALL_LOG_ENTER; - if (!IsValid(sensorId)) { + if (!IsValidSensorId(sensorId)) { SEN_HILOGE("sensorId is invalid"); return SENSOR_NATIVE_SAM_ERR; } diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 30508cac..d98c24d3 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -798,6 +798,8 @@ static napi_value GetSensorList(napi_env env, napi_callback_info info) for (int32_t i = 0; i < count; ++i) { asyncCallbackInfo->sensorInfos.push_back(*(sensorInfos + i)); } + free(sensorInfos); + sensorInfos = nullptr; } if (argc == 0) { napi_value promise = nullptr; @@ -844,6 +846,8 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) SEN_HILOGE("Not find sensorTypeId: %{public}d", sensorTypeId); asyncCallbackInfo->type = FAIL; } + free(sensorInfos); + sensorInfos = nullptr; } if (argc == 1) { napi_value promise = nullptr; diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h index 42ce32db..f7ced51e 100644 --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -277,14 +277,6 @@ private: } \ } while (0) -#define CHKCR(cond, r) \ - do { \ - if (!(cond)) { \ - SEN_HILOGE("CK(%{public}s), errCode:%{public}d", #cond, r); \ - return r; \ - } \ - } while (0) - #endif } // namespace Sensors } // namespace OHOS -- Gitee