1bae4d13cSopenharmony_ci/* 2bae4d13cSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3bae4d13cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bae4d13cSopenharmony_ci * you may not use this file except in compliance with the License. 5bae4d13cSopenharmony_ci * You may obtain a copy of the License at 6bae4d13cSopenharmony_ci * 7bae4d13cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bae4d13cSopenharmony_ci * 9bae4d13cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bae4d13cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bae4d13cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bae4d13cSopenharmony_ci * See the License for the specific language governing permissions and 13bae4d13cSopenharmony_ci * limitations under the License. 14bae4d13cSopenharmony_ci */ 15bae4d13cSopenharmony_ci 16bae4d13cSopenharmony_ci#include "sensor_manager.h" 17bae4d13cSopenharmony_ci 18bae4d13cSopenharmony_ci#include <cinttypes> 19bae4d13cSopenharmony_ci 20bae4d13cSopenharmony_ci#include "iservice_registry.h" 21bae4d13cSopenharmony_ci 22bae4d13cSopenharmony_ci#include "sensor.h" 23bae4d13cSopenharmony_ci#include "sensor_data_event.h" 24bae4d13cSopenharmony_ci#include "sensor_errors.h" 25bae4d13cSopenharmony_ci 26bae4d13cSopenharmony_ci#undef LOG_TAG 27bae4d13cSopenharmony_ci#define LOG_TAG "SensorManager" 28bae4d13cSopenharmony_ci 29bae4d13cSopenharmony_cinamespace OHOS { 30bae4d13cSopenharmony_cinamespace Sensors { 31bae4d13cSopenharmony_ciusing namespace OHOS::HiviewDFX; 32bae4d13cSopenharmony_cinamespace { 33bae4d13cSopenharmony_ci#ifdef HDF_DRIVERS_INTERFACE_SENSOR 34bae4d13cSopenharmony_ciconstexpr int32_t INVALID_SENSOR_ID = -1; 35bae4d13cSopenharmony_ci#endif // HDF_DRIVERS_INTERFACE_SENSOR 36bae4d13cSopenharmony_ciconstexpr uint32_t PROXIMITY_SENSOR_ID = 50331904; 37bae4d13cSopenharmony_ciconstexpr float PROXIMITY_FAR = 5.0; 38bae4d13cSopenharmony_ci} // namespace 39bae4d13cSopenharmony_ci 40bae4d13cSopenharmony_ci#ifdef HDF_DRIVERS_INTERFACE_SENSOR 41bae4d13cSopenharmony_civoid SensorManager::InitSensorMap(const std::unordered_map<int32_t, Sensor> &sensorMap, 42bae4d13cSopenharmony_ci sptr<SensorDataProcesser> dataProcesser, sptr<ReportDataCallback> dataCallback) 43bae4d13cSopenharmony_ci{ 44bae4d13cSopenharmony_ci std::lock_guard<std::mutex> sensorLock(sensorMapMutex_); 45bae4d13cSopenharmony_ci sensorMap_.insert(sensorMap.begin(), sensorMap.end()); 46bae4d13cSopenharmony_ci sensorDataProcesser_ = dataProcesser; 47bae4d13cSopenharmony_ci reportDataCallback_ = dataCallback; 48bae4d13cSopenharmony_ci SEN_HILOGD("Begin sensorMap_.size:%{public}zu", sensorMap_.size()); 49bae4d13cSopenharmony_ci} 50bae4d13cSopenharmony_ci 51bae4d13cSopenharmony_cibool SensorManager::SetBestSensorParams(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) 52bae4d13cSopenharmony_ci{ 53bae4d13cSopenharmony_ci CALL_LOG_ENTER; 54bae4d13cSopenharmony_ci if (sensorId == INVALID_SENSOR_ID) { 55bae4d13cSopenharmony_ci SEN_HILOGE("sensorId is invalid"); 56bae4d13cSopenharmony_ci return false; 57bae4d13cSopenharmony_ci } 58bae4d13cSopenharmony_ci SensorBasicInfo sensorInfo = clientInfo_.GetBestSensorInfo(sensorId); 59bae4d13cSopenharmony_ci int64_t bestSamplingPeriodNs = sensorInfo.GetSamplingPeriodNs(); 60bae4d13cSopenharmony_ci int64_t bestReportDelayNs = sensorInfo.GetMaxReportDelayNs(); 61bae4d13cSopenharmony_ci if ((samplingPeriodNs > bestSamplingPeriodNs) && (maxReportDelayNs > bestReportDelayNs)) { 62bae4d13cSopenharmony_ci SEN_HILOGD("No need to reset sensor params"); 63bae4d13cSopenharmony_ci return true; 64bae4d13cSopenharmony_ci } 65bae4d13cSopenharmony_ci bestSamplingPeriodNs = (samplingPeriodNs < bestSamplingPeriodNs) ? samplingPeriodNs : bestSamplingPeriodNs; 66bae4d13cSopenharmony_ci bestReportDelayNs = (maxReportDelayNs < bestReportDelayNs) ? maxReportDelayNs : bestReportDelayNs; 67bae4d13cSopenharmony_ci SEN_HILOGD("bestSamplingPeriodNs : %{public}" PRId64, bestSamplingPeriodNs); 68bae4d13cSopenharmony_ci auto ret = sensorHdiConnection_.SetBatch(sensorId, bestSamplingPeriodNs, bestReportDelayNs); 69bae4d13cSopenharmony_ci if (ret != ERR_OK) { 70bae4d13cSopenharmony_ci SEN_HILOGE("SetBatch is failed"); 71bae4d13cSopenharmony_ci return false; 72bae4d13cSopenharmony_ci } 73bae4d13cSopenharmony_ci return true; 74bae4d13cSopenharmony_ci} 75bae4d13cSopenharmony_ci 76bae4d13cSopenharmony_cibool SensorManager::ResetBestSensorParams(int32_t sensorId) 77bae4d13cSopenharmony_ci{ 78bae4d13cSopenharmony_ci CALL_LOG_ENTER; 79bae4d13cSopenharmony_ci if (sensorId == INVALID_SENSOR_ID) { 80bae4d13cSopenharmony_ci SEN_HILOGE("sensorId is invalid"); 81bae4d13cSopenharmony_ci return false; 82bae4d13cSopenharmony_ci } 83bae4d13cSopenharmony_ci SensorBasicInfo sensorInfo = clientInfo_.GetBestSensorInfo(sensorId); 84bae4d13cSopenharmony_ci auto ret = sensorHdiConnection_.SetBatch(sensorId, 85bae4d13cSopenharmony_ci sensorInfo.GetSamplingPeriodNs(), sensorInfo.GetMaxReportDelayNs()); 86bae4d13cSopenharmony_ci if (ret != ERR_OK) { 87bae4d13cSopenharmony_ci SEN_HILOGE("SetBatch is failed"); 88bae4d13cSopenharmony_ci return false; 89bae4d13cSopenharmony_ci } 90bae4d13cSopenharmony_ci return true; 91bae4d13cSopenharmony_ci} 92bae4d13cSopenharmony_ci 93bae4d13cSopenharmony_civoid SensorManager::StartDataReportThread() 94bae4d13cSopenharmony_ci{ 95bae4d13cSopenharmony_ci CALL_LOG_ENTER; 96bae4d13cSopenharmony_ci if (!dataThread_.joinable()) { 97bae4d13cSopenharmony_ci SEN_HILOGW("dataThread_ started"); 98bae4d13cSopenharmony_ci std::thread dataProcessThread(SensorDataProcesser::DataThread, sensorDataProcesser_, reportDataCallback_); 99bae4d13cSopenharmony_ci dataThread_ = std::move(dataProcessThread); 100bae4d13cSopenharmony_ci } 101bae4d13cSopenharmony_ci} 102bae4d13cSopenharmony_ci#else 103bae4d13cSopenharmony_civoid SensorManager::InitSensorMap(const std::unordered_map<int32_t, Sensor> &sensorMap) 104bae4d13cSopenharmony_ci{ 105bae4d13cSopenharmony_ci std::lock_guard<std::mutex> sensorLock(sensorMapMutex_); 106bae4d13cSopenharmony_ci sensorMap_ = sensorMap; 107bae4d13cSopenharmony_ci SEN_HILOGD("Begin sensorMap_.size:%{public}zu", sensorMap_.size()); 108bae4d13cSopenharmony_ci} 109bae4d13cSopenharmony_ci#endif // HDF_DRIVERS_INTERFACE_SENSOR 110bae4d13cSopenharmony_ci 111bae4d13cSopenharmony_cibool SensorManager::SaveSubscriber(int32_t sensorId, uint32_t pid, int64_t samplingPeriodNs, 112bae4d13cSopenharmony_ci int64_t maxReportDelayNs) 113bae4d13cSopenharmony_ci{ 114bae4d13cSopenharmony_ci SensorBasicInfo sensorInfo = GetSensorInfo(sensorId, samplingPeriodNs, maxReportDelayNs); 115bae4d13cSopenharmony_ci if (!clientInfo_.UpdateSensorInfo(sensorId, pid, sensorInfo)) { 116bae4d13cSopenharmony_ci SEN_HILOGE("UpdateSensorInfo is failed"); 117bae4d13cSopenharmony_ci return false; 118bae4d13cSopenharmony_ci } 119bae4d13cSopenharmony_ci return true; 120bae4d13cSopenharmony_ci} 121bae4d13cSopenharmony_ci 122bae4d13cSopenharmony_ciSensorBasicInfo SensorManager::GetSensorInfo(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) 123bae4d13cSopenharmony_ci{ 124bae4d13cSopenharmony_ci CALL_LOG_ENTER; 125bae4d13cSopenharmony_ci SensorBasicInfo sensorInfo; 126bae4d13cSopenharmony_ci std::lock_guard<std::mutex> sensorMapLock(sensorMapMutex_); 127bae4d13cSopenharmony_ci auto it = sensorMap_.find(sensorId); 128bae4d13cSopenharmony_ci if (it == sensorMap_.end()) { 129bae4d13cSopenharmony_ci sensorInfo.SetSamplingPeriodNs(samplingPeriodNs); 130bae4d13cSopenharmony_ci sensorInfo.SetMaxReportDelayNs(maxReportDelayNs); 131bae4d13cSopenharmony_ci sensorInfo.SetSensorState(true); 132bae4d13cSopenharmony_ci SEN_HILOGE("sensorId is invalid"); 133bae4d13cSopenharmony_ci return sensorInfo; 134bae4d13cSopenharmony_ci } 135bae4d13cSopenharmony_ci int64_t curSamplingPeriodNs = 136bae4d13cSopenharmony_ci (samplingPeriodNs < it->second.GetMinSamplePeriodNs()) ? it->second.GetMinSamplePeriodNs() : samplingPeriodNs; 137bae4d13cSopenharmony_ci int32_t maxEventCount = it->second.GetFifoMaxEventCount(); 138bae4d13cSopenharmony_ci if ((samplingPeriodNs == 0) || (maxEventCount > (INT64_MAX / samplingPeriodNs))) { 139bae4d13cSopenharmony_ci SEN_HILOGE("Failed, samplingPeriodNs overflow"); 140bae4d13cSopenharmony_ci return sensorInfo; 141bae4d13cSopenharmony_ci } 142bae4d13cSopenharmony_ci int64_t supportDelay = samplingPeriodNs * maxEventCount; 143bae4d13cSopenharmony_ci int64_t curReportDelayNs = (maxReportDelayNs > supportDelay) ? supportDelay : maxReportDelayNs; 144bae4d13cSopenharmony_ci sensorInfo.SetSamplingPeriodNs(curSamplingPeriodNs); 145bae4d13cSopenharmony_ci sensorInfo.SetMaxReportDelayNs(curReportDelayNs); 146bae4d13cSopenharmony_ci sensorInfo.SetSensorState(true); 147bae4d13cSopenharmony_ci return sensorInfo; 148bae4d13cSopenharmony_ci} 149bae4d13cSopenharmony_ci 150bae4d13cSopenharmony_cibool SensorManager::IsOtherClientUsingSensor(int32_t sensorId, int32_t clientPid) 151bae4d13cSopenharmony_ci{ 152bae4d13cSopenharmony_ci CALL_LOG_ENTER; 153bae4d13cSopenharmony_ci if (clientInfo_.OnlyCurPidSensorEnabled(sensorId, clientPid)) { 154bae4d13cSopenharmony_ci SEN_HILOGD("Only current client using this sensor"); 155bae4d13cSopenharmony_ci return false; 156bae4d13cSopenharmony_ci } 157bae4d13cSopenharmony_ci clientInfo_.ClearCurPidSensorInfo(sensorId, clientPid); 158bae4d13cSopenharmony_ci#ifdef HDF_DRIVERS_INTERFACE_SENSOR 159bae4d13cSopenharmony_ci if (!ResetBestSensorParams(sensorId)) { 160bae4d13cSopenharmony_ci SEN_HILOGW("ResetBestSensorParams is failed"); 161bae4d13cSopenharmony_ci } 162bae4d13cSopenharmony_ci#endif // HDF_DRIVERS_INTERFACE_SENSOR 163bae4d13cSopenharmony_ci SEN_HILOGD("Other client is using this sensor"); 164bae4d13cSopenharmony_ci return true; 165bae4d13cSopenharmony_ci} 166bae4d13cSopenharmony_ci 167bae4d13cSopenharmony_ciErrCode SensorManager::AfterDisableSensor(int32_t sensorId) 168bae4d13cSopenharmony_ci{ 169bae4d13cSopenharmony_ci CALL_LOG_ENTER; 170bae4d13cSopenharmony_ci clientInfo_.ClearSensorInfo(sensorId); 171bae4d13cSopenharmony_ci if (sensorId == PROXIMITY_SENSOR_ID) { 172bae4d13cSopenharmony_ci SensorData sensorData; 173bae4d13cSopenharmony_ci auto ret = clientInfo_.GetStoreEvent(sensorId, sensorData); 174bae4d13cSopenharmony_ci if (ret == ERR_OK) { 175bae4d13cSopenharmony_ci SEN_HILOGD("Change the default state is far"); 176bae4d13cSopenharmony_ci sensorData.data[0] = PROXIMITY_FAR; 177bae4d13cSopenharmony_ci clientInfo_.StoreEvent(sensorData); 178bae4d13cSopenharmony_ci } 179bae4d13cSopenharmony_ci } 180bae4d13cSopenharmony_ci return ERR_OK; 181bae4d13cSopenharmony_ci} 182bae4d13cSopenharmony_ci 183bae4d13cSopenharmony_civoid SensorManager::GetPackageName(AccessTokenID tokenId, std::string &packageName) 184bae4d13cSopenharmony_ci{ 185bae4d13cSopenharmony_ci CALL_LOG_ENTER; 186bae4d13cSopenharmony_ci int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId); 187bae4d13cSopenharmony_ci switch (tokenType) { 188bae4d13cSopenharmony_ci case ATokenTypeEnum::TOKEN_HAP: { 189bae4d13cSopenharmony_ci HapTokenInfo hapInfo; 190bae4d13cSopenharmony_ci if (AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != 0) { 191bae4d13cSopenharmony_ci SEN_HILOGE("Get hap token info fail"); 192bae4d13cSopenharmony_ci return; 193bae4d13cSopenharmony_ci } 194bae4d13cSopenharmony_ci packageName = hapInfo.bundleName; 195bae4d13cSopenharmony_ci break; 196bae4d13cSopenharmony_ci } 197bae4d13cSopenharmony_ci case ATokenTypeEnum::TOKEN_NATIVE: 198bae4d13cSopenharmony_ci case ATokenTypeEnum::TOKEN_SHELL: { 199bae4d13cSopenharmony_ci NativeTokenInfo tokenInfo; 200bae4d13cSopenharmony_ci if (AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) { 201bae4d13cSopenharmony_ci SEN_HILOGE("Get native token info fail"); 202bae4d13cSopenharmony_ci return; 203bae4d13cSopenharmony_ci } 204bae4d13cSopenharmony_ci packageName = tokenInfo.processName; 205bae4d13cSopenharmony_ci break; 206bae4d13cSopenharmony_ci } 207bae4d13cSopenharmony_ci default: { 208bae4d13cSopenharmony_ci SEN_HILOGW("Token type not match"); 209bae4d13cSopenharmony_ci break; 210bae4d13cSopenharmony_ci } 211bae4d13cSopenharmony_ci } 212bae4d13cSopenharmony_ci} 213bae4d13cSopenharmony_ci} // namespace Sensors 214bae4d13cSopenharmony_ci} // namespace OHOS 215