142103316Sopenharmony_ci/*
242103316Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
342103316Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
442103316Sopenharmony_ci * you may not use this file except in compliance with the License.
542103316Sopenharmony_ci * You may obtain a copy of the License at
642103316Sopenharmony_ci *
742103316Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
842103316Sopenharmony_ci *
942103316Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1042103316Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1142103316Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1242103316Sopenharmony_ci * See the License for the specific language governing permissions and
1342103316Sopenharmony_ci * limitations under the License.
1442103316Sopenharmony_ci */
1542103316Sopenharmony_ci
1642103316Sopenharmony_ci#include "usb_service_subscriber.h"
1742103316Sopenharmony_ci#include "common_event_data.h"
1842103316Sopenharmony_ci#include "common_event_manager.h"
1942103316Sopenharmony_ci#include "common_event_support.h"
2042103316Sopenharmony_ci#include "delayed_sp_singleton.h"
2142103316Sopenharmony_ci#include "cJSON.h"
2242103316Sopenharmony_ci#include "string_ex.h"
2342103316Sopenharmony_ci#include "usb_common.h"
2442103316Sopenharmony_ci#include "usb_errors.h"
2542103316Sopenharmony_ci#include "usb_service.h"
2642103316Sopenharmony_ci#include "want.h"
2742103316Sopenharmony_ci
2842103316Sopenharmony_ciusing namespace OHOS::AAFwk;
2942103316Sopenharmony_ciusing namespace OHOS::EventFwk;
3042103316Sopenharmony_ciusing namespace OHOS::HDI::Usb::V1_0;
3142103316Sopenharmony_ci
3242103316Sopenharmony_cinamespace OHOS {
3342103316Sopenharmony_cinamespace USB {
3442103316Sopenharmony_ciUsbServiceSubscriber::UsbServiceSubscriber() {}
3542103316Sopenharmony_ciextern sptr<UsbService> g_serviceInstance;
3642103316Sopenharmony_ciint32_t UsbServiceSubscriber::PortChangedEvent(const PortInfo &info)
3742103316Sopenharmony_ci{
3842103316Sopenharmony_ci    auto pms = g_serviceInstance;
3942103316Sopenharmony_ci    if (pms == nullptr) {
4042103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_SERVICE, "failed to GetInstance");
4142103316Sopenharmony_ci        return UEC_SERVICE_GET_USB_SERVICE_FAILED;
4242103316Sopenharmony_ci    }
4342103316Sopenharmony_ci    cJSON* portJson = cJSON_CreateObject();
4442103316Sopenharmony_ci    if (!portJson) {
4542103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_SERVICE, "Create portJson error");
4642103316Sopenharmony_ci        return UEC_SERVICE_OBJECT_CREATE_FAILED;
4742103316Sopenharmony_ci    }
4842103316Sopenharmony_ci    cJSON_AddNumberToObject(portJson, "portId", static_cast<double>(info.portId));
4942103316Sopenharmony_ci    cJSON_AddNumberToObject(portJson, "powerRole", static_cast<double>(info.powerRole));
5042103316Sopenharmony_ci    cJSON_AddNumberToObject(portJson, "dataRole", static_cast<double>(info.dataRole));
5142103316Sopenharmony_ci    cJSON_AddNumberToObject(portJson, "mode", static_cast<double>(info.mode));
5242103316Sopenharmony_ci    auto jsonString = cJSON_PrintUnformatted(portJson);
5342103316Sopenharmony_ci    Want want;
5442103316Sopenharmony_ci    want.SetAction(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
5542103316Sopenharmony_ci    pms->UpdateUsbPort(info.portId, info.powerRole, info.dataRole, info.mode);
5642103316Sopenharmony_ci    CommonEventData data;
5742103316Sopenharmony_ci    data.SetData(jsonString);
5842103316Sopenharmony_ci    data.SetWant(want);
5942103316Sopenharmony_ci    cJSON_Delete(portJson);
6042103316Sopenharmony_ci    cJSON_free(jsonString);
6142103316Sopenharmony_ci    CommonEventPublishInfo publishInfo;
6242103316Sopenharmony_ci    bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
6342103316Sopenharmony_ci    if (!isSuccess) {
6442103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_SERVICE, "failed to publish PortChangedEvent");
6542103316Sopenharmony_ci    }
6642103316Sopenharmony_ci    return isSuccess;
6742103316Sopenharmony_ci}
6842103316Sopenharmony_ci
6942103316Sopenharmony_ciint32_t UsbServiceSubscriber::DeviceEvent(const USBDeviceInfo &info)
7042103316Sopenharmony_ci{
7142103316Sopenharmony_ci    int32_t status = info.status;
7242103316Sopenharmony_ci    auto pms = g_serviceInstance;
7342103316Sopenharmony_ci    USB_HILOGE(MODULE_USB_SERVICE, "DeviceEvent =========");
7442103316Sopenharmony_ci    if (pms == nullptr) {
7542103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_SERVICE, "failed to GetInstance");
7642103316Sopenharmony_ci        return UEC_SERVICE_GET_USB_SERVICE_FAILED;
7742103316Sopenharmony_ci    }
7842103316Sopenharmony_ci
7942103316Sopenharmony_ci    if (status == ACT_UPDEVICE || status == ACT_DOWNDEVICE) {
8042103316Sopenharmony_ci        pms->UpdateDeviceState(status);
8142103316Sopenharmony_ci        pms->UnLoadSelf(UsbService::UnLoadSaType::UNLOAD_SA_DELAY);
8242103316Sopenharmony_ci        return UEC_OK;
8342103316Sopenharmony_ci    }
8442103316Sopenharmony_ci
8542103316Sopenharmony_ci    int32_t busNum = info.busNum;
8642103316Sopenharmony_ci    int32_t devAddr = info.devNum;
8742103316Sopenharmony_ci    if (status == ACT_DEVUP) {
8842103316Sopenharmony_ci        USB_HILOGI(MODULE_USB_SERVICE, "usb attached");
8942103316Sopenharmony_ci        pms->AddDevice(busNum, devAddr);
9042103316Sopenharmony_ci    } else {
9142103316Sopenharmony_ci        USB_HILOGI(MODULE_USB_SERVICE, "usb detached");
9242103316Sopenharmony_ci        pms->DelDevice(busNum, devAddr);
9342103316Sopenharmony_ci    }
9442103316Sopenharmony_ci    pms->UnLoadSelf(UsbService::UnLoadSaType::UNLOAD_SA_DELAY);
9542103316Sopenharmony_ci    return UEC_OK;
9642103316Sopenharmony_ci}
9742103316Sopenharmony_ci} // namespace USB
9842103316Sopenharmony_ci} // namespace OHOS
99