1 /*
2 * Copyright (c) 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 "policy_provider_stub.h"
17 #include "audio_log.h"
18 #include "audio_errors.h"
19
20 namespace OHOS {
21 namespace AudioStandard {
CheckInterfaceToken(MessageParcel & data)22 bool PolicyProviderStub::CheckInterfaceToken(MessageParcel &data)
23 {
24 static auto localDescriptor = IPolicyProviderIpc::GetDescriptor();
25 auto remoteDescriptor = data.ReadInterfaceToken();
26 if (remoteDescriptor != localDescriptor) {
27 AUDIO_ERR_LOG("CheckInterFfaceToken failed.");
28 return false;
29 }
30 return true;
31 }
32
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int PolicyProviderStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
34 MessageOption &option)
35 {
36 if (!CheckInterfaceToken(data)) {
37 return AUDIO_ERR;
38 }
39 if (code >= IPolicyProviderMsg::POLICY_PROVIDER_MAX_MSG) {
40 AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
41 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
42 }
43 return (this->*funcList_[code])(data, reply);
44 }
45
HandleGetProcessDeviceInfo(MessageParcel & data,MessageParcel & reply)46 int32_t PolicyProviderStub::HandleGetProcessDeviceInfo(MessageParcel &data, MessageParcel &reply)
47 {
48 AudioProcessConfig config;
49 int32_t ret = ProcessConfig::ReadConfigFromParcel(config, data);
50 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "ReadConfigFromParcel failed %{public}d", ret);
51 DeviceInfo deviceInfo;
52 ret = GetProcessDeviceInfo(config, deviceInfo);
53 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "GetProcessDeviceInfo failed %{public}d", ret);
54
55 reply.WriteInt32(deviceInfo.deviceType);
56 reply.WriteInt32(deviceInfo.deviceRole);
57 reply.WriteInt32(deviceInfo.deviceId);
58 reply.WriteInt32(deviceInfo.channelMasks);
59 reply.WriteString(deviceInfo.deviceName);
60 reply.WriteString(deviceInfo.macAddress);
61
62 // AudioStreamInfo
63 reply.WriteInt32(deviceInfo.audioStreamInfo.samplingRate);
64 reply.WriteInt32(deviceInfo.audioStreamInfo.encoding);
65 reply.WriteInt32(deviceInfo.audioStreamInfo.format);
66 reply.WriteInt32(deviceInfo.audioStreamInfo.channels);
67
68 reply.WriteString(deviceInfo.networkId);
69 reply.WriteString(deviceInfo.displayName);
70 reply.WriteInt32(deviceInfo.interruptGroupId);
71 reply.WriteInt32(deviceInfo.volumeGroupId);
72 reply.WriteBool(deviceInfo.isLowLatencyDevice);
73
74 return AUDIO_OK;
75 }
76
HandleInitSharedVolume(MessageParcel & data,MessageParcel & reply)77 int32_t PolicyProviderStub::HandleInitSharedVolume(MessageParcel &data, MessageParcel &reply)
78 {
79 (void)data;
80 std::shared_ptr<AudioSharedMemory> buffer = nullptr;
81 int32_t ret = InitSharedVolume(buffer);
82 if (ret == SUCCESS && buffer != nullptr) {
83 ret = AudioSharedMemory::WriteToParcel(buffer, reply);
84 } else {
85 AUDIO_ERR_LOG("error: ResolveBuffer failed.");
86 return AUDIO_INVALID_PARAM;
87 }
88 return ret;
89 }
90
~PolicyProviderWrapper()91 PolicyProviderWrapper::~PolicyProviderWrapper()
92 {
93 AUDIO_INFO_LOG("~PolicyProviderWrapper()");
94 policyWorker_ = nullptr;
95 }
96
PolicyProviderWrapper(IPolicyProvider * policyWorker)97 PolicyProviderWrapper::PolicyProviderWrapper(IPolicyProvider *policyWorker) : policyWorker_(policyWorker)
98 {
99 AUDIO_INFO_LOG("PolicyProviderWrapper()");
100 }
101
GetProcessDeviceInfo(const AudioProcessConfig & config,DeviceInfo & deviceInfo)102 int32_t PolicyProviderWrapper::GetProcessDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo)
103 {
104 CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
105 return policyWorker_->GetProcessDeviceInfo(config, deviceInfo);
106 }
107
InitSharedVolume(std::shared_ptr<AudioSharedMemory> & buffer)108 int32_t PolicyProviderWrapper::InitSharedVolume(std::shared_ptr<AudioSharedMemory> &buffer)
109 {
110 CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
111 return policyWorker_->InitSharedVolume(buffer);
112 }
113 } // namespace AudioStandard
114 } // namespace OHOS
115