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_service_subscriber.h"
17 #include "common_event_data.h"
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "delayed_sp_singleton.h"
21 #include "json.h"
22 #include "string_ex.h"
23 #include "usb_common.h"
24 #include "usb_errors.h"
25 #include "usb_service.h"
26 #include "want.h"
27
28 using namespace OHOS::AAFwk;
29 using namespace OHOS::EventFwk;
30 using namespace OHOS::HDI::Usb::V1_0;
31
32 namespace OHOS {
33 namespace USB {
UsbServiceSubscriber()34 UsbServiceSubscriber::UsbServiceSubscriber() {}
35
PortChangedEvent(const PortInfo & info)36 int32_t UsbServiceSubscriber::PortChangedEvent(const PortInfo &info)
37 {
38 auto pms = DelayedSpSingleton<UsbService>::GetInstance();
39 if (pms == nullptr) {
40 USB_HILOGE(MODULE_USB_SERVICE, "failed to GetInstance");
41 return UEC_SERVICE_GET_USB_SERVICE_FAILED;
42 }
43
44 Json::Value port;
45 port["portId"] = info.portId;
46 port["powerRole"] = info.powerRole;
47 port["dataRole"] = info.dataRole;
48 port["mode"] = info.mode;
49
50 Json::StreamWriterBuilder builder;
51 builder["indentation"] = "";
52 auto jsonString = Json::writeString(builder, port);
53
54 Want want;
55 want.SetAction(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
56 pms->UpdateUsbPort(info.portId, info.powerRole, info.dataRole, info.mode);
57 CommonEventData data;
58 data.SetData(jsonString);
59 data.SetWant(want);
60 CommonEventPublishInfo publishInfo;
61 bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
62 if (!isSuccess) {
63 USB_HILOGE(MODULE_USB_SERVICE, "failed to publish PortChangedEvent");
64 }
65 return isSuccess;
66 }
67
DeviceEvent(const USBDeviceInfo & info)68 int32_t UsbServiceSubscriber::DeviceEvent(const USBDeviceInfo &info)
69 {
70 int32_t status = info.status;
71 auto pms = DelayedSpSingleton<UsbService>::GetInstance();
72 if (pms == nullptr) {
73 USB_HILOGE(MODULE_USB_SERVICE, "failed to GetInstance");
74 return UEC_SERVICE_GET_USB_SERVICE_FAILED;
75 }
76
77 if (status == ACT_UPDEVICE || status == ACT_DOWNDEVICE) {
78 pms->UpdateDeviceState(status);
79 pms->UnLoadSelf(UsbService::UnLoadSaType::UNLOAD_SA_DELAY);
80 return UEC_OK;
81 }
82
83 int32_t busNum = info.busNum;
84 int32_t devAddr = info.devNum;
85 if (status == ACT_DEVUP) {
86 USB_HILOGI(MODULE_USB_SERVICE, "usb attached");
87 pms->AddDevice(busNum, devAddr);
88 } else {
89 USB_HILOGI(MODULE_USB_SERVICE, "usb detached");
90 pms->DelDevice(busNum, devAddr);
91 }
92 pms->UnLoadSelf(UsbService::UnLoadSaType::UNLOAD_SA_DELAY);
93 return UEC_OK;
94 }
95 } // namespace USB
96 } // namespace OHOS
97