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