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 "av_trans_control_center_callback_proxy.h"
17 
18 #include <unordered_set>
19 #include "parcel.h"
20 
21 #include "av_trans_constants.h"
22 #include "av_trans_errno.h"
23 #include "av_trans_log.h"
24 #include "av_trans_types.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 #undef DH_LOG_TAG
29 #define DH_LOG_TAG "AVTransControlCenterCallbackProxy"
30 
SetParameter(AVTransTag tag,const std::string & value)31 int32_t AVTransControlCenterCallbackProxy::SetParameter(AVTransTag tag, const std::string& value)
32 {
33     sptr<IRemoteObject> remote = Remote();
34     if (remote == nullptr) {
35         AVTRANS_LOGE("remote service is null");
36         return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL;
37     }
38 
39     MessageParcel data;
40     MessageParcel reply;
41     MessageOption option;
42 
43     if (!data.WriteInterfaceToken(GetDescriptor())) {
44         AVTRANS_LOGE("WriteInterfaceToken fail!");
45         return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL;
46     }
47     if (!data.WriteUint32((uint32_t)tag)) {
48         AVTRANS_LOGE("Write tag failed");
49         return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
50     }
51     if (!data.WriteString(value)) {
52         AVTRANS_LOGE("Write value failed");
53         return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
54     }
55     int32_t ret = remote->SendRequest((uint32_t)IAVTransControlCenterCallback::Message::SET_PARAMETER,
56         data, reply, option);
57     if (ret != NO_ERROR) {
58         AVTRANS_LOGE("Send Request failed, ret: %{public}d", ret);
59         return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL;
60     }
61 
62     return reply.ReadInt32();
63 }
64 
SetSharedMemory(const AVTransSharedMemory & memory)65 int32_t AVTransControlCenterCallbackProxy::SetSharedMemory(const AVTransSharedMemory &memory)
66 {
67     sptr<IRemoteObject> remote = Remote();
68     if (remote == nullptr) {
69         AVTRANS_LOGE("remote service is null");
70         return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL;
71     }
72 
73     MessageParcel data;
74     MessageParcel reply;
75     MessageOption option;
76 
77     if (!data.WriteInterfaceToken(GetDescriptor())) {
78         AVTRANS_LOGE("WriteInterfaceToken fail!");
79         return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL;
80     }
81     if (!data.WriteFileDescriptor(memory.fd)) {
82         AVTRANS_LOGE("Write memory fd failed");
83         return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
84     }
85     if (!data.WriteInt32(memory.size)) {
86         AVTRANS_LOGE("Write memory size failed");
87         return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
88     }
89     if (!data.WriteString(memory.name)) {
90         AVTRANS_LOGE("Write memory name failed");
91         return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
92     }
93     int32_t ret = remote->SendRequest((uint32_t)IAVTransControlCenterCallback::Message::SET_SHARED_MEMORY,
94         data, reply, option);
95     if (ret != NO_ERROR) {
96         AVTRANS_LOGE("Send Request failed, ret: %{public}d", ret);
97         return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL;
98     }
99 
100     return reply.ReadInt32();
101 }
102 
Notify(const AVTransEvent & event)103 int32_t AVTransControlCenterCallbackProxy::Notify(const AVTransEvent& event)
104 {
105     sptr<IRemoteObject> remote = Remote();
106     if (remote == nullptr) {
107         AVTRANS_LOGE("remote service is null");
108         return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL;
109     }
110 
111     MessageParcel data;
112     MessageParcel reply;
113     MessageOption option;
114 
115     if (!data.WriteInterfaceToken(GetDescriptor())) {
116         AVTRANS_LOGE("WriteInterfaceToken fail!");
117         return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL;
118     }
119     if (!data.WriteUint32((uint32_t)event.type)) {
120         AVTRANS_LOGE("Write event type failed");
121         return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
122     }
123     if (!data.WriteString(event.content)) {
124         AVTRANS_LOGE("Write event content failed");
125         return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
126     }
127     if (!data.WriteString(event.peerDevId)) {
128         AVTRANS_LOGE("Write event peerDevId failed");
129         return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
130     }
131     int32_t ret = remote->SendRequest((uint32_t)IAVTransControlCenterCallback::Message::NOTIFY_AV_EVENT,
132         data, reply, option);
133     if (ret != NO_ERROR) {
134         AVTRANS_LOGE("Send Request failed, ret: %{public}d", ret);
135         return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL;
136     }
137 
138     return reply.ReadInt32();
139 }
140 } // namespace DistributedHardware
141 } // namespace OHOS