1/*
2 * Copyright (c) 2024 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 "anco_channel_stub.h"
17
18#include "string_ex.h"
19
20#include "define_multimodal.h"
21
22#undef MMI_LOG_DOMAIN
23#define MMI_LOG_DOMAIN MMI_LOG_HANDLER
24#undef MMI_LOG_TAG
25#define MMI_LOG_TAG "AncoChannelStub"
26
27namespace OHOS {
28namespace MMI {
29
30AncoChannelStub::AncoChannelStub()
31{
32    handlers_ = {
33        { AncoRequestId::SYNC_POINTER_EVENT, &AncoChannelStub::StubSyncPointerEvent },
34        { AncoRequestId::SYNC_KEY_EVENT, &AncoChannelStub::StubSyncKeyEvent },
35        { AncoRequestId::UPDATE_WINDOW_INFO, &AncoChannelStub::StubUpdateWindowInfo },
36    };
37}
38
39int32_t AncoChannelStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
40    MessageParcel &reply, MessageOption &option)
41{
42    CALL_INFO_TRACE;
43    std::u16string descriptor = data.ReadInterfaceToken();
44    if (descriptor != IAncoChannel::GetDescriptor()) {
45        MMI_HILOGE("Got unexpected descriptor:%{public}s", Str16ToStr8(descriptor).c_str());
46        return ERR_INVALID_STATE;
47    }
48    int32_t ret = RET_ERR;
49    auto cbIter = handlers_.find(static_cast<AncoRequestId>(code));
50
51    if (cbIter == handlers_.end()) {
52        MMI_HILOGE("Unexpected request: %{public}u", code);
53    } else {
54        ret = (this->*(cbIter->second))(data, reply);
55    }
56    WRITEINT32(reply, ret);
57    return ret;
58}
59
60int32_t AncoChannelStub::StubSyncPointerEvent(MessageParcel &data, MessageParcel &reply)
61{
62    CALL_INFO_TRACE;
63    auto pointerEvent = PointerEvent::Create();
64    CHKPR(pointerEvent, RET_ERR);
65    if (!pointerEvent->ReadFromParcel(data)) {
66        MMI_HILOGE("Failed to unmarshal PointerEvent");
67        return RET_ERR;
68    }
69    return SyncInputEvent(pointerEvent);
70}
71
72int32_t AncoChannelStub::StubSyncKeyEvent(MessageParcel &data, MessageParcel &reply)
73{
74    auto keyEvent = KeyEvent::Create();
75    CHKPR(keyEvent, RET_ERR);
76    if (!keyEvent->ReadFromParcel(data)) {
77        MMI_HILOGE("Failed to unmarshal KeyEvent");
78        return RET_ERR;
79    }
80    return SyncInputEvent(keyEvent);
81}
82
83int32_t AncoChannelStub::StubUpdateWindowInfo(MessageParcel &data, MessageParcel &reply)
84{
85    auto windows = std::make_shared<AncoWindows>();
86    if (!AncoWindows::Unmarshalling(data, *windows)) {
87        MMI_HILOGE("Failed to unmarshal anco windows");
88        return RET_ERR;
89    }
90    return UpdateWindowInfo(windows);
91}
92} // namespace MMI
93} // namespace OHOS
94