1 /*
2 * Copyright (c) 2023 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_uhdf_log.h"
17 #include "sensor_clients_manager.h"
18 #include <cinttypes>
19
20 #define HDF_LOG_TAG uhdf_sensor_clients_manager
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Sensor {
25 namespace V2_0 {
26
27 namespace {
28 const std::vector<int32_t> continuesSensor = {HDF_SENSOR_TYPE_ACCELEROMETER, HDF_SENSOR_TYPE_GYROSCOPE,
29 HDF_SENSOR_TYPE_MAGNETIC_FIELD, HDF_SENSOR_TYPE_SAR,
30 HDF_SENSOR_TYPE_ORIENTATION, HDF_SENSOR_TYPE_GRAVITY,
31 HDF_SENSOR_TYPE_LINEAR_ACCELERATION, HDF_SENSOR_TYPE_ROTATION_VECTOR,
32 HDF_SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
33 HDF_SENSOR_TYPE_GAME_ROTATION_VECTOR,
34 HDF_SENSOR_TYPE_GYROSCOPE_UNCALIBRATED, HDF_SENSOR_TYPE_DROP_DETECT,
35 HDF_SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR,
36 HDF_SENSOR_TYPE_ACCELEROMETER_UNCALIBRATED};
37 constexpr int64_t ERROR_INTERVAL = 0;
38 constexpr int64_t STOP_INTERVAL = 0;
39 constexpr int32_t INIT_CUR_COUNT = 0;
40 }
41
42 SensorClientsManager* SensorClientsManager::instance = nullptr;
43 std::mutex SensorClientsManager::instanceMutex_;
44
SensorClientsManager()45 SensorClientsManager::SensorClientsManager()
46 {
47 }
48
~SensorClientsManager()49 SensorClientsManager::~SensorClientsManager()
50 {
51 clients_.clear();
52 sensorUsed_.clear();
53 sensorConfig_.clear();
54 sdcSensorConfig_.clear();
55 }
56
GetServiceId(int groupId,const sptr<ISensorCallback> & callbackObj)57 int SensorClientsManager::GetServiceId(int groupId, const sptr<ISensorCallback> &callbackObj)
58 {
59 std::unique_lock<std::mutex> lock(clientsMutex_);
60 for (auto &iter : clients_[groupId]) {
61 if (iter.second.GetReportDataCb() == callbackObj) {
62 return iter.first;
63 }
64 }
65 return HDF_FAILURE;
66 }
67
ReportDataCbRegister(int groupId,int serviceId,const sptr<ISensorCallback> & callbackObj)68 void SensorClientsManager::ReportDataCbRegister(int groupId, int serviceId, const sptr<ISensorCallback> &callbackObj)
69 {
70 std::unique_lock<std::mutex> lock(clientsMutex_);
71 if (clients_.find(groupId) == clients_.end() || clients_[groupId].find(serviceId) == clients_[groupId].end()) {
72 if (callbackObj == nullptr) {
73 HDF_LOGE("%{public}s: the callback of service %{public}d is null", __func__, serviceId);
74 return;
75 }
76 clients_[groupId].emplace(serviceId, callbackObj);
77 HDF_LOGD("%{public}s: service %{public}d insert the callback", __func__, serviceId);
78 return;
79 }
80
81 auto it = clients_[groupId].find(serviceId);
82 it -> second.SetReportDataCb(callbackObj);
83 HDF_LOGD("%{public}s: service %{public}d update the callback", __func__, serviceId);
84
85 return;
86 }
87
ReportDataCbUnRegister(int groupId,int serviceId,const sptr<ISensorCallback> & callbackObj)88 void SensorClientsManager::ReportDataCbUnRegister(int groupId, int serviceId, const sptr<ISensorCallback> &callbackObj)
89 {
90 std::unique_lock<std::mutex> lock(clientsMutex_);
91 if (clients_.find(groupId) == clients_.end() || clients_[groupId].find(serviceId) == clients_[groupId].end()) {
92 HDF_LOGD("%{public}s: service %{public}d already UnRegister", __func__, serviceId);
93 return;
94 }
95
96 auto it = clients_[groupId].find(serviceId);
97 clients_[groupId].erase(it);
98 HDF_LOGD("%{public}s: service: %{public}d, UnRegisterCB Success", __func__, serviceId);
99 return;
100 }
101
UpdateSensorConfig(int sensorId,int64_t samplingInterval,int64_t reportInterval)102 void SensorClientsManager::UpdateSensorConfig(int sensorId, int64_t samplingInterval, int64_t reportInterval)
103 {
104 std::unique_lock<std::mutex> lock(sensorConfigMutex_);
105 auto it = sensorConfig_.find(sensorId);
106 if (it != sensorConfig_.end()) {
107 it->second.samplingInterval = samplingInterval <= it->second.samplingInterval ? samplingInterval
108 : it->second.samplingInterval;
109 it->second.reportInterval = reportInterval <= it->second.reportInterval ? reportInterval
110 : it->second.reportInterval;
111 } else {
112 BestSensorConfig config = {samplingInterval, reportInterval};
113 sensorConfig_.emplace(sensorId, config);
114 }
115 }
116
UpdateSdcSensorConfig(int sensorId,int64_t samplingInterval,int64_t reportInterval)117 void SensorClientsManager::UpdateSdcSensorConfig(int sensorId, int64_t samplingInterval, int64_t reportInterval)
118 {
119 std::unique_lock<std::mutex> lock(sdcSensorConfigMutex_);
120 auto it = sdcSensorConfig_.find(sensorId);
121 if (it != sdcSensorConfig_.end()) {
122 it->second.samplingInterval = samplingInterval <= it->second.samplingInterval ? samplingInterval
123 : it->second.samplingInterval;
124 it->second.reportInterval = reportInterval <= it->second.reportInterval ? reportInterval
125 : it->second.reportInterval;
126 } else {
127 BestSensorConfig config = {samplingInterval, reportInterval};
128 sdcSensorConfig_.emplace(sensorId, config);
129 }
130 }
131
UpdateClientPeriodCount(int sensorId,int64_t samplingInterval,int64_t reportInterval)132 void SensorClientsManager::UpdateClientPeriodCount(int sensorId, int64_t samplingInterval, int64_t reportInterval)
133 {
134 std::unique_lock<std::mutex> lock(clientsMutex_);
135 if (samplingInterval <= ERROR_INTERVAL || reportInterval < ERROR_INTERVAL) {
136 HDF_LOGE("%{public}s: sensorId is %{public}d, samplingInterval is [%{public}" PRId64 "], \
137 reportInterval is [%{public}" PRId64 "].", __func__, sensorId, samplingInterval, reportInterval);
138 return;
139 }
140 int32_t groupId = HDF_TRADITIONAL_SENSOR_TYPE;
141 if (clients_.find(groupId) == clients_.end() || clients_[groupId].empty()) {
142 return;
143 }
144 for (auto &entry : clients_[groupId]) {
145 auto &client = entry.second;
146 if (client.curCountMap_.find(sensorId) == client.curCountMap_.end()) {
147 client.curCountMap_[sensorId] = INIT_CUR_COUNT;
148 }
149 if (client.sensorConfigMap_.find(sensorId) != client.sensorConfigMap_.end()) {
150 int32_t periodCount = client.sensorConfigMap_.find(sensorId)->second.samplingInterval / samplingInterval;
151 if (client.periodCountMap_.find(sensorId) == client.periodCountMap_.end() ||
152 periodCount > client.periodCountMap_[sensorId]) {
153 client.periodCountMap_[sensorId] = periodCount;
154 }
155 }
156 }
157 }
158
SetSensorBestConfig(int sensorId,int64_t & samplingInterval,int64_t & reportInterval)159 void SensorClientsManager::SetSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval)
160 {
161 std::unique_lock<std::mutex> lock(sensorConfigMutex_);
162 auto it = sensorConfig_.find(sensorId);
163 if (it == sensorConfig_.end()) {
164 HDF_LOGD("%{public}s: sensor: %{public}d is enabled first time", __func__, sensorId);
165 return;
166 }
167
168 samplingInterval = samplingInterval < it->second.samplingInterval ? samplingInterval : it->second.samplingInterval;
169 reportInterval = reportInterval < it->second.reportInterval ? reportInterval : it->second.reportInterval;
170 HDF_LOGD("%{public}s: sensorId is %{public}d, after SetSensorBestConfig, samplingInterval is %{public}s, "
171 "reportInterval is %{public}s", __func__, sensorId, std::to_string(samplingInterval).c_str(),
172 std::to_string(reportInterval).c_str());
173 return;
174 }
175
SetSdcSensorBestConfig(int sensorId,int64_t & samplingInterval,int64_t & reportInterval)176 void SensorClientsManager::SetSdcSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval)
177 {
178 std::unique_lock<std::mutex> lock(sdcSensorConfigMutex_);
179 auto it = sdcSensorConfig_.find(sensorId);
180 if (it == sdcSensorConfig_.end()) {
181 HDF_LOGD("%{public}s: sensor: %{public}d is enabled by sdc first time", __func__, sensorId);
182 return;
183 }
184
185 samplingInterval = samplingInterval < it->second.samplingInterval ? samplingInterval : it->second.samplingInterval;
186 reportInterval = reportInterval < it->second.reportInterval ? reportInterval : it->second.reportInterval;
187 HDF_LOGD("%{public}s: sensorId is %{public}d, after SetSdcSensorBestConfig, samplingInterval is %{public}s, "
188 "reportInterval is %{public}s", __func__, sensorId, std::to_string(samplingInterval).c_str(),
189 std::to_string(reportInterval).c_str());
190 return;
191 }
192
193
GetSensorBestConfig(int sensorId,int64_t & samplingInterval,int64_t & reportInterval)194 void SensorClientsManager::GetSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval)
195 {
196 std::unique_lock<std::mutex> lock(sensorConfigMutex_);
197 auto it = sensorConfig_.find(sensorId);
198 if (it == sensorConfig_.end()) {
199 samplingInterval = STOP_INTERVAL;
200 reportInterval = STOP_INTERVAL;
201 HDF_LOGD("%{public}s: sensor: %{public}d has no best config", __func__, sensorId);
202 return;
203 }
204
205 samplingInterval = it->second.samplingInterval;
206 reportInterval = it->second.reportInterval;
207 HDF_LOGD("%{public}s: sensorId is %{public}d, after GetSensorBestConfig, samplingInterval is %{public}s, "
208 "reportInterval is %{public}s", __func__, sensorId, std::to_string(samplingInterval).c_str(),
209 std::to_string(reportInterval).c_str());
210 return;
211 }
212
EraseSdcSensorBestConfig(int sensorId)213 void SensorClientsManager::EraseSdcSensorBestConfig(int sensorId)
214 {
215 std::unique_lock<std::mutex> lock(sdcSensorConfigMutex_);
216 auto it = sdcSensorConfig_.find(sensorId);
217 if (it == sdcSensorConfig_.end()) {
218 HDF_LOGD("%{public}s: sensor: %{public}d sdcSensorBestConfig not exist, not need erase", __func__, sensorId);
219 return;
220 }
221 sdcSensorConfig_.erase(it);
222 HDF_LOGD("%{public}s: sensor: %{public}d config has been erase from sdcSensorConfig_", __func__, sensorId);
223 return;
224 }
225
OpenSensor(int sensorId,int serviceId)226 void SensorClientsManager::OpenSensor(int sensorId, int serviceId)
227 {
228 std::unique_lock<std::mutex> lock(sensorUsedMutex_);
229 std::set<int> service = {serviceId};
230 sensorUsed_.emplace(sensorId, service);
231 HDF_LOGD("%{public}s: service: %{public}d enabled sensor %{public}d", __func__, serviceId, sensorId);
232 }
233
IsNeedOpenSensor(int sensorId,int serviceId)234 bool SensorClientsManager::IsNeedOpenSensor(int sensorId, int serviceId)
235 {
236 auto it = sensorUsed_.find(sensorId);
237 if (it == sensorUsed_.end()) {
238 HDF_LOGD("%{public}s: sensor %{public}d is enabled by service: %{public}d", __func__, sensorId, serviceId);
239 return true;
240 }
241 auto service = sensorUsed_[sensorId].find(serviceId);
242 if (service == sensorUsed_[sensorId].end()) {
243 sensorUsed_[sensorId].insert(serviceId);
244 HDF_LOGD("%{public}s: service: %{public}d enabled sensor %{public}d", __func__, serviceId, sensorId);
245 }
246 return false;
247 }
248
IsNeedCloseSensor(int sensorId,int serviceId)249 bool SensorClientsManager::IsNeedCloseSensor(int sensorId, int serviceId)
250 {
251 auto it = sensorUsed_.find(sensorId);
252 if (it == sensorUsed_.end()) {
253 HDF_LOGE("%{public}s: sensor %{public}d has been disabled or not support", __func__, sensorId);
254 return true;
255 }
256 sensorUsed_[sensorId].erase(serviceId);
257 if (sensorUsed_[sensorId].empty()) {
258 sensorUsed_.erase(sensorId);
259 sensorConfig_.erase(sensorId);
260 HDF_LOGD("%{public}s: disabled sensor %{public}d", __func__, sensorId);
261 return true;
262 }
263 for (auto sid : sensorUsed_[sensorId]) {
264 HDF_LOGD("%{public}s: sensor %{public}d also is enable by service %{public}d", __func__, sensorId, sid);
265 }
266 return false;
267 }
268
IsExistSdcSensorEnable(int sensorId)269 bool SensorClientsManager::IsExistSdcSensorEnable(int sensorId)
270 {
271 std::unique_lock<std::mutex> lock(sdcSensorConfigMutex_);
272 auto it = sdcSensorConfig_.find(sensorId);
273 if (it == sdcSensorConfig_.end()) {
274 return false;
275 }
276 HDF_LOGE("%{public}s: sensor %{public}d has been enabled by sdc service %{public}d", __func__, sensorId, it->first);
277 return true;
278 }
279
IsUpadateSensorState(int sensorId,int serviceId,bool isOpen)280 bool SensorClientsManager::IsUpadateSensorState(int sensorId, int serviceId, bool isOpen)
281 {
282 std::unique_lock<std::mutex> lock(sensorUsedMutex_);
283 if (isOpen && IsNeedOpenSensor(sensorId, serviceId)) {
284 return true;
285 }
286 if (!isOpen && IsNeedCloseSensor(sensorId, serviceId)) {
287 return true;
288 }
289 return false;
290 }
291
IsClientsEmpty(int groupId)292 bool SensorClientsManager::IsClientsEmpty(int groupId)
293 {
294 std::unique_lock<std::mutex> lock(clientsMutex_);
295 if (clients_.find(groupId) == clients_.end() || clients_[groupId].empty()) {
296 return true;
297 }
298 return false;
299 }
300
GetClients(int groupId,std::unordered_map<int32_t,SensorClientInfo> & client)301 bool SensorClientsManager::GetClients(int groupId, std::unordered_map<int32_t, SensorClientInfo> &client)
302 {
303 std::unique_lock<std::mutex> lock(clientsMutex_);
304 auto it = clients_.find(groupId);
305 if (it == clients_.end() || it->second.empty()) {
306 return false;
307 }
308 client = it->second;
309 return true;
310 }
311
SetClientSenSorConfig(int32_t sensorId,int32_t serviceId,int64_t samplingInterval,int64_t & reportInterval)312 void SensorClientsManager::SetClientSenSorConfig(int32_t sensorId, int32_t serviceId, int64_t samplingInterval,
313 int64_t &reportInterval)
314 {
315 std::unique_lock<std::mutex> lock(clientsMutex_);
316 HDF_LOGD("%{public}s: service %{public}d enter the SetClientSenSorConfig function, sensorId is %{public}d, "
317 "samplingInterval is %{public}s, reportInterval is %{public}s", __func__, serviceId, sensorId,
318 std::to_string(samplingInterval).c_str(), std::to_string(reportInterval).c_str());
319
320 int32_t groupId = HDF_TRADITIONAL_SENSOR_TYPE;
321 if (clients_.find(groupId) == clients_.end() || clients_[groupId].find(serviceId) == clients_[groupId].end()) {
322 HDF_LOGE("%{public}s: service %{public}d already UnRegister", __func__, serviceId);
323 return;
324 }
325
326 auto &client = clients_[groupId].find(serviceId)->second;
327 SensorConfig sensorConfig = {samplingInterval, reportInterval};
328 client.sensorConfigMap_[sensorId] = sensorConfig;
329 }
330
IsNotNeedReportData(int32_t serviceId,int32_t sensorId)331 bool SensorClientsManager::IsNotNeedReportData(int32_t serviceId, int32_t sensorId)
332 {
333 if (!IsSensorContinues(sensorId)) {
334 return false;
335 }
336 int32_t groupId = HDF_TRADITIONAL_SENSOR_TYPE;
337 if (clients_.find(groupId) == clients_.end() || clients_[groupId].find(serviceId) == clients_[groupId].end()) {
338 HDF_LOGE("%{public}s: service %{public}d already UnRegister", __func__, serviceId);
339 return false;
340 }
341 auto &sensorClientInfo = clients_[groupId].find(serviceId)->second;
342 if (sensorClientInfo.periodCountMap_.find(sensorId) == sensorClientInfo.periodCountMap_.end()) {
343 return false;
344 }
345 sensorClientInfo.PrintClientMapInfo(serviceId, sensorId);
346 sensorClientInfo.curCountMap_[sensorId]++;
347 if (sensorClientInfo.curCountMap_[sensorId] >= sensorClientInfo.periodCountMap_[sensorId]) {
348 sensorClientInfo.curCountMap_[sensorId] = 0;
349 return false;
350 }
351 return true;
352 }
353
IsSensorContinues(int32_t sensorId)354 bool SensorClientsManager::IsSensorContinues(int32_t sensorId)
355 {
356 return std::find(continuesSensor.begin(), continuesSensor.end(), sensorId) != continuesSensor.end();
357 }
358
GetSensorUsed()359 std::unordered_map<int32_t, std::set<int32_t>> SensorClientsManager::GetSensorUsed()
360 {
361 std::unique_lock<std::mutex> lock(sensorUsedMutex_);
362 return sensorUsed_;
363 }
364
GetInstance()365 SensorClientsManager* SensorClientsManager::GetInstance()
366 {
367 if (instance == nullptr) {
368 std::unique_lock<std::mutex> lock(instanceMutex_);
369 if (instance == nullptr) {
370 instance = new SensorClientsManager();
371 }
372 }
373 return instance;
374 }
375
376 } // V2_0
377 } // Sensor
378 } // HDI
379 } // OHOS