1 /*
2 * Copyright (c) 2025 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_manager_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 "cJSON.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::V2_0;
31
32 namespace OHOS {
33 namespace USB {
UsbManagerSubscriber()34 UsbManagerSubscriber::UsbManagerSubscriber() {}
PortChangedEvent(const PortInfo & info)35 int32_t UsbManagerSubscriber::PortChangedEvent(const PortInfo &info)
36 {
37 #ifdef USB_MANAGER_FEATURE_PORT
38 auto pms = UsbService::GetGlobalInstance();
39 if (pms == nullptr) {
40 USB_HILOGE(MODULE_USB_SERVICE, "failed to GetInstance");
41 return UEC_SERVICE_GET_USB_SERVICE_FAILED;
42 }
43 cJSON* portJson = cJSON_CreateObject();
44 if (!portJson) {
45 USB_HILOGE(MODULE_USB_SERVICE, "Create portJson error");
46 return UEC_SERVICE_OBJECT_CREATE_FAILED;
47 }
48 cJSON_AddNumberToObject(portJson, "portId", static_cast<double>(info.portId));
49 cJSON_AddNumberToObject(portJson, "powerRole", static_cast<double>(info.powerRole));
50 cJSON_AddNumberToObject(portJson, "dataRole", static_cast<double>(info.dataRole));
51 cJSON_AddNumberToObject(portJson, "mode", static_cast<double>(info.mode));
52 cJSON_AddNumberToObject(portJson, "supportedModes", static_cast<double>(info.supportedModes));
53 auto jsonString = cJSON_PrintUnformatted(portJson);
54 Want want;
55 want.SetAction(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
56 pms->UpdateUsbPort(info.portId, info.powerRole, info.dataRole, info.mode, info.supportedModes);
57 CommonEventData data;
58 data.SetData(jsonString);
59 data.SetWant(want);
60 cJSON_Delete(portJson);
61 cJSON_free(jsonString);
62 CommonEventPublishInfo publishInfo;
63 bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
64 if (!isSuccess) {
65 USB_HILOGE(MODULE_USB_SERVICE, "failed to publish PortChangedEvent");
66 }
67 return isSuccess;
68 #else
69 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: Port feature is not supported currently.", __FUNCTION__);
70 return UEC_SERVICE_NOT_SUPPORT_SWITCH_PORT;
71 #endif // USB_MANAGER_FEATURE_PORT
72 }
73
DeviceEvent(const USBDeviceInfo & info)74 int32_t UsbManagerSubscriber::DeviceEvent(const USBDeviceInfo &info)
75 {
76 auto pms = UsbService::GetGlobalInstance();
77 if (pms == nullptr) {
78 USB_HILOGE(MODULE_USB_SERVICE, "failed to GetInstance");
79 return UEC_SERVICE_GET_USB_SERVICE_FAILED;
80 }
81
82 OHOS::HDI::Usb::V1_0::USBDeviceInfo deviceInfo {info.status, info.busNum, info.devNum};
83 return pms->DeviceEvent(deviceInfo);
84 }
85 } // namespace USB
86 } // namespace OHOS
87