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 #ifndef DEVICESTATUS_MSDP_CLIENT_IMPL_H
17 #define DEVICESTATUS_MSDP_CLIENT_IMPL_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <map>
22 
23 #include "devicestatus_msdp_interface.h"
24 #include "stationary_data.h"
25 
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 class DeviceStatusMsdpClientImpl : public IMsdp::MsdpAlgoCallback,
30     public std::enable_shared_from_this<DeviceStatusMsdpClientImpl> {
31 public:
32     using CallbackManager = std::function<int32_t(const Data&)>;
33     using LoadMockLibraryFunc = IMsdp* (*)();
34     using LoadMockLibraryPtr = void *(*)(IMsdp*);
35     DeviceStatusMsdpClientImpl();
36     ErrCode InitMsdpImpl(Type type);
37     ErrCode Disable(Type type);
38     ErrCode GetSensorHdi(Type type);
39     ErrCode GetAlgoAbility(Type type);
40     ErrCode RegisterImpl(const CallbackManager &callback);
41     int32_t MsdpCallback(const Data &data);
42     ErrCode StartMock(Type type);
43     ErrCode RegisterMock();
44     ErrCode UnregisterMock();
45     ErrCode RegisterAlgo();
46     ErrCode UnregisterAlgo();
47     Data SaveObserverData(const Data &data);
48     std::map<Type, OnChangedValue> GetObserverData() const;
49     void GetDeviceStatusTimestamp();
50     void GetLongtitude();
51     void GetLatitude();
52     ErrCode LoadAlgoLibrary();
53     ErrCode UnloadAlgoLibrary();
54     ErrCode LoadMockLibrary();
55     ErrCode UnloadMockLibrary();
56     ErrCode MockHandle(Type type);
57     ErrCode AlgoHandle(Type type);
58     ErrCode StartAlgo(Type type);
59     ErrCode AlgoDisable(Type type);
60     ErrCode MockDisable(Type type);
61     ErrCode SensorHdiDisable(Type type);
62 
63 private:
64     ErrCode ImplCallback(const Data &data);
65     IMsdp* GetAlgoInst(Type type);
66     IMsdp* GetMockInst(Type type);
67     void OnResult(const Data &data) override;
68 
69 private:
70     std::shared_ptr<MsdpAlgoCallback> callback_ { nullptr };
71     MsdpAlgoHandle mock_;
72     MsdpAlgoHandle algo_;
73     std::map<Type, uint32_t> algoCallCounts_;
74     std::map<Type, uint32_t> mockCallCounts_;
75     std::map<Type, OnChangedValue> deviceStatusDatas_;
76     DeviceStatusMsdpClientImpl::CallbackManager callBacksMgr_;
77     IMsdp* iAlgo_ { nullptr };
78     IMsdp* iMock_ { nullptr };
79     std::mutex mutex_;
80     bool notifyManagerFlag_ { false };
81 };
82 } // namespace DeviceStatus
83 } // namespace Msdp
84 } // namespace OHOS
85 #endif // DEVICESTATUS_MSDP_CLIENT_IMPL_H
86