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/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 21c4671577bdce7b748f5c046c2fa7520d1340bf..4666392ba7929a66b376dfe8fab8e80045a976e6 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 @@ -330,5 +330,25 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) *count = sensorInfoCount_; return SUCCESS; } + +int32_t SensorAgentProxy::SuspendSensors(int32_t pid) const +{ + return SUCCESS; +} + +int32_t SensorAgentProxy::ResumeSensors(int32_t pid) const +{ + return SUCCESS; +} + +int32_t SensorAgentProxy::GetAppSensors(int32_t pid, AppSensorInfo **appSensorInfos, int32_t *count) const +{ + return SUCCESS; +} + +int32_t SensorAgentProxy::RegisterCallback(sptr callback) const +{ + return SUCCESS; +} } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn index 1050549d522b9148360c3a4b4ac6a2f606a5fb52..d475b35dda5dee140f16b2052ce1605408922e17 100644 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -1,4 +1,4 @@ -# 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 @@ -44,6 +44,7 @@ ohos_shared_library("sensor_interface_native") { "src/geomagnetic_field.cpp", "src/sensor_agent.cpp", "src/sensor_algorithm.cpp", + "src/sensor_status_callback_stub.cpp", ] configs = [ ":sensor_private_config" ] @@ -57,6 +58,7 @@ ohos_shared_library("sensor_interface_native") { external_deps = [ "c_utils:utils", "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", ] part_name = "sensor" subsystem_name = "sensors" diff --git a/interfaces/native/include/i_sensor_status_callback.h b/interfaces/native/include/i_sensor_status_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..c61337b7e66b0f5be491f8ad1a48c69562dd6a92 --- /dev/null +++ b/interfaces/native/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 + +#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/interfaces/native/include/sensor_agent.h b/interfaces/native/include/sensor_agent.h index ead362c940d06fbf0c0bbac8ae409765c13a1933..c2688da2f97c420c72e69dac971ed63719f9728a 100755 --- a/interfaces/native/include/sensor_agent.h +++ b/interfaces/native/include/sensor_agent.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 @@ -38,6 +38,7 @@ #ifndef SENSOR_AGENT_H #define SENSOR_AGENT_H +#include "i_sensor_status_callback.h" #include "sensor_agent_type.h" #ifdef __cplusplus @@ -127,6 +128,44 @@ int32_t DeactivateSensor(int32_t sensorTypeId, const SensorUser *user); * @since 5 */ int32_t SetMode(int32_t sensorTypeId, const SensorUser *user, int32_t mode); +/** + * @brief 休眠一个进程订阅的所有传感器 + * + * @param pid 进程号 + * @return 返回0表示成功,否则表示失败 + * + * @since 10 + */ +int32_t SuspendSensors(int32_t pid); +/** + * @brief 唤醒一个进程订阅的所有传感器 + * + * @param pid 进程号 + * @return 返回0表示成功,否则表示失败 + * + * @since 10 + */ +int32_t ResumeSensors(int32_t pid); +/** + * @brief 查询一个进程订阅的所有传感器的信息 + * + * @param pid 进程号 + * @param appSensorInfos 返回进程订阅的所有传感器信息 + * @param count 返回进程订阅的传感器数量 + * @return 返回0表示成功,否则表示失败 + * + * @since 10 + */ +int32_t GetAppSensors(int32_t pid, AppSensorInfo **appSensorInfos, int32_t *count); +/** + * @brief 订阅传感器变更,当传感器发生变化时上报传感器信息 + * + * @param callback 传感器变更回调对象 + * @return 返回0表示成功,否则表示失败 + * + * @since 10 + */ +int32_t RegisterCallback(OHOS::sptr callback); #ifdef __cplusplus #if __cplusplus diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 37b0de0e57418519152160a8e4307367945e1d8f..8ab20e0b86e8a47fa66825ed7a279a6fdcf4db39 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.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 @@ -395,6 +395,14 @@ typedef struct WearDetectionData { float value; } WearDetectionData; +typedef struct AppSensorInfo { + int32_t pid; /**< PID */ + int32_t sensorId; /**< Sensor ID */ + bool isActive; /**< Whether the process enable the sensor */ + 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/include/sensor_status_callback_stub.h b/interfaces/native/include/sensor_status_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..1619f95cf2d57b01d04184527456cb00a23df8fb --- /dev/null +++ b/interfaces/native/include/sensor_status_callback_stub.h @@ -0,0 +1,36 @@ +/* + * 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 SENSOR_STATUS_CALLBACK_STUB_H +#define SENSOR_STATUS_CALLBACK_STUB_H + +#include "iremote_stub.h" +#include "nocopyable.h" + +#include "i_sensor_status_callback.h" + +namespace OHOS { +namespace Sensors { +class SensorStatusCallbackStub : public IRemoteStub { +public: + DISALLOW_COPY_AND_MOVE(SensorStatusCallbackStub); + SensorStatusCallbackStub() = default; + virtual ~SensorStatusCallbackStub() = 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_STATUS_CALLBACK_STUB_H diff --git a/interfaces/native/src/sensor_agent.cpp b/interfaces/native/src/sensor_agent.cpp index ccae1ff7ad4a70a9a3263e0613f6de74f845fc1e..8cf08cba22e33502d3952b5e2c526f89b2c20013 100755 --- a/interfaces/native/src/sensor_agent.cpp +++ b/interfaces/native/src/sensor_agent.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 @@ -146,4 +146,66 @@ 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(int32_t pid, AppSensorInfo **appSensorInfos, int32_t *count) +{ + CHKPR(appSensorInfos, OHOS::Sensors::ERROR); + CHKPR(count, OHOS::Sensors::ERROR); + const SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + SEN_HILOGE("Proxy is nullptr"); + return SERVICE_EXCEPTION; + } + int32_t ret = proxy->GetAppSensors(pid, appSensorInfos, count); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("GetAppSensors failed, ret:%{public}d", ret); + return NormalizeErrCode(ret); + } + 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/native/src/sensor_status_callback_stub.cpp b/interfaces/native/src/sensor_status_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..663a375b5b447daddc74f762a1b43c04ba091d14 --- /dev/null +++ b/interfaces/native/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