From 24e501f6a0c3a2b30d9d0e134541857a7dd9ca16 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 27 Dec 2022 16:22:03 +0800 Subject: [PATCH 1/8] =?UTF-8?q?sensor=E4=BD=8E=E5=8A=9F=E8=80=97=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I52e9d37b8446a6e70e6a3bb876eab7f46382bd70 --- frameworks/native/sensor/BUILD.gn | 1 + .../native/sensor/include/i_sensor_service.h | 7 + .../sensor/include/sensor_agent_proxy.h | 4 + .../sensor/include/sensor_service_client.h | 4 + .../sensor/include/sensor_service_proxy.h | 3 + .../native/sensor/src/sensor_agent_proxy.cpp | 58 ++++++ .../sensor/src/sensor_service_client.cpp | 48 +++++ .../sensor/src/sensor_service_proxy.cpp | 87 +++++++++ interfaces/native/BUILD.gn | 1 + interfaces/native/include/sensor_agent.h | 6 + interfaces/native/include/sensor_agent_type.h | 8 + interfaces/native/src/sensor_agent.cpp | 47 ++++- .../test/unittest/sensor_agent_test.cpp | 33 ++++ interfaces/plugin/BUILD.gn | 1 + services/sensor/BUILD.gn | 1 + services/sensor/include/client_info.h | 2 + services/sensor/include/sensor_service.h | 3 + services/sensor/include/sensor_service_stub.h | 3 + .../sensor/include/sensor_suspend_policy.h | 53 ++++++ services/sensor/src/client_info.cpp | 22 +++ services/sensor/src/sensor_service.cpp | 38 +++- services/sensor/src/sensor_service_stub.cpp | 59 +++++- services/sensor/src/sensor_suspend_policy.cpp | 173 +++++++++--------- utils/BUILD.gn | 1 + utils/include/app_sensor.h | 45 +++++ utils/src/app_sensor.cpp | 78 ++++++++ 26 files changed, 697 insertions(+), 89 deletions(-) create mode 100755 services/sensor/include/sensor_suspend_policy.h create mode 100755 utils/include/app_sensor.h create mode 100755 utils/src/app_sensor.cpp diff --git a/frameworks/native/sensor/BUILD.gn b/frameworks/native/sensor/BUILD.gn index 9f9c6c3f..3f52ca56 100755 --- a/frameworks/native/sensor/BUILD.gn +++ b/frameworks/native/sensor/BUILD.gn @@ -44,6 +44,7 @@ ohos_shared_library("libsensor_native") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "samgr:samgr_proxy", + "access_token:libaccesstoken_sdk", ] part_name = "sensor" diff --git a/frameworks/native/sensor/include/i_sensor_service.h b/frameworks/native/sensor/include/i_sensor_service.h index e4400ba5..cee03021 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.h @@ -24,6 +24,7 @@ #include "i_sensor_client.h" #include "sensor_basic_data_channel.h" #include "sensor.h" +#include "app_sensor.h" namespace OHOS { namespace Sensors { @@ -39,6 +40,9 @@ public: virtual ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) = 0; virtual ErrCode DestroySensorChannel(sptr sensorClient) = 0; + virtual ErrCode SuspendSensors(int32_t pid) = 0; + virtual ErrCode ResumeSensors(int32_t pid) = 0; + virtual std::vector GetAppSensorList() = 0; enum { ENABLE_SENSOR = 0, DISABLE_SENSOR, @@ -47,6 +51,9 @@ public: GET_SENSOR_LIST, TRANSFER_DATA_CHANNEL, DESTROY_SENSOR_CHANNEL, + SUSPEND_SENSORS, + RESUME_SENSORS, + GET_APP_SENSOR_LIST, }; }; } // namespace Sensors diff --git a/frameworks/native/sensor/include/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index a14ea60d..9cb91e44 100644 --- a/frameworks/native/sensor/include/sensor_agent_proxy.h +++ b/frameworks/native/sensor/include/sensor_agent_proxy.h @@ -18,6 +18,7 @@ #include #include +#include #include "refbase.h" @@ -43,6 +44,9 @@ public: int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) const; int32_t SetOption(int32_t sensorId, const SensorUser *user, int32_t option) const; int32_t GetAllSensors(SensorInfo **sensorInfo, int32_t *count) const; + int32_t SuspendSensors(int32_t pid) const; + int32_t ResumeSensors(int32_t pid) const; + int32_t GetAppSensors(AppSensorInfo **appSensorInfos, int32_t *count) const; private: int32_t CreateSensorDataChannel() const; diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index 40b4d78d..b5a29931 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -28,6 +28,7 @@ #include "sensor_client_stub.h" #include "sensor_data_channel.h" #include "sensor.h" +#include "app_sensor.h" #include "sensor_service_proxy.h" @@ -42,6 +43,9 @@ public: int32_t DestroyDataChannel(); void ProcessDeathObserver(const wptr &object); bool IsValid(uint32_t sensorId); + int32_t SuspendSensors(int32_t pid); + int32_t ResumeSensors(int32_t pid); + std::vector GetAppSensorList(); private: int32_t InitServiceClient(); diff --git a/frameworks/native/sensor/include/sensor_service_proxy.h b/frameworks/native/sensor/include/sensor_service_proxy.h index 70a43eca..128200ad 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.h @@ -35,6 +35,9 @@ public: ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) override; ErrCode DestroySensorChannel(sptr sensorClient) override; + ErrCode SuspendSensors(int32_t pid) override; + ErrCode ResumeSensors(int32_t pid) override; + std::vector GetAppSensorList() override; private: DISALLOW_COPY_AND_MOVE(SensorServiceProxy); diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 3791a1b9..282b3c31 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -331,5 +331,63 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) *count = sensorInfoCount_; return SUCCESS; } + +int32_t SensorAgentProxy::SuspendSensors(int32_t pid) const +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("pid is invalid, %{public}d", pid); + return PARAMETER_ERROR; + } + int32_t ret = SenClient.SuspendSensors(pid); + if (ret != 0) { + SEN_HILOGE("Suspend pid sensors failed, ret:%{public}d", ret); + } + return ret; +} + +int32_t SensorAgentProxy::ResumeSensors(int32_t pid) const +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("pid is invalid, %{public}d", pid); + return PARAMETER_ERROR; + } + int32_t ret = SenClient.ResumeSensors(pid); + if (ret != 0) { + SEN_HILOGE("Resume pid sensors failed, ret:%{public}d", ret); + } + return ret; +} + +int32_t SensorAgentProxy::GetAppSensors(AppSensorInfo **appSensorInfos, int32_t *count) const +{ + CALL_LOG_ENTER; + CHKPR(appSensorInfos, OHOS::Sensors::ERROR); + CHKPR(count, OHOS::Sensors::ERROR); + std::vector appSensorList = SenClient.GetAppSensorList(); + if (appSensorList.empty()) { + SEN_HILOGE("get app sensor lists failed"); + return ERROR; + } + size_t appSensorInfoCount = appSensorList.size(); + if (appSensorInfoCount > MAX_SENSOR_LIST_SIZE) { + SEN_HILOGE("The number of app sensors exceeds the maximum value"); + return ERROR; + } + AppSensorInfo *appSensor = (AppSensorInfo *)malloc(sizeof(AppSensorInfo) * appSensorInfoCount); + CHKPR(appSensor, ERROR); + for (size_t i = 0; i < appSensorInfoCount; ++i) { + AppSensorInfo *curAppSensor = appSensor + i; + curAppSensor->appThreadInfo = AppThreadInfo(appSensorList[i].pid, appSensorList[i].uid, + static_cast(appSensorList[i].tokenId)); + curAppSensor->sensorId = appSensorList[i].sensorId; + curAppSensor->samplingPeriodNs = appSensorList[i].samplingPeriodNs; + curAppSensor->maxReportDelayNs = appSensorList[i].maxReportDelayNs; + } + *appSensorInfos = appSensor; + *count = static_cast(appSensorInfoCount); + return 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 8effd56e..064db041 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -224,5 +224,53 @@ void SensorServiceClient::DeleteSensorInfoItem(uint32_t sensorId) } return; } + +int32_t SensorServiceClient::SuspendSensors(int32_t pid) +{ + CALL_LOG_ENTER; + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); + return ret; + } + CHKPR(sensorServer_, ERROR); + StartTrace(HITRACE_TAG_SENSORS, "SuspendSensors"); + ret = sensorServer_->SuspendSensors(pid); + FinishTrace(HITRACE_TAG_SENSORS); + return ret; +} + +int32_t SensorServiceClient::ResumeSensors(int32_t pid) +{ + CALL_LOG_ENTER; + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); + return ret; + } + CHKPR(sensorServer_, ERROR); + StartTrace(HITRACE_TAG_SENSORS, "ResumeSensors"); + ret = sensorServer_->ResumeSensors(pid); + FinishTrace(HITRACE_TAG_SENSORS); + return ret; +} + +std::vector SensorServiceClient::GetAppSensorList() +{ + CALL_LOG_ENTER; + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); + return {}; + } + if (sensorServer_ == nullptr) { + SEN_HILOGE("sensorServer_ is nullptr"); + return {}; + } + StartTrace(HITRACE_TAG_SENSORS, "GetAppSensorList"); + std::vector appSensorList = sensorServer_->GetAppSensorList(); + FinishTrace(HITRACE_TAG_SENSORS); + return appSensorList; +} } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index 3a6cb1ea..0b4aaad6 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -190,5 +190,92 @@ ErrCode SensorServiceProxy::DestroySensorChannel(sptr sensorClien } return static_cast(ret); } + +ErrCode SensorServiceProxy::SuspendSensors(int32_t pid) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { + SEN_HILOGE("write descriptor failed"); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(pid)) { + SEN_HILOGE("write pid failed"); + return WRITE_MSG_ERR; + } + sptr remote = Remote(); + CHKPR(remote, ERROR); + int32_t ret = remote->SendRequest(ISensorService::SUSPEND_SENSORS, data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "SuspendSensors", "ERROR_CODE", ret); + SEN_HILOGE("failed, ret:%{public}d", ret); + } + return static_cast(ret); +} + +ErrCode SensorServiceProxy::ResumeSensors(int32_t pid) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { + SEN_HILOGE("write descriptor failed"); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(pid)) { + SEN_HILOGE("write pid failed"); + return WRITE_MSG_ERR; + } + sptr remote = Remote(); + CHKPR(remote, ERROR); + int32_t ret = remote->SendRequest(ISensorService::RESUME_SENSORS, data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "ResumeSensors", "ERROR_CODE", ret); + SEN_HILOGE("failed, ret:%{public}d", ret); + } + return static_cast(ret); +} + +std::vector SensorServiceProxy::GetAppSensorList() +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + std::vector appSensors; + if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { + SEN_HILOGE("write descriptor failed"); + return appSensors; + } + sptr remote = Remote(); + if (remote == nullptr) { + SEN_HILOGE("remote is null"); + return appSensors; + } + int32_t ret = remote->SendRequest(ISensorService::GET_APP_SENSOR_LIST, data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "GetAppSensorList", "ERROR_CODE", ret); + SEN_HILOGE("failed, ret:%{public}d", ret); + return appSensors; + } + int32_t appSensorCount; + if (!reply.ReadInt32(appSensorCount)) { + SEN_HILOGE("Parcel read appSensorCount failed"); + return appSensors; + } + SEN_HILOGD("appSensorCount:%{public}d", appSensorCount); + AppSensor appSensor; + for (int32_t i = 0; i < appSensorCount; i++) { + auto tmpAppSensor = appSensor.Unmarshalling(reply); + if (tmpAppSensor == nullptr) { + continue; + } + appSensors.push_back(*tmpAppSensor); + } + return appSensors; +} } // namespace Sensors } // namespace OHOS diff --git a/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn index 1050549d..bb22a1dd 100644 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -57,6 +57,7 @@ ohos_shared_library("sensor_interface_native") { external_deps = [ "c_utils:utils", "hiviewdfx_hilog_native:libhilog", + "access_token:libaccesstoken_sdk", ] part_name = "sensor" subsystem_name = "sensors" diff --git a/interfaces/native/include/sensor_agent.h b/interfaces/native/include/sensor_agent.h index ead362c9..39d2fd78 100755 --- a/interfaces/native/include/sensor_agent.h +++ b/interfaces/native/include/sensor_agent.h @@ -128,6 +128,12 @@ int32_t DeactivateSensor(int32_t sensorTypeId, const SensorUser *user); */ int32_t SetMode(int32_t sensorTypeId, const SensorUser *user, int32_t mode); +int32_t SuspendSensors(int32_t pid); + +int32_t ResumeSensors(int32_t pid); + +int32_t GetAppSensors(AppSensorInfo **appSensorInfos, int32_t *count); + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index f780e3e5..b04a5afe 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -38,6 +38,7 @@ #define SENSOR_AGENT_TYPE_H #include +#include "app_thread_info.h" #ifdef __cplusplus #if __cplusplus @@ -395,6 +396,13 @@ typedef struct WearDetectionData { float value; } WearDetectionData; +typedef struct AppSensorInfo { + OHOS::Sensors::AppThreadInfo appThreadInfo; /**< PID, UID, TokenID */ + uint32_t sensorId; /**< Sensor ID */ + int64_t samplingPeriodNs; /**< Sample period, in ns */ + int64_t maxReportDelayNs; /**< Maximum Report Delay, in ns */ +} AppSensorInfo; + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/native/src/sensor_agent.cpp b/interfaces/native/src/sensor_agent.cpp index ccae1ff7..ed1bbd7d 100755 --- a/interfaces/native/src/sensor_agent.cpp +++ b/interfaces/native/src/sensor_agent.cpp @@ -146,4 +146,49 @@ int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) return SERVICE_EXCEPTION; } return proxy->SetMode(sensorId, user, mode); -} \ No newline at end of file +} + +int32_t SuspendSensors(int32_t pid) +{ + const SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + SEN_HILOGE("proxy is nullptr"); + return SERVICE_EXCEPTION; + } + int32_t ret = proxy->SuspendSensors(pid); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("Suspend pid sensors failed, ret:%{public}d", ret); + return NormalizeErrCode(ret); + } + return ret; +} + +int32_t ResumeSensors(int32_t pid) +{ + const SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + SEN_HILOGE("proxy is nullptr"); + return SERVICE_EXCEPTION; + } + int32_t ret = proxy->ResumeSensors(pid); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("Resume pid sensors failed, ret:%{public}d", ret); + return NormalizeErrCode(ret); + } + return ret; +} + +int32_t GetAppSensors(AppSensorInfo **appSensorInfos, int32_t *count) +{ + const SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + SEN_HILOGE("proxy is nullptr"); + return SERVICE_EXCEPTION; + } + int32_t ret = proxy->GetAppSensors(appSensorInfos, count); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("GetAppSensors failed, ret:%{public}d", ret); + return NormalizeErrCode(ret); + } + return ret; +} diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index 2f31742f..ff5b2310 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "accesstoken_kit.h" #include "nativetoken_kit.h" @@ -458,5 +459,37 @@ HWTEST_F(SensorAgentTest, SensorNativeApiTest_004, TestSize.Level1) int32_t ret = SetMode(sensorId, &user, SENSOR_DEFAULT_MODE); ASSERT_NE(ret, OHOS::Sensors::SUCCESS); } + +HWTEST_F(SensorAgentTest, SuspendSensorsTest_001, TestSize.Level1) +{ + SEN_HILOGI("SuspendSensorsTest_001 in"); + int32_t ret = SuspendSensors(1526); + sleep(10); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorAgentTest, ResumeSensorsTest_001, TestSize.Level1) +{ + SEN_HILOGI("ResumeSensorsTest_001 in"); + int32_t ret = ResumeSensors(1526); + sleep(5); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorAgentTest, GetAppSensorsTest_001, TestSize.Level1) +{ + SEN_HILOGI("GetAppSensorsTest_001 in"); + AppSensorInfo *appSensorInfos {nullptr}; + int32_t count { 0 }; + int32_t ret = GetAppSensors(&appSensorInfos, &count); + SEN_HILOGI("count:%{public}d", count); + if (appSensorInfos != nullptr) { + SEN_HILOGI("free appSensorInfos"); + free(appSensorInfos); + appSensorInfos = nullptr; + } + ASSERT_EQ(ret, 0); + ASSERT_NE(count, 0); +} } // namespace Sensors } // namespace OHOS diff --git a/interfaces/plugin/BUILD.gn b/interfaces/plugin/BUILD.gn index d8445259..5494e73c 100644 --- a/interfaces/plugin/BUILD.gn +++ b/interfaces/plugin/BUILD.gn @@ -39,6 +39,7 @@ ohos_shared_library("libsensor") { "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "napi:ace_napi", + "access_token:libaccesstoken_sdk", ] relative_install_dir = "module" part_name = "sensor" diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index 3f15192b..d2cf701b 100644 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -29,6 +29,7 @@ ohos_shared_library("libsensor_service") { "src/sensor_manager.cpp", "src/sensor_service.cpp", "src/sensor_service_stub.cpp", + "src/sensor_suspend_policy.cpp", ] include_dirs = [ diff --git a/services/sensor/include/client_info.h b/services/sensor/include/client_info.h index e4797138..e70ee18d 100644 --- a/services/sensor/include/client_info.h +++ b/services/sensor/include/client_info.h @@ -33,6 +33,7 @@ #include "sensor_basic_info.h" #include "sensor_channel_info.h" #include "sensor_data_event.h" +#include "app_sensor.h" namespace OHOS { namespace Sensors { @@ -74,6 +75,7 @@ public: void ClearDataQueue(int32_t sensorId); int32_t GetUidByPid(int32_t pid); AccessTokenID GetTokenIdByPid(int32_t pid); + std::vector GetAppSensorList(); private: DISALLOW_COPY_AND_MOVE(ClientInfo); diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index b5de13a8..ecc692fd 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -54,6 +54,9 @@ public: const sptr &sensorClient) override; ErrCode DestroySensorChannel(sptr sensorClient) override; void ProcessDeathObserver(const wptr &object); + ErrCode SuspendSensors(int32_t pid) override; + ErrCode ResumeSensors(int32_t pid) override; + std::vector GetAppSensorList() override; private: DISALLOW_COPY_AND_MOVE(SensorService); diff --git a/services/sensor/include/sensor_service_stub.h b/services/sensor/include/sensor_service_stub.h index 9259e793..0af17c3a 100644 --- a/services/sensor/include/sensor_service_stub.h +++ b/services/sensor/include/sensor_service_stub.h @@ -39,6 +39,9 @@ private: ErrCode GetAllSensorsInner(MessageParcel &data, MessageParcel &reply); ErrCode CreateDataChannelInner(MessageParcel &data, MessageParcel &reply); ErrCode DestroyDataChannelInner(MessageParcel &data, MessageParcel &reply); + ErrCode SuspendSensorsInner(MessageParcel &data, MessageParcel &reply); + ErrCode ResumeSensorsInner(MessageParcel &data, MessageParcel &reply); + ErrCode GetAppSensorListInner(MessageParcel &data, MessageParcel &reply); std::unordered_map baseFuncs_; }; } // namespace Sensors diff --git a/services/sensor/include/sensor_suspend_policy.h b/services/sensor/include/sensor_suspend_policy.h new file mode 100755 index 00000000..bd825601 --- /dev/null +++ b/services/sensor/include/sensor_suspend_policy.h @@ -0,0 +1,53 @@ +/* + * 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. + */ + +#ifndef SENSOR_SUSPEND_POLICY_H +#define SENSOR_SUSPEND_POLICY_H + +#include +#include + +#include "singleton.h" +#include "client_info.h" +#include "sensor_manager.h" +#include "flush_info_record.h" +#include "sensor_hdi_connection.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +class SensorSuspendPolicy { + DECLARE_DELAYED_SINGLETON(SensorSuspendPolicy); +public: + DISALLOW_COPY_AND_MOVE(SensorSuspendPolicy); + ErrCode DoSuspend(int32_t pid); + ErrCode DoResume(int32_t pid); + +private: + bool CheckFreezingSensor(uint32_t sensorId); + ErrCode DisableSensor(uint32_t sensorId, int32_t pid); + ErrCode EnableSensor(uint32_t sensorId, int32_t pid, int64_t samplingPeriodNs, int64_t maxReportDelayNs); + ErrCode RestoreSensorInfo(uint32_t sensorId, int32_t pid, int64_t samplingPeriodNs, int64_t maxReportDelayNs); + std::mutex suspendMutex_; + SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); + ClientInfo &clientInfo_ = ClientInfo::GetInstance(); + SensorManager &sensorManager_ = SensorManager::GetInstance(); + FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); + std::unordered_map> pidSensorInfoMap_; +}; +#define SuspendPolicy DelayedSingleton::GetInstance() +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_SUSPEND_POLICY_H diff --git a/services/sensor/src/client_info.cpp b/services/sensor/src/client_info.cpp index f0374d7d..ada3e38b 100644 --- a/services/sensor/src/client_info.cpp +++ b/services/sensor/src/client_info.cpp @@ -685,5 +685,27 @@ void ClientInfo::ClearDataQueue(int32_t sensorId) dumpQueue_.erase(it); } } + +std::vector ClientInfo::GetAppSensorList() +{ + std::vector appSensors; + std::lock_guard clientLock(clientMutex_); + for (const auto &clientMapIt : clientMap_) { + for (const auto &pidSensorIt : clientMapIt.second) { + if (pidSensorIt.second.GetSensorState()) { + int32_t pid = pidSensorIt.first; + int32_t uid = GetUidByPid(pid); + AccessTokenID tokenId = GetTokenIdByPid(pid); + uint32_t sensorId = clientMapIt.first; + int64_t samplingPeriodNs = pidSensorIt.second.GetSamplingPeriodNs(); + int64_t maxReportDelayNs = pidSensorIt.second.GetMaxReportDelayNs(); + AppSensor appSensor(pid, uid, static_cast(tokenId), sensorId, + samplingPeriodNs, maxReportDelayNs); + appSensors.push_back(appSensor); + } + } + } + return appSensors; +} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index fd38c139..7f120bab 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -26,6 +26,7 @@ #include "securec.h" #include "sensor.h" #include "sensor_dump.h" +#include "sensor_suspend_policy.h" #include "sensors_errors.h" #include "system_ability_definition.h" @@ -325,7 +326,6 @@ ErrCode SensorService::DestroySensorChannel(sptr sensorClient) const int32_t clientPid = GetCallingPid(); if (clientPid < 0) { SEN_HILOGE("clientPid is invalid, clientPid:%{public}d", clientPid); - return CLIENT_PID_INVALID_ERR; } std::lock_guard serviceLock(serviceLock_); @@ -404,5 +404,41 @@ int32_t SensorService::Dump(int32_t fd, const std::vector &args) sensorDump.ParseCommand(fd, argList, sensors_, clientInfo_); return ERR_OK; } + +ErrCode SensorService::SuspendSensors(int32_t pid) +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("pid is invalid, pid:%{public}d", pid); + return CLIENT_PID_INVALID_ERR; + } + int32_t ret = SuspendPolicy->DoSuspend(pid); + if (ret != ERR_OK) { + SEN_HILOGE("Suspend pid sensors is failed, pid:%{public}d", pid); + return ERROR; + } + return ERR_OK; +} + +ErrCode SensorService::ResumeSensors(int32_t pid) +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("pid is invalid, pid:%{public}d", pid); + return CLIENT_PID_INVALID_ERR; + } + int32_t ret = SuspendPolicy->DoResume(pid); + if (ret != ERR_OK) { + SEN_HILOGE("Resume pid sensors is failed, pid:%{public}d", pid); + return ERROR; + } + return ERR_OK; +} + +std::vector SensorService::GetAppSensorList() +{ + CALL_LOG_ENTER; + return clientInfo_.GetAppSensorList(); +} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 4696378b..ab94de7e 100644 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -23,6 +23,7 @@ #include "hisysevent.h" #include "ipc_skeleton.h" #include "message_parcel.h" +#include "accesstoken_kit.h" #include "permission_util.h" #include "sensor_client_proxy.h" #include "sensors_errors.h" @@ -30,7 +31,7 @@ namespace OHOS { namespace Sensors { using namespace OHOS::HiviewDFX; - +using namespace Security::AccessToken; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorServiceStub" }; } // namespace @@ -43,6 +44,9 @@ SensorServiceStub::SensorServiceStub() baseFuncs_[GET_SENSOR_LIST] = &SensorServiceStub::GetAllSensorsInner; baseFuncs_[TRANSFER_DATA_CHANNEL] = &SensorServiceStub::CreateDataChannelInner; baseFuncs_[DESTROY_SENSOR_CHANNEL] = &SensorServiceStub::DestroyDataChannelInner; + baseFuncs_[SUSPEND_SENSORS] = &SensorServiceStub::SuspendSensorsInner; + baseFuncs_[RESUME_SENSORS] = &SensorServiceStub::ResumeSensorsInner; + baseFuncs_[GET_APP_SENSOR_LIST] = &SensorServiceStub::GetAppSensorListInner; } SensorServiceStub::~SensorServiceStub() @@ -153,5 +157,58 @@ ErrCode SensorServiceStub::DestroyDataChannelInner(MessageParcel &data, MessageP CHKPR(sensorClient, OBJECT_NULL); return DestroySensorChannel(sensorClient); } + +ErrCode SensorServiceStub::SuspendSensorsInner(MessageParcel &data, MessageParcel &reply) +{ + int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()); + if (tokenType != ATokenTypeEnum::TOKEN_NATIVE) { + SEN_HILOGE("tokenType is not TOKEN_NATIVE, tokenType:%{public}d", tokenType); + // return PERMISSION_DENIED; + } + (void)reply; + int32_t pid; + if (!data.ReadInt32(pid)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + return SuspendSensors(pid); +} + +ErrCode SensorServiceStub::ResumeSensorsInner(MessageParcel &data, MessageParcel &reply) +{ + int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()); + if (tokenType != ATokenTypeEnum::TOKEN_NATIVE) { + SEN_HILOGE("tokenType is not TOKEN_NATIVE, tokenType:%{public}d", tokenType); + // return PERMISSION_DENIED; + } + (void)reply; + int32_t pid; + if (!data.ReadInt32(pid)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + return ResumeSensors(pid); +} + +ErrCode SensorServiceStub::GetAppSensorListInner(MessageParcel &data, MessageParcel &reply) +{ + int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()); + if (tokenType != ATokenTypeEnum::TOKEN_NATIVE) { + SEN_HILOGE("tokenType is not TOKEN_NATIVE, tokenType:%{public}d", tokenType); + // return PERMISSION_DENIED; + } + (void)data; + std::vector appSensors = GetAppSensorList(); + int32_t appSensorCount = int32_t { appSensors.size() }; + reply.WriteInt32(appSensorCount); + for (int32_t i = 0; i < appSensorCount; i++) { + bool flag = appSensors[i].Marshalling(reply); + if (!flag) { + SEN_HILOGE("appSensor %{public}d failed", i); + return ERROR; + } + } + return NO_ERROR; +} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_suspend_policy.cpp b/services/sensor/src/sensor_suspend_policy.cpp index 1e9f2444..69c63558 100644 --- a/services/sensor/src/sensor_suspend_policy.cpp +++ b/services/sensor/src/sensor_suspend_policy.cpp @@ -13,11 +13,9 @@ * limitations under the License. */ -#include "sensor_suspend_policy.h" - #include "sensor.h" -#include "sensor_service.h" -#include "system_ability_definition.h" +#include "sensors_errors.h" +#include "sensor_suspend_policy.h" namespace OHOS { namespace Sensors { @@ -27,20 +25,56 @@ namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorSuspendPolicy" }; constexpr uint32_t INVALID_SENSOR_ID = -1; constexpr int64_t MAX_EVENT_COUNT = 1000; -constexpr int64_t DEFAULT_SAMPLING_RATE = 200000000; -constexpr int64_t DEFAULT_REPORT_DELAY = 0; constexpr uint32_t STEP_COUNTER_ID = 524544; constexpr uint32_t STEP_DETECTOR_ID = 590080; } // namespace -SensorSuspendPolicy::~SensorSuspendPolicy() -{} +SensorSuspendPolicy::SensorSuspendPolicy() {} + +SensorSuspendPolicy::~SensorSuspendPolicy() {} bool SensorSuspendPolicy::CheckFreezingSensor(uint32_t sensorId) { return ((sensorId == STEP_COUNTER_ID) || (sensorId == STEP_DETECTOR_ID)); } +ErrCode SensorSuspendPolicy::DoSuspend(int32_t pid) +{ + CALL_LOG_ENTER; + std::lock_guard suspendLock(suspendMutex_); + auto pidSensorInfoIt = pidSensorInfoMap_.find(pid); + if (pidSensorInfoIt != pidSensorInfoMap_.end()) { + SEN_HILOGE("pid sensors already suspend, not need suspend again, pid: %{public}d", pid); + return ERROR; + } + std::vector sensorIdList = clientInfo_.GetSensorIdByPid(pid); + if (sensorIdList.empty()) { + SEN_HILOGE("pid sensorId list is empty, pid: %{public}d", pid); + return ERROR; + } + bool suspendAllSensors = true; + std::unordered_map SensorInfoMap; + for (auto &sensorId : sensorIdList) { + if (CheckFreezingSensor(sensorId)) { + continue; + } + auto sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); + SensorInfoMap.emplace(std::make_pair(sensorId, sensorInfo)); + auto ret = DisableSensor(sensorId, pid); + if (ret != ERR_OK) { + SEN_HILOGE("Disable Sensor is failed. sensorId: %{public}u, pid: %{public}d, ret: %{public}d", + sensorId, pid, ret); + suspendAllSensors = false; + } + } + pidSensorInfoMap_.emplace(std::make_pair(pid, SensorInfoMap)); + if (!suspendAllSensors) { + SEN_HILOGE("some sensor disable failed"); + return DISABLE_SENSOR_ERR; + } + return ERR_OK; +} + ErrCode SensorSuspendPolicy::DisableSensor(uint32_t sensorId, int32_t pid) { CALL_LOG_ENTER; @@ -52,50 +86,41 @@ ErrCode SensorSuspendPolicy::DisableSensor(uint32_t sensorId, int32_t pid) SEN_HILOGW("other client is using this sensor now, cannot disable"); return ERR_OK; } - if (interface_.DisableSensor(sensorId) != ERR_OK) { + if (sensorHdiConnection_.DisableSensor(sensorId) != ERR_OK) { SEN_HILOGE("DisableSensor is failed"); return DISABLE_SENSOR_ERR; } return sensorManager_.AfterDisableSensor(sensorId); } -void SensorSuspendPolicy::DoSuspend(const std::shared_ptr &info) +ErrCode SensorSuspendPolicy::DoResume(int32_t pid) { CALL_LOG_ENTER; std::lock_guard suspendLock(suspendMutex_); - auto &list = info->GetAppIPCInfoList(); - for (const auto &appInfo : list) { - std::vector sensorIdList = clientInfo_.GetSensorIdByPid(appInfo.pid); - if (sensorIdList.empty()) { - continue; - } - pidSensorIdMap_.emplace(std::make_pair(appInfo.pid, sensorIdList)); - for (auto &sensorId : sensorIdList) { - if (CheckFreezingSensor(sensorId)) { - continue; - } - auto sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, appInfo.pid); - sensorIdInfoMap_.emplace(std::make_pair(sensorId, sensorInfo)); - DisableSensor(sensorId, appInfo.pid); - } + auto pidSensorInfoIt = pidSensorInfoMap_.find(pid); + if (pidSensorInfoIt == pidSensorInfoMap_.end()) { + SEN_HILOGE("pid not have suspend sensors, pid: %{public}d", pid); + return ERROR; } -} - -ErrCode SensorSuspendPolicy::SaveSubscriber(uint32_t sensorId, int64_t samplingPeriodNs, - int64_t maxReportDelayNs, int32_t pid) -{ - auto ret = sensorManager_.SaveSubscriber(sensorId, pid, samplingPeriodNs, maxReportDelayNs); - if (ret != ERR_OK) { - SEN_HILOGE("SaveSubscriber is failed"); - return ret; + bool resumeAllSensors = true; + std::unordered_map SensorInfoMap = pidSensorInfoIt->second; + for (auto &it : SensorInfoMap) { + uint32_t sensorId = it.first; + int64_t samplePeriod = it.second.GetSamplingPeriodNs(); + int64_t maxReportDelay = it.second.GetMaxReportDelayNs(); + auto ret = EnableSensor(sensorId, pid, samplePeriod, maxReportDelay); + if (ret != ERR_OK) { + SEN_HILOGE("Enable Sensor is failed. sensorId: %{public}u, pid: %{public}d, ret: %{public}d", + sensorId, pid, ret); + resumeAllSensors = false; + } } - sensorManager_.StartDataReportThread(); - if (!sensorManager_.SetBestSensorParams(sensorId, samplingPeriodNs, maxReportDelayNs)) { - SEN_HILOGE("SetBestSensorParams is failed"); - clientInfo_.RemoveSubscriber(sensorId, pid); + if (!resumeAllSensors) { + SEN_HILOGE("some sensor enable failed"); return ENABLE_SENSOR_ERR; } - return ret; + pidSensorInfoMap_.erase(pidSensorInfoIt); + return ERR_OK; } ErrCode SensorSuspendPolicy::EnableSensor(uint32_t sensorId, int32_t pid, int64_t samplingPeriodNs, @@ -107,73 +132,49 @@ ErrCode SensorSuspendPolicy::EnableSensor(uint32_t sensorId, int32_t pid, int64_ SEN_HILOGE("sensorId is 0 or maxReportDelayNs exceed the maximum value"); return ERR_NO_INIT; } - if (clientInfo_.GetSensorState(sensorId) == SENSOR_ENABLED) { - SEN_HILOGW("sensor has been enabled already"); - auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs, pid); + if (clientInfo_.GetSensorState(sensorId)) { + SEN_HILOGW("sensor has been enabled already, sensorId: %{public}d", sensorId); + auto ret = RestoreSensorInfo(sensorId, pid, samplingPeriodNs, maxReportDelayNs); if (ret != ERR_OK) { - SEN_HILOGE("SaveSubscriber is failed"); + SEN_HILOGE("RestoreSensorInfo is failed, ret: %{public}d", ret); return ret; } uint32_t flag = sensorManager_.GetSensorFlag(sensorId); ret = flushInfo_.FlushProcess(sensorId, flag, pid, true); if (ret != ERR_OK) { - SEN_HILOGW("ret:%{public}d", ret); + SEN_HILOGE("FlushProcess is failed, ret: %{public}d", ret); } return ERR_OK; } - auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs, pid); + auto ret = RestoreSensorInfo(sensorId, pid, samplingPeriodNs, maxReportDelayNs); if (ret != ERR_OK) { - SEN_HILOGE("SaveSubscriber is failed"); + SEN_HILOGE("RestoreSensorInfo is failed, ret: %{public}d", ret); return ret; } - ret = interface_.EnableSensor(sensorId); + ret = sensorHdiConnection_.EnableSensor(sensorId); if (ret != ERR_OK) { - SEN_HILOGE("EnableSensor is failed"); + SEN_HILOGE("EnableSensor is failed, ret: %{public}d", ret); clientInfo_.RemoveSubscriber(sensorId, pid); return ENABLE_SENSOR_ERR; } - return ret; -} - -std::vector SensorSuspendPolicy::GetSensorIdByPid(int32_t pid) -{ - SEN_HILOGD("pid:%{public}d", pid); - auto it = pidSensorIdMap_.find(pid); - if (it != pidSensorIdMap_.end()) { - SEN_HILOGD("pid:%{public}d found", pid); - return it->second; - } - SEN_HILOGD("pid:%{public}d not found", pid); - return {}; + return ERR_OK; } -void SensorSuspendPolicy::DoActive(const std::shared_ptr &info) +ErrCode SensorSuspendPolicy::RestoreSensorInfo(uint32_t sensorId, int32_t pid, int64_t samplingPeriodNs, + int64_t maxReportDelayNs) { - CALL_LOG_ENTER; - int64_t samplePeriod = DEFAULT_SAMPLING_RATE; - int64_t maxReportDelay = DEFAULT_REPORT_DELAY; - std::vector sensorIdList; - std::lock_guard suspendLock(suspendMutex_); - auto &list = info->GetAppIPCInfoList(); - for (const auto &appInfo : list) { - sensorIdList = GetSensorIdByPid(appInfo.pid); - for (auto &sensorId : sensorIdList) { - if (CheckFreezingSensor(sensorId)) { - continue; - } - auto it = sensorIdInfoMap_.find(sensorId); - if (it != sensorIdInfoMap_.end()) { - samplePeriod = it->second.GetSamplingPeriodNs(); - maxReportDelay = it->second.GetMaxReportDelayNs(); - } - auto ret = EnableSensor(sensorId, appInfo.pid, samplePeriod, maxReportDelay); - if (ret != ERR_OK) { - SEN_HILOGE("sensorId:%{public}u,pid:%{public}d,ret:%{public}d", sensorId, appInfo.pid, ret); - } - } + auto ret = sensorManager_.SaveSubscriber(sensorId, pid, samplingPeriodNs, maxReportDelayNs); + if (ret != ERR_OK) { + SEN_HILOGE("RestoreSensorInfo is failed, ret: %{public}d", ret); + return ret; + } + sensorManager_.StartDataReportThread(); + if (!sensorManager_.SetBestSensorParams(sensorId, samplingPeriodNs, maxReportDelayNs)) { + SEN_HILOGE("SetBestSensorParams is failed"); + clientInfo_.RemoveSubscriber(sensorId, pid); + return ENABLE_SENSOR_ERR; } - pidSensorIdMap_.clear(); - sensorIdInfoMap_.clear(); + return ret; } } // namespace Sensors } // namespace OHOS diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 7a971bb7..b976cc5b 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -21,6 +21,7 @@ ohos_shared_library("libsensor_utils") { "src/sensor_basic_data_channel.cpp", "src/sensor_basic_info.cpp", "src/sensor_channel_info.cpp", + "src/app_sensor.cpp", ] include_dirs = [ diff --git a/utils/include/app_sensor.h b/utils/include/app_sensor.h new file mode 100755 index 00000000..e4e3c008 --- /dev/null +++ b/utils/include/app_sensor.h @@ -0,0 +1,45 @@ +/* + * 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. + */ + +#ifndef APP_SENSOR_H +#define APP_SENSOR_H + +#include + +#include "parcel.h" + +namespace OHOS { +namespace Sensors { +struct AppSensor : public Parcelable { + int32_t pid { 0 }; + int32_t uid { 0 }; + uint32_t tokenId { 0 }; + uint32_t sensorId { 0 }; + int64_t samplingPeriodNs { 0 }; + int64_t maxReportDelayNs { 0 }; + + AppSensor() = default; + AppSensor(int32_t pid, int32_t uid, uint32_t tokenId, uint32_t sensorId, + int64_t samplingPeriodNs, int64_t maxReportDelayNs) + : pid(pid), uid(uid), tokenId(tokenId), sensorId(sensorId), + samplingPeriodNs(samplingPeriodNs), maxReportDelayNs(maxReportDelayNs) {} + + bool Marshalling(Parcel &parcel) const; + std::unique_ptr Unmarshalling(Parcel &parcel); + bool ReadFromParcel(Parcel &parcel); +}; +} // namespace Sensors +} // namespace OHOS +#endif // APP_SENSOR_H diff --git a/utils/src/app_sensor.cpp b/utils/src/app_sensor.cpp new file mode 100755 index 00000000..3c31f77d --- /dev/null +++ b/utils/src/app_sensor.cpp @@ -0,0 +1,78 @@ +/* + * 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 "app_sensor.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "AppSensor" }; +} + +bool AppSensor::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteInt32(pid)) { + SEN_HILOGE("failed, write pid failed"); + return false; + } + if (!parcel.WriteInt32(uid)) { + SEN_HILOGE("failed, write uid failed"); + return false; + } + if (!parcel.WriteUint32(tokenId)) { + SEN_HILOGE("failed, write tokenId failed"); + return false; + } + if (!parcel.WriteUint32(sensorId)) { + SEN_HILOGE("failed, write sensorId failed"); + return false; + } + if (!parcel.WriteInt64(samplingPeriodNs)) { + SEN_HILOGE("failed, write samplingPeriodNs failed"); + return false; + } + if (!parcel.WriteInt64(maxReportDelayNs)) { + SEN_HILOGE("failed, write maxReportDelayNs failed"); + return false; + } + return true; +} + +std::unique_ptr AppSensor::Unmarshalling(Parcel &parcel) +{ + auto appSensor = std::make_unique(); + if (!appSensor->ReadFromParcel(parcel)) { + SEN_HILOGE("ReadFromParcel is failed"); + return nullptr; + } + return appSensor; +} + +bool AppSensor::ReadFromParcel(Parcel &parcel) +{ + if ((!parcel.ReadInt32(pid)) || + (!parcel.ReadInt32(uid)) || + (!parcel.ReadUint32(tokenId)) || + (!parcel.ReadUint32(sensorId)) || + (!parcel.ReadInt64(samplingPeriodNs)) || + (!parcel.ReadInt64(maxReportDelayNs))) { + return false; + } + return true; +} +} // namespace Sensors +} // namespace OHOS -- Gitee From e3cfc9bf7975095a317b2a92f124a37dbc31ea23 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 29 Dec 2022 20:26:07 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I9c528827df3c8b26b22bd32e483985d485d3e55c --- .../test/unittest/sensor_agent_test.cpp | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index ff5b2310..bd31c6c2 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -463,28 +463,44 @@ HWTEST_F(SensorAgentTest, SensorNativeApiTest_004, TestSize.Level1) HWTEST_F(SensorAgentTest, SuspendSensorsTest_001, TestSize.Level1) { SEN_HILOGI("SuspendSensorsTest_001 in"); - int32_t ret = SuspendSensors(1526); + int32_t ret = SuspendSensors(1534); sleep(10); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); } +HWTEST_F(SensorAgentTest, GetAppSensorsTest_001, TestSize.Level1) +{ + SEN_HILOGI("GetAppSensorsTest_001 in"); + AppSensorInfo *appSensorInfos {nullptr}; + int32_t count { 0 }; + int32_t ret = GetAppSensors(&appSensorInfos, &count); + SEN_HILOGI("Test_001 count:%{public}d", count); + if (appSensorInfos != nullptr) { + SEN_HILOGI("Test_001 free memory"); + free(appSensorInfos); + appSensorInfos = nullptr; + } + ASSERT_NE(ret, 0); + ASSERT_EQ(count, 0); +} + HWTEST_F(SensorAgentTest, ResumeSensorsTest_001, TestSize.Level1) { SEN_HILOGI("ResumeSensorsTest_001 in"); - int32_t ret = ResumeSensors(1526); + int32_t ret = ResumeSensors(1534); sleep(5); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); } -HWTEST_F(SensorAgentTest, GetAppSensorsTest_001, TestSize.Level1) +HWTEST_F(SensorAgentTest, GetAppSensorsTest_002, TestSize.Level1) { - SEN_HILOGI("GetAppSensorsTest_001 in"); + SEN_HILOGI("GetAppSensorsTest_002 in"); AppSensorInfo *appSensorInfos {nullptr}; int32_t count { 0 }; int32_t ret = GetAppSensors(&appSensorInfos, &count); - SEN_HILOGI("count:%{public}d", count); + SEN_HILOGI("Test_002 count:%{public}d", count); if (appSensorInfos != nullptr) { - SEN_HILOGI("free appSensorInfos"); + SEN_HILOGI("Test_002 free memory"); free(appSensorInfos); appSensorInfos = nullptr; } -- Gitee From bce3a3f89f4853008640551175d47ae8705bf015 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 9 Jan 2023 13:17:58 +0000 Subject: [PATCH 3/8] update sensor low power Signed-off-by: hui1975 Change-Id: I4d2101f7b172fcc05703579fe093120fcd376ff5 --- .../native/sensor/include/i_sensor_callback.h | 38 ++++++++++ .../native/sensor/include/i_sensor_service.h | 3 + .../sensor/include/sensor_agent_proxy.h | 2 + .../sensor/include/sensor_service_client.h | 2 + .../sensor/include/sensor_service_proxy.h | 1 + .../native/sensor/src/sensor_agent_proxy.cpp | 11 +++ .../sensor/src/sensor_callback_stub.cpp | 70 ++++++++++++++++++ .../sensor/src/sensor_service_client.cpp | 15 ++++ .../sensor/src/sensor_service_proxy.cpp | 24 ++++++ interfaces/native/BUILD.gn | 2 + interfaces/native/include/sensor_agent.h | 3 + .../native/include/sensor_callback_stub.h | 36 +++++++++ interfaces/native/src/sensor_agent.cpp | 15 ++++ interfaces/plugin/BUILD.gn | 1 + services/sensor/BUILD.gn | 1 + .../sensor/include/sensor_callback_proxy.h | 39 ++++++++++ services/sensor/include/sensor_service.h | 2 + services/sensor/include/sensor_service_stub.h | 1 + .../sensor/include/sensor_suspend_policy.h | 12 ++- services/sensor/src/sensor_callback_proxy.cpp | 74 +++++++++++++++++++ services/sensor/src/sensor_service.cpp | 13 +++- services/sensor/src/sensor_service_stub.cpp | 22 ++++++ services/sensor/src/sensor_suspend_policy.cpp | 46 +++++++++++- 23 files changed, 429 insertions(+), 4 deletions(-) create mode 100644 frameworks/native/sensor/include/i_sensor_callback.h create mode 100644 frameworks/native/sensor/src/sensor_callback_stub.cpp create mode 100644 interfaces/native/include/sensor_callback_stub.h create mode 100644 services/sensor/include/sensor_callback_proxy.h create mode 100644 services/sensor/src/sensor_callback_proxy.cpp diff --git a/frameworks/native/sensor/include/i_sensor_callback.h b/frameworks/native/sensor/include/i_sensor_callback.h new file mode 100644 index 00000000..0926f6c8 --- /dev/null +++ b/frameworks/native/sensor/include/i_sensor_callback.h @@ -0,0 +1,38 @@ +/* + * 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. + */ + +#ifndef I_SENSOR_CALLBACK_H +#define I_SENSOR_CALLBACK_H + +#include "iremote_broker.h" +#include "iremote_object.h" + +#include "sensor_agent_type.h" + +namespace OHOS { +namespace Sensors { +class ISensorCallback : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.sensors.ISensorCallback"); + + virtual void OnSensorChanged(const AppSensorInfo &appSensorInfo) = 0; + + enum { + SENSOR_CHANGE = 0, + }; +}; +} // namespace Sensors +} // namespace OHOS +#endif // I_SENSOR_CALLBACK_H diff --git a/frameworks/native/sensor/include/i_sensor_service.h b/frameworks/native/sensor/include/i_sensor_service.h index cee03021..025c2d29 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.h @@ -22,6 +22,7 @@ #include "iremote_broker.h" #include "i_sensor_client.h" +#include "i_sensor_callback.h" #include "sensor_basic_data_channel.h" #include "sensor.h" #include "app_sensor.h" @@ -43,6 +44,7 @@ public: virtual ErrCode SuspendSensors(int32_t pid) = 0; virtual ErrCode ResumeSensors(int32_t pid) = 0; virtual std::vector GetAppSensorList() = 0; + virtual ErrCode RegisterCallback(sptr callback) = 0; enum { ENABLE_SENSOR = 0, DISABLE_SENSOR, @@ -54,6 +56,7 @@ public: SUSPEND_SENSORS, RESUME_SENSORS, GET_APP_SENSOR_LIST, + REGISTER_CALLBACK, }; }; } // namespace Sensors diff --git a/frameworks/native/sensor/include/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index 9cb91e44..da0b3319 100644 --- a/frameworks/native/sensor/include/sensor_agent_proxy.h +++ b/frameworks/native/sensor/include/sensor_agent_proxy.h @@ -22,6 +22,7 @@ #include "refbase.h" +#include "i_sensor_callback.h" #include "sensor_agent_type.h" #include "sensor_data_channel.h" @@ -47,6 +48,7 @@ public: int32_t SuspendSensors(int32_t pid) const; int32_t ResumeSensors(int32_t pid) const; int32_t GetAppSensors(AppSensorInfo **appSensorInfos, int32_t *count) const; + int32_t RegisterCallback(sptr callback) const; private: int32_t CreateSensorDataChannel() const; diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index b5a29931..b6176d22 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -22,6 +22,7 @@ #include "iservice_registry.h" #include "singleton.h" +#include "i_sensor_callback.h" #include "sensor_agent_type.h" #include "sensor_basic_data_channel.h" #include "sensor_basic_info.h" @@ -46,6 +47,7 @@ public: int32_t SuspendSensors(int32_t pid); int32_t ResumeSensors(int32_t pid); std::vector GetAppSensorList(); + int32_t RegisterCallback(sptr callback); private: int32_t InitServiceClient(); diff --git a/frameworks/native/sensor/include/sensor_service_proxy.h b/frameworks/native/sensor/include/sensor_service_proxy.h index 128200ad..36f6f2b8 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.h @@ -38,6 +38,7 @@ public: ErrCode SuspendSensors(int32_t pid) override; ErrCode ResumeSensors(int32_t pid) override; std::vector GetAppSensorList() override; + ErrCode RegisterCallback(sptr callback) override; private: DISALLOW_COPY_AND_MOVE(SensorServiceProxy); diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 282b3c31..5f357bf9 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -389,5 +389,16 @@ int32_t SensorAgentProxy::GetAppSensors(AppSensorInfo **appSensorInfos, int32_t *count = static_cast(appSensorInfoCount); return SUCCESS; } + +int32_t SensorAgentProxy::RegisterCallback(sptr callback) const +{ + CALL_LOG_ENTER; + CHKPR(callback, OHOS::Sensors::ERROR); + int32_t ret = SenClient.RegisterCallback(callback); + if (ret != 0) { + SEN_HILOGE("Register sensor callback failed, ret:%{public}d", ret); + } + return ret; +} } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/sensor/src/sensor_callback_stub.cpp b/frameworks/native/sensor/src/sensor_callback_stub.cpp new file mode 100644 index 00000000..f0103a5f --- /dev/null +++ b/frameworks/native/sensor/src/sensor_callback_stub.cpp @@ -0,0 +1,70 @@ +/* + * 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 "sensor_callback_stub.h" + +#include "hisysevent.h" +#include "message_parcel.h" + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorCallbackStub" }; +} // namespace + +int32_t SensorCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + SEN_HILOGD("begin, cmd:%{public}u, flags:%{public}d", code, option.GetFlags()); + std::u16string descriptor = SensorCallbackStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + SEN_HILOGE("SensorCallbackStub::OnRemoteRequest failed, descriptor mismatch"); + return OBJECT_NULL; + } + int32_t msgCode = static_cast(code); + if (msgCode == ISensorCallback::SENSOR_CHANGE) { + int32_t pid; + int32_t uid; + uint32_t tokenId; + uint32_t sensorId; + int64_t samplingPeriodNs; + int64_t maxReportDelayNs; + if ((!data.ReadInt32(pid)) || + (!data.ReadInt32(uid)) || + (!data.ReadUint32(tokenId)) || + (!data.ReadUint32(sensorId)) || + (!data.ReadInt64(samplingPeriodNs)) || + (!data.ReadInt64(maxReportDelayNs))) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + AppSensorInfo appSensorInfo; + appSensorInfo.appThreadInfo.pid = pid; + appSensorInfo.appThreadInfo.uid = uid; + appSensorInfo.appThreadInfo.callerToken = static_cast(tokenId); + appSensorInfo.sensorId = sensorId; + appSensorInfo.samplingPeriodNs = samplingPeriodNs; + appSensorInfo.maxReportDelayNs = maxReportDelayNs; + OnSensorChanged(appSensorInfo); + } else { + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return NO_ERROR; +} +} // namespace Sensors +} // namespace OHOS diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index 064db041..25892e16 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -272,5 +272,20 @@ std::vector SensorServiceClient::GetAppSensorList() FinishTrace(HITRACE_TAG_SENSORS); return appSensorList; } + +int32_t SensorServiceClient::RegisterCallback(sptr callback) +{ + CALL_LOG_ENTER; + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); + return ret; + } + CHKPR(sensorServer_, ERROR); + StartTrace(HITRACE_TAG_SENSORS, "RegisterCallback"); + ret = sensorServer_->RegisterCallback(callback); + FinishTrace(HITRACE_TAG_SENSORS); + return ret; +} } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index 0b4aaad6..b3f0a051 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -277,5 +277,29 @@ std::vector SensorServiceProxy::GetAppSensorList() } return appSensors; } + +ErrCode SensorServiceProxy::RegisterCallback(sptr callback) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { + SEN_HILOGE("write descriptor failed"); + return WRITE_MSG_ERR; + } + if (!data.WriteRemoteObject(callback->AsObject())) { + SEN_HILOGE("write callback failed"); + return WRITE_MSG_ERR; + } + sptr remote = Remote(); + CHKPR(remote, ERROR); + int32_t ret = remote->SendRequest(ISensorService::REGISTER_CALLBACK, data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "RegisterCallback", "ERROR_CODE", ret); + SEN_HILOGE("failed, ret:%{public}d", ret); + } + return static_cast(ret); +} } // namespace Sensors } // namespace OHOS diff --git a/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn index bb22a1dd..96608d72 100644 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -44,6 +44,7 @@ ohos_shared_library("sensor_interface_native") { "src/geomagnetic_field.cpp", "src/sensor_agent.cpp", "src/sensor_algorithm.cpp", + "$SUBSYSTEM_DIR/frameworks/native/sensor/src/sensor_callback_stub.cpp", ] configs = [ ":sensor_private_config" ] @@ -58,6 +59,7 @@ ohos_shared_library("sensor_interface_native") { "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "access_token:libaccesstoken_sdk", + "ipc:ipc_core", ] part_name = "sensor" subsystem_name = "sensors" diff --git a/interfaces/native/include/sensor_agent.h b/interfaces/native/include/sensor_agent.h index 39d2fd78..b528355d 100755 --- a/interfaces/native/include/sensor_agent.h +++ b/interfaces/native/include/sensor_agent.h @@ -39,6 +39,7 @@ #define SENSOR_AGENT_H #include "sensor_agent_type.h" +#include "i_sensor_callback.h" #ifdef __cplusplus #if __cplusplus @@ -134,6 +135,8 @@ int32_t ResumeSensors(int32_t pid); int32_t GetAppSensors(AppSensorInfo **appSensorInfos, int32_t *count); +int32_t RegisterCallback(OHOS::sptr callback); + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/native/include/sensor_callback_stub.h b/interfaces/native/include/sensor_callback_stub.h new file mode 100644 index 00000000..501f417f --- /dev/null +++ b/interfaces/native/include/sensor_callback_stub.h @@ -0,0 +1,36 @@ +/* + * 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. + */ + +#ifndef SENSOR_CALLBACK_STUB_H +#define SENSOR_CALLBACK_STUB_H + +#include "iremote_stub.h" +#include "nocopyable.h" + +#include "i_sensor_callback.h" + +namespace OHOS { +namespace Sensors { +class SensorCallbackStub : public IRemoteStub { +public: + DISALLOW_COPY_AND_MOVE(SensorCallbackStub); + SensorCallbackStub() = default; + virtual ~SensorCallbackStub() = default; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + void OnSensorChanged(const AppSensorInfo &appSensorInfo) override {} +}; +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_CALLBACK_STUB_H diff --git a/interfaces/native/src/sensor_agent.cpp b/interfaces/native/src/sensor_agent.cpp index ed1bbd7d..48a5b3dc 100755 --- a/interfaces/native/src/sensor_agent.cpp +++ b/interfaces/native/src/sensor_agent.cpp @@ -192,3 +192,18 @@ int32_t GetAppSensors(AppSensorInfo **appSensorInfos, int32_t *count) } return ret; } + +int32_t RegisterCallback(OHOS::sptr callback) +{ + const SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + SEN_HILOGE("proxy is nullptr"); + return SERVICE_EXCEPTION; + } + int32_t ret = proxy->RegisterCallback(callback); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("Register sensor callback failed, ret:%{public}d", ret); + return NormalizeErrCode(ret); + } + return ret; +} diff --git a/interfaces/plugin/BUILD.gn b/interfaces/plugin/BUILD.gn index 5494e73c..7c2e3ff5 100644 --- a/interfaces/plugin/BUILD.gn +++ b/interfaces/plugin/BUILD.gn @@ -23,6 +23,7 @@ ohos_shared_library("libsensor") { "./include", "//base/sensors/sensor/utils/include", "//foundation/arkui/napi/interfaces/inner_api/napi", + "//base/sensors/sensor/frameworks/native/sensor/include", ] defines = [ "APP_LOG_TAG = \"sensorJs\"", diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index d2cf701b..b20f3174 100644 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -24,6 +24,7 @@ ohos_shared_library("libsensor_service") { "src/client_info.cpp", "src/fifo_cache_data.cpp", "src/flush_info_record.cpp", + "src/sensor_callback_proxy.cpp", "src/sensor_data_processer.cpp", "src/sensor_dump.cpp", "src/sensor_manager.cpp", diff --git a/services/sensor/include/sensor_callback_proxy.h b/services/sensor/include/sensor_callback_proxy.h new file mode 100644 index 00000000..da8b6161 --- /dev/null +++ b/services/sensor/include/sensor_callback_proxy.h @@ -0,0 +1,39 @@ +/* + * 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. + */ + +#ifndef SENSOR_CALLBACK_PROXY_H +#define SENSOR_CALLBACK_PROXY_H + +#include "iremote_proxy.h" +#include "nocopyable.h" + +#include "i_sensor_callback.h" +#include "errors.h" + +namespace OHOS { +namespace Sensors { +class SensorCallbackProxy : public IRemoteProxy { +public: + explicit SensorCallbackProxy(const sptr &impl) : IRemoteProxy(impl) {} + ~SensorCallbackProxy() = default; + DISALLOW_COPY_AND_MOVE(SensorCallbackProxy); + void OnSensorChanged(const AppSensorInfo &appSensorInfo) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_CALLBACK_PROXY_H diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index ecc692fd..647ac219 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -26,6 +26,7 @@ #include "client_info.h" #include "death_recipient_template.h" #include "sensor_data_event.h" +#include "i_sensor_callback.h" #include "sensor_hdi_connection.h" #include "sensor_manager.h" #include "sensor_service_stub.h" @@ -57,6 +58,7 @@ public: ErrCode SuspendSensors(int32_t pid) override; ErrCode ResumeSensors(int32_t pid) override; std::vector GetAppSensorList() override; + ErrCode RegisterCallback(sptr callback) override; private: DISALLOW_COPY_AND_MOVE(SensorService); diff --git a/services/sensor/include/sensor_service_stub.h b/services/sensor/include/sensor_service_stub.h index 0af17c3a..e0049d27 100644 --- a/services/sensor/include/sensor_service_stub.h +++ b/services/sensor/include/sensor_service_stub.h @@ -42,6 +42,7 @@ private: ErrCode SuspendSensorsInner(MessageParcel &data, MessageParcel &reply); ErrCode ResumeSensorsInner(MessageParcel &data, MessageParcel &reply); ErrCode GetAppSensorListInner(MessageParcel &data, MessageParcel &reply); + ErrCode RegisterCallbackInner(MessageParcel &data, MessageParcel &reply); std::unordered_map baseFuncs_; }; } // namespace Sensors diff --git a/services/sensor/include/sensor_suspend_policy.h b/services/sensor/include/sensor_suspend_policy.h index bd825601..f6caa2a9 100755 --- a/services/sensor/include/sensor_suspend_policy.h +++ b/services/sensor/include/sensor_suspend_policy.h @@ -17,12 +17,16 @@ #define SENSOR_SUSPEND_POLICY_H #include +#include #include #include "singleton.h" #include "client_info.h" #include "sensor_manager.h" #include "flush_info_record.h" +#include "app_sensor.h" +#include "i_sensor_callback.h" +#include "sensor_agent_type.h" #include "sensor_hdi_connection.h" #include "sensors_errors.h" @@ -34,18 +38,24 @@ public: DISALLOW_COPY_AND_MOVE(SensorSuspendPolicy); ErrCode DoSuspend(int32_t pid); ErrCode DoResume(int32_t pid); + std::vector GetAppSensorList(); + ErrCode AddCallback(sptr callback); + void ExecuteCallbackAsync(AppThreadInfo &appThreadInfo, uint32_t sensorId, + int64_t samplingPeriodNs, int64_t maxReportDelayNs); private: bool CheckFreezingSensor(uint32_t sensorId); ErrCode DisableSensor(uint32_t sensorId, int32_t pid); ErrCode EnableSensor(uint32_t sensorId, int32_t pid, int64_t samplingPeriodNs, int64_t maxReportDelayNs); ErrCode RestoreSensorInfo(uint32_t sensorId, int32_t pid, int64_t samplingPeriodNs, int64_t maxReportDelayNs); - std::mutex suspendMutex_; SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); + std::mutex pidSensorInfoMutex_; std::unordered_map> pidSensorInfoMap_; + std::mutex callbackMutex_; + std::vector> callbackList_; }; #define SuspendPolicy DelayedSingleton::GetInstance() } // namespace Sensors diff --git a/services/sensor/src/sensor_callback_proxy.cpp b/services/sensor/src/sensor_callback_proxy.cpp new file mode 100644 index 00000000..279b4589 --- /dev/null +++ b/services/sensor/src/sensor_callback_proxy.cpp @@ -0,0 +1,74 @@ +/* + * 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 "sensor_callback_proxy.h" + +#include "message_parcel.h" +#include "hisysevent.h" + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorCallbackProxy" }; +} // namespace + + +void SensorCallbackProxy::OnSensorChanged(const AppSensorInfo &appSensorInfo) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(SensorCallbackProxy::GetDescriptor())) { + SEN_HILOGE("write descriptor failed"); + return; + } + if (!data.WriteInt32(appSensorInfo.appThreadInfo.pid)) { + SEN_HILOGE("write pid failed"); + return; + } + if (!data.WriteInt32(appSensorInfo.appThreadInfo.uid)) { + SEN_HILOGE("write uid failed"); + return; + } + if (!data.WriteUint32(static_cast(appSensorInfo.appThreadInfo.callerToken))) { + SEN_HILOGE("write tokenId failed"); + return; + } + if (!data.WriteUint32(appSensorInfo.sensorId)) { + SEN_HILOGE("write sensorId failed"); + return; + } + if (!data.WriteInt64(appSensorInfo.samplingPeriodNs)) { + SEN_HILOGE("write samplingPeriodNs failed"); + return; + } + if (!data.WriteInt64(appSensorInfo.maxReportDelayNs)) { + SEN_HILOGE("write maxReportDelayNs failed"); + return; + } + sptr remote = Remote(); + CHKPV(remote); + int32_t ret = remote->SendRequest(ISensorCallback::SENSOR_CHANGE, data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_CALLBACK_IPC_EXCEPTION", + HiviewDFX::HiSysEvent::EventType::FAULT, "PKG_NAME", "OnSensorChanged", "ERROR_CODE", ret); + SEN_HILOGE("failed, ret:%{public}d", ret); + return; + } +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 7f120bab..37aa9a01 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -249,6 +249,11 @@ ErrCode SensorService::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, return ENABLE_SENSOR_ERR; } ReportSensorSysEvent(sensorId, true, pid); + + int32_t uid = clientInfo_.GetUidByPid(pid); + AccessTokenID tokenId = clientInfo_.GetTokenIdByPid(pid); + AppThreadInfo appThreadInfo(pid, uid, tokenId); + SuspendPolicy->ExecuteCallbackAsync(appThreadInfo, sensorId, samplingPeriodNs, maxReportDelayNs); return ret; } @@ -438,7 +443,13 @@ ErrCode SensorService::ResumeSensors(int32_t pid) std::vector SensorService::GetAppSensorList() { CALL_LOG_ENTER; - return clientInfo_.GetAppSensorList(); + return SuspendPolicy->GetAppSensorList(); +} + +ErrCode SensorService::RegisterCallback(sptr callback) +{ + CALL_LOG_ENTER; + return SuspendPolicy->AddCallback(callback); } } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index ab94de7e..df6243e1 100644 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -47,6 +47,7 @@ SensorServiceStub::SensorServiceStub() baseFuncs_[SUSPEND_SENSORS] = &SensorServiceStub::SuspendSensorsInner; baseFuncs_[RESUME_SENSORS] = &SensorServiceStub::ResumeSensorsInner; baseFuncs_[GET_APP_SENSOR_LIST] = &SensorServiceStub::GetAppSensorListInner; + baseFuncs_[REGISTER_CALLBACK] = &SensorServiceStub::RegisterCallbackInner; } SensorServiceStub::~SensorServiceStub() @@ -210,5 +211,26 @@ ErrCode SensorServiceStub::GetAppSensorListInner(MessageParcel &data, MessagePar } return NO_ERROR; } + +ErrCode SensorServiceStub::RegisterCallbackInner(MessageParcel &data, MessageParcel &reply) +{ + int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()); + if (tokenType != ATokenTypeEnum::TOKEN_NATIVE) { + SEN_HILOGE("tokenType is not TOKEN_NATIVE, tokenType:%{public}d", tokenType); + // return PERMISSION_DENIED; + } + (void)reply; + sptr obj = data.ReadRemoteObject(); + if (obj == nullptr) { + SEN_HILOGE("data ReadRemoteObject() is nullptr"); + return ERROR; + } + sptr callback = iface_cast(obj); + if (callback == nullptr) { + SEN_HILOGE("obj iface_cast ISensorCallback failed"); + return ERROR; + } + return RegisterCallback(callback); +} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_suspend_policy.cpp b/services/sensor/src/sensor_suspend_policy.cpp index 69c63558..7cf1b914 100644 --- a/services/sensor/src/sensor_suspend_policy.cpp +++ b/services/sensor/src/sensor_suspend_policy.cpp @@ -41,7 +41,7 @@ bool SensorSuspendPolicy::CheckFreezingSensor(uint32_t sensorId) ErrCode SensorSuspendPolicy::DoSuspend(int32_t pid) { CALL_LOG_ENTER; - std::lock_guard suspendLock(suspendMutex_); + std::lock_guard pidSensorInfoLock(pidSensorInfoMutex_); auto pidSensorInfoIt = pidSensorInfoMap_.find(pid); if (pidSensorInfoIt != pidSensorInfoMap_.end()) { SEN_HILOGE("pid sensors already suspend, not need suspend again, pid: %{public}d", pid); @@ -96,7 +96,7 @@ ErrCode SensorSuspendPolicy::DisableSensor(uint32_t sensorId, int32_t pid) ErrCode SensorSuspendPolicy::DoResume(int32_t pid) { CALL_LOG_ENTER; - std::lock_guard suspendLock(suspendMutex_); + std::lock_guard pidSensorInfoLock(pidSensorInfoMutex_); auto pidSensorInfoIt = pidSensorInfoMap_.find(pid); if (pidSensorInfoIt == pidSensorInfoMap_.end()) { SEN_HILOGE("pid not have suspend sensors, pid: %{public}d", pid); @@ -176,5 +176,47 @@ ErrCode SensorSuspendPolicy::RestoreSensorInfo(uint32_t sensorId, int32_t pid, i } return ret; } + +std::vector SensorSuspendPolicy::GetAppSensorList() +{ + return clientInfo_.GetAppSensorList(); +} + +ErrCode SensorSuspendPolicy::AddCallback(sptr callback) +{ + CALL_LOG_ENTER; + if (callback == nullptr) { + SEN_HILOGE("callback is nullptr"); + return ERROR; + } + auto object = callback->AsObject(); + if (object == nullptr) { + SEN_HILOGE("callback->AsObject() is nullptr"); + return ERROR; + } + std::lock_guard callbackLock(callbackMutex_); + callbackList_.emplace_back(callback); + // object->AddDeathRecipient(); + return ERR_OK; +} + +void SensorSuspendPolicy::ExecuteCallbackAsync(AppThreadInfo &appThreadInfo, uint32_t sensorId, + int64_t samplingPeriodNs, int64_t maxReportDelayNs) +{ + CALL_LOG_ENTER; + AppSensorInfo appSensorInfo; + appSensorInfo.appThreadInfo = appThreadInfo; + appSensorInfo.sensorId = sensorId; + appSensorInfo.samplingPeriodNs = samplingPeriodNs; + appSensorInfo.maxReportDelayNs = maxReportDelayNs; + + std::lock_guard callbackLock(callbackMutex_); + for (auto it = callbackList_.begin(); it != callbackList_.end(); ++it) { + auto callback = *it; + if (callback != nullptr) { + callback->OnSensorChanged(appSensorInfo); + } + } +} } // namespace Sensors } // namespace OHOS -- Gitee From bba8ef62d243697bb228d6429185accfdf3804b4 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 10 Jan 2023 07:16:12 +0000 Subject: [PATCH 4/8] =?UTF-8?q?=E5=B0=86vector=E6=9B=BF=E6=8D=A2=E4=B8=BAs?= =?UTF-8?q?et?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: Ic1a59a9890cb94fc570e5532d52adcf9e96cd16a --- frameworks/native/sensor/src/sensor_callback_stub.cpp | 7 ++----- services/sensor/include/sensor_suspend_policy.h | 3 ++- services/sensor/src/sensor_service.cpp | 1 - services/sensor/src/sensor_suspend_policy.cpp | 6 ++---- 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/frameworks/native/sensor/src/sensor_callback_stub.cpp b/frameworks/native/sensor/src/sensor_callback_stub.cpp index f0103a5f..14b745e7 100644 --- a/frameworks/native/sensor/src/sensor_callback_stub.cpp +++ b/frameworks/native/sensor/src/sensor_callback_stub.cpp @@ -44,11 +44,8 @@ int32_t SensorCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, uint32_t sensorId; int64_t samplingPeriodNs; int64_t maxReportDelayNs; - if ((!data.ReadInt32(pid)) || - (!data.ReadInt32(uid)) || - (!data.ReadUint32(tokenId)) || - (!data.ReadUint32(sensorId)) || - (!data.ReadInt64(samplingPeriodNs)) || + if ((!data.ReadInt32(pid)) || (!data.ReadInt32(uid)) || (!data.ReadUint32(tokenId)) || + (!data.ReadUint32(sensorId)) || (!data.ReadInt64(samplingPeriodNs)) || (!data.ReadInt64(maxReportDelayNs))) { SEN_HILOGE("Parcel read failed"); return ERROR; diff --git a/services/sensor/include/sensor_suspend_policy.h b/services/sensor/include/sensor_suspend_policy.h index f6caa2a9..5291a3a4 100755 --- a/services/sensor/include/sensor_suspend_policy.h +++ b/services/sensor/include/sensor_suspend_policy.h @@ -17,6 +17,7 @@ #define SENSOR_SUSPEND_POLICY_H #include +#include #include #include @@ -55,7 +56,7 @@ private: std::mutex pidSensorInfoMutex_; std::unordered_map> pidSensorInfoMap_; std::mutex callbackMutex_; - std::vector> callbackList_; + std::set> callbackSet_; }; #define SuspendPolicy DelayedSingleton::GetInstance() } // namespace Sensors diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 37aa9a01..27eed8f9 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -249,7 +249,6 @@ ErrCode SensorService::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, return ENABLE_SENSOR_ERR; } ReportSensorSysEvent(sensorId, true, pid); - int32_t uid = clientInfo_.GetUidByPid(pid); AccessTokenID tokenId = clientInfo_.GetTokenIdByPid(pid); AppThreadInfo appThreadInfo(pid, uid, tokenId); diff --git a/services/sensor/src/sensor_suspend_policy.cpp b/services/sensor/src/sensor_suspend_policy.cpp index 7cf1b914..dd705904 100644 --- a/services/sensor/src/sensor_suspend_policy.cpp +++ b/services/sensor/src/sensor_suspend_policy.cpp @@ -195,8 +195,7 @@ ErrCode SensorSuspendPolicy::AddCallback(sptr callback) return ERROR; } std::lock_guard callbackLock(callbackMutex_); - callbackList_.emplace_back(callback); - // object->AddDeathRecipient(); + callbackSet_.emplace(callback); return ERR_OK; } @@ -209,9 +208,8 @@ void SensorSuspendPolicy::ExecuteCallbackAsync(AppThreadInfo &appThreadInfo, uin appSensorInfo.sensorId = sensorId; appSensorInfo.samplingPeriodNs = samplingPeriodNs; appSensorInfo.maxReportDelayNs = maxReportDelayNs; - std::lock_guard callbackLock(callbackMutex_); - for (auto it = callbackList_.begin(); it != callbackList_.end(); ++it) { + for (auto it = callbackSet_.begin(); it != callbackSet_.end(); ++it) { auto callback = *it; if (callback != nullptr) { callback->OnSensorChanged(appSensorInfo); -- Gitee From c08e178f6adc4724cf663038ebd971c4734331c7 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 10 Jan 2023 07:21:03 +0000 Subject: [PATCH 5/8] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=8D=A2=E8=A1=8C?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I53709b5acb2dc5b96b87031e2107573f7c46383f --- utils/src/app_sensor.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/utils/src/app_sensor.cpp b/utils/src/app_sensor.cpp index 3c31f77d..96a1cb66 100755 --- a/utils/src/app_sensor.cpp +++ b/utils/src/app_sensor.cpp @@ -64,11 +64,8 @@ std::unique_ptr AppSensor::Unmarshalling(Parcel &parcel) bool AppSensor::ReadFromParcel(Parcel &parcel) { - if ((!parcel.ReadInt32(pid)) || - (!parcel.ReadInt32(uid)) || - (!parcel.ReadUint32(tokenId)) || - (!parcel.ReadUint32(sensorId)) || - (!parcel.ReadInt64(samplingPeriodNs)) || + if ((!parcel.ReadInt32(pid)) || (!parcel.ReadInt32(uid)) || (!parcel.ReadUint32(tokenId)) || + (!parcel.ReadUint32(sensorId)) || (!parcel.ReadInt64(samplingPeriodNs)) || (!parcel.ReadInt64(maxReportDelayNs))) { return false; } -- Gitee From 77d859ccbc38ac55c0660e5940b62803f7e55854 Mon Sep 17 00:00:00 2001 From: ZhaoMinghui Date: Tue, 10 Jan 2023 09:59:11 +0000 Subject: [PATCH 6/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20fram?= =?UTF-8?q?eworks/native/sensor/BUILD.gn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frameworks/native/sensor/BUILD.gn | 57 ------------------------------- 1 file changed, 57 deletions(-) delete mode 100755 frameworks/native/sensor/BUILD.gn diff --git a/frameworks/native/sensor/BUILD.gn b/frameworks/native/sensor/BUILD.gn deleted file mode 100755 index 3f52ca56..00000000 --- a/frameworks/native/sensor/BUILD.gn +++ /dev/null @@ -1,57 +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/ohos.gni") - -SUBSYSTEM_DIR = "//base/sensors" - -############################################## -ohos_shared_library("libsensor_native") { - sources = [ - "src/sensor_agent_proxy.cpp", - "src/sensor_client_stub.cpp", - "src/sensor_data_channel.cpp", - "src/sensor_event_handler.cpp", - "src/sensor_file_descriptor_listener.cpp", - "src/sensor_service_client.cpp", - "src/sensor_service_proxy.cpp", - ] - - include_dirs = [ - "include", - "//commonlibrary/c_utils/base/include", - "//utils/system/safwk/native/include", - "$SUBSYSTEM_DIR/sensor/utils/include", - "$SUBSYSTEM_DIR/sensor/interfaces/native/include", - ] - deps = [ "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils" ] - - external_deps = [ - "c_utils:utils", - "eventhandler:libeventhandler", - "hisysevent_native:libhisysevent", - "hitrace_native:hitrace_meter", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "samgr:samgr_proxy", - "access_token:libaccesstoken_sdk", - ] - - part_name = "sensor" - subsystem_name = "sensors" -} - -############################################## -group("sensor_native_target") { - deps = [ ":libsensor_native" ] -} -- Gitee From b9ac7a3c67c0bd9f780992e58bd7e2aa878a2645 Mon Sep 17 00:00:00 2001 From: ZhaoMinghui Date: Tue, 10 Jan 2023 12:22:29 +0000 Subject: [PATCH 7/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20fram?= =?UTF-8?q?eworks/native/sensor/src/sensor=5Fagent=5Fproxy.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../native/sensor/src/sensor_agent_proxy.cpp | 404 ------------------ 1 file changed, 404 deletions(-) delete mode 100644 frameworks/native/sensor/src/sensor_agent_proxy.cpp diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp deleted file mode 100644 index 5f357bf9..00000000 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ /dev/null @@ -1,404 +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 "sensor_agent_proxy.h" - -#include - -#include "securec.h" -#include "sensor_catalog.h" -#include "sensor_service_client.h" -#include "sensors_errors.h" - -using namespace OHOS::HiviewDFX; -namespace OHOS { -namespace Sensors { -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorAgentProxy" }; -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::recursive_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; - -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(); - } - return sensorObj_; -} - -void SensorAgentProxy::HandleSensorData(SensorEvent *events, int32_t num, void *data) -{ - CHKPV(events); - if (num <= 0) { - SEN_HILOGE("events is null or num is invalid"); - return; - } - SensorEvent eventStream; - for (int32_t i = 0; i < num; ++i) { - eventStream = events[i]; - std::lock_guard subscribeLock(subscribeMutex_); - auto iter = g_subscribeMap.find(eventStream.sensorTypeId); - if (iter == g_subscribeMap.end()) { - SEN_HILOGE("sensor is not subscribed"); - return; - } - const SensorUser *user = iter->second; - CHKPV(user); - user->callback(&eventStream); - } -} - -int32_t SensorAgentProxy::CreateSensorDataChannel() const -{ - CALL_LOG_ENTER; - std::lock_guard chanelLock(chanelMutex_); - if (g_isChannelCreated) { - SEN_HILOGI("the channel has already been created"); - return ERR_OK; - } - CHKPR(dataChannel_, INVALID_POINTER); - auto ret = dataChannel_->CreateSensorDataChannel(HandleSensorData, nullptr); - if (ret != ERR_OK) { - SEN_HILOGE("create data channel failed, ret:%{public}d", ret); - return ret; - } - ret = SenClient.TransferDataChannel(dataChannel_); - if (ret != ERR_OK) { - auto destroyRet = dataChannel_->DestroySensorDataChannel(); - SEN_HILOGE("transfer data channel failed, ret:%{public}d,destroyRet:%{public}d", ret, destroyRet); - return ret; - } - g_isChannelCreated = true; - return ERR_OK; -} - -int32_t SensorAgentProxy::DestroySensorDataChannel() const -{ - CALL_LOG_ENTER; - std::lock_guard chanelLock(chanelMutex_); - if (!g_isChannelCreated) { - SEN_HILOGI("channel has been destroyed"); - return ERR_OK; - } - CHKPR(dataChannel_, INVALID_POINTER); - int32_t ret = dataChannel_->DestroySensorDataChannel(); - if (ret != ERR_OK) { - SEN_HILOGE("destroy data channel failed, ret:%{public}d", ret); - return ret; - } - ret = SenClient.DestroyDataChannel(); - if (ret != ERR_OK) { - SEN_HILOGE("destroy service data channel fail, ret:%{public}d", ret); - return ret; - } - g_isChannelCreated = false; - return ERR_OK; -} - -int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *user) const -{ - CHKPR(user, OHOS::Sensors::ERROR); - CHKPR(user->callback, OHOS::Sensors::ERROR); - if (g_samplingInterval < 0 || g_reportInterval < 0) { - SEN_HILOGE("samplingPeriod or g_reportInterval is invalid"); - return ERROR; - } - if (!SenClient.IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); - return PARAMETER_ERROR; - } - std::lock_guard subscribeLock(subscribeMutex_); - if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { - SEN_HILOGE("subscribe sensorId first"); - return ERROR; - } - int32_t ret = SenClient.EnableSensor(sensorId, g_samplingInterval, g_reportInterval); - g_samplingInterval = -1; - g_reportInterval = -1; - if (ret != 0) { - SEN_HILOGE("enable sensor failed, ret:%{public}d", ret); - g_subscribeMap.erase(sensorId); - return ret; - } - return ret; -} - -int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *user) const -{ - CHKPR(user, OHOS::Sensors::ERROR); - CHKPR(user->callback, OHOS::Sensors::ERROR); - if (!SenClient.IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); - return PARAMETER_ERROR; - } - std::lock_guard subscribeLock(subscribeMutex_); - if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { - SEN_HILOGE("subscribe sensorId first"); - return OHOS::Sensors::ERROR; - } - g_subscribeMap.erase(sensorId); - g_unsubscribeMap[sensorId] = user; - int32_t ret = SenClient.DisableSensor(sensorId); - if (ret != 0) { - SEN_HILOGE("DisableSensor failed, ret:%{public}d", ret); - return ret; - } - return ret; -} - -int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, - int64_t reportInterval) const -{ - CHKPR(user, OHOS::Sensors::ERROR); - if (!SenClient.IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); - return PARAMETER_ERROR; - } - if (samplingInterval < 0 || reportInterval < 0) { - SEN_HILOGE("samplingInterval or reportInterval is invalid"); - return OHOS::Sensors::ERROR; - } - std::lock_guard subscribeLock(subscribeMutex_); - if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { - SEN_HILOGE("subscribe sensorId first"); - return OHOS::Sensors::ERROR; - } - g_samplingInterval = samplingInterval; - g_reportInterval = reportInterval; - return OHOS::Sensors::SUCCESS; -} - -int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *user) const -{ - 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 PARAMETER_ERROR; - } - int32_t ret = CreateSensorDataChannel(); - if (ret != ERR_OK) { - SEN_HILOGE("create sensor data chanel failed"); - return OHOS::Sensors::ERROR; - } - std::lock_guard subscribeLock(subscribeMutex_); - g_subscribeMap[sensorId] = user; - return OHOS::Sensors::SUCCESS; -} - -int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser *user) const -{ - 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 PARAMETER_ERROR; - } - std::lock_guard subscribeLock(subscribeMutex_); - if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { - SEN_HILOGE("deactivate sensorId first"); - return OHOS::Sensors::ERROR; - } - if (g_subscribeMap.empty()) { - int32_t ret = DestroySensorDataChannel(); - if (ret != ERR_OK) { - SEN_HILOGE("destroy data channel fail, ret:%{public}d", 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 -{ - CHKPR(user, OHOS::Sensors::ERROR); - CHKPR(user->callback, OHOS::Sensors::ERROR); - if (!SenClient.IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); - return ERROR; - } - std::lock_guard subscribeLock(subscribeMutex_); - if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { - SEN_HILOGE("subscribe sensorId first"); - return OHOS::Sensors::ERROR; - } - return OHOS::Sensors::SUCCESS; -} - -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) { - SensorInfo *sensorInfo = sensorInfos_ + i; - errno_t ret = strcpy_s(sensorInfo->sensorName, NAME_MAX_LEN, - sensorList[i].GetSensorName().c_str()); - CHKCR(ret == EOK, ERROR); - ret = strcpy_s(sensorInfo->vendorName, NAME_MAX_LEN, - sensorList[i].GetVendorName().c_str()); - CHKCR(ret == EOK, ERROR); - ret = strcpy_s(sensorInfo->hardwareVersion, VERSION_MAX_LEN, - sensorList[i].GetHardwareVersion().c_str()); - CHKCR(ret == EOK, ERROR); - ret = strcpy_s(sensorInfo->firmwareVersion, VERSION_MAX_LEN, - sensorList[i].GetFirmwareVersion().c_str()); - CHKCR(ret == EOK, ERROR); - sensorInfo->sensorId = static_cast(sensorList[i].GetSensorId()); - sensorInfo->sensorTypeId = static_cast(sensorList[i].GetSensorTypeId()); - sensorInfo->maxRange = sensorList[i].GetMaxRange(); - sensorInfo->precision = sensorList[i].GetResolution(); - sensorInfo->power = sensorList[i].GetPower(); - sensorInfo->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs(); - sensorInfo->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs(); - } - sensorInfoCount_ = static_cast(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; - } - } - *sensorInfo = sensorInfos_; - *count = sensorInfoCount_; - return SUCCESS; -} - -int32_t SensorAgentProxy::SuspendSensors(int32_t pid) const -{ - CALL_LOG_ENTER; - if (pid < 0) { - SEN_HILOGE("pid is invalid, %{public}d", pid); - return PARAMETER_ERROR; - } - int32_t ret = SenClient.SuspendSensors(pid); - if (ret != 0) { - SEN_HILOGE("Suspend pid sensors failed, ret:%{public}d", ret); - } - return ret; -} - -int32_t SensorAgentProxy::ResumeSensors(int32_t pid) const -{ - CALL_LOG_ENTER; - if (pid < 0) { - SEN_HILOGE("pid is invalid, %{public}d", pid); - return PARAMETER_ERROR; - } - int32_t ret = SenClient.ResumeSensors(pid); - if (ret != 0) { - SEN_HILOGE("Resume pid sensors failed, ret:%{public}d", ret); - } - return ret; -} - -int32_t SensorAgentProxy::GetAppSensors(AppSensorInfo **appSensorInfos, int32_t *count) const -{ - CALL_LOG_ENTER; - CHKPR(appSensorInfos, OHOS::Sensors::ERROR); - CHKPR(count, OHOS::Sensors::ERROR); - std::vector appSensorList = SenClient.GetAppSensorList(); - if (appSensorList.empty()) { - SEN_HILOGE("get app sensor lists failed"); - return ERROR; - } - size_t appSensorInfoCount = appSensorList.size(); - if (appSensorInfoCount > MAX_SENSOR_LIST_SIZE) { - SEN_HILOGE("The number of app sensors exceeds the maximum value"); - return ERROR; - } - AppSensorInfo *appSensor = (AppSensorInfo *)malloc(sizeof(AppSensorInfo) * appSensorInfoCount); - CHKPR(appSensor, ERROR); - for (size_t i = 0; i < appSensorInfoCount; ++i) { - AppSensorInfo *curAppSensor = appSensor + i; - curAppSensor->appThreadInfo = AppThreadInfo(appSensorList[i].pid, appSensorList[i].uid, - static_cast(appSensorList[i].tokenId)); - curAppSensor->sensorId = appSensorList[i].sensorId; - curAppSensor->samplingPeriodNs = appSensorList[i].samplingPeriodNs; - curAppSensor->maxReportDelayNs = appSensorList[i].maxReportDelayNs; - } - *appSensorInfos = appSensor; - *count = static_cast(appSensorInfoCount); - return SUCCESS; -} - -int32_t SensorAgentProxy::RegisterCallback(sptr callback) const -{ - CALL_LOG_ENTER; - CHKPR(callback, OHOS::Sensors::ERROR); - int32_t ret = SenClient.RegisterCallback(callback); - if (ret != 0) { - SEN_HILOGE("Register sensor callback failed, ret:%{public}d", ret); - } - return ret; -} -} // namespace Sensors -} // namespace OHOS \ No newline at end of file -- Gitee From fde5859aa888adf0233e1ffe562374bccf1522b8 Mon Sep 17 00:00:00 2001 From: ZhaoMinghui Date: Tue, 10 Jan 2023 12:24:00 +0000 Subject: [PATCH 8/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20util?= =?UTF-8?q?s/src/app=5Fsensor.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/src/app_sensor.cpp | 75 ---------------------------------------- 1 file changed, 75 deletions(-) delete mode 100755 utils/src/app_sensor.cpp diff --git a/utils/src/app_sensor.cpp b/utils/src/app_sensor.cpp deleted file mode 100755 index 96a1cb66..00000000 --- a/utils/src/app_sensor.cpp +++ /dev/null @@ -1,75 +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 "app_sensor.h" -#include "sensors_errors.h" - -namespace OHOS { -namespace Sensors { -using namespace OHOS::HiviewDFX; -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "AppSensor" }; -} - -bool AppSensor::Marshalling(Parcel &parcel) const -{ - if (!parcel.WriteInt32(pid)) { - SEN_HILOGE("failed, write pid failed"); - return false; - } - if (!parcel.WriteInt32(uid)) { - SEN_HILOGE("failed, write uid failed"); - return false; - } - if (!parcel.WriteUint32(tokenId)) { - SEN_HILOGE("failed, write tokenId failed"); - return false; - } - if (!parcel.WriteUint32(sensorId)) { - SEN_HILOGE("failed, write sensorId failed"); - return false; - } - if (!parcel.WriteInt64(samplingPeriodNs)) { - SEN_HILOGE("failed, write samplingPeriodNs failed"); - return false; - } - if (!parcel.WriteInt64(maxReportDelayNs)) { - SEN_HILOGE("failed, write maxReportDelayNs failed"); - return false; - } - return true; -} - -std::unique_ptr AppSensor::Unmarshalling(Parcel &parcel) -{ - auto appSensor = std::make_unique(); - if (!appSensor->ReadFromParcel(parcel)) { - SEN_HILOGE("ReadFromParcel is failed"); - return nullptr; - } - return appSensor; -} - -bool AppSensor::ReadFromParcel(Parcel &parcel) -{ - if ((!parcel.ReadInt32(pid)) || (!parcel.ReadInt32(uid)) || (!parcel.ReadUint32(tokenId)) || - (!parcel.ReadUint32(sensorId)) || (!parcel.ReadInt64(samplingPeriodNs)) || - (!parcel.ReadInt64(maxReportDelayNs))) { - return false; - } - return true; -} -} // namespace Sensors -} // namespace OHOS -- Gitee