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 #include "cinttypes"
16 #include "usb_bus_extension.h"
17 #include "iostream"
18 #include "hilog_wrapper.h"
19 namespace OHOS {
20 namespace ExternalDeviceManager {
21 constexpr const char *HELP_TEXT = "usb_bus_extension_mt help:\n\
22 Before run this tool, please make sure the selinux is disabled.\n\
23 use commond below to disable selinux:\n\
24 setenfore 0\n\
25 use command below to check: (Permissive means selinux is disabled)\n\
26 getenfore\n\
27 ------------------------------------------------------------------\n\
28 \n";
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 constexpr const char *EXIT_TEXT = "Exit now!";
33 using namespace std;
34 class BusExtensionUsbMt {
35 public:
36 /*
37 运行前要先关闭selinux,
38 1、临时方法:运行 setenforce 0,重启还会开启
39 验证:运行 getenforce 应该打印Permissive
40 2、修改 /etc/selinux/config文件中的SELINUX值改为disable。(开启为enforcing)
41 */
Entry(void)42 int Entry(void)
43 {
44 cout << HELP_TEXT << endl;
45 auto usbExt = std::make_shared<UsbBusExtension>();
46 auto cb = make_shared<TestDevChangeCallback>();
47 cout << START_TEXT << endl;
48 usbExt->SetDevChangeCallback(cb);
49 while (true) {
50 string in;
51 cin >> in;
52 if (in == "q") {
53 break;
54 } else if (in == "p") {
55 cb->PrintAllDevice();
56 } else {
57 cout << in;
58 }
59 }
60 cout << EXIT_TEXT << endl;
61 return 0;
62 };
63 class TestDevChangeCallback : public IDevChangeCallback {
64 public:
65 map<uint64_t, shared_ptr<DeviceInfo>> devInfoMap_;
TestDevChangeCallback()66 TestDevChangeCallback() { };
OnDeviceAdd(shared_ptr<DeviceInfo> device)67 int32_t OnDeviceAdd(shared_ptr<DeviceInfo> device) override
68 {
69 devInfoMap_[device->GetDeviceId()] = device;
70 EDM_LOGI(MODULE_COMMON, "OnDeviceAdd: Id = %{public}016" PRIx64 ", Desc = %{public}s",
71 device->GetDeviceId(), device->GetDeviceDescription().c_str());
72 PrintAllDevice();
73 return 0;
74 };
OnDeviceRemove(shared_ptr<DeviceInfo> device)75 int32_t OnDeviceRemove(shared_ptr<DeviceInfo> device) override
76 {
77 devInfoMap_.erase(device->GetDeviceId());
78 EDM_LOGI(MODULE_COMMON, "OnDeviceRemove: Id = %{public}016" PRIx64 ", Desc = %{public}s",
79 device->GetDeviceId(), device->GetDeviceDescription().c_str());
80 PrintAllDevice();
81 return 0;
82 };
PrintAllDevice(void)83 void PrintAllDevice(void)
84 {
85 cout << "++++++++, all usb device, count = " << devInfoMap_.size() << " detail:" << endl;
86 for (auto &devItem : devInfoMap_) {
87 cout << devItem.second->GetDeviceDescription().c_str() << endl;
88 }
89 cout << "--------" << endl;
90 }
91 };
92 };
93 }
94 }
main(int argc,char ** argv)95 int main(int argc, char **argv)
96 {
97 int ret = OHOS::ExternalDeviceManager::BusExtensionUsbMt().Entry();
98 return ret;
99 }