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()
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 std::u16string desc = DAudioSourceStub::GetDescriptor();
53 std::u16string remoteDesc = data.ReadInterfaceToken();
54 if (desc != remoteDesc) {
55 DHLOGE("Remote desc is invalid.");
56 return ERR_DH_AUDIO_SA_INVALID_INTERFACE_TOKEN;
57 }
58
59 const auto &iter = memberFuncMap_.find(code);
60 if (iter == memberFuncMap_.end()) {
61 DHLOGE("Invalid request code.");
62 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
63 }
64 DAudioSourceServiceFunc &func = iter->second;
65 return (this->*func)(data, reply, option);
66 }
67
VerifyPermission()68 bool DAudioSourceStub::VerifyPermission()
69 {
70 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
71 int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, AUDIO_PERMISSION_NAME);
72 if (result == Security::AccessToken::PERMISSION_GRANTED) {
73 return true;
74 }
75 return false;
76 }
77
InitSourceInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)78 int32_t DAudioSourceStub::InitSourceInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
79 {
80 if (!VerifyPermission()) {
81 DHLOGE("Permission verification fail.");
82 return ERR_DH_AUDIO_SA_PERMISSION_FAIED;
83 }
84 std::string param = data.ReadString();
85 sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
86 CHECK_NULL_RETURN(remoteObject, ERR_DH_AUDIO_NULLPTR);
87 sptr<DAudioIpcCallbackProxy> dAudioIpcCallbackProxy(new DAudioIpcCallbackProxy(remoteObject));
88 int32_t ret = InitSource(param, dAudioIpcCallbackProxy);
89 reply.WriteInt32(ret);
90 return DH_SUCCESS;
91 }
92
ReleaseSourceInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)93 int32_t DAudioSourceStub::ReleaseSourceInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
94 {
95 if (!VerifyPermission()) {
96 DHLOGE("Permission verification fail.");
97 return ERR_DH_AUDIO_SA_PERMISSION_FAIED;
98 }
99 int32_t ret = ReleaseSource();
100 reply.WriteInt32(ret);
101 return DH_SUCCESS;
102 }
103
RegisterDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)104 int32_t DAudioSourceStub::RegisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
105 MessageOption &option)
106 {
107 if (!VerifyPermission()) {
108 DHLOGE("Permission verification fail.");
109 return ERR_DH_AUDIO_SA_PERMISSION_FAIED;
110 }
111 std::string networkId = data.ReadString();
112 std::string dhId = data.ReadString();
113 std::string version = data.ReadString();
114 std::string attrs = data.ReadString();
115 std::string reqId = data.ReadString();
116 EnableParam enableParam;
117 enableParam.sinkVersion = version;
118 enableParam.sinkAttrs = attrs;
119
120 int32_t ret = RegisterDistributedHardware(networkId, dhId, enableParam, reqId);
121 reply.WriteInt32(ret);
122 return DH_SUCCESS;
123 }
124
UnregisterDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)125 int32_t DAudioSourceStub::UnregisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
126 MessageOption &option)
127 {
128 if (!VerifyPermission()) {
129 DHLOGE("Permission verification fail.");
130 return ERR_DH_AUDIO_SA_PERMISSION_FAIED;
131 }
132 std::string networkId = data.ReadString();
133 std::string dhId = data.ReadString();
134 std::string reqId = data.ReadString();
135
136 int32_t ret = UnregisterDistributedHardware(networkId, dhId, reqId);
137 reply.WriteInt32(ret);
138 return DH_SUCCESS;
139 }
140
ConfigDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)141 int32_t DAudioSourceStub::ConfigDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
142 MessageOption &option)
143 {
144 std::string networkId = data.ReadString();
145 std::string dhId = data.ReadString();
146 std::string key = data.ReadString();
147 std::string value = data.ReadString();
148
149 int32_t ret = ConfigDistributedHardware(networkId, dhId, key, value);
150 reply.WriteInt32(ret);
151 return DH_SUCCESS;
152 }
153
DAudioNotifyInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)154 int32_t DAudioSourceStub::DAudioNotifyInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
155 {
156 std::string networkId = data.ReadString();
157 std::string dhId = data.ReadString();
158 int32_t eventType = data.ReadInt32();
159 std::string eventContent = data.ReadString();
160
161 DAudioNotify(networkId, dhId, eventType, eventContent);
162 return DH_SUCCESS;
163 }
164 } // namespace DistributedHardware
165 } // namespace OHOS
166