diff --git a/bundle.json b/bundle.json index 110455dba46cfa3347d855473a2f6cc5afc2cc4c..e3faa5d471b8b12e172e6b1699d87c7539c3ab9f 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 cdbddd442f14f87aa6630f53a9c35b5c09f41ebd..e1d4ad4b9a62212005eeff6b4df2bcabf4d3decc 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 58e8bd1f5ff5d0d2be2c815839a6694491ccc6d2..9313d814e020bfea91aee939046aed9d691d5757 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 a6d5209050cfdd5ec84a360b5495382ba9c5dc47..713ec60292696a945368f702687aba4e26ac96be 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 3a8d0e4ff94d4924a698a0c3b56a711d10f3fc0f..f4088949f427ce49243e57cb0fc4a400b0e41c10 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 2d023b4d0eef11e2ccd2c755ccf232d6c9f80411..2707756b7dc9d9acc8fb2f9d16ec37269fee4aa6 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 b6cc099c269aa896ae5e91bc950689a87490def2..b6a257c49f45ff8062c57aefacfa54f302db52af 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 246e42a5afc1032bb3add343fc777ca36998f07d..6ce08dc72f416c7d3106b67bf011939d7c1ff335 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 7012db5ec7ea7c1d3ba671c78d2bd71ab11276e3..85a7e96993febca68d20c1b100d1bc408cc3c081 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 20606c5368314e548723436792f8478a284faa92..44f81a118ab3e182b658fbc946b0511decb6cff6 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 f0b76b40af8311027018fa29a774f9d8d4d7d082..01c4e5b5c51f23b0d026fc5e27d9d845b9521f0e 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 22b8cedeb409c37bbc23239f7b0c31f702253595..39286c8a47d6d9531e05ed6ff2a61c076ed4e67d 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 e683b0595ceb889a2d8f494df91be00e0ee3d1fa..7746625e6d1103364dcc5bc5e995c3e3e738d351 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 11d81157f9ba7757eaedb10fa49f384bab608a88..34e69cc38fa4c8ee5e05a2566a51b71e9fc17150 --- 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 5d8e63d50851f10195c455a822d047d1ed350f99..e79c215e1e81eabd8d6695d2b4227befa6c41aea 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 e9b6d22f1b22a17747793a7c47a2b034512dd858..6364091a9ab503f0ab00126b6d116bcbd8085661 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 84ff5e44328d45a81455a8f1a40189fd6311afaf..3fb0e5377317139f2120b2ca926816b0b6b3b89d 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 0000000000000000000000000000000000000000..baf6a3b466e367209d82d6bf94fb2bb97e5cdfa7 --- /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 449dbf9d8f4a0f5a4d92f0b269b01cbbe3bc8f5a..7ad633dad0565685a98db84cc7b9ed74a4aa5ea6 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 0000000000000000000000000000000000000000..3aa7951575156ee05f4b5086a692528e7f2c8037 --- /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 4d278d9c731ad144771367509d4af2171ba04e76..857cf0de1e5153ae91a155121db5ecc902357ac5 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