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_sink_stub.h"
17
18 #include "daudio_constants.h"
19 #include "daudio_errorcode.h"
20 #include "daudio_ipc_interface_code.h"
21 #include "daudio_log.h"
22
23 #undef DH_LOG_TAG
24 #define DH_LOG_TAG "DAudioSinkStub"
25
26 namespace OHOS {
27 namespace DistributedHardware {
DAudioSinkStub()28 DAudioSinkStub::DAudioSinkStub()
29 {
30 DHLOGD("Distributed audio sink stub constructed.");
31 memberFuncMap_[static_cast<uint32_t>(IDAudioSinkInterfaceCode::INIT_SINK)] =
32 &DAudioSinkStub::InitSinkInner;
33 memberFuncMap_[static_cast<uint32_t>(IDAudioSinkInterfaceCode::RELEASE_SINK)] =
34 &DAudioSinkStub::ReleaseSinkInner;
35 memberFuncMap_[static_cast<uint32_t>(IDAudioSinkInterfaceCode::SUBSCRIBE_LOCAL_HARDWARE)] =
36 &DAudioSinkStub::SubscribeLocalHardwareInner;
37 memberFuncMap_[static_cast<uint32_t>(IDAudioSinkInterfaceCode::UNSUBSCRIBE_LOCAL_HARDWARE)] =
38 &DAudioSinkStub::UnsubscribeLocalHardwareInner;
39 memberFuncMap_[static_cast<uint32_t>(IDAudioSinkInterfaceCode::DAUDIO_NOTIFY)] =
40 &DAudioSinkStub::DAudioNotifyInner;
41 }
42
~DAudioSinkStub()43 DAudioSinkStub::~DAudioSinkStub()
44 {
45 DHLOGD("Distributed audio sink stub deconstructed.");
46 }
47
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int32_t DAudioSinkStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
49 {
50 DHLOGD("On remote request, code: %d.", code);
51 std::u16string desc = DAudioSinkStub::GetDescriptor();
52 std::u16string remoteDesc = data.ReadInterfaceToken();
53 if (desc != remoteDesc) {
54 DHLOGE("RemoteDesc is invalid.");
55 return ERR_DH_AUDIO_SA_INVALID_INTERFACE_TOKEN;
56 }
57
58 const auto &iter = memberFuncMap_.find(code);
59 if (iter == memberFuncMap_.end()) {
60 DHLOGE("Invalid request code.");
61 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 }
63 DAudioSinkServiceFunc &func = iter->second;
64 return (this->*func)(data, reply, option);
65 }
66
InitSinkInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)67 int32_t DAudioSinkStub::InitSinkInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
68 {
69 std::string param = data.ReadString();
70 int32_t ret = InitSink(param);
71 reply.WriteInt32(ret);
72 return DH_SUCCESS;
73 }
74
ReleaseSinkInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)75 int32_t DAudioSinkStub::ReleaseSinkInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
76 {
77 int32_t ret = ReleaseSink();
78 reply.WriteInt32(ret);
79 return DH_SUCCESS;
80 }
81
SubscribeLocalHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)82 int32_t DAudioSinkStub::SubscribeLocalHardwareInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
83 {
84 std::string dhId = data.ReadString();
85 std::string param = data.ReadString();
86 int32_t ret = SubscribeLocalHardware(dhId, param);
87 reply.WriteInt32(ret);
88 return DH_SUCCESS;
89 }
90
UnsubscribeLocalHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)91 int32_t DAudioSinkStub::UnsubscribeLocalHardwareInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
92 {
93 std::string dhId = data.ReadString();
94 int32_t ret = UnsubscribeLocalHardware(dhId);
95 reply.WriteInt32(ret);
96 return DH_SUCCESS;
97 }
98
DAudioNotifyInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)99 int32_t DAudioSinkStub::DAudioNotifyInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
100 {
101 std::string networkId = data.ReadString();
102 std::string dhId = data.ReadString();
103 int32_t eventType = data.ReadInt32();
104 std::string eventContent = data.ReadString();
105
106 DAudioNotify(networkId, dhId, eventType, eventContent);
107 return DH_SUCCESS;
108 }
109 } // namespace DistributedHardware
110 } // namespace OHOS