1 /*
2 * Copyright (c) 2022-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 "devicestatus_msdp_client_impl.h"
17
18 #include <string>
19
20 #include <dlfcn.h>
21
22 #include "devicestatus_define.h"
23 #include "devicestatus_dumper.h"
24 #include "fi_log.h"
25
26 #undef LOG_TAG
27 #define LOG_TAG "DeviceStatusMsdpClientImpl"
28
29 namespace OHOS {
30 namespace Msdp {
31 namespace DeviceStatus {
32 namespace {
33 #ifdef __aarch64__
34 const std::string DEVICESTATUS_MOCK_LIB_PATH { "/system/lib64/libdevicestatus_mock.z.so" };
35 const std::string DEVICESTATUS_ALGO_LIB_PATH { "/system/lib64/libdevicestatus_algo.z.so" };
36 #else
37 const std::string DEVICESTATUS_MOCK_LIB_PATH { "/system/lib/libdevicestatus_mock.z.so" };
38 const std::string DEVICESTATUS_ALGO_LIB_PATH { "/system/lib/libdevicestatus_algo.z.so" };
39 #endif
40 using ClientType = Type;
41 using ClientValue = OnChangedValue;
42 } // namespace
43
DeviceStatusMsdpClientImpl()44 DeviceStatusMsdpClientImpl::DeviceStatusMsdpClientImpl()
45 {
46 for (int32_t type = 0; type < static_cast<int32_t>(Type::TYPE_MAX); ++type) {
47 algoCallCounts_[static_cast<Type>(type)] = 0;
48 mockCallCounts_[static_cast<Type>(type)] = 0;
49 }
50 }
51
InitMsdpImpl(Type type)52 ErrCode DeviceStatusMsdpClientImpl::InitMsdpImpl(Type type)
53 {
54 CALL_DEBUG_ENTER;
55 if (GetSensorHdi(type) == RET_OK) {
56 FI_HILOGI("GetSensorHdi is support");
57 return RET_OK;
58 }
59 #ifdef DEVICE_STATUS_SENSOR_ENABLE
60 if (AlgoHandle(type) == RET_OK) {
61 FI_HILOGI("AlgoHandle is support");
62 return RET_OK;
63 }
64 #else
65 FI_HILOGE("Enable:sensor is not exist");
66 #endif // DEVICE_STATUS_SENSOR_ENABLE
67 if (MockHandle(type) == RET_OK) {
68 FI_HILOGI("MockHandle is support");
69 return RET_OK;
70 }
71 return RET_ERR;
72 }
73
MockHandle(Type type)74 ErrCode DeviceStatusMsdpClientImpl::MockHandle(Type type)
75 {
76 if (StartMock(type) == RET_ERR) {
77 FI_HILOGE("Start mock Library failed");
78 return RET_ERR;
79 }
80 CHKPR(iMock_, RET_ERR);
81 iMock_->Enable(type);
82 std::unique_lock lock(mutex_);
83 auto iter = mockCallCounts_.find(type);
84 if (iter == mockCallCounts_.end()) {
85 auto ret = mockCallCounts_.emplace(type, 0);
86 if (!ret.second) {
87 FI_HILOGW("type is duplicated");
88 return RET_ERR;
89 }
90 } else {
91 iter->second++;
92 }
93 RegisterMock();
94 FI_HILOGI("mockCallCounts_:%{public}d", mockCallCounts_[type]);
95 return RET_OK;
96 }
97
AlgoHandle(Type type)98 ErrCode DeviceStatusMsdpClientImpl::AlgoHandle(Type type)
99 {
100 if (GetAlgoAbility(type) == RET_ERR) {
101 FI_HILOGE("Algo Library is not support");
102 return RET_ERR;
103 }
104 if (StartAlgo(type) == RET_ERR) {
105 FI_HILOGE("Start algo Library failed");
106 return RET_ERR;
107 }
108 CHKPR(iAlgo_, RET_ERR);
109 if ((iAlgo_->Enable(type)) == RET_ERR) {
110 FI_HILOGE("Enable algo Library failed");
111 return RET_ERR;
112 }
113
114 auto iter = algoCallCounts_.find(type);
115 if (iter == algoCallCounts_.end()) {
116 auto ret = algoCallCounts_.emplace(type, 0);
117 if (!ret.second) {
118 FI_HILOGW("Type is duplicated");
119 return RET_ERR;
120 }
121 } else {
122 iter->second++;
123 }
124 RegisterAlgo();
125 FI_HILOGI("algoCallCounts_:%{public}d", algoCallCounts_[type]);
126 return RET_OK;
127 }
128
StartAlgo(Type type)129 ErrCode DeviceStatusMsdpClientImpl::StartAlgo(Type type)
130 {
131 if (LoadAlgoLibrary() == RET_ERR) {
132 FI_HILOGE("Load algo Library failed");
133 return RET_ERR;
134 }
135 iAlgo_ = GetAlgoInst(type);
136 CHKPR(iAlgo_, RET_ERR);
137 return RET_OK;
138 }
139
StartMock(Type type)140 ErrCode DeviceStatusMsdpClientImpl::StartMock(Type type)
141 {
142 if (LoadMockLibrary() == RET_ERR) {
143 FI_HILOGE("Load mock Library failed");
144 return RET_ERR;
145 }
146 iMock_ = GetMockInst(type);
147 if (iMock_ == nullptr) {
148 FI_HILOGE("Get mock module failed");
149 return RET_ERR;
150 }
151 return RET_OK;
152 }
153
GetSensorHdi(Type type)154 ErrCode DeviceStatusMsdpClientImpl::GetSensorHdi(Type type)
155 {
156 return RET_ERR;
157 }
158
GetAlgoAbility(Type type)159 ErrCode DeviceStatusMsdpClientImpl::GetAlgoAbility(Type type)
160 {
161 if ((type == Type::TYPE_ABSOLUTE_STILL) || (type == Type::TYPE_HORIZONTAL_POSITION) ||
162 (type == Type::TYPE_VERTICAL_POSITION)) {
163 FI_HILOGI("Support ability type:%{public}d", type);
164 return RET_OK;
165 }
166 FI_HILOGI("Not support ability");
167 return RET_ERR;
168 }
169
Disable(Type type)170 ErrCode DeviceStatusMsdpClientImpl::Disable(Type type)
171 {
172 CALL_DEBUG_ENTER;
173 return (((SensorHdiDisable(type) == RET_OK) || (AlgoDisable(type) == RET_OK) || (MockDisable(type) == RET_OK)) ?
174 RET_OK : RET_ERR);
175 }
176
SensorHdiDisable(Type type)177 ErrCode DeviceStatusMsdpClientImpl::SensorHdiDisable(Type type)
178 {
179 return RET_ERR;
180 }
181
AlgoDisable(Type type)182 ErrCode DeviceStatusMsdpClientImpl::AlgoDisable(Type type)
183 {
184 #ifdef DEVICE_STATUS_SENSOR_ENABLE
185 CALL_DEBUG_ENTER;
186 CHKPR(iAlgo_, RET_ERR);
187 auto iter = algoCallCounts_.find(type);
188 if (iter == algoCallCounts_.end()) {
189 FI_HILOGE("Failed to find record type");
190 return RET_ERR;
191 }
192 if (iter->second == 0) {
193 algoCallCounts_.erase(type);
194 } else {
195 iAlgo_->Disable(type);
196 UnregisterAlgo();
197 }
198 algoCallCounts_.erase(type);
199 if (algoCallCounts_.empty()) {
200 if (UnloadAlgoLibrary() == RET_ERR) {
201 FI_HILOGE("Failed to close algorithm library");
202 return RET_ERR;
203 }
204 FI_HILOGI("Close algorithm library");
205 iAlgo_ = nullptr;
206 callBacksMgr_ = nullptr;
207 }
208 FI_HILOGI("algoCallCounts_:%{public}d", algoCallCounts_[type]);
209 return RET_OK;
210 #else
211 FI_HILOGE("Disable:sensor is not exist");
212 return RET_ERR;
213 #endif // DEVICE_STATUS_SENSOR_ENABLE
214 }
215
MockDisable(Type type)216 ErrCode DeviceStatusMsdpClientImpl::MockDisable(Type type)
217 {
218 CALL_DEBUG_ENTER;
219 CHKPR(iMock_, RET_ERR);
220 std::unique_lock lock(mutex_);
221 auto iter = mockCallCounts_.find(type);
222 if (iter == mockCallCounts_.end()) {
223 FI_HILOGE("Failed to find record type");
224 return RET_ERR;
225 }
226 if (iter->second == 0) {
227 mockCallCounts_.erase(type);
228 } else {
229 iMock_->DisableCount(type);
230 iMock_->Disable(type);
231 UnregisterMock();
232 }
233 iter->second--;
234 if (mockCallCounts_.empty()) {
235 if (UnloadMockLibrary() == RET_ERR) {
236 FI_HILOGE("Failed to close library");
237 return RET_ERR;
238 }
239 iMock_ = nullptr;
240 callBacksMgr_ = nullptr;
241 }
242 return RET_OK;
243 }
244
ImplCallback(const Data & data)245 ErrCode DeviceStatusMsdpClientImpl::ImplCallback(const Data &data)
246 {
247 CHKPR(callBacksMgr_, RET_ERR);
248 callBacksMgr_(data);
249 return RET_OK;
250 }
251
RegisterImpl(const CallbackManager & callback)252 ErrCode DeviceStatusMsdpClientImpl::RegisterImpl(const CallbackManager &callback)
253 {
254 callBacksMgr_ = callback;
255 return RET_OK;
256 }
257
OnResult(const Data & data)258 void DeviceStatusMsdpClientImpl::OnResult(const Data &data)
259 {
260 FI_HILOGD("type:%{public}d, value:%{public}d", data.type, data.value);
261 MsdpCallback(data);
262 }
263
RegisterMock()264 ErrCode DeviceStatusMsdpClientImpl::RegisterMock()
265 {
266 CALL_DEBUG_ENTER;
267 if (iMock_ != nullptr) {
268 std::shared_ptr<IMsdp::MsdpAlgoCallback> callback = shared_from_this();
269 iMock_->RegisterCallback(callback);
270 }
271 return RET_OK;
272 }
273
UnregisterMock()274 ErrCode DeviceStatusMsdpClientImpl::UnregisterMock()
275 {
276 CALL_DEBUG_ENTER;
277 CHKPR(iMock_, RET_ERR);
278 iMock_->UnregisterCallback();
279 return RET_OK;
280 }
281
RegisterAlgo()282 ErrCode DeviceStatusMsdpClientImpl::RegisterAlgo()
283 {
284 CALL_DEBUG_ENTER;
285 if (iAlgo_ != nullptr) {
286 std::shared_ptr<IMsdp::MsdpAlgoCallback> callback_ = shared_from_this();
287 iAlgo_->RegisterCallback(callback_);
288 }
289 return RET_OK;
290 }
291
UnregisterAlgo()292 ErrCode DeviceStatusMsdpClientImpl::UnregisterAlgo()
293 {
294 CALL_DEBUG_ENTER;
295 CHKPR(iAlgo_, RET_ERR);
296 iAlgo_->UnregisterCallback();
297 return RET_OK;
298 }
299
MsdpCallback(const Data & data)300 int32_t DeviceStatusMsdpClientImpl::MsdpCallback(const Data &data)
301 {
302 CALL_DEBUG_ENTER;
303 DS_DUMPER->PushDeviceStatus(data);
304 SaveObserverData(data);
305 if (notifyManagerFlag_) {
306 ImplCallback(data);
307 notifyManagerFlag_ = false;
308 }
309 return RET_OK;
310 }
311
SaveObserverData(const Data & data)312 Data DeviceStatusMsdpClientImpl::SaveObserverData(const Data &data)
313 {
314 CALL_DEBUG_ENTER;
315 for (auto iter = deviceStatusDatas_.begin(); iter != deviceStatusDatas_.end(); ++iter) {
316 if (iter->first == data.type) {
317 iter->second = data.value;
318 notifyManagerFlag_ = true;
319 return data;
320 }
321 }
322 auto ret = deviceStatusDatas_.insert(std::make_pair(data.type, data.value));
323 if (!ret.second) {
324 FI_HILOGW("type is duplicated");
325 return data;
326 }
327 notifyManagerFlag_ = true;
328 return data;
329 }
330
GetObserverData() const331 std::map<ClientType, ClientValue> DeviceStatusMsdpClientImpl::GetObserverData() const
332 {
333 return deviceStatusDatas_;
334 }
335
GetDeviceStatusTimestamp()336 void DeviceStatusMsdpClientImpl::GetDeviceStatusTimestamp()
337 {}
338
GetLongtitude()339 void DeviceStatusMsdpClientImpl::GetLongtitude()
340 {}
341
GetLatitude()342 void DeviceStatusMsdpClientImpl::GetLatitude()
343 {}
344
LoadMockLibrary()345 ErrCode DeviceStatusMsdpClientImpl::LoadMockLibrary()
346 {
347 CALL_DEBUG_ENTER;
348 std::unique_lock lock(mutex_);
349 if (mock_.handle != nullptr) {
350 FI_HILOGW("mock handle is not nullptr");
351 return RET_OK;
352 }
353 std::string dlName = DEVICESTATUS_MOCK_LIB_PATH;
354 char libRealPath[PATH_MAX] = { 0 };
355 if (realpath(dlName .c_str(), libRealPath) == nullptr) {
356 FI_HILOGE("Get absolute algoPath is error, errno:%{public}d", errno);
357 return RET_ERR;
358 }
359 mock_.handle = dlopen(libRealPath, RTLD_LAZY);
360 if (mock_.handle == nullptr) {
361 FI_HILOGE("Cannot load library error:%{public}s", dlerror());
362 return RET_ERR;
363 }
364 FI_HILOGI("Start create pointer");
365 mock_.create = reinterpret_cast<LoadMockLibraryFunc>(dlsym(mock_.handle, "Create"));
366 FI_HILOGI("Start destroy pointer");
367 mock_.destroy = reinterpret_cast<LoadMockLibraryPtr>(dlsym(mock_.handle, "Destroy"));
368 if ((mock_.create == nullptr) || (mock_.destroy == nullptr)) {
369 FI_HILOGE("%{public}s dlsym Create or Destroy failed",
370 dlName.c_str());
371 dlclose(mock_.handle);
372 mock_.Clear();
373 if (mock_.handle == nullptr) {
374 return RET_OK;
375 }
376 FI_HILOGE("Load mock failed");
377 return RET_ERR;
378 }
379 return RET_OK;
380 }
381
UnloadMockLibrary()382 ErrCode DeviceStatusMsdpClientImpl::UnloadMockLibrary()
383 {
384 CALL_DEBUG_ENTER;
385 std::unique_lock lock(mutex_);
386 CHKPR(mock_.handle, RET_ERR);
387 if (mock_.pAlgorithm != nullptr) {
388 mock_.destroy(mock_.pAlgorithm);
389 mock_.pAlgorithm = nullptr;
390 }
391 dlclose(mock_.handle);
392 mock_.Clear();
393 return RET_OK;
394 }
395
GetMockInst(Type type)396 IMsdp* DeviceStatusMsdpClientImpl::GetMockInst(Type type)
397 {
398 CALL_DEBUG_ENTER;
399 std::unique_lock lock(mutex_);
400 CHKPP(mock_.handle);
401 if (mock_.pAlgorithm == nullptr) {
402 mock_.pAlgorithm = mock_.create();
403 mockCallCounts_[type] = 0;
404 }
405 return mock_.pAlgorithm;
406 }
407
LoadAlgoLibrary()408 ErrCode DeviceStatusMsdpClientImpl::LoadAlgoLibrary()
409 {
410 CALL_DEBUG_ENTER;
411 std::unique_lock lock(mutex_);
412 if (algo_.handle != nullptr) {
413 FI_HILOGE("Algo handle has exists");
414 return RET_OK;
415 }
416 std::string dlName = DEVICESTATUS_ALGO_LIB_PATH;
417 char libRealPath[PATH_MAX] = { 0 };
418 if (realpath(dlName .c_str(), libRealPath) == nullptr) {
419 FI_HILOGE("Get absolute algoPath is error, errno:%{public}d", errno);
420 return RET_ERR;
421 }
422 algo_.handle = dlopen(libRealPath, RTLD_LAZY);
423 if (algo_.handle == nullptr) {
424 FI_HILOGE("Cannot load library error:%{public}s", dlerror());
425 return RET_ERR;
426 }
427 FI_HILOGI("Start create pointer");
428 algo_.create = reinterpret_cast<LoadMockLibraryFunc>(dlsym(algo_.handle, "Create"));
429 FI_HILOGI("Start destroy pointer");
430 algo_.destroy = reinterpret_cast<LoadMockLibraryPtr>(dlsym(algo_.handle, "Destroy"));
431 if ((algo_.create == nullptr) || (algo_.destroy == nullptr)) {
432 FI_HILOGE("%{public}s dlsym Create or Destroy failed",
433 dlName.c_str());
434 dlclose(algo_.handle);
435 algo_.Clear();
436 if (algo_.handle == nullptr) {
437 return RET_OK;
438 }
439 FI_HILOGE("Load algo failed");
440 return RET_ERR;
441 }
442 return RET_OK;
443 }
444
UnloadAlgoLibrary()445 ErrCode DeviceStatusMsdpClientImpl::UnloadAlgoLibrary()
446 {
447 CALL_DEBUG_ENTER;
448 std::unique_lock lock(mutex_);
449 CHKPR(algo_.handle, RET_ERR);
450 if (algo_.pAlgorithm != nullptr) {
451 algo_.destroy(algo_.pAlgorithm);
452 algo_.pAlgorithm = nullptr;
453 }
454 dlclose(algo_.handle);
455 algo_.Clear();
456 return RET_OK;
457 }
458
GetAlgoInst(Type type)459 IMsdp* DeviceStatusMsdpClientImpl::GetAlgoInst(Type type)
460 {
461 CALL_DEBUG_ENTER;
462 std::unique_lock lock(mutex_);
463 CHKPP(algo_.handle);
464 if (algo_.pAlgorithm == nullptr) {
465 algo_.pAlgorithm = algo_.create();
466 }
467 return algo_.pAlgorithm;
468 }
469 } // namespace DeviceStatus
470 } // namespace Msdp
471 } // namespace OHOS
472