1/*
2 * Copyright (c) 2022-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 "input_callback_impl.h"
17#include <hdf_base.h>
18#include <hdf_log.h>
19#include "input_uhdf_log.h"
20
21namespace OHOS {
22namespace HDI {
23namespace Input {
24namespace V1_0 {
25InputCallbackImpl::InputCallbackImpl(const wptr<IInputInterfaces> &inputInterfaces_,
26    const wptr<InputCallbackImpl> &reportCallback_) : inputInterfaces_(inputInterfaces_),
27    reportCallback_(reportCallback_)
28{}
29
30int32_t InputCallbackImpl::EventPkgCallback(const std::vector<EventPackage> &pkgs, uint32_t devIndex)
31{
32    if (pkgs.empty()) {
33        HDF_LOGE("%s: event packages are null\n", __func__);
34        return HDF_FAILURE;
35    }
36    for (uint32_t i = 0; i < pkgs.size(); i++) {
37        printf("%s: pkgs[%u] = 0x%x, 0x%x, %d\n", __func__, i, pkgs[i].type, pkgs[i].code, pkgs[i].value);
38    }
39    return HDF_SUCCESS;
40}
41
42int32_t InputCallbackImpl::HotPlugCallback(const HotPlugEvent &event)
43{
44    if (event.devIndex == 0) {
45        return HDF_FAILURE;
46    }
47
48    int32_t ret;
49    HDF_LOGI("%s: status = %d devId= %d type = %d", __func__, event.status, event.devIndex, event.devType);
50
51    if (event.status == 0) {
52        ret = inputInterfaces_->OpenInputDevice(event.devIndex);
53        if (ret != HDF_SUCCESS) {
54            HDF_LOGE("%s: open device[%u] failed, ret %d", __func__, event.devIndex, ret);
55            return HDF_FAILURE;
56        }
57
58        ret  = inputInterfaces_->RegisterReportCallback(event.devIndex, reportCallback_.GetRefPtr());
59        if (ret != HDF_SUCCESS) {
60            HDF_LOGE("%s: register callback failed for device[%d], ret %d", __func__, event.devIndex, ret);
61            return HDF_FAILURE;
62        }
63    } else {
64        ret = inputInterfaces_->UnregisterReportCallback(event.devIndex);
65        if (ret != HDF_SUCCESS) {
66            HDF_LOGE("%s: unregister callback failed, ret %d", __func__, ret);
67            return HDF_FAILURE;
68        }
69
70        ret = inputInterfaces_->CloseInputDevice(event.devIndex);
71        if (ret != HDF_SUCCESS) {
72            HDF_LOGE("%s: close device failed, ret %d", __func__, ret);
73            return HDF_FAILURE;
74        }
75    }
76    return HDF_SUCCESS;
77}
78} // V1_0
79} // Input
80} // HDI
81} // OHOS
82