From 422782d4057811195afa9bfa1134c86b2eb59aff Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Fri, 11 Apr 2025 10:31:22 +0800 Subject: [PATCH] Adapter rotation Signed-off-by: wuzhihuitmac Change-Id: I855a4f510f4e7b095091b8f464877a618c20b3f9 --- bundle.json | 3 +- frameworks/native/ISensorService.idl | 1 + .../native/include/sensor_agent_proxy.h | 1 + .../native/include/sensor_service_client.h | 1 + frameworks/native/src/sensor_agent.cpp | 5 ++ frameworks/native/src/sensor_agent_proxy.cpp | 6 ++ .../native/src/sensor_service_client.cpp | 27 +++++++ interfaces/inner_api/sensor_agent.h | 2 + sensor.gni | 9 +++ services/BUILD.gn | 10 +++ services/include/client_info.h | 3 + services/include/sensor_service.h | 1 + services/src/client_info.cpp | 10 +++ services/src/sensor_data_processer.cpp | 11 ++- services/src/sensor_manager.cpp | 8 +- services/src/sensor_service.cpp | 29 ++++++++ utils/common/BUILD.gn | 1 + utils/common/include/motion_plugin.h | 42 +++++++++++ .../include/sensor_basic_data_channel.h | 4 + utils/common/src/motion_plugin.cpp | 74 +++++++++++++++++++ .../common/src/sensor_basic_data_channel.cpp | 13 ++++ 21 files changed, 255 insertions(+), 6 deletions(-) mode change 100755 => 100644 services/src/sensor_data_processer.cpp create mode 100644 utils/common/include/motion_plugin.h create mode 100644 utils/common/src/motion_plugin.cpp diff --git a/bundle.json b/bundle.json index 110455db..e3faa5d4 100755 --- a/bundle.json +++ b/bundle.json @@ -33,7 +33,8 @@ "safwk", "samgr", "eventhandler", - "hicollie" + "hicollie", + "window_manager" ], "third_party": [] }, diff --git a/frameworks/native/ISensorService.idl b/frameworks/native/ISensorService.idl index cdbddd44..e1d4ad4b 100644 --- a/frameworks/native/ISensorService.idl +++ b/frameworks/native/ISensorService.idl @@ -31,4 +31,5 @@ interface OHOS.Sensors.ISensorService { void EnableActiveInfoCB(); void DisableActiveInfoCB(); void ResetSensors(); + void SetDeviceStatus([in] unsigned int deviceStatus); } \ No newline at end of file diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index 58e8bd1f..9313d814 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -47,6 +47,7 @@ public: int32_t Unregister(SensorActiveInfoCB callback); void HandleSensorData(SensorEvent *events, int32_t num, void *data); int32_t ResetSensors() const; + void SetDeviceStatus(uint32_t deviceStatus) const; private: int32_t CreateSensorDataChannel(); diff --git a/frameworks/native/include/sensor_service_client.h b/frameworks/native/include/sensor_service_client.h index a6d52090..713ec602 100755 --- a/frameworks/native/include/sensor_service_client.h +++ b/frameworks/native/include/sensor_service_client.h @@ -48,6 +48,7 @@ public: void ReceiveMessage(const char *buf, size_t size); void Disconnect(); void HandleNetPacke(NetPacket &pkt); + void SetDeviceStatus(uint32_t deviceStatus); private: int32_t InitServiceClient(); diff --git a/frameworks/native/src/sensor_agent.cpp b/frameworks/native/src/sensor_agent.cpp index 3a8d0e4f..f4088949 100644 --- a/frameworks/native/src/sensor_agent.cpp +++ b/frameworks/native/src/sensor_agent.cpp @@ -169,4 +169,9 @@ int32_t ResetSensors() return NormalizeErrCode(ret); } return ret; +} + +void SetDeviceStatus(uint32_t deviceStatus) +{ + SENSOR_AGENT_IMPL->SetDeviceStatus(deviceStatus); } \ No newline at end of file diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 2d023b4d..2707756b 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -560,5 +560,11 @@ int32_t SensorAgentProxy::ResetSensors() const } return ret; } + +void SensorAgentProxy::SetDeviceStatus(uint32_t deviceStatus) const +{ + SEN_HILOGI("SetDeviceStatus in, deviceStatus:%{public}d", deviceStatus); + SEN_CLIENT.SetDeviceStatus(deviceStatus); +} } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/src/sensor_service_client.cpp b/frameworks/native/src/sensor_service_client.cpp index b6cc099c..b6a257c4 100644 --- a/frameworks/native/src/sensor_service_client.cpp +++ b/frameworks/native/src/sensor_service_client.cpp @@ -305,6 +305,10 @@ void SensorServiceClient::WriteHiSysIPCEvent(ISensorServiceIpcCode code, int32_t HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT, "PKG_NAME", "ResetSensors", "ERROR_CODE", ret); break; + case ISensorServiceIpcCode::COMMAND_SET_DEVICE_STATUS: + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT, + "PKG_NAME", "SetDeviceStatus", "ERROR_CODE", ret); + break; default: SEN_HILOGW("Code does not exist, code:%{public}d", static_cast(code)); break; @@ -659,5 +663,28 @@ int32_t SensorServiceClient::CreateSocketChannel() isConnected_ = true; return ERR_OK; } + +void SensorServiceClient::SetDeviceStatus(uint32_t deviceStatus) +{ + CALL_LOG_ENTER; + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); + return; + } + std::lock_guard clientLock(clientMutex_); + CHKPV(sensorServer_); +#ifdef HIVIEWDFX_HITRACE_ENABLE + StartTrace(HITRACE_TAG_SENSORS, "SetDeviceStatus"); +#endif // HIVIEWDFX_HITRACE_ENABLE + ret = sensorServer_->SetDeviceStatus(deviceStatus); + if (ret != ERR_OK) { + SEN_HILOGE("SetDeviceStatus failed, ret:%{public}d", ret); + } + WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_SET_DEVICE_STATUS, ret); +#ifdef HIVIEWDFX_HITRACE_ENABLE + FinishTrace(HITRACE_TAG_SENSORS); +#endif // HIVIEWDFX_HITRACE_ENABLE +} } // namespace Sensors } // namespace OHOS diff --git a/interfaces/inner_api/sensor_agent.h b/interfaces/inner_api/sensor_agent.h index 246e42a5..6ce08dc7 100755 --- a/interfaces/inner_api/sensor_agent.h +++ b/interfaces/inner_api/sensor_agent.h @@ -189,6 +189,8 @@ int32_t Unregister(SensorActiveInfoCB callback); */ int32_t ResetSensors(); +void SetDeviceStatus(uint32_t deviceStatus); + #ifdef __cplusplus #if __cplusplus } diff --git a/sensor.gni b/sensor.gni index 7012db5e..85a7e969 100644 --- a/sensor.gni +++ b/sensor.gni @@ -51,6 +51,15 @@ if (!defined(global_parts_info) || sensor_access_token_enable = false } +if (!defined(global_parts_info) || + defined(global_parts_info.msdp_motion)) { + sensor_msdp_motion_enable = true + print("Msdp motion ability is enabled.") +} else { + sensor_msdp_motion_enable = false + print("Msdp motion ability is disabled.") +} + if (build_variant == "root") { sensor_default_defines += [ "BUILD_VARIANT_ENG" ] sensor_build_eng = true diff --git a/services/BUILD.gn b/services/BUILD.gn index 20606c53..44f81a11 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -76,6 +76,7 @@ ohos_shared_library("libsensor_service") { "ipc:ipc_single", "safwk:system_ability_fwk", "samgr:samgr_proxy", + "window_manager:libdm", ] if (sensor_memmgr_enable) { @@ -87,6 +88,10 @@ ohos_shared_library("libsensor_service") { defines += [ "ACCESS_TOKEN_ENABLE" ] } + if (sensor_msdp_motion_enable) { + defines += [ "MSDP_MOTION_ENABLE" ] + } + if (hiviewdfx_hisysevent_enable) { external_deps += [ "hisysevent:libhisysevent" ] } @@ -189,6 +194,7 @@ ohos_static_library("libsensor_service_static") { "ipc:ipc_single", "safwk:system_ability_fwk", "samgr:samgr_proxy", + "window_manager:libdm", ] if (sensor_memmgr_enable) { @@ -200,6 +206,10 @@ ohos_static_library("libsensor_service_static") { defines += [ "ACCESS_TOKEN_ENABLE" ] } + if (sensor_msdp_motion_enable) { + defines += [ "MSDP_MOTION_ENABLE" ] + } + if (hiviewdfx_hisysevent_enable) { external_deps += [ "hisysevent:libhisysevent" ] } diff --git a/services/include/client_info.h b/services/include/client_info.h index f0b76b40..01c4e5b5 100644 --- a/services/include/client_info.h +++ b/services/include/client_info.h @@ -77,6 +77,8 @@ public: int32_t GetPidByTokenId(AccessTokenID tokenId); void UpdatePermState(int32_t pid, int32_t sensorId, bool state); void ChangeSensorPerm(AccessTokenID tokenId, const std::string &permName, bool state); + void SetDeviceStatus(uint32_t deviceStatus); + uint32_t GetDeviceStatus(); private: DISALLOW_COPY_AND_MOVE(ClientInfo); @@ -98,6 +100,7 @@ private: std::mutex activeInfoCBPidMutex_; std::unordered_set activeInfoCBPidSet_; static std::unordered_map> userGrantPermMap_; + std::atomic deviceStatus_; }; } // namespace Sensors } // namespace OHOS diff --git a/services/include/sensor_service.h b/services/include/sensor_service.h index 22b8cede..39286c8a 100644 --- a/services/include/sensor_service.h +++ b/services/include/sensor_service.h @@ -57,6 +57,7 @@ public: ErrCode EnableActiveInfoCB() override; ErrCode DisableActiveInfoCB() override; ErrCode ResetSensors() override; + ErrCode SetDeviceStatus(uint32_t deviceStatus) override; private: DISALLOW_COPY_AND_MOVE(SensorService); diff --git a/services/src/client_info.cpp b/services/src/client_info.cpp index e683b059..7746625e 100644 --- a/services/src/client_info.cpp +++ b/services/src/client_info.cpp @@ -804,5 +804,15 @@ void ClientInfo::ChangeSensorPerm(AccessTokenID tokenId, const std::string &perm UpdatePermState(pid, sensorId, state); } } + +void ClientInfo::SetDeviceStatus(uint32_t deviceStatus) +{ + deviceStatus_ = deviceStatus; +} + +uint32_t ClientInfo::GetDeviceStatus() +{ + return deviceStatus_; +} } // namespace Sensors } // namespace OHOS diff --git a/services/src/sensor_data_processer.cpp b/services/src/sensor_data_processer.cpp old mode 100755 new mode 100644 index 11d81157..34e69cc3 --- a/services/src/sensor_data_processer.cpp +++ b/services/src/sensor_data_processer.cpp @@ -22,6 +22,7 @@ #ifdef HIVIEWDFX_HISYSEVENT_ENABLE #include "hisysevent.h" #endif // HIVIEWDFX_HISYSEVENT_ENABLE +#include "motion_plugin.h" #include "print_sensor_data.h" #undef LOG_TAG @@ -265,11 +266,19 @@ void SensorDataProcesser::EventFilter(CircularEventBuf &eventsBuf) } std::vector> channelList = clientInfo_.GetSensorChannel(sensorId); for (auto &channel : channelList) { + if (channel == nullptr) { + SEN_HILOGE("channel is null"); + continue; + } if (!channel->GetSensorStatus()) { SEN_HILOGW("Sensor status is not active"); continue; } - SendEvents(channel, eventsBuf.circularBuf[eventsBuf.readPos]); + SensorData sensorData = eventsBuf.circularBuf[eventsBuf.readPos]; +#ifdef MSDP_MOTION_ENABLE + MotionTransformIfRequired(channel->GetPackageName(), clientInfo_.GetDeviceStatus(), &sensorData); +#endif // MSDP_MOTION_ENABLE + SendEvents(channel, sensorData); } } diff --git a/services/src/sensor_manager.cpp b/services/src/sensor_manager.cpp index 5d8e63d5..e79c215e 100644 --- a/services/src/sensor_manager.cpp +++ b/services/src/sensor_manager.cpp @@ -184,10 +184,6 @@ ErrCode SensorManager::AfterDisableSensor(int32_t sensorId) void SensorManager::GetPackageName(AccessTokenID tokenId, std::string &packageName, bool isAccessTokenServiceActive) { CALL_LOG_ENTER; - if (!isAccessTokenServiceActive) { - SEN_HILOGE("Access token service is inactive"); - return; - } int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId); switch (tokenType) { case ATokenTypeEnum::TOKEN_HAP: { @@ -201,6 +197,10 @@ void SensorManager::GetPackageName(AccessTokenID tokenId, std::string &packageNa } case ATokenTypeEnum::TOKEN_NATIVE: case ATokenTypeEnum::TOKEN_SHELL: { + if (!isAccessTokenServiceActive) { + SEN_HILOGE("Access token service is inactive"); + return; + } NativeTokenInfo tokenInfo; if (AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) { SEN_HILOGE("Get native token info fail"); diff --git a/services/src/sensor_service.cpp b/services/src/sensor_service.cpp index e9b6d22f..6364091a 100644 --- a/services/src/sensor_service.cpp +++ b/services/src/sensor_service.cpp @@ -19,12 +19,14 @@ #include #include +#include "display_manager.h" #ifdef HIVIEWDFX_HISYSEVENT_ENABLE #include "hisysevent.h" #endif // HIVIEWDFX_HISYSEVENT_ENABLE #ifdef MEMMGR_ENABLE #include "mem_mgr_client.h" #endif // MEMMGR_ENABLE +#include "motion_plugin.h" #include "ipc_skeleton.h" #include "permission_util.h" @@ -75,6 +77,18 @@ void SensorService::OnAddSystemAbility(int32_t systemAbilityId, const std::strin isAccessTokenServiceActive_ = true; } #endif // ACCESS_TOKEN_ENABLE +#ifdef MSDP_MOTION_ENABLE + if (systemAbilityId == MSDP_MOTION_SERVICE_ID) { + if (!LoadMotionSensor()) { + SEN_HILOGI("LoadMotionSensor fail"); + } + } +#endif // MSDP_MOTION_ENABLE + if (systemAbilityId == DISPLAY_MANAGER_SERVICE_SA_ID) { + uint32_t status = Rosen::DisplayManager::GetInstance().GetDeviceStatus(); + clientInfo_.SetDeviceStatus(status); + SEN_HILOGI("GetDeviceStatus, deviceStatus:%{public}d", status); + } } void SensorService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) @@ -126,6 +140,10 @@ void SensorService::OnStart() #ifdef ACCESS_TOKEN_ENABLE AddSystemAbilityListener(ACCESS_TOKEN_MANAGER_SERVICE_ID); #endif // ACCESS_TOKEN_ENABLE +#ifdef MSDP_MOTION_ENABLE + AddSystemAbilityListener(MSDP_MOTION_SERVICE_ID); +#endif // MSDP_MOTION_ENABLE + AddSystemAbilityListener(DISPLAY_MANAGER_SERVICE_SA_ID); } #ifdef HDF_DRIVERS_INTERFACE_SENSOR @@ -489,6 +507,10 @@ ErrCode SensorService::TransferDataChannel(int32_t sendFd, const sptrSetSensorStatus(true); + std::string packageName(""); + sensorManager_.GetPackageName(callerToken, packageName, isAccessTokenServiceActive_); + SEN_HILOGI("Calling packageName:%{public}s", packageName.c_str()); + sensorBasicDataChannel->SetPackageName(packageName); RegisterClientDeathRecipient(sensorClient, pid); SEN_HILOGI("Done"); return ERR_OK; @@ -785,5 +807,12 @@ void SensorService::PermStateChangeCb::PermStateChangeCallback(Security::AccessT server_->clientInfo_.ChangeSensorPerm(result.tokenID, result.permissionName, (result.permStateChangeType != 0)); } + +ErrCode SensorService::SetDeviceStatus(uint32_t deviceStatus) +{ + SEN_HILOGI("SetDeviceStatus in, deviceStatus:%{public}d", deviceStatus); + clientInfo_.SetDeviceStatus(deviceStatus); + return ERR_OK; +} } // namespace Sensors } // namespace OHOS diff --git a/utils/common/BUILD.gn b/utils/common/BUILD.gn index 84ff5e44..3fb0e537 100644 --- a/utils/common/BUILD.gn +++ b/utils/common/BUILD.gn @@ -17,6 +17,7 @@ import("./../../sensor.gni") ohos_shared_library("libsensor_utils") { sources = [ "src/active_info.cpp", + "src/motion_plugin.cpp", "src/permission_util.cpp", "src/print_sensor_data.cpp", "src/report_data_callback.cpp", diff --git a/utils/common/include/motion_plugin.h b/utils/common/include/motion_plugin.h new file mode 100644 index 00000000..baf6a3b4 --- /dev/null +++ b/utils/common/include/motion_plugin.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 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 MOTION_PLUGIN_H +#define MOTION_PLUGIN_H + +#include +#include +#include + +#include +#include + +#include "sensor_data_event.h" + +namespace OHOS { +namespace Sensors { +#if (defined(__aarch64__) || defined(__x86_64__)) +const std::string PLUGIN_SO_PATH = "/system/lib64/libmotion_param.z.so"; +#else +const std::string PLUGIN_SO_PATH = "/system/lib/libmotion_param.z.so"; +#endif + +using MotionTransformIfRequiredPtr = void (*)(const std::string& pkName, uint32_t state, SensorData* sensorData); +bool LoadMotionSensor(void); +void UnloadMotionSensor(void); +void MotionTransformIfRequired(const std::string& pkName, uint32_t state, SensorData* sensorData); +} +} +#endif /* MOTION_PLUGIN_H */ \ No newline at end of file diff --git a/utils/common/include/sensor_basic_data_channel.h b/utils/common/include/sensor_basic_data_channel.h index 449dbf9d..7ad633da 100644 --- a/utils/common/include/sensor_basic_data_channel.h +++ b/utils/common/include/sensor_basic_data_channel.h @@ -42,6 +42,8 @@ public: bool GetSensorStatus() const; void SetSensorStatus(bool isActive); const std::unordered_map &GetDataCacheBuf() const; + std::string GetPackageName(); + void SetPackageName(std::string packageName); private: std::mutex fdLock_; @@ -50,6 +52,8 @@ private: bool isActive_; std::mutex statusLock_; std::unordered_map dataCacheBuf_; + std::string packageName_; + std::mutex pkNameLock_; }; } // namespace Sensors } // namespace OHOS diff --git a/utils/common/src/motion_plugin.cpp b/utils/common/src/motion_plugin.cpp new file mode 100644 index 00000000..3aa79515 --- /dev/null +++ b/utils/common/src/motion_plugin.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2025 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 "motion_plugin.h" + +#include "sensor_log.h" + +#undef LOG_TAG +#define LOG_TAG "MotionPlugin" + +namespace OHOS { +namespace Sensors { +namespace { + constexpr uint32_t SLEEP_TIME_US = 10000; +} + +static void *g_handle = nullptr; + +bool LoadMotionSensor(void) +{ + SEN_HILOGI("Load motion plugin in"); + if (g_handle != nullptr) { + SEN_HILOGW("Motion plugin has already exits"); + return true; + } + int32_t cnt = 0; + int32_t retryTimes = 3; + do { + cnt++; + g_handle = dlopen(PLUGIN_SO_PATH.c_str(), RTLD_LAZY); + SEN_HILOGI("dlopen %{public}s, retry cnt: %{public}d", PLUGIN_SO_PATH.c_str(), cnt); + usleep(SLEEP_TIME_US); + } while (!g_handle && cnt < retryTimes); + return (g_handle != nullptr); +} + +void UnloadMotionSensor(void) +{ + SEN_HILOGI("Unload motion plugin in"); + if (g_handle != nullptr) { + dlclose(g_handle); + g_handle = nullptr; + } +} + +__attribute__((no_sanitize("cfi"))) void MotionTransformIfRequired(const std::string& pkName, + uint32_t state, SensorData* sensorData) +{ + if (g_handle == nullptr) { + SEN_HILOGE("g_handle is nullptr"); + return; + } + MotionTransformIfRequiredPtr func = (MotionTransformIfRequiredPtr)(dlsym(g_handle, "TransformIfRequired")); + const char* dlsymError = dlerror(); + if (dlsymError) { + SEN_HILOGE("dlsym error: %{public}s", dlsymError); + return; + } + return func(pkName, state, sensorData); +} +} +} \ No newline at end of file diff --git a/utils/common/src/sensor_basic_data_channel.cpp b/utils/common/src/sensor_basic_data_channel.cpp index 4d278d9c..857cf0de 100644 --- a/utils/common/src/sensor_basic_data_channel.cpp +++ b/utils/common/src/sensor_basic_data_channel.cpp @@ -295,5 +295,18 @@ void SensorBasicDataChannel::SetSensorStatus(bool isActive) isActive_ = isActive; return; } + +std::string SensorBasicDataChannel::GetPackageName() +{ + std::unique_lock lock(pkNameLock_); + return packageName_; +} + +void SensorBasicDataChannel::SetPackageName(std::string packageName) +{ + SEN_HILOGD("SetPackageName in, packageName:%{public}s", packageName.c_str()); + std::unique_lock lock(pkNameLock_); + packageName_ = packageName; +} } // namespace Sensors } // namespace OHOS -- Gitee