1 /*
2 * Copyright (c) 2022-2023 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 "daudio_source_stub.h"
17
18 #include "accesstoken_kit.h"
19 #include "ipc_skeleton.h"
20 #include "tokenid_kit.h"
21
22 #include "daudio_constants.h"
23 #include "daudio_errorcode.h"
24 #include "daudio_ipc_callback_proxy.h"
25 #include "daudio_ipc_interface_code.h"
26 #include "daudio_log.h"
27
28 #undef DH_LOG_TAG
29 #define DH_LOG_TAG "DAudioSourceStub"
30
31 namespace OHOS {
32 namespace DistributedHardware {
DAudioSourceStub()33 DAudioSourceStub::DAudioSourceStub() : IRemoteStub(true)
34 {
35 memberFuncMap_[static_cast<uint32_t>(IDAudioSourceInterfaceCode::INIT_SOURCE)] =
36 &DAudioSourceStub::InitSourceInner;
37 memberFuncMap_[static_cast<uint32_t>(IDAudioSourceInterfaceCode::RELEASE_SOURCE)] =
38 &DAudioSourceStub::ReleaseSourceInner;
39 memberFuncMap_[static_cast<uint32_t>(IDAudioSourceInterfaceCode::REGISTER_DISTRIBUTED_HARDWARE)] =
40 &DAudioSourceStub::RegisterDistributedHardwareInner;
41 memberFuncMap_[static_cast<uint32_t>(IDAudioSourceInterfaceCode::UNREGISTER_DISTRIBUTED_HARDWARE)] =
42 &DAudioSourceStub::UnregisterDistributedHardwareInner;
43 memberFuncMap_[static_cast<uint32_t>(IDAudioSourceInterfaceCode::CONFIG_DISTRIBUTED_HARDWARE)] =
44 &DAudioSourceStub::ConfigDistributedHardwareInner;
45 memberFuncMap_[static_cast<uint32_t>(IDAudioSourceInterfaceCode::DAUDIO_NOTIFY)] =
46 &DAudioSourceStub::DAudioNotifyInner;
47 }
48
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)49 int32_t DAudioSourceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
50 MessageOption &option)
51 {
52 DHLOGI("On remote request, code: %{public}d.", code);
53 std::u16string desc = DAudioSourceStub::GetDescriptor();
54 std::u16string remoteDesc = data.ReadInterfaceToken();
55 if (desc != remoteDesc) {
56 DHLOGE("Remote desc is invalid.");
57 return ERR_DH_AUDIO_SA_INVALID_INTERFACE_TOKEN;
58 }
59
60 switch (code) {
61 case static_cast<uint32_t>(IDAudioSourceInterfaceCode::INIT_SOURCE):
62 return InitSourceInner(data, reply, option);
63 case static_cast<uint32_t>(IDAudioSourceInterfaceCode::RELEASE_SOURCE):
64 return ReleaseSourceInner(data, reply, option);
65 case static_cast<uint32_t>(IDAudioSourceInterfaceCode::REGISTER_DISTRIBUTED_HARDWARE):
66 return RegisterDistributedHardwareInner(data, reply, option);
67 case static_cast<uint32_t>(IDAudioSourceInterfaceCode::UNREGISTER_DISTRIBUTED_HARDWARE):
68 return UnregisterDistributedHardwareInner(data, reply, option);
69 case static_cast<uint32_t>(IDAudioSourceInterfaceCode::CONFIG_DISTRIBUTED_HARDWARE):
70 return ConfigDistributedHardwareInner(data, reply, option);
71 case static_cast<uint32_t>(IDAudioSourceInterfaceCode::DAUDIO_NOTIFY):
72 return DAudioNotifyInner(data, reply, option);
73 default:
74 DHLOGE("Invalid request code.");
75 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
76 }
77 return ERR_DH_AUDIO_NOT_FOUND_KEY;
78 }
79
VerifyPermission()80 bool DAudioSourceStub::VerifyPermission()
81 {
82 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
83 int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, AUDIO_PERMISSION_NAME);
84 if (result == Security::AccessToken::PERMISSION_GRANTED) {
85 return true;
86 }
87 return false;
88 }
89
InitSourceInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)90 int32_t DAudioSourceStub::InitSourceInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
91 {
92 if (!VerifyPermission()) {
93 DHLOGE("Permission verification fail.");
94 return ERR_DH_AUDIO_SA_PERMISSION_FAIED;
95 }
96 std::string param = data.ReadString();
97 sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
98 CHECK_NULL_RETURN(remoteObject, ERR_DH_AUDIO_NULLPTR);
99 sptr<DAudioIpcCallbackProxy> dAudioIpcCallbackProxy(new DAudioIpcCallbackProxy(remoteObject));
100 int32_t ret = InitSource(param, dAudioIpcCallbackProxy);
101 reply.WriteInt32(ret);
102 return DH_SUCCESS;
103 }
104
ReleaseSourceInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)105 int32_t DAudioSourceStub::ReleaseSourceInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
106 {
107 if (!VerifyPermission()) {
108 DHLOGE("Permission verification fail.");
109 return ERR_DH_AUDIO_SA_PERMISSION_FAIED;
110 }
111 int32_t ret = ReleaseSource();
112 reply.WriteInt32(ret);
113 return DH_SUCCESS;
114 }
115
RegisterDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)116 int32_t DAudioSourceStub::RegisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
117 MessageOption &option)
118 {
119 if (!VerifyPermission()) {
120 DHLOGE("Permission verification fail.");
121 return ERR_DH_AUDIO_SA_PERMISSION_FAIED;
122 }
123 std::string networkId = data.ReadString();
124 std::string dhId = data.ReadString();
125 std::string version = data.ReadString();
126 std::string attrs = data.ReadString();
127 std::string reqId = data.ReadString();
128 EnableParam enableParam;
129 enableParam.sinkVersion = version;
130 enableParam.sinkAttrs = attrs;
131
132 int32_t ret = RegisterDistributedHardware(networkId, dhId, enableParam, reqId);
133 reply.WriteInt32(ret);
134 return DH_SUCCESS;
135 }
136
UnregisterDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)137 int32_t DAudioSourceStub::UnregisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
138 MessageOption &option)
139 {
140 if (!VerifyPermission()) {
141 DHLOGE("Permission verification fail.");
142 return ERR_DH_AUDIO_SA_PERMISSION_FAIED;
143 }
144 std::string networkId = data.ReadString();
145 std::string dhId = data.ReadString();
146 std::string reqId = data.ReadString();
147
148 int32_t ret = UnregisterDistributedHardware(networkId, dhId, reqId);
149 reply.WriteInt32(ret);
150 return DH_SUCCESS;
151 }
152
ConfigDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)153 int32_t DAudioSourceStub::ConfigDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
154 MessageOption &option)
155 {
156 if (!VerifyPermission()) {
157 DHLOGE("Permission verification fail.");
158 return ERR_DH_AUDIO_SA_PERMISSION_FAIED;
159 }
160 std::string networkId = data.ReadString();
161 std::string dhId = data.ReadString();
162 std::string key = data.ReadString();
163 std::string value = data.ReadString();
164
165 int32_t ret = ConfigDistributedHardware(networkId, dhId, key, value);
166 reply.WriteInt32(ret);
167 return DH_SUCCESS;
168 }
169
DAudioNotifyInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)170 int32_t DAudioSourceStub::DAudioNotifyInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
171 {
172 std::string networkId = data.ReadString();
173 std::string dhId = data.ReadString();
174 int32_t eventType = data.ReadInt32();
175 std::string eventContent = data.ReadString();
176
177 DAudioNotify(networkId, dhId, eventType, eventContent);
178 return DH_SUCCESS;
179 }
180 } // namespace DistributedHardware
181 } // namespace OHOS
182