1 /*
2  * Copyright (c) 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 #ifndef OHOS_DM_DISCOVERY_MANAGER_H
17 #define OHOS_DM_DISCOVERY_MANAGER_H
18 
19 #include <queue>
20 
21 #include "dm_discovery_filter.h"
22 #include "idevice_manager_service_listener.h"
23 #include "dm_timer.h"
24 #include "hichain_connector.h"
25 #include "softbus_connector.h"
26 namespace OHOS {
27 namespace DistributedHardware {
28 typedef struct DmDiscoveryContext {
29     std::string pkgName;
30     std::string extra;
31     uint16_t subscribeId;
32     std::string filterOp;
33     std::vector<DmDeviceFilters> filters;
34 } DmDiscoveryContext;
35 
36 class DmDiscoveryManager final : public ISoftbusDiscoveryCallback,
37                                  public std::enable_shared_from_this<DmDiscoveryManager> {
38 public:
39     DmDiscoveryManager(std::shared_ptr<SoftbusConnector> softbusConnector,
40                        std::shared_ptr<IDeviceManagerServiceListener> listener,
41                        std::shared_ptr<HiChainConnector> hiChainConnector);
42     ~DmDiscoveryManager();
43 
44     /**
45      * @tc.name: DmDiscoveryManager::StartDeviceDiscovery
46      * @tc.desc: Start Device Discovery of the Dm Discovery Manager
47      * @tc.type: FUNC
48      */
49     int32_t StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
50                                  const std::string &extra);
51 
52     /**
53      * @tc.name: DmDiscoveryManager::StartDeviceDiscovery
54      * @tc.desc: Start Device Discovery of the Dm Discovery Manager
55      * @tc.type: FUNC
56      */
57     int32_t StartDeviceDiscovery(const std::string &pkgName, const uint16_t subscribeId,
58                                  const std::string &filterOptions);
59 
60     /**
61      * @tc.name: DmDiscoveryManager::StopDeviceDiscovery
62      * @tc.desc: Stop Device Discovery of the Dm Discovery Manager
63      * @tc.type: FUNC
64      */
65     int32_t StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId);
66 
67     /**
68      * @tc.name: DmDiscoveryManager::OnDeviceFound
69      * @tc.desc: OnDevice Found of the Dm Discovery Manager
70      * @tc.type: FUNC
71      */
72     void OnDeviceFound(const std::string &pkgName, DmDeviceInfo &info, bool isOnline);
73 
74     /**
75      * @tc.name: DmDiscoveryManager::OnDeviceFound
76      * @tc.desc: OnDevice Found of the Dm Discovery Manager
77      * @tc.type: FUNC
78      */
79     void OnDeviceFound(const std::string &pkgName, DmDeviceBasicInfo &info, const int32_t range, bool isOnline);
80 
81     /**
82      * @tc.name: DmDiscoveryManager::OnDiscoverySuccess
83      * @tc.desc: OnDiscovery Success of the Dm Discovery Manager
84      * @tc.type: FUNC
85      */
86     void OnDiscoverySuccess(const std::string &pkgName, int32_t subscribeId);
87 
88     /**
89      * @tc.name: DmDiscoveryManager::OnDiscoveryFailed
90      * @tc.desc: OnDiscovery Failed of the Dm Discovery Manager
91      * @tc.type: FUNC
92      */
93     void OnDiscoveryFailed(const std::string &pkgName, int32_t subscribeId, int32_t failedReason);
94 
95     /**
96      * @tc.name: DmDiscoveryManager::HandleDiscoveryTimeout
97      * @tc.desc: Handle Discovery Timeout of the Dm Discovery Manager
98      * @tc.type: FUNC
99      */
100     void HandleDiscoveryTimeout(std::string name);
101 private:
102     void CfgDiscoveryTimer();
103     int32_t CheckDiscoveryQueue(const std::string &pkgName);
104     int32_t GetAuthForm(const std::string &localDeviceId, const std::string &deviceId,
105         bool &isTrusted, DmAuthForm &authForm);
106 
107 private:
108     std::shared_ptr<SoftbusConnector> softbusConnector_;
109     std::shared_ptr<IDeviceManagerServiceListener> listener_;
110     std::shared_ptr<HiChainConnector> hiChainConnector_;
111     std::queue<std::string> discoveryQueue_;
112     std::map<std::string, DmDiscoveryContext> discoveryContextMap_;
113     std::shared_ptr<DmTimer> timer_;
114     std::mutex locks_;
115 };
116 } // namespace DistributedHardware
117 } // namespace OHOS
118 #endif // OHOS_DM_DISCOVERY_MANAGER_H
119