diff --git a/frameworks/native/sensor/BUILD.gn b/frameworks/native/sensor/BUILD.gn deleted file mode 100755 index 9f9c6c3f30563805223a86e625fbc60b0b62ed08..0000000000000000000000000000000000000000 --- a/frameworks/native/sensor/BUILD.gn +++ /dev/null @@ -1,56 +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", - ] - - part_name = "sensor" - subsystem_name = "sensors" -} - -############################################## -group("sensor_native_target") { - deps = [ ":libsensor_native" ] -} 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 0000000000000000000000000000000000000000..0926f6c8b1f6aac9aa5a85e9d7e33836150564bf --- /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 e4400ba5a5e14f6e8ec31349bc7a29174256f6f5..025c2d29d303fe7aedb719d0cd1f99498ed696bc 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.h @@ -22,8 +22,10 @@ #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" namespace OHOS { namespace Sensors { @@ -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 std::vector GetAppSensorList() = 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/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index a14ea60d025a305583ed80e6abbfe291b4e07a40..da0b33194c57f9d56faef2eb71ef607f3bb2707e 100644 --- a/frameworks/native/sensor/include/sensor_agent_proxy.h +++ b/frameworks/native/sensor/include/sensor_agent_proxy.h @@ -18,9 +18,11 @@ #include #include +#include #include "refbase.h" +#include "i_sensor_callback.h" #include "sensor_agent_type.h" #include "sensor_data_channel.h" @@ -43,6 +45,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(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 40b4d78d66953a468558b0332964bcdde03366d6..b6176d22f88e2654ee67858bbff78f823ef2522c 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -22,12 +22,14 @@ #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" #include "sensor_client_stub.h" #include "sensor_data_channel.h" #include "sensor.h" +#include "app_sensor.h" #include "sensor_service_proxy.h" @@ -42,6 +44,10 @@ 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(); + 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 70a43ecaa55676cd114765aadbbdd5fe2695abbe..36f6f2b8f717ad4505f6268b2a766faf048a0d4c 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.h @@ -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; + 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 deleted file mode 100644 index 3791a1b9299ad2a859ae2e2e01bed1a44373c0fb..0000000000000000000000000000000000000000 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ /dev/null @@ -1,335 +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; -} -} // 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 0000000000000000000000000000000000000000..14b745e71f089c7ce850a14f7a0efdef4e62acbb --- /dev/null +++ b/frameworks/native/sensor/src/sensor_callback_stub.cpp @@ -0,0 +1,67 @@ +/* + * 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 8effd56ea010d8ba32fdeb7fea45d464522a2044..25892e16c322d4e6da11eed944e08a784125aefd 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -224,5 +224,68 @@ 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; +} + +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 3a6cb1eaeb24f3eb769aa24618c090effdf2d150..b3f0a05191a7e19c8b3a2b9518edafba65f90519 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -190,5 +190,116 @@ 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; +} + +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 1050549d522b9148360c3a4b4ac6a2f606a5fb52..96608d72ac0a6fc5067f8bbe6307081ee4f8e532 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" ] @@ -57,6 +58,8 @@ ohos_shared_library("sensor_interface_native") { external_deps = [ "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 ead362c940d06fbf0c0bbac8ae409765c13a1933..b528355ddb3ac36b70ec7ef8b06700bb45137c20 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 @@ -128,6 +129,14 @@ 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); + +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 f780e3e5e818aac4462df839dd6b713ac98a19c2..b04a5afe4886457e895be1a49b64fbc3d221f4cb 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/include/sensor_callback_stub.h b/interfaces/native/include/sensor_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..501f417fba9b84fb302555fa3b583877d3dd1f20 --- /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 ccae1ff7ad4a70a9a3263e0613f6de74f845fc1e..48a5b3dc7e212b0d0d0cf957b4701327b67b1485 100755 --- a/interfaces/native/src/sensor_agent.cpp +++ b/interfaces/native/src/sensor_agent.cpp @@ -146,4 +146,64 @@ 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; +} + +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/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index 2f31742fc750022f42280a550c2965b806a5de01..bd31c6c27503b5e26b6eb517369e89079da29879 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,53 @@ 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(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(1534); + sleep(5); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorAgentTest, GetAppSensorsTest_002, TestSize.Level1) +{ + SEN_HILOGI("GetAppSensorsTest_002 in"); + AppSensorInfo *appSensorInfos {nullptr}; + int32_t count { 0 }; + int32_t ret = GetAppSensors(&appSensorInfos, &count); + SEN_HILOGI("Test_002 count:%{public}d", count); + if (appSensorInfos != nullptr) { + SEN_HILOGI("Test_002 free memory"); + 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 d84452593cb927399ef09017acafd3ecc0080477..7c2e3ff506a77ab07d150c50a0907bc026d523a0 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\"", @@ -39,6 +40,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 3f15192b0a36576f2a526a7d18741881b8dcee17..b20f31748cf8857c2fa98de89dc5eab0ef2088f4 100644 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -24,11 +24,13 @@ 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", "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 e479713829c49939c2f749fc95fc723f935757aa..e70ee18d8a0db69dff2170db25c7a14220f92282 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_callback_proxy.h b/services/sensor/include/sensor_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..da8b61610ce23d54ebf4fe5529543479a160581f --- /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 b5de13a84afa6ac5754dfd987a74262ff27d02ea..647ac219feb260e040288e9b0edc6d96f76635e7 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" @@ -54,6 +55,10 @@ 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; + 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 9259e793f11101593e9d19465052a4a08266169e..e0049d27a6190b691f943bb4001adacab8979e22 100644 --- a/services/sensor/include/sensor_service_stub.h +++ b/services/sensor/include/sensor_service_stub.h @@ -39,6 +39,10 @@ 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); + 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 new file mode 100755 index 0000000000000000000000000000000000000000..5291a3a499f0ea232dbb5ea1e51acfec9697118c --- /dev/null +++ b/services/sensor/include/sensor_suspend_policy.h @@ -0,0 +1,64 @@ +/* + * 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 +#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" + +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); + 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); + 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::set> callbackSet_; +}; +#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 f0374d7d9dcda411256e59771ab1eb7cc2c84d65..ada3e38b34bf8880952a4e2d717dc34d5bf23e4f 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_callback_proxy.cpp b/services/sensor/src/sensor_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..279b45891ad9f177b38df4fab9c498dd7cc58cda --- /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 fd38c139ec561d6be723387e8ff8ea9db161ca24..27eed8f9c877fadcdb0fd439007a53c97432230b 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" @@ -248,6 +249,10 @@ 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; } @@ -325,7 +330,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 +408,47 @@ 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 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 4696378b46e849387fe48f8dfdfada40d3549b63..df6243e104cf656c83f5d7fd61b284af7fb71074 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,10 @@ 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; + baseFuncs_[REGISTER_CALLBACK] = &SensorServiceStub::RegisterCallbackInner; } SensorServiceStub::~SensorServiceStub() @@ -153,5 +158,79 @@ 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; +} + +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 1e9f2444062ab558b0b26e8d8a023caf417cc843..dd705904ec96459667f445006dd52dc26d2f91fe 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 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); + 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); - } + 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); + 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,89 @@ 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, ret: %{public}d", ret); + clientInfo_.RemoveSubscriber(sensorId, pid); + return ENABLE_SENSOR_ERR; + } + return ERR_OK; +} + +ErrCode SensorSuspendPolicy::RestoreSensorInfo(uint32_t sensorId, int32_t pid, int64_t samplingPeriodNs, + int64_t maxReportDelayNs) +{ + auto ret = sensorManager_.SaveSubscriber(sensorId, pid, samplingPeriodNs, maxReportDelayNs); if (ret != ERR_OK) { - SEN_HILOGE("EnableSensor is failed"); + 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; } return ret; } -std::vector SensorSuspendPolicy::GetSensorIdByPid(int32_t pid) +std::vector SensorSuspendPolicy::GetAppSensorList() { - 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 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_); + callbackSet_.emplace(callback); + return ERR_OK; } -void SensorSuspendPolicy::DoActive(const std::shared_ptr &info) +void SensorSuspendPolicy::ExecuteCallbackAsync(AppThreadInfo &appThreadInfo, uint32_t sensorId, + 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); - } + AppSensorInfo appSensorInfo; + appSensorInfo.appThreadInfo = appThreadInfo; + appSensorInfo.sensorId = sensorId; + appSensorInfo.samplingPeriodNs = samplingPeriodNs; + appSensorInfo.maxReportDelayNs = maxReportDelayNs; + std::lock_guard callbackLock(callbackMutex_); + for (auto it = callbackSet_.begin(); it != callbackSet_.end(); ++it) { + auto callback = *it; + if (callback != nullptr) { + callback->OnSensorChanged(appSensorInfo); } } - pidSensorIdMap_.clear(); - sensorIdInfoMap_.clear(); } } // namespace Sensors } // namespace OHOS diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 7a971bb7473fb530b345c66e18a97912ea76e55d..b976cc5b7329b737c71e5710201aa801281ee146 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 0000000000000000000000000000000000000000..e4e3c008675ae93b5a8e6d597228315752a320bb --- /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