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