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 <dlfcn.h>
17 
18 #include "edm_errors.h"
19 #include "hilog_wrapper.h"
20 #include "iostream"
21 #define private public
22 #include "bus_extension_core.h"
23 #include "dev_change_callback.h"
24 #include "driver_pkg_manager.h"
25 #include "etx_device_mgr.h"
26 
27 #undef private
28 
29 constexpr const char *START_TEXT = "Begin to loop and listen usb event:\n\
30 enter q to exit.\n\
31 enter p to print all usb device.";
32 using namespace OHOS::ExternalDeviceManager;
33 using namespace std;
PrintAllDevice()34 static void PrintAllDevice()
35 {
36     ExtDeviceManager &devmgr = ExtDeviceManager::GetInstance();
37     cout << "------------------" << endl;
38     std::vector<shared_ptr<DeviceInfo>> devicesInfo = devmgr.QueryDevice(BUS_TYPE_USB);
39     cout << "usb device size: " << devicesInfo.size() << endl;
40     for (auto &iter :devicesInfo) {
41         cout << "description: " << iter->GetDeviceDescription().c_str() << endl;
42         cout << "deviceId: " << std::hex << iter->GetDeviceId() << endl;
43     }
44     std::unordered_map<string, unordered_set<uint64_t>> &bundleMatchMap = devmgr.bundleMatchMap_;
45     cout << "bundleMatchMap size:" << bundleMatchMap.size() << endl;
46     for (auto &iter : bundleMatchMap) {
47         cout << "bundleInfo [" << iter.first << "]: ";
48         for (auto &devId : iter.second) {
49             cout << std::hex << devId << " ";
50         }
51         cout << endl;
52     }
53     cout << "------------------" << endl;
54 }
55 
main(int argc,char ** argv)56 int main(int argc, char **argv)
57 {
58     cout << START_TEXT << endl;
59     BusExtensionCore::GetInstance().LoadBusExtensionLibs();
60     int32_t ret = DriverPkgManager::GetInstance().Init();
61     if (ret != EDM_OK) {
62         cout << "DriverPkgManager Init failed, ret = " << ret << endl;
63         return -1;
64     }
65     ret = ExtDeviceManager::GetInstance().Init();
66     if (ret != EDM_OK) {
67         cout << "ExtDeviceManager Init failed, ret = " << ret << endl;
68         return -1;
69     }
70     std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
71     ret = BusExtensionCore::GetInstance().Init(callback);
72     if (ret != EDM_OK) {
73         cout << "BusExtensionCore Init failed, ret = " << ret << endl;
74         return -1;
75     }
76 
77     while (true) {
78         std::string in;
79         cin >> in;
80         if (in == "q") {
81             break;
82         } else if (in == "p") {
83             PrintAllDevice();
84         } else {
85             cout << in;
86         }
87     }
88     cout << "exit!" << endl;
89     return ret;
90 }