1 /*
2  * Copyright (c) 2021-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 "event_filter_stub.h"
17 
18 #include <sys/socket.h>
19 #include <sys/types.h>
20 
21 #include "ipc_skeleton.h"
22 #include "string_ex.h"
23 
24 #include "mmi_log.h"
25 #include "multimodalinput_ipc_interface_code.h"
26 
27 #undef MMI_LOG_DOMAIN
28 #define MMI_LOG_DOMAIN MMI_LOG_HANDLER
29 #undef MMI_LOG_TAG
30 #define MMI_LOG_TAG "EventFilterStub"
31 
32 namespace OHOS {
33 namespace MMI {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t EventFilterStub::OnRemoteRequest(
35     uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
36 {
37     CALL_DEBUG_ENTER;
38     MMI_HILOGD("code:%{public}u", code);
39 
40     std::u16string descriptor = data.ReadInterfaceToken();
41     if (descriptor != IEventFilter::GetDescriptor()) {
42         MMI_HILOGE("Get unexpect descriptor:%{public}s", Str16ToStr8(descriptor).c_str());
43         return ERR_INVALID_STATE;
44     }
45 
46     switch (code) {
47         case static_cast<uint32_t>(MultimodalinputEventInterfaceCode::HANDLE_KEY_EVENT): {
48             return StubHandleKeyEvent(data, reply);
49         }
50         case static_cast<uint32_t>(MultimodalinputEventInterfaceCode::HANDLE_POINTER_EVENT): {
51             return StubHandlePointerEvent(data, reply);
52         }
53         default: {
54             MMI_HILOGE("Unknown code:%{public}u, go switch default", code);
55             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
56         }
57     }
58 }
59 
StubHandleKeyEvent(MessageParcel & data,MessageParcel & reply)60 int32_t EventFilterStub::StubHandleKeyEvent(MessageParcel& data, MessageParcel& reply)
61 {
62     CALL_DEBUG_ENTER;
63     std::shared_ptr<KeyEvent> event = KeyEvent::Create();
64     CHKPR(event, RET_ERR);
65 
66     if (!event->ReadFromParcel(data)) {
67         MMI_HILOGE("Read data error");
68         return RET_ERR;
69     }
70 
71     bool ret = HandleKeyEvent(event);
72     WRITEBOOL(reply, ret, RET_ERR);
73     return RET_OK;
74 }
75 
StubHandlePointerEvent(MessageParcel & data,MessageParcel & reply)76 int32_t EventFilterStub::StubHandlePointerEvent(MessageParcel& data, MessageParcel& reply)
77 {
78     CALL_DEBUG_ENTER;
79     std::shared_ptr<PointerEvent> event = PointerEvent::Create();
80     CHKPR(event, RET_ERR);
81 
82     if (!event->ReadFromParcel(data)) {
83         MMI_HILOGE("Read data error");
84         return RET_ERR;
85     }
86 
87     bool ret = HandlePointerEvent(event);
88     WRITEBOOL(reply, ret, RET_ERR);
89     return RET_OK;
90 }
91 } // namespace MMI
92 } // namespace OHOS