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 #include "device_manager.h"
16 
17 #include <algorithm>
18 #include <unistd.h>
19 
20 #include "bus_center/softbus_bus_center.h"
21 #include "test_suite.h"
22 
23 DeviceManager* DeviceManager::m_instance = nullptr;
24 
Instance()25 DeviceManager *DeviceManager::Instance()
26 {
27     if (m_instance == nullptr) {
28         m_instance = new DeviceManager();
29     }
30     return m_instance;
31 }
32 
GetRemoteByIndex(uint32_t index)33 std::string DeviceManager::GetRemoteByIndex(uint32_t index)
34 {
35     if (index >= m_remoteList.size()) {
36         return "NoSuchDevice";
37     }
38     return m_remoteList[index];
39 }
40 
WaitNetworkSizeMoreThan(uint32_t count)41 void DeviceManager::WaitNetworkSizeMoreThan(uint32_t count)
42 {
43     NodeBasicInfo *nodeInfo = nullptr;
44     int32_t infoNum = 0;
45     do {
46         int ret = GetAllNodeDeviceInfo(ECHO_SERVICE_PKGNAME, &nodeInfo, &infoNum);
47         if (ret == 0) {
48             if (infoNum >= count) {
49                 break;
50             }
51             LOG("device count=%d", infoNum);
52             for (uint32_t i = 0; i < infoNum; i++) {
53                 LOG("%s:networkId=%s", __func__, nodeInfo[i].networkId);
54             }
55         }
56 
57         FreeNodeInfo(nodeInfo);
58         nodeInfo = nullptr;
59         sleep(3L);
60     } while (infoNum < count);
61 
62     NodeBasicInfo localNode;
63 
64     GetLocalNodeDeviceInfo(ECHO_SERVICE_PKGNAME, &localNode);
65     m_localNetworkId = localNode.networkId;
66 
67     m_remoteList.clear();
68     if (nodeInfo != nullptr) {
69         for (uint32_t i = 0; i < infoNum; i++) {
70             if (m_localNetworkId != nodeInfo[i].networkId) {
71                 m_remoteList.push_back(nodeInfo[i].networkId);
72             }
73         }
74     }
75     std::sort(m_remoteList.begin(), m_remoteList.end());
76     FreeNodeInfo(nodeInfo);
77 }
78