1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "input_callback_impl.h"
17094332d3Sopenharmony_ci#include <hdf_base.h>
18094332d3Sopenharmony_ci#include <hdf_log.h>
19094332d3Sopenharmony_ci#include "input_uhdf_log.h"
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_cinamespace OHOS {
22094332d3Sopenharmony_cinamespace HDI {
23094332d3Sopenharmony_cinamespace Input {
24094332d3Sopenharmony_cinamespace V1_0 {
25094332d3Sopenharmony_ciInputCallbackImpl::InputCallbackImpl(const wptr<IInputInterfaces> &inputInterfaces_,
26094332d3Sopenharmony_ci    const wptr<InputCallbackImpl> &reportCallback_) : inputInterfaces_(inputInterfaces_),
27094332d3Sopenharmony_ci    reportCallback_(reportCallback_)
28094332d3Sopenharmony_ci{}
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_ciint32_t InputCallbackImpl::EventPkgCallback(const std::vector<EventPackage> &pkgs, uint32_t devIndex)
31094332d3Sopenharmony_ci{
32094332d3Sopenharmony_ci    if (pkgs.empty()) {
33094332d3Sopenharmony_ci        HDF_LOGE("%s: event packages are null\n", __func__);
34094332d3Sopenharmony_ci        return HDF_FAILURE;
35094332d3Sopenharmony_ci    }
36094332d3Sopenharmony_ci    for (uint32_t i = 0; i < pkgs.size(); i++) {
37094332d3Sopenharmony_ci        printf("%s: pkgs[%u] = 0x%x, 0x%x, %d\n", __func__, i, pkgs[i].type, pkgs[i].code, pkgs[i].value);
38094332d3Sopenharmony_ci    }
39094332d3Sopenharmony_ci    return HDF_SUCCESS;
40094332d3Sopenharmony_ci}
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ciint32_t InputCallbackImpl::HotPlugCallback(const HotPlugEvent &event)
43094332d3Sopenharmony_ci{
44094332d3Sopenharmony_ci    if (event.devIndex == 0) {
45094332d3Sopenharmony_ci        return HDF_FAILURE;
46094332d3Sopenharmony_ci    }
47094332d3Sopenharmony_ci
48094332d3Sopenharmony_ci    int32_t ret;
49094332d3Sopenharmony_ci    HDF_LOGI("%s: status = %d devId= %d type = %d", __func__, event.status, event.devIndex, event.devType);
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ci    if (event.status == 0) {
52094332d3Sopenharmony_ci        ret = inputInterfaces_->OpenInputDevice(event.devIndex);
53094332d3Sopenharmony_ci        if (ret != HDF_SUCCESS) {
54094332d3Sopenharmony_ci            HDF_LOGE("%s: open device[%u] failed, ret %d", __func__, event.devIndex, ret);
55094332d3Sopenharmony_ci            return HDF_FAILURE;
56094332d3Sopenharmony_ci        }
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_ci        ret  = inputInterfaces_->RegisterReportCallback(event.devIndex, reportCallback_.GetRefPtr());
59094332d3Sopenharmony_ci        if (ret != HDF_SUCCESS) {
60094332d3Sopenharmony_ci            HDF_LOGE("%s: register callback failed for device[%d], ret %d", __func__, event.devIndex, ret);
61094332d3Sopenharmony_ci            return HDF_FAILURE;
62094332d3Sopenharmony_ci        }
63094332d3Sopenharmony_ci    } else {
64094332d3Sopenharmony_ci        ret = inputInterfaces_->UnregisterReportCallback(event.devIndex);
65094332d3Sopenharmony_ci        if (ret != HDF_SUCCESS) {
66094332d3Sopenharmony_ci            HDF_LOGE("%s: unregister callback failed, ret %d", __func__, ret);
67094332d3Sopenharmony_ci            return HDF_FAILURE;
68094332d3Sopenharmony_ci        }
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_ci        ret = inputInterfaces_->CloseInputDevice(event.devIndex);
71094332d3Sopenharmony_ci        if (ret != HDF_SUCCESS) {
72094332d3Sopenharmony_ci            HDF_LOGE("%s: close device failed, ret %d", __func__, ret);
73094332d3Sopenharmony_ci            return HDF_FAILURE;
74094332d3Sopenharmony_ci        }
75094332d3Sopenharmony_ci    }
76094332d3Sopenharmony_ci    return HDF_SUCCESS;
77094332d3Sopenharmony_ci}
78094332d3Sopenharmony_ci} // V1_0
79094332d3Sopenharmony_ci} // Input
80094332d3Sopenharmony_ci} // HDI
81094332d3Sopenharmony_ci} // OHOS
82