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