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