19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include "input_callback_impl.h"
179762338dSopenharmony_ci#include <hdf_base.h>
189762338dSopenharmony_ci#include <hdf_log.h>
199762338dSopenharmony_ci
209762338dSopenharmony_cinamespace OHOS {
219762338dSopenharmony_cinamespace HDI {
229762338dSopenharmony_cinamespace Input {
239762338dSopenharmony_cinamespace V1_0 {
249762338dSopenharmony_ciInputCallbackImpl::InputCallbackImpl(const wptr<IInputInterfaces> &inputInterfaces_,
259762338dSopenharmony_ci    const wptr<InputCallbackImpl> &reportCallback_) : inputInterfaces_(inputInterfaces_),
269762338dSopenharmony_ci    reportCallback_(reportCallback_)
279762338dSopenharmony_ci{}
289762338dSopenharmony_ci
299762338dSopenharmony_ciint32_t InputCallbackImpl::EventPkgCallback(const std::vector<EventPackage> &pkgs, uint32_t devIndex)
309762338dSopenharmony_ci{
319762338dSopenharmony_ci    if (pkgs.empty()) {
329762338dSopenharmony_ci        HDF_LOGE("%s: event packages are null\n", __func__);
339762338dSopenharmony_ci        return HDF_FAILURE;
349762338dSopenharmony_ci    }
359762338dSopenharmony_ci    for (uint32_t i = 0; i < pkgs.size(); i++) {
369762338dSopenharmony_ci        printf("%s: pkgs[%u] = 0x%x, 0x%x, %d\n", __func__, i, pkgs[i].type, pkgs[i].code, pkgs[i].value);
379762338dSopenharmony_ci    }
389762338dSopenharmony_ci    return HDF_SUCCESS;
399762338dSopenharmony_ci}
409762338dSopenharmony_ci
419762338dSopenharmony_ciint32_t InputCallbackImpl::HotPlugCallback(const HotPlugEvent &event)
429762338dSopenharmony_ci{
439762338dSopenharmony_ci    if (event.devIndex == 0) {
449762338dSopenharmony_ci        return HDF_FAILURE;
459762338dSopenharmony_ci    }
469762338dSopenharmony_ci
479762338dSopenharmony_ci    int32_t ret;
489762338dSopenharmony_ci    HDF_LOGI("%s: status = %d devId= %d type = %d", __func__, event.status, event.devIndex, event.devType);
499762338dSopenharmony_ci
509762338dSopenharmony_ci    if (event.status == 0) {
519762338dSopenharmony_ci        ret = inputInterfaces_->OpenInputDevice(event.devIndex);
529762338dSopenharmony_ci        if (ret != HDF_SUCCESS) {
539762338dSopenharmony_ci            HDF_LOGE("%s: open device[%u] failed, ret %d", __func__, event.devIndex, ret);
549762338dSopenharmony_ci            return HDF_FAILURE;
559762338dSopenharmony_ci        }
569762338dSopenharmony_ci
579762338dSopenharmony_ci        ret  = inputInterfaces_->RegisterReportCallback(event.devIndex, reportCallback_.GetRefPtr());
589762338dSopenharmony_ci        if (ret != HDF_SUCCESS) {
599762338dSopenharmony_ci            HDF_LOGE("%s: register callback failed for device[%d], ret %d", __func__, event.devIndex, ret);
609762338dSopenharmony_ci            return HDF_FAILURE;
619762338dSopenharmony_ci        }
629762338dSopenharmony_ci    } else {
639762338dSopenharmony_ci        ret = inputInterfaces_->UnregisterReportCallback(event.devIndex);
649762338dSopenharmony_ci        if (ret != HDF_SUCCESS) {
659762338dSopenharmony_ci            HDF_LOGE("%s: unregister callback failed, ret %d", __func__, ret);
669762338dSopenharmony_ci            return HDF_FAILURE;
679762338dSopenharmony_ci        }
689762338dSopenharmony_ci
699762338dSopenharmony_ci        ret = inputInterfaces_->CloseInputDevice(event.devIndex);
709762338dSopenharmony_ci        if (ret != HDF_SUCCESS) {
719762338dSopenharmony_ci            HDF_LOGE("%s: close device failed, ret %d", __func__, ret);
729762338dSopenharmony_ci            return HDF_FAILURE;
739762338dSopenharmony_ci        }
749762338dSopenharmony_ci    }
759762338dSopenharmony_ci    return HDF_SUCCESS;
769762338dSopenharmony_ci}
779762338dSopenharmony_ci} // V1_0
789762338dSopenharmony_ci} // Input
799762338dSopenharmony_ci} // HDI
809762338dSopenharmony_ci} // OHOS
81