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<int32_t,Sensor> & sensorMap,sptr<SensorDataProcesser> dataProcesser,sptr<ReportDataCallback> dataCallback)35 void SensorManager::InitSensorMap(const std::unordered_map<int32_t, 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(int32_t sensorId,int64_t samplingPeriodNs,int64_t maxReportDelayNs)45 bool SensorManager::SetBestSensorParams(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs)
46 {
47 SEN_HILOGI("In, sensorId:%{public}d", sensorId);
48 if (sensorId == INVALID_SENSOR_ID) {
49 SEN_HILOGE("sensorId is invalid");
50 return false;
51 }
52 SensorBasicInfo sensorInfo = clientInfo_.GetBestSensorInfo(sensorId);
53 int64_t bestSamplingPeriodNs = sensorInfo.GetSamplingPeriodNs();
54 int64_t bestReportDelayNs = sensorInfo.GetMaxReportDelayNs();
55 if ((samplingPeriodNs > bestSamplingPeriodNs) && (maxReportDelayNs > bestReportDelayNs)) {
56 SEN_HILOGD("No need to reset sensor params");
57 return true;
58 }
59 bestSamplingPeriodNs = (samplingPeriodNs < bestSamplingPeriodNs) ? samplingPeriodNs : bestSamplingPeriodNs;
60 bestReportDelayNs = (maxReportDelayNs < bestReportDelayNs) ? maxReportDelayNs : bestReportDelayNs;
61 SEN_HILOGD("bestSamplingPeriodNs : %{public}" PRId64, bestSamplingPeriodNs);
62 auto ret = sensorHdiConnection_.SetBatch(sensorId, bestSamplingPeriodNs, bestReportDelayNs);
63 if (ret != ERR_OK) {
64 SEN_HILOGE("SetBatch is failed");
65 return false;
66 }
67 SEN_HILOGI("Done, sensorId:%{public}d", sensorId);
68 return true;
69 }
70
ResetBestSensorParams(int32_t sensorId)71 bool SensorManager::ResetBestSensorParams(int32_t sensorId)
72 {
73 SEN_HILOGI("In, sensorId:%{public}d", sensorId);
74 if (sensorId == INVALID_SENSOR_ID) {
75 SEN_HILOGE("sensorId is invalid");
76 return false;
77 }
78 SensorBasicInfo sensorInfo = clientInfo_.GetBestSensorInfo(sensorId);
79 auto ret = sensorHdiConnection_.SetBatch(sensorId,
80 sensorInfo.GetSamplingPeriodNs(), sensorInfo.GetMaxReportDelayNs());
81 if (ret != ERR_OK) {
82 SEN_HILOGE("SetBatch is failed");
83 return false;
84 }
85 SEN_HILOGI("Done, sensorId:%{public}d", sensorId);
86 return true;
87 }
88
StartDataReportThread()89 void SensorManager::StartDataReportThread()
90 {
91 CALL_LOG_ENTER;
92 if (!dataThread_.joinable()) {
93 SEN_HILOGW("dataThread_ started");
94 std::thread dataProcessThread(SensorDataProcesser::DataThread, sensorDataProcesser_, reportDataCallback_);
95 dataThread_ = std::move(dataProcessThread);
96 }
97 }
98 #else
InitSensorMap(const std::unordered_map<int32_t,Sensor> & sensorMap)99 void SensorManager::InitSensorMap(const std::unordered_map<int32_t, Sensor> &sensorMap)
100 {
101 std::lock_guard<std::mutex> sensorLock(sensorMapMutex_);
102 sensorMap_ = sensorMap;
103 SEN_HILOGD("Begin sensorMap_.size:%{public}zu", sensorMap_.size());
104 }
105 #endif // HDF_DRIVERS_INTERFACE_SENSOR
106
SaveSubscriber(int32_t sensorId,uint32_t pid,int64_t samplingPeriodNs,int64_t maxReportDelayNs)107 bool SensorManager::SaveSubscriber(int32_t sensorId, uint32_t pid, int64_t samplingPeriodNs,
108 int64_t maxReportDelayNs)
109 {
110 SEN_HILOGI("In, sensorId:%{public}d, pid:%{public}u", sensorId, pid);
111 SensorBasicInfo sensorInfo = GetSensorInfo(sensorId, samplingPeriodNs, maxReportDelayNs);
112 if (!clientInfo_.UpdateSensorInfo(sensorId, pid, sensorInfo)) {
113 SEN_HILOGE("UpdateSensorInfo is failed");
114 return false;
115 }
116 SEN_HILOGI("Done, sensorId:%{public}d, pid:%{public}u", sensorId, pid);
117 return true;
118 }
119
GetSensorInfo(int32_t sensorId,int64_t samplingPeriodNs,int64_t maxReportDelayNs)120 SensorBasicInfo SensorManager::GetSensorInfo(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs)
121 {
122 SEN_HILOGI("In, sensorId:%{public}d", sensorId);
123 SensorBasicInfo sensorInfo;
124 std::lock_guard<std::mutex> sensorMapLock(sensorMapMutex_);
125 auto it = sensorMap_.find(sensorId);
126 if (it == sensorMap_.end()) {
127 sensorInfo.SetSamplingPeriodNs(samplingPeriodNs);
128 sensorInfo.SetMaxReportDelayNs(maxReportDelayNs);
129 sensorInfo.SetSensorState(true);
130 SEN_HILOGE("sensorId is invalid");
131 return sensorInfo;
132 }
133 int64_t curSamplingPeriodNs =
134 (samplingPeriodNs < it->second.GetMinSamplePeriodNs()) ? it->second.GetMinSamplePeriodNs() : samplingPeriodNs;
135 int32_t maxEventCount = it->second.GetFifoMaxEventCount();
136 if ((samplingPeriodNs == 0) || (maxEventCount > (INT64_MAX / samplingPeriodNs))) {
137 SEN_HILOGE("Failed, samplingPeriodNs overflow");
138 return sensorInfo;
139 }
140 int64_t supportDelay = samplingPeriodNs * maxEventCount;
141 int64_t curReportDelayNs = (maxReportDelayNs > supportDelay) ? supportDelay : maxReportDelayNs;
142 sensorInfo.SetSamplingPeriodNs(curSamplingPeriodNs);
143 sensorInfo.SetMaxReportDelayNs(curReportDelayNs);
144 sensorInfo.SetSensorState(true);
145 SEN_HILOGI("Done, sensorId:%{public}d", sensorId);
146 return sensorInfo;
147 }
148
IsOtherClientUsingSensor(int32_t sensorId,int32_t clientPid)149 bool SensorManager::IsOtherClientUsingSensor(int32_t sensorId, int32_t clientPid)
150 {
151 SEN_HILOGI("In, sensorId:%{public}d, clientPid:%{public}d", sensorId, clientPid);
152 if (clientInfo_.OnlyCurPidSensorEnabled(sensorId, clientPid)) {
153 SEN_HILOGD("Only current client using this sensor");
154 return false;
155 }
156 clientInfo_.ClearCurPidSensorInfo(sensorId, clientPid);
157 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
158 if (!ResetBestSensorParams(sensorId)) {
159 SEN_HILOGW("ResetBestSensorParams is failed");
160 }
161 #endif // HDF_DRIVERS_INTERFACE_SENSOR
162 SEN_HILOGD("Other client is using this sensor");
163 SEN_HILOGI("Done, sensorId:%{public}d, clientPid:%{public}d", sensorId, clientPid);
164 return true;
165 }
166
AfterDisableSensor(int32_t sensorId)167 ErrCode SensorManager::AfterDisableSensor(int32_t sensorId)
168 {
169 SEN_HILOGI("In, sensorId:%{public}d", sensorId);
170 clientInfo_.ClearSensorInfo(sensorId);
171 if (sensorId == PROXIMITY_SENSOR_ID) {
172 SensorData sensorData;
173 auto ret = clientInfo_.GetStoreEvent(sensorId, sensorData);
174 if (ret == ERR_OK) {
175 SEN_HILOGD("Change the default state is far");
176 sensorData.data[0] = PROXIMITY_FAR;
177 clientInfo_.StoreEvent(sensorData);
178 }
179 }
180 SEN_HILOGI("Done, sensorId:%{public}d", sensorId);
181 return ERR_OK;
182 }
183
GetPackageName(AccessTokenID tokenId,std::string & packageName,bool isAccessTokenServiceActive)184 void SensorManager::GetPackageName(AccessTokenID tokenId, std::string &packageName, bool isAccessTokenServiceActive)
185 {
186 CALL_LOG_ENTER;
187 if (!isAccessTokenServiceActive) {
188 SEN_HILOGE("Access token service is inactive");
189 return;
190 }
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 NativeTokenInfo tokenInfo;
205 if (AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) {
206 SEN_HILOGE("Get native token info fail");
207 return;
208 }
209 packageName = tokenInfo.processName;
210 break;
211 }
212 default: {
213 SEN_HILOGW("Token type not match");
214 break;
215 }
216 }
217 }
218 } // namespace Sensors
219 } // namespace OHOS
220