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 "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::V1_0;
31
32 namespace OHOS {
33 namespace USB {
UsbServiceSubscriber()34 UsbServiceSubscriber::UsbServiceSubscriber() {}
PortChangedEvent(const PortInfo & info)35 int32_t UsbServiceSubscriber::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 auto jsonString = cJSON_PrintUnformatted(portJson);
53 Want want;
54 want.SetAction(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
55 pms->UpdateUsbPort(info.portId, info.powerRole, info.dataRole, info.mode);
56 CommonEventData data;
57 data.SetData(jsonString);
58 data.SetWant(want);
59 cJSON_Delete(portJson);
60 cJSON_free(jsonString);
61 CommonEventPublishInfo publishInfo;
62 bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
63 if (!isSuccess) {
64 USB_HILOGE(MODULE_USB_SERVICE, "failed to publish PortChangedEvent");
65 }
66 return isSuccess;
67 #else
68 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: Port feature is not supported.", __FUNCTION__);
69 return UEC_SERVICE_NOT_SUPPORT_SWITCH_PORT;
70 #endif // USB_MANAGER_FEATURE_PORT
71 }
72
DeviceEvent(const USBDeviceInfo & info)73 int32_t UsbServiceSubscriber::DeviceEvent(const USBDeviceInfo &info)
74 {
75 auto pms = UsbService::GetGlobalInstance();
76 if (pms == nullptr) {
77 USB_HILOGE(MODULE_USB_SERVICE, "failed to GetInstance");
78 return UEC_SERVICE_GET_USB_SERVICE_FAILED;
79 }
80 return pms->DeviceEvent(info);
81 }
82 } // namespace USB
83 } // namespace OHOS
84