From 01f156c2cc651b19975eefe2d9732e8257223acc Mon Sep 17 00:00:00 2001 From: h00514358 Date: Thu, 26 May 2022 17:17:40 +0800 Subject: [PATCH 1/5] Modify sensor list interfaces Signed-off-by: h00514358 --- .../sensor/include/sensor_agent_proxy.h | 5 +- .../sensor/include/sensor_service_client.h | 2 +- .../native/sensor/src/sensor_agent_proxy.cpp | 191 +++++++++--------- utils/include/sensors_errors.h | 8 + 4 files changed, 103 insertions(+), 103 deletions(-) diff --git a/frameworks/native/sensor/include/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index 9ca8a8e6..2416608f 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() = default; + ~SensorAgentProxy(); static const SensorAgentProxy *GetSensorsObj(); int32_t ActivateSensor(int32_t sensorId, const SensorUser *user) const; int32_t DeactivateSensor(int32_t sensorId, const SensorUser *user) const; @@ -57,6 +57,9 @@ private: static int64_t g_reportInterval; static std::map g_subscribeMap; static std::map g_unsubscribeMap; + static std::mutex sensorInfoMutex_; + int32_t ConvertSensorInfos() const; + void FreeMemory(void *pointer) const; }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index 57a07166..c8998a75 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 IsValidSensorId(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 a87511f4..d0e86cf0 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -28,42 +28,6 @@ namespace OHOS { namespace Sensors { namespace { constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::SensorsLogDomain::SENSORS_IMPLEMENT, "SensorAgentProxy" }; - -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 OHOS::sptr SensorAgentProxy::sensorObj_ = nullptr; @@ -72,6 +36,9 @@ int64_t SensorAgentProxy::g_samplingInterval; int64_t SensorAgentProxy::g_reportInterval; std::mutex SensorAgentProxy::subscribeMutex_; std::mutex SensorAgentProxy::chanelMutex_; +std::mutex SensorAgentProxy::sensorInfoMutex_; +SensorInfo *sensorInfos_ = nullptr; +int32_t sensorInfoCount_ = 0; std::map SensorAgentProxy::g_subscribeMap; std::map SensorAgentProxy::g_unsubscribeMap; @@ -79,10 +46,15 @@ SensorAgentProxy::SensorAgentProxy() : dataChannel_(new (std::nothrow) SensorDataChannel()) {} -const SensorAgentProxy *SensorAgentProxy::GetSensorsObj() +SensorAgentProxy::~SensorAgentProxy() { CALL_LOG_ENTER; + FreeMemory(sensorInfos_); +} +const SensorAgentProxy *SensorAgentProxy::GetSensorsObj() +{ + CALL_LOG_ENTER; if (sensorObj_ == nullptr) { SEN_HILOGD("sensorObj_ new object"); sensorObj_ = new (std::nothrow) SensorAgentProxy(); @@ -162,20 +134,20 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use { CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - if (sensorId < 0) { - SEN_HILOGE("user is null or sensorId is invalid"); - return ERROR; - } + std::lock_guard subscribeLock(subscribeMutex_); if (g_samplingInterval < 0 || g_reportInterval < 0) { SEN_HILOGE("samplingPeroid or g_reportInterval is invalid"); return ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + SensorServiceClient &client = SensorServiceClient::GetInstance(); + if (!client.IsValidSensorId(sensorId)) { + SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); + return ERROR; + } if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { SEN_HILOGE("subscribe sensorId first"); return ERROR; } - SensorServiceClient &client = SensorServiceClient::GetInstance(); int32_t ret = client.EnableSensor(sensorId, g_samplingInterval, g_reportInterval); g_samplingInterval = -1; g_reportInterval = -1; @@ -192,9 +164,10 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u { CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - if (sensorId < 0) { - SEN_HILOGE("user is null or sensorId is invalid"); - return OHOS::Sensors::ERROR; + SensorServiceClient &client = SensorServiceClient::GetInstance(); + if (!client.IsValidSensorId(sensorId)) { + SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); + return ERROR; } std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { @@ -203,7 +176,6 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u } g_subscribeMap.erase(sensorId); g_unsubscribeMap[sensorId] = user; - SensorServiceClient &client = SensorServiceClient::GetInstance(); int32_t ret = client.DisableSensor(sensorId); if (ret != 0) { SEN_HILOGE("disable sensor failed, ret: %{public}d", ret); @@ -216,9 +188,10 @@ int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int int64_t reportInterval) const { CHKPR(user, OHOS::Sensors::ERROR); - if (sensorId < 0) { - SEN_HILOGE("user is null or sensorId is invalid"); - return OHOS::Sensors::ERROR; + SensorServiceClient &client = SensorServiceClient::GetInstance(); + if (!client.IsValidSensorId(sensorId)) { + SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); + return ERROR; } if (samplingInterval < 0 || reportInterval < 0) { SEN_HILOGE("samplingInterval or reportInterval is invalid"); @@ -239,9 +212,10 @@ 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 (sensorId < 0) { - SEN_HILOGE("user or sensorId is invalid"); - return OHOS::Sensors::ERROR; + SensorServiceClient &client = SensorServiceClient::GetInstance(); + if (!client.IsValidSensorId(sensorId)) { + SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); + return ERROR; } int32_t ret = CreateSensorDataChannel(); if (ret != ERR_OK) { @@ -258,9 +232,10 @@ 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 (sensorId < 0) { - SEN_HILOGE("user is null or sensorId is invalid"); - return OHOS::Sensors::ERROR; + SensorServiceClient &client = SensorServiceClient::GetInstance(); + if (!client.IsValidSensorId(sensorId)) { + SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); + return ERROR; } std::lock_guard subscribeLock(subscribeMutex_); if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { @@ -282,9 +257,10 @@ int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int3 { CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - if (sensorId < 0) { - SEN_HILOGE("user is null or sensorId is invalid"); - return OHOS::Sensors::ERROR; + SensorServiceClient &client = SensorServiceClient::GetInstance(); + if (!client.IsValidSensorId(sensorId)) { + SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); + return ERROR; } std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { @@ -298,9 +274,10 @@ int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, in { CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - if (sensorId < 0) { - SEN_HILOGE("user is null or sensorId is invalid"); - return OHOS::Sensors::ERROR; + SensorServiceClient &client = SensorServiceClient::GetInstance(); + if (!client.IsValidSensorId(sensorId)) { + SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); + return ERROR; } std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { @@ -310,51 +287,63 @@ int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, in return OHOS::Sensors::SUCCESS; } -int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) const +void SensorAgentProxy::FreeMemory(void *pointer) const +{ + CHKPV(pointer); + free(pointer); + pointer = nullptr; +} + +int32_t SensorAgentProxy::ConvertSensorInfos() const { - CHKPR(sensorInfo, OHOS::Sensors::ERROR); - CHKPR(count, OHOS::Sensors::ERROR); SensorServiceClient &client = SensorServiceClient::GetInstance(); - std::vector sensorList_ = client.GetSensorList(); - if (sensorList_.empty()) { + std::vector sensorList = client.GetSensorList(); + if (sensorList.empty()) { SEN_HILOGE("get sensor lists failed"); - return OHOS::Sensors::ERROR; + return 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; + int32_t count = sensorList.size(); + sensorInfos_ = (SensorInfo *)malloc(sizeof(SensorInfo) * count); + CHKPR(sensorInfos_, ERROR); + for (int32_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 +{ + 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"); + FreeMemory(sensorInfos_); + return 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(); } - return OHOS::Sensors::SUCCESS; + *sensorInfo = sensorInfos_; + *count = sensorInfoCount_; + return SUCCESS; } } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h index f7ced51e..42ce32db 100644 --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -277,6 +277,14 @@ 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 From 6950e5611381f6cf8b196028beac6f6966348576 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Fri, 27 May 2022 10:17:20 +0800 Subject: [PATCH 2/5] Modify sensor list interfaces Signed-off-by: h00514358 --- frameworks/native/sensor/src/sensor_agent_proxy.cpp | 7 ++++++- frameworks/native/sensor/src/sensor_service_client.cpp | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index d0e86cf0..a4208cb4 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -28,6 +28,7 @@ namespace OHOS { namespace Sensors { namespace { constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::SensorsLogDomain::SENSORS_IMPLEMENT, "SensorAgentProxy" }; +constexpr uint32_t MAX_SENSOR_LIST_SIZE = 0Xffff; } // namespace OHOS::sptr SensorAgentProxy::sensorObj_ = nullptr; @@ -302,7 +303,11 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const SEN_HILOGE("get sensor lists failed"); return ERROR; } - int32_t count = sensorList.size(); + uint32_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 (int32_t i = 0; i < count; ++i) { diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index af3883aa..d491a5c4 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -74,6 +74,11 @@ int32_t SensorServiceClient::InitServiceClient() 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; -- Gitee From 306e9244cc3dca58103d480048124b1f88da2eed Mon Sep 17 00:00:00 2001 From: h00514358 Date: Fri, 27 May 2022 15:42:08 +0800 Subject: [PATCH 3/5] Modify sensor list interfaces Signed-off-by: h00514358 --- frameworks/native/sensor/src/sensor_agent_proxy.cpp | 2 ++ interfaces/plugin/src/sensor_js.cpp | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index a4208cb4..285f305e 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -297,6 +297,7 @@ void SensorAgentProxy::FreeMemory(void *pointer) const int32_t SensorAgentProxy::ConvertSensorInfos() const { + CALL_LOG_ENTER; SensorServiceClient &client = SensorServiceClient::GetInstance(); std::vector sensorList = client.GetSensorList(); if (sensorList.empty()) { @@ -335,6 +336,7 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const 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_); diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index d98c24d3..30508cac 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -798,8 +798,6 @@ 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; @@ -846,8 +844,6 @@ 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; -- Gitee From 21f7947d63fc1f2d09c9e52a46a6dbc8341299ad Mon Sep 17 00:00:00 2001 From: h00514358 Date: Fri, 27 May 2022 16:48:39 +0800 Subject: [PATCH 4/5] Modify sensor list interfaces Signed-off-by: h00514358 --- frameworks/native/sensor/src/sensor_agent_proxy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 285f305e..00c0f4d6 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -311,7 +311,7 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const } sensorInfos_ = (SensorInfo *)malloc(sizeof(SensorInfo) * count); CHKPR(sensorInfos_, ERROR); - for (int32_t i = 0; i < count; ++i) { + for (uint32_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); -- Gitee From 25ac16c27c9adce3b688605874b9633f077edeb2 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Sat, 28 May 2022 18:04:36 +0800 Subject: [PATCH 5/5] Modify sensor list interfaces Signed-off-by: h00514358 --- .../sensor/include/sensor_agent_proxy.h | 6 +-- .../sensor/include/sensor_service_client.h | 2 +- .../native/sensor/src/sensor_agent_proxy.cpp | 53 ++++++++----------- .../sensor/src/sensor_service_client.cpp | 8 +-- 4 files changed, 30 insertions(+), 39 deletions(-) diff --git a/frameworks/native/sensor/include/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index 2416608f..2393cf4d 100644 --- a/frameworks/native/sensor/include/sensor_agent_proxy.h +++ b/frameworks/native/sensor/include/sensor_agent_proxy.h @@ -48,6 +48,9 @@ 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_; @@ -57,9 +60,6 @@ private: static int64_t g_reportInterval; static std::map g_subscribeMap; static std::map g_unsubscribeMap; - static std::mutex sensorInfoMutex_; - int32_t ConvertSensorInfos() const; - void FreeMemory(void *pointer) const; }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index c8998a75..38c29496 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -42,7 +42,7 @@ public: int32_t TransferDataChannel(sptr sensorDataChannel); int32_t DestroyDataChannel(); void ProcessDeathObserver(const wptr &object); - bool IsValidSensorId(uint32_t sensorId); + bool IsValid(uint32_t sensorId); private: int32_t InitServiceClient(); diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 00c0f4d6..bb65c746 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -31,13 +31,14 @@ constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::SensorsLogDomain::SENSORS_IMPLEME constexpr uint32_t MAX_SENSOR_LIST_SIZE = 0Xffff; } // 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 SensorAgentProxy::sensorInfoMutex_; +std::mutex sensorInfoMutex_; SensorInfo *sensorInfos_ = nullptr; int32_t sensorInfoCount_ = 0; std::map SensorAgentProxy::g_subscribeMap; @@ -50,7 +51,7 @@ SensorAgentProxy::SensorAgentProxy() SensorAgentProxy::~SensorAgentProxy() { CALL_LOG_ENTER; - FreeMemory(sensorInfos_); + ClearSensorInfos(); } const SensorAgentProxy *SensorAgentProxy::GetSensorsObj() @@ -96,8 +97,7 @@ int32_t SensorAgentProxy::CreateSensorDataChannel() const SEN_HILOGE("create data channel failed, ret: %{public}d", ret); return ret; } - auto &client = SensorServiceClient::GetInstance(); - ret = client.TransferDataChannel(dataChannel_); + ret = SenClient.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,8 +121,7 @@ int32_t SensorAgentProxy::DestroySensorDataChannel() const SEN_HILOGE("destory data channel failed, ret : %{public}d", ret); return ret; } - SensorServiceClient &client = SensorServiceClient::GetInstance(); - ret = client.DestroyDataChannel(); + ret = SenClient.DestroyDataChannel(); if (ret != ERR_OK) { SEN_HILOGE("destory service data channel fail, ret : %{public}d", ret); return ret; @@ -140,8 +139,7 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use SEN_HILOGE("samplingPeroid or g_reportInterval is invalid"); return ERROR; } - SensorServiceClient &client = SensorServiceClient::GetInstance(); - if (!client.IsValidSensorId(sensorId)) { + if (!SenClient.IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return ERROR; } @@ -149,7 +147,7 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use SEN_HILOGE("subscribe sensorId first"); return ERROR; } - int32_t ret = client.EnableSensor(sensorId, g_samplingInterval, g_reportInterval); + int32_t ret = SenClient.EnableSensor(sensorId, g_samplingInterval, g_reportInterval); g_samplingInterval = -1; g_reportInterval = -1; if (ret != 0) { @@ -165,8 +163,7 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u { CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - SensorServiceClient &client = SensorServiceClient::GetInstance(); - if (!client.IsValidSensorId(sensorId)) { + if (!SenClient.IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return ERROR; } @@ -177,7 +174,7 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u } g_subscribeMap.erase(sensorId); g_unsubscribeMap[sensorId] = user; - int32_t ret = client.DisableSensor(sensorId); + int32_t ret = SenClient.DisableSensor(sensorId); if (ret != 0) { SEN_HILOGE("disable sensor failed, ret: %{public}d", ret); return OHOS::Sensors::ERROR; @@ -189,8 +186,7 @@ int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int int64_t reportInterval) const { CHKPR(user, OHOS::Sensors::ERROR); - SensorServiceClient &client = SensorServiceClient::GetInstance(); - if (!client.IsValidSensorId(sensorId)) { + if (!SenClient.IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return ERROR; } @@ -213,8 +209,7 @@ 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); - SensorServiceClient &client = SensorServiceClient::GetInstance(); - if (!client.IsValidSensorId(sensorId)) { + if (!SenClient.IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return ERROR; } @@ -233,8 +228,7 @@ 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); - SensorServiceClient &client = SensorServiceClient::GetInstance(); - if (!client.IsValidSensorId(sensorId)) { + if (!SenClient.IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return ERROR; } @@ -258,8 +252,7 @@ int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int3 { CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - SensorServiceClient &client = SensorServiceClient::GetInstance(); - if (!client.IsValidSensorId(sensorId)) { + if (!SenClient.IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return ERROR; } @@ -275,8 +268,7 @@ int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, in { CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - SensorServiceClient &client = SensorServiceClient::GetInstance(); - if (!client.IsValidSensorId(sensorId)) { + if (!SenClient.IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return ERROR; } @@ -288,30 +280,29 @@ int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, in return OHOS::Sensors::SUCCESS; } -void SensorAgentProxy::FreeMemory(void *pointer) const +void SensorAgentProxy::ClearSensorInfos() const { - CHKPV(pointer); - free(pointer); - pointer = nullptr; + CHKPV(sensorInfos_); + free(sensorInfos_); + sensorInfos_ = nullptr; } int32_t SensorAgentProxy::ConvertSensorInfos() const { CALL_LOG_ENTER; - SensorServiceClient &client = SensorServiceClient::GetInstance(); - std::vector sensorList = client.GetSensorList(); + std::vector sensorList = SenClient.GetSensorList(); if (sensorList.empty()) { SEN_HILOGE("get sensor lists failed"); return ERROR; } - uint32_t count = sensorList.size(); + 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 (uint32_t i = 0; i < count; ++i) { + 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); @@ -344,7 +335,7 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) int32_t ret = ConvertSensorInfos(); if (ret != SUCCESS) { SEN_HILOGE("convert sensor lists failed"); - FreeMemory(sensorInfos_); + ClearSensorInfos(); return ERROR; } } diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index d491a5c4..84f2b2a5 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -72,7 +72,7 @@ int32_t SensorServiceClient::InitServiceClient() return SENSOR_NATIVE_GET_SERVICE_ERR; } -bool SensorServiceClient::IsValidSensorId(uint32_t sensorId) +bool SensorServiceClient::IsValid(uint32_t sensorId) { int32_t ret = InitServiceClient(); if (ret != ERR_OK) { @@ -94,7 +94,7 @@ bool SensorServiceClient::IsValidSensorId(uint32_t sensorId) int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay) { CALL_LOG_ENTER; - if (!IsValidSensorId(sensorId)) { + if (!IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid"); return SENSOR_NATIVE_SAM_ERR; } @@ -113,7 +113,7 @@ int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPer int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) { CALL_LOG_ENTER; - if (!IsValidSensorId(sensorId)) { + if (!IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid"); return SENSOR_NATIVE_SAM_ERR; } @@ -132,7 +132,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 (!IsValidSensorId(sensorId)) { + if (!IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid"); return SENSOR_NATIVE_SAM_ERR; } -- Gitee