1 /*
2  * Copyright (c) 2022-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 "sharing_dhid_listener_proxy.h"
17 
18 #include "ipc_types.h"
19 #include "parcel.h"
20 
21 #include "dinput_errcode.h"
22 #include "dinput_log.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace DistributedInput {
SharingDhIdListenerProxy(const sptr<IRemoteObject> & object)27 SharingDhIdListenerProxy::SharingDhIdListenerProxy(const sptr<IRemoteObject> &object)
28     : IRemoteProxy<ISharingDhIdListener>(object)
29 {
30 }
31 
~SharingDhIdListenerProxy()32 SharingDhIdListenerProxy::~SharingDhIdListenerProxy() {}
33 
OnSharing(const std::string & dhId)34 int32_t SharingDhIdListenerProxy::OnSharing(const std::string &dhId)
35 {
36     int32_t result = ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
37     sptr<IRemoteObject> remote = Remote();
38     if (remote == nullptr) {
39         DHLOGE("SharingDhIdListenerProxy get remote failed");
40         return result;
41     }
42 
43     MessageParcel data;
44     MessageParcel reply;
45     MessageOption option;
46     if (!data.WriteInterfaceToken(GetDescriptor())) {
47         DHLOGE("SharingDhIdListenerProxy write token valid failed");
48         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
49     }
50     if (!data.WriteString(dhId)) {
51         DHLOGE("SharingDhIdListenerProxy write dhId failed");
52         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
53     }
54 
55     int32_t ret =
56         remote->SendRequest(static_cast<uint32_t>(ISharingDhIdListener::Message::SHARING), data, reply, option);
57     if (ret == DH_SUCCESS) {
58         result = reply.ReadInt32();
59     } else {
60         DHLOGE("SharingDhIdListenerProxy SendRequest error: %{public}d", ret);
61     }
62     return result;
63 }
64 
OnNoSharing(const std::string & dhId)65 int32_t SharingDhIdListenerProxy::OnNoSharing(const std::string &dhId)
66 {
67     int32_t result = ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
68     sptr<IRemoteObject> remote = Remote();
69     if (remote == nullptr) {
70         DHLOGE("SharingDhIdListenerProxy get remote failed");
71         return result;
72     }
73 
74     MessageParcel data;
75     MessageParcel reply;
76     MessageOption option;
77     if (!data.WriteInterfaceToken(GetDescriptor())) {
78         DHLOGE("SharingDhIdListenerProxy write token valid failed");
79         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
80     }
81     if (!data.WriteString(dhId)) {
82         DHLOGE("SharingDhIdListenerProxy write dhId failed");
83         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
84     }
85 
86     int32_t ret =
87         remote->SendRequest(static_cast<uint32_t>(ISharingDhIdListener::Message::NO_SHARING), data, reply, option);
88     if (ret == DH_SUCCESS) {
89         result = reply.ReadInt32();
90     } else {
91         DHLOGE("SharingDhIdListenerProxy SendRequest error: %{public}d", ret);
92     }
93     return result;
94 }
95 } // namespace DistributedInput
96 } // namespace DistributedHardware
97 } // namespace OHOS
98