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() {}
35
PortChangedEvent(const PortInfo & info)36 int32_t UsbServiceSubscriber::PortChangedEvent(const PortInfo &info)
37 {
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 }
68
DeviceEvent(const USBDeviceInfo & info)69 int32_t UsbServiceSubscriber::DeviceEvent(const USBDeviceInfo &info)
70 {
71 int32_t status = info.status;
72 auto pms = UsbService::GetGlobalInstance();
73 if (pms == nullptr) {
74 USB_HILOGE(MODULE_USB_SERVICE, "failed to GetInstance");
75 return UEC_SERVICE_GET_USB_SERVICE_FAILED;
76 }
77
78 if (status == ACT_UPDEVICE || status == ACT_DOWNDEVICE ||
79 status == ACT_ACCESSORYUP || status == ACT_ACCESSORYDOWN || status == ACT_ACCESSORYSEND) {
80 pms->UpdateDeviceState(status);
81 pms->UnLoadSelf(UsbService::UnLoadSaType::UNLOAD_SA_DELAY);
82 return UEC_OK;
83 }
84
85 int32_t busNum = info.busNum;
86 int32_t devAddr = info.devNum;
87 if (status == ACT_DEVUP) {
88 USB_HILOGI(MODULE_USB_SERVICE, "usb attached");
89 pms->AddDevice(busNum, devAddr);
90 } else {
91 USB_HILOGI(MODULE_USB_SERVICE, "usb detached");
92 pms->DelDevice(busNum, devAddr);
93 }
94 pms->UnLoadSelf(UsbService::UnLoadSaType::UNLOAD_SA_DELAY);
95 return UEC_OK;
96 }
97 } // namespace USB
98 } // namespace OHOS
99