• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "usb_host_manager.h"
17 #include "common_event_data.h"
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "hilog_wrapper.h"
21 #include "hisysevent.h"
22 #include "usb_serial_reader.h"
23 
24 using namespace OHOS::AAFwk;
25 using namespace OHOS::EventFwk;
26 using namespace OHOS::HiviewDFX;
27 
28 namespace OHOS {
29 namespace USB {
UsbHostManager(SystemAbility * systemAbility)30 UsbHostManager::UsbHostManager(SystemAbility *systemAbility)
31 {
32     systemAbility_ = systemAbility;
33 }
34 
~UsbHostManager()35 UsbHostManager::~UsbHostManager() {}
36 
GetDevices(MAP_STR_DEVICE & devices)37 void UsbHostManager::GetDevices(MAP_STR_DEVICE &devices)
38 {
39     devices = devices_;
40 }
41 
DelDevice(uint8_t busNum,uint8_t devNum)42 bool UsbHostManager::DelDevice(uint8_t busNum, uint8_t devNum)
43 {
44     std::string name = std::to_string(busNum) + "-" + std::to_string(devNum);
45     MAP_STR_DEVICE::iterator iter = devices_.find(name);
46     if (iter == devices_.end()) {
47         USB_HILOGF(MODULE_SERVICE, "name:%{public}s bus:%{public}hhu dev:%{public}hhu not exist", name.c_str(), busNum,
48             devNum);
49         return false;
50     }
51     USB_HILOGI(
52         MODULE_SERVICE, "device:%{public}s bus:%{public}hhu dev:%{public}hhu erase ", name.c_str(), busNum, devNum);
53     UsbDevice *devOld = iter->second;
54     if (devOld == nullptr) {
55         USB_HILOGE(MODULE_SERVICE, "invalid device");
56         return false;
57     }
58 
59     auto isSuccess = PublishCommonEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED, *devOld);
60     if (!isSuccess) {
61         USB_HILOGW(MODULE_SERVICE, "send device attached broadcast failed");
62     }
63 
64     delete devOld;
65     devices_.erase(iter);
66     return true;
67 }
68 
AddDevice(UsbDevice * dev)69 bool UsbHostManager::AddDevice(UsbDevice *dev)
70 {
71     if (dev == nullptr) {
72         USB_HILOGF(MODULE_SERVICE, "device is NULL");
73         return false;
74     }
75 
76     auto isSuccess = PublishCommonEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED, *dev);
77     if (!isSuccess) {
78         USB_HILOGW(MODULE_SERVICE, "send device attached broadcast failed");
79     }
80 
81     uint8_t busNum = dev->GetBusNum();
82     uint8_t devNum = dev->GetDevAddr();
83     std::string name = std::to_string(busNum) + "-" + std::to_string(devNum);
84     MAP_STR_DEVICE::iterator iter = devices_.find(name);
85     if (iter != devices_.end()) {
86         USB_HILOGF(MODULE_SERVICE, "device:%{public}s bus:%{public}hhu dev:%{public}hhu already exist", name.c_str(),
87             busNum, devNum);
88         UsbDevice *devOld = iter->second;
89         delete devOld;
90         devices_.erase(iter);
91     }
92     USB_HILOGI(
93         MODULE_SERVICE, "device:%{public}s bus:%{public}hhu dev:%{public}hhu insert", name.c_str(), busNum, devNum);
94     devices_.insert(std::pair<std::string, UsbDevice *>(name, dev));
95     return true;
96 }
97 
PublishCommonEvent(const std::string & event,const UsbDevice & dev)98 bool UsbHostManager::PublishCommonEvent(const std::string &event, const UsbDevice &dev)
99 {
100     Want want;
101     want.SetAction(event);
102 
103     CommonEventData data(want);
104     Json::StreamWriterBuilder builder;
105     builder["indentation"] = "";
106     auto jsonString = Json::writeString(builder, dev.ToJson());
107     data.SetData(jsonString);
108 
109     CommonEventPublishInfo publishInfo;
110     USB_HILOGI(MODULE_SERVICE, "send %{public}s broadcast device:%{public}s", event.c_str(), jsonString.c_str());
111     ReportHostPlugSysEvent(event, dev);
112     return CommonEventManager::PublishCommonEvent(data, publishInfo);
113 }
114 
Dump(int fd,const std::string & args)115 bool UsbHostManager::Dump(int fd, const std::string &args)
116 {
117     if (args.compare("-a") != 0) {
118         dprintf(fd, "args is not -a\n");
119         return false;
120     }
121 
122     dprintf(fd, "Usb Host all device list info:\n");
123     Json::StreamWriterBuilder builder;
124     builder["indentation"] = "  ";
125     for (const auto &item : devices_) {
126         dprintf(fd, "usb host list info: %s\n", Json::writeString(builder, item.second->ToJson()).c_str());
127     }
128     return true;
129 }
130 
ReportHostPlugSysEvent(const std::string & event,const UsbDevice & dev)131 void UsbHostManager::ReportHostPlugSysEvent(const std::string &event, const UsbDevice &dev)
132 {
133     USB_HILOGI(MODULE_SERVICE, "Host mode Indicates the insertion and removal information");
134     HiSysEventWrite(HiSysEvent::Domain::USB, "PLUG_IN_OUT_HOST_MODE", HiSysEvent::EventType::BEHAVIOR,
135         "DEVICE_NAME", dev.GetName(), "DEVICE_PROTOCOL", dev.GetProtocol(), "DEVICE_CLASS", dev.GetClass(),
136         "VENDOR_ID", dev.GetVendorId(), "PRODUCT_ID", dev.GetProductId(), "VERSION", dev.GetVersion(),
137         "EVENT_NAME", event);
138 }
139 } // namespace USB
140 } // namespace OHOS
141