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