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 "ipc_types.h"
17 #include "parcel.h"
18 
19 #include "dinput_errcode.h"
20 #include "dinput_log.h"
21 #include "start_stop_result_call_back_proxy.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
25 namespace DistributedInput {
StartStopResultCallbackProxy(const sptr<IRemoteObject> & object)26 StartStopResultCallbackProxy::StartStopResultCallbackProxy(const sptr<IRemoteObject> &object)
27     : IRemoteProxy<IStartStopResultCallback>(object)
28 {
29 }
30 
~StartStopResultCallbackProxy()31 StartStopResultCallbackProxy::~StartStopResultCallbackProxy() {}
32 
OnStart(const std::string & srcId,const std::string & sinkId,std::vector<std::string> & dhIds)33 void StartStopResultCallbackProxy::OnStart(
34     const std::string &srcId, const std::string &sinkId, std::vector<std::string> &dhIds)
35 {
36     sptr<IRemoteObject> remote = Remote();
37     if (remote == nullptr) {
38         DHLOGE("StartStopResultCallbackProxy get IRemoteObject failed");
39         return;
40     }
41 
42     MessageParcel data;
43     MessageParcel reply;
44     MessageOption option;
45 
46     if (!data.WriteInterfaceToken(GetDescriptor())) {
47         DHLOGE("StartStopResultCallbackProxy write token valid failed");
48         return;
49     }
50     if (!data.WriteString(srcId)) {
51         DHLOGE("StartStopResultCallbackProxy write srcId valid failed");
52         return;
53     }
54     if (!data.WriteString(sinkId)) {
55         DHLOGE("StartStopResultCallbackProxy write sinkId valid failed");
56         return;
57     }
58     if (!data.WriteUint32(dhIds.size())) {
59         DHLOGE("StartStopResultCallbackProxy write devData size valid failed");
60         return;
61     }
62     for (auto it = dhIds.begin(); it != dhIds.end(); ++it) {
63         if (!data.WriteString(*it)) {
64             DHLOGE("StartStopResultCallbackProxy write devData valid failed");
65             return;
66         }
67     }
68 
69     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IStartStopResultCallback::Message::RESULT_START), data,
70         reply, option);
71     if (ret != 0) {
72         DHLOGE("StartStopResultCallbackProxy SendRequest errno:%{public}d", ret);
73         return;
74     }
75     DHLOGE("OnStart success.");
76 }
77 
OnStop(const std::string & srcId,const std::string & sinkId,std::vector<std::string> & dhIds)78 void StartStopResultCallbackProxy::OnStop(
79     const std::string &srcId, const std::string &sinkId, std::vector<std::string> &dhIds)
80 {
81     sptr<IRemoteObject> remote = Remote();
82     if (remote == nullptr) {
83         DHLOGE("StartStopResultCallbackProxy get IRemoteObject failed");
84         return;
85     }
86 
87     MessageParcel data;
88     MessageParcel reply;
89     MessageOption option;
90     if (!data.WriteInterfaceToken(GetDescriptor())) {
91         DHLOGE("StartStopResultCallbackProxy write token valid failed");
92         return;
93     }
94     if (!data.WriteString(srcId)) {
95         DHLOGE("StartStopResultCallbackProxy write srcId valid failed");
96         return;
97     }
98     if (!data.WriteString(sinkId)) {
99         DHLOGE("StartStopResultCallbackProxy write sinkId valid failed");
100         return;
101     }
102 
103     if (!data.WriteUint32(dhIds.size())) {
104         DHLOGE("StartStopResultCallbackProxy write devData size valid failed");
105         return;
106     }
107     for (auto it = dhIds.begin(); it != dhIds.end(); ++it) {
108         if (!data.WriteString(*it)) {
109             DHLOGE("StartStopResultCallbackProxy write devData valid failed");
110             return;
111         }
112     }
113 
114     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IStartStopResultCallback::Message::RESULT_STOP), data,
115         reply, option);
116     if (ret != 0) {
117         DHLOGE("StartStopResultCallbackProxy SendRequest errno:%{public}d", ret);
118         return;
119     }
120     DHLOGI("OnStop success.");
121 }
122 } // namespace DistributedInput
123 } // namespace DistributedHardware
124 } // namespace OHOS
125