1 /*
2 * Copyright (c) 2021-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 "audio_manager_base.h"
17 #include "audio_system_manager.h"
18 #include "media_log.h"
19
20 using namespace std;
21
22 namespace OHOS {
23 namespace AudioStandard {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int AudioManagerStub::OnRemoteRequest(
25 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
26 {
27 MEDIA_DEBUG_LOG("OnRemoteRequest, cmd = %{public}u", code);
28 if (data.ReadInterfaceToken() != GetDescriptor()) {
29 MEDIA_ERR_LOG("AudioManagerStub: ReadInterfaceToken failed");
30 return -1;
31 }
32 if (!IsPermissionValid()) {
33 MEDIA_ERR_LOG("caller app not acquired audio permission");
34 return MEDIA_PERMISSION_DENIED;
35 }
36
37 switch (code) {
38 case GET_MAX_VOLUME: {
39 MEDIA_DEBUG_LOG("GET_MAX_VOLUME AudioManagerStub");
40 int volumeType = data.ReadInt32();
41 MEDIA_DEBUG_LOG("GET_MAX_VOLUME volumeType received from client= %{public}d", volumeType);
42 AudioSystemManager::AudioVolumeType volumeStreamConfig =
43 static_cast<AudioSystemManager::AudioVolumeType>(volumeType);
44 MEDIA_DEBUG_LOG("GET_MAX_VOLUME volumeType= %{public}d", volumeStreamConfig);
45 int32_t ret = GetMaxVolume(volumeStreamConfig);
46 reply.WriteInt32(ret);
47 return MEDIA_OK;
48 }
49 case GET_MIN_VOLUME: {
50 MEDIA_DEBUG_LOG("GET_MIN_VOLUME AudioManagerStub");
51 int volumeType = data.ReadInt32();
52 MEDIA_DEBUG_LOG("GET_MIN_VOLUME volumeType received from client= %{public}d", volumeType);
53 AudioSystemManager::AudioVolumeType volumeStreamConfig =
54 static_cast<AudioSystemManager::AudioVolumeType>(volumeType);
55 MEDIA_DEBUG_LOG("GET_MIN_VOLUME volumeType= %{public}d", volumeStreamConfig);
56 int32_t ret = GetMinVolume(volumeStreamConfig);
57 reply.WriteInt32(ret);
58 return MEDIA_OK;
59 }
60 case SET_AUDIO_PARAMETER: {
61 MEDIA_DEBUG_LOG("SET_AUDIO_PARAMETER AudioManagerStub");
62 const std::string key = data.ReadString();
63 const std::string value = data.ReadString();
64 MEDIA_DEBUG_LOG("SET_AUDIO_PARAMETER key-value pair from client= %{public}s, %{public}s",
65 key.c_str(), value.c_str());
66 SetAudioParameter(key, value);
67 return MEDIA_OK;
68 }
69 case GET_AUDIO_PARAMETER: {
70 MEDIA_DEBUG_LOG("GET_AUDIO_PARAMETER AudioManagerStub");
71 const std::string key = data.ReadString();
72 MEDIA_DEBUG_LOG("GET_AUDIO_PARAMETER key received from client= %{public}s", key.c_str());
73 const std::string value = GetAudioParameter(key);
74 reply.WriteString(value);
75 return MEDIA_OK;
76 }
77 case RETRIEVE_COOKIE: {
78 MEDIA_DEBUG_LOG("RETRIEVE_COOKIE AudioManagerStub");
79 int32_t size = 0;
80 const char *cookieInfo = RetrieveCookie(size);
81 reply.WriteInt32(size);
82 if (size > 0) {
83 MEDIA_DEBUG_LOG("cookie received from server");
84 reply.WriteRawData(static_cast<const void *>(cookieInfo), size);
85 free((void *)cookieInfo);
86 cookieInfo = nullptr;
87 }
88
89 return MEDIA_OK;
90 }
91 case SET_MICROPHONE_MUTE: {
92 MEDIA_DEBUG_LOG("SET_MICROPHONE_MUTE AudioManagerStub");
93 bool isMute = data.ReadBool();
94 MEDIA_DEBUG_LOG("SET_MICROPHONE_MUTE isMute value from client= %{public}d", isMute);
95 int32_t result = SetMicrophoneMute(isMute);
96 reply.WriteInt32(result);
97 return MEDIA_OK;
98 }
99 case IS_MICROPHONE_MUTE: {
100 MEDIA_DEBUG_LOG("IS_MICROPHONE_MUTE AudioManagerStub");
101 bool isMute = IsMicrophoneMute();
102 reply.WriteBool(isMute);
103 return MEDIA_OK;
104 }
105 case SET_AUDIO_SCENE: {
106 MEDIA_DEBUG_LOG("SET_AUDIO_SCENE AudioManagerStub");
107 AudioScene audioScene = (static_cast<AudioScene>(data.ReadInt32()));
108 int32_t result = SetAudioScene(audioScene);
109 reply.WriteInt32(result);
110 return MEDIA_OK;
111 }
112 case UPDATE_ROUTE_REQ: {
113 MEDIA_DEBUG_LOG("UPDATE_ROUTE_REQ AudioManagerStub");
114 DeviceType type = static_cast<DeviceType>(data.ReadInt32());
115 DeviceFlag flag = static_cast<DeviceFlag>(data.ReadInt32());
116 int32_t ret = UpdateActiveDeviceRoute(type, flag);
117 reply.WriteInt32(ret);
118 return MEDIA_OK;
119 }
120 default: {
121 MEDIA_ERR_LOG("default case, need check AudioManagerStub");
122 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
123 }
124 }
125 }
126
IsPermissionValid()127 bool AudioManagerStub::IsPermissionValid()
128 {
129 return true;
130 }
131 } // namespace AudioStandard
132 } // namespace OHOS
133