diff --git a/frameworks/native/sensor/include/i_sensor_service.h b/frameworks/native/sensor/include/i_sensor_service.h index dab25cb40366ea6aa2068974715969ec12757660..1fa19127dbacd6300fc050b10812bcab3ba749b2 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -21,7 +21,9 @@ #include "errors.h" #include "iremote_broker.h" +#include "app_sensor.h" #include "i_sensor_client.h" +#include "i_sensor_status_callback.h" #include "sensor_basic_data_channel.h" #include "sensor.h" @@ -39,6 +41,10 @@ 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 ErrCode GetAppSensorList(int32_t pid, std::vector &appSensorList) = 0; + virtual ErrCode RegisterCallback(sptr callback) = 0; enum { ENABLE_SENSOR = 0, DISABLE_SENSOR, @@ -47,6 +53,10 @@ public: GET_SENSOR_LIST, TRANSFER_DATA_CHANNEL, DESTROY_SENSOR_CHANNEL, + SUSPEND_SENSORS, + RESUME_SENSORS, + GET_APP_SENSOR_LIST, + REGISTER_CALLBACK, }; }; } // namespace Sensors diff --git a/frameworks/native/sensor/include/i_sensor_status_callback.h b/frameworks/native/sensor/include/i_sensor_status_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..98d40bec7bb7a12dd2a6f5045f2dfa79aaa7b6d0 --- /dev/null +++ b/frameworks/native/sensor/include/i_sensor_status_callback.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 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_STATUS_CALLBACK_H +#define I_SENSOR_STATUS_CALLBACK_H + +#include "iremote_broker.h" + +#include "sensor_agent_type.h" + +namespace OHOS { +namespace Sensors { +class ISensorStatusCallback : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.sensors.ISensorStatusCallback"); + virtual void OnSensorChanged(const AppSensorInfo &appSensorInfo) = 0; + enum : uint32_t { + SENSOR_CHANGE = 0, + }; +}; +} // namespace Sensors +} // namespace OHOS +#endif // I_SENSOR_STATUS_CALLBACK_H diff --git a/frameworks/native/sensor/include/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index a14ea60d025a305583ed80e6abbfe291b4e07a40..f799fb71e508f322c8e82eb2aae6e417a63dbdc9 100644 --- a/frameworks/native/sensor/include/sensor_agent_proxy.h +++ b/frameworks/native/sensor/include/sensor_agent_proxy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -21,6 +21,7 @@ #include "refbase.h" +#include "i_sensor_status_callback.h" #include "sensor_agent_type.h" #include "sensor_data_channel.h" @@ -43,6 +44,10 @@ 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(int32_t pid, 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 ce45bad04dff32dcdd559d2f02cf4d3f34e490e8..414ca74a367a7328f139b3e94a60663a9f2374ee 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -22,6 +22,8 @@ #include "iservice_registry.h" #include "singleton.h" +#include "app_sensor.h" +#include "i_sensor_status_callback.h" #include "sensor_agent_type.h" #include "sensor_basic_data_channel.h" #include "sensor_basic_info.h" @@ -42,6 +44,10 @@ public: int32_t DestroyDataChannel(); void ProcessDeathObserver(const wptr &object); bool IsValid(int32_t sensorId); + int32_t SuspendSensors(int32_t pid); + int32_t ResumeSensors(int32_t pid); + int32_t GetAppSensorList(int32_t pid, std::vector &appSensorList); + 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 01bfde3eadfbf5e818a10c2a0fcec2ebe8c2ceff..ed9a90f32055aec110a927803e0c1c2914b987d3 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -35,6 +35,10 @@ 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; + ErrCode GetAppSensorList(int32_t pid, std::vector &appSensorList) 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 21c4671577bdce7b748f5c046c2fa7520d1340bf..e373ea0804ae55f25a7af85fb6203e8193578650 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -38,6 +38,8 @@ std::recursive_mutex SensorAgentProxy::subscribeMutex_; std::mutex SensorAgentProxy::chanelMutex_; std::mutex sensorInfoMutex_; SensorInfo *sensorInfos_ = nullptr; +std::mutex appSensorInfoMutex_; +AppSensorInfo *appSensorInfo_ = nullptr; int32_t sensorInfoCount_ = 0; std::map SensorAgentProxy::g_subscribeMap; std::map SensorAgentProxy::g_unsubscribeMap; @@ -266,6 +268,10 @@ int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int3 void SensorAgentProxy::ClearSensorInfos() const { + if (appSensorInfo_ != nullptr) { + free(appSensorInfo_); + appSensorInfo_ = nullptr; + } CHKPV(sensorInfos_); free(sensorInfos_); sensorInfos_ = nullptr; @@ -330,5 +336,88 @@ 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(int32_t pid, AppSensorInfo **appSensorInfos, int32_t *count) const +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("Pid is invalid, %{public}d", pid); + return PARAMETER_ERROR; + } + CHKPR(appSensorInfos, OHOS::Sensors::ERROR); + CHKPR(count, OHOS::Sensors::ERROR); + std::lock_guard appSensorInfoLock(appSensorInfoMutex_); + if (appSensorInfo_ != nullptr) { + free(appSensorInfo_); + appSensorInfo_ = nullptr; + } + std::vector appSensorList; + int32_t ret = SenClient.GetAppSensorList(pid, appSensorList); + if (ret != 0) { + SEN_HILOGE("Get app sensor list failed, ret:%{public}d", ret); + return ERROR; + } + if (appSensorList.empty()) { + SEN_HILOGE("App sensor list is empty, pid:%{public}d", pid); + return ERROR; + } + size_t appSensorInfoCount = appSensorList.size(); + if (appSensorInfoCount > MAX_SENSOR_LIST_SIZE) { + SEN_HILOGE("The number of app sensors exceeds the maximum value, count:%{public}d", appSensorInfoCount); + return ERROR; + } + appSensorInfo_ = (AppSensorInfo *)malloc(sizeof(AppSensorInfo) * appSensorInfoCount); + CHKPR(appSensorInfo_, ERROR); + for (size_t i = 0; i < appSensorInfoCount; ++i) { + AppSensorInfo *curAppSensor = appSensorInfo_ + i; + curAppSensor->pid = appSensorList[i].pid; + curAppSensor->sensorId = appSensorList[i].sensorId; + curAppSensor->isActive = appSensorList[i].isActive; + curAppSensor->samplingPeriodNs = appSensorList[i].samplingPeriodNs; + curAppSensor->maxReportDelayNs = appSensorList[i].maxReportDelayNs; + } + *appSensorInfos = appSensorInfo_; + *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_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index 419f0e14def90aab8098fc6931c3420e3d2bcd50..02865dfb595d4d047f89827b11adea159db6c964 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -224,5 +224,64 @@ void SensorServiceClient::DeleteSensorInfoItem(int32_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; +} +int32_t SensorServiceClient::GetAppSensorList(int32_t pid, std::vector &appSensorList) +{ + 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, "GetAppSensorList"); + ret = sensorServer_->GetAppSensorList(pid, appSensorList); + FinishTrace(HITRACE_TAG_SENSORS); + return ret; +} + +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 5ed9c4b2f3aeca04e6490e91f7cb97a1c91080ae..8c6a46f228fc7f84d1b95b4876da0d98ec9eafe9 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -190,5 +190,116 @@ ErrCode SensorServiceProxy::DestroySensorChannel(sptr sensorClien } return static_cast(ret); } + +ErrCode SensorServiceProxy::SuspendSensors(int32_t pid) +{ + MessageParcel data; + 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); + MessageParcel reply; + MessageOption option; + 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; + 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); + MessageParcel reply; + MessageOption option; + 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); +} + +ErrCode SensorServiceProxy::GetAppSensorList(int32_t pid, std::vector &appSensorList) +{ + MessageParcel data; + 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); + MessageParcel reply; + MessageOption option; + 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 static_cast(ret); + } + int32_t appSensorCount; + if (!reply.ReadInt32(appSensorCount)) { + SEN_HILOGE("Parcel read appSensorCount failed"); + return READ_MSG_ERR; + } + AppSensor appSensor; + for (int32_t i = 0; i < appSensorCount; ++i) { + auto tmpAppSensor = appSensor.Unmarshalling(reply); + if (tmpAppSensor == nullptr) { + SEN_HILOGE("Current appSensor is nullptr, i:%{public}d", i); + continue; + } + appSensorList.push_back(*tmpAppSensor); + } + return static_cast(ret); +} + +ErrCode SensorServiceProxy::RegisterCallback(sptr callback) +{ + MessageParcel data; + 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); + MessageParcel reply; + MessageOption option; + 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/frameworks/native/sensor/src/sensor_status_callback_stub.cpp b/frameworks/native/sensor/src/sensor_status_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..663a375b5b447daddc74f762a1b43c04ba091d14 --- /dev/null +++ b/frameworks/native/sensor/src/sensor_status_callback_stub.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 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_status_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, "SensorStatusCallbackStub" }; +} // namespace + +int32_t SensorStatusCallbackStub::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 = SensorStatusCallbackStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + SEN_HILOGE("SensorStatusCallbackStub::OnRemoteRequest failed, descriptor mismatch"); + return OBJECT_NULL; + } + if (code != ISensorStatusCallback::SENSOR_CHANGE) { + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + int32_t pid; + int32_t sensorId; + bool isActive; + int64_t samplingPeriodNs; + int64_t maxReportDelayNs; + if (!(data.ReadInt32(pid) && data.ReadInt32(sensorId) && data.ReadBool(isActive) && + data.ReadInt64(samplingPeriodNs) && data.ReadInt64(maxReportDelayNs))) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + AppSensorInfo appSensorInfo; + appSensorInfo.pid = pid; + appSensorInfo.sensorId = sensorId; + appSensorInfo.isActive = isActive; + appSensorInfo.samplingPeriodNs = samplingPeriodNs; + appSensorInfo.maxReportDelayNs = maxReportDelayNs; + OnSensorChanged(appSensorInfo); + return NO_ERROR; +} +} // namespace Sensors +} // namespace OHOS