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 <cstdio>
17 #include <cstdlib>
18 #include <cstring>
19 #include <iostream>
20 #include <securec.h>
21 #include <unistd.h>
22
23 #include "audio_errors.h"
24 #include "audio_system_manager.h"
25 #include "media_log.h"
26
27 using namespace std;
28 using namespace OHOS;
29 using namespace OHOS::AudioStandard;
30
31 namespace AudioPolicyTest {
32 const int FIRST_ARG = 1;
33 const int SECOND_ARG = 2;
34 const int OPT_ARG_BASE = 10;
35 const int OPT_SHORT_LEN = 3;
36 }
37
PrintUsage(void)38 static void PrintUsage(void)
39 {
40 cout << "NAME" << endl << endl;
41 cout << "\taudio_policy_test - Audio Policy Test " << endl << endl;
42 cout << "SYNOPSIS" << endl << endl;
43 cout << "\t#include <audio_system_manager.h>" << endl << endl;
44 cout << "\t./audio_policy_test [OPTIONS]..." << endl << endl;
45 cout << "DESCRIPTION" << endl << endl;
46 cout << "\tControls audio volume, audio routing, audio mute" << endl << endl;
47 cout << "-V\n\tSets Volume for streams, -S to setStream" << endl << endl;
48 cout << "-v\n\tGets Volume for streams, -S to setStream" << endl << endl;
49 cout << "-S\n\tSet stream type" << endl << endl;
50 cout << "\tSupported Streams are" << endl << endl;
51 cout << "\t4\tMUSIC" << endl << endl;
52 cout << "\t3\tRING" << endl << endl;
53 cout << "-D\n\tSets Device Active" << endl << endl;
54 cout << "\tSupported Devices are" << endl << endl;
55 cout << "\t2\tSPEAKER" << endl << endl;
56 cout << "\t7\tBLUETOOTH_SCO" << endl << endl;
57 cout << "-d\n\tGets Device Active" << endl << endl;
58 cout << "-M\n\tSets Mute for streams, -S to setStream" << endl << endl;
59 cout << "-m\n\tGets Mute for streams, -S to setStream" << endl << endl;
60 cout << "-U\n\t Mutes the Microphone" << endl << endl;
61 cout << "-u\n\t Checks if the Microphone is muted " << endl << endl;
62 cout << "-R\n\tSets RingerMode" << endl << endl;
63 cout << "-r\n\tGets RingerMode status" << endl << endl;
64 cout << "-s\n\tGet Stream Status" << endl << endl;
65 cout << "AUTHOR" << endl << endl;
66 cout << "\tWritten by Sajeesh Sidharthan and Anurup M" << endl << endl;
67 }
68
HandleVolume(int streamType,char option)69 static void HandleVolume(int streamType, char option)
70 {
71 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
72 if (option == 'v') {
73 float volume = audioSystemMgr->GetVolume(static_cast<AudioSystemManager::AudioVolumeType>(streamType));
74 cout << "Get Volume : " << volume << endl;
75 } else {
76 float volume = strtof(optarg, nullptr);
77 cout << "Set Volume : " << volume << endl;
78 int32_t result = audioSystemMgr->SetVolume(static_cast<AudioSystemManager::AudioVolumeType>(streamType),
79 volume);
80 cout << "Set Volume Result: " << result << endl;
81 }
82 }
83
HandleMute(int streamType,char option)84 static void HandleMute(int streamType, char option)
85 {
86 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
87 if (option == 'm') {
88 bool muteStatus = audioSystemMgr->IsStreamMute(static_cast<AudioSystemManager::AudioVolumeType>(streamType));
89 cout << "Get Mute : " << muteStatus << endl;
90 } else {
91 int mute = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
92 cout << "Set Mute : " << mute << endl;
93 int32_t result = audioSystemMgr->SetMute(static_cast<AudioSystemManager::AudioVolumeType>(streamType),
94 (mute) ? true : false);
95 cout << "Set Mute Result: " << result << endl;
96 }
97 }
98
HandleMicMute(char option)99 static void HandleMicMute(char option)
100 {
101 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
102 if (option == 'u') {
103 bool muteStatus = audioSystemMgr->IsMicrophoneMute();
104 cout << "Is Mic Mute : " << muteStatus << endl;
105 } else {
106 int mute = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
107 cout << "Set Mic Mute : " << mute << endl;
108 int32_t result = audioSystemMgr->SetMicrophoneMute((mute) ? true : false);
109 cout << "Set Mic Mute Result: " << result << endl;
110 }
111 }
112
SetStreamType(int & streamType)113 static void SetStreamType(int &streamType)
114 {
115 streamType = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
116 cout << "Set Stream : " << streamType << endl;
117 }
118
IsStreamActive()119 static void IsStreamActive()
120 {
121 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
122 int streamType = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
123 cout << "Stream Active: " << audioSystemMgr->IsStreamActive(
124 static_cast<AudioSystemManager::AudioVolumeType>(streamType)) << endl;
125 }
126
SetDeviceActive(int argc,char * argv[])127 static void SetDeviceActive(int argc, char *argv[])
128 {
129 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
130 int active = -1;
131 int device = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
132 cout << "Set Device : " << device << endl;
133
134 if (optind < argc && *argv[optind] != '-') {
135 active = strtol(argv[optind], nullptr, AudioPolicyTest::OPT_ARG_BASE);
136 optind++;
137 }
138 cout << "Active : " << active << endl << endl;
139
140 int32_t result = audioSystemMgr->SetDeviceActive(ActiveDeviceType(device),
141 (active) ? true : false);
142 cout << "Set DeviceActive Result: " << result << endl;
143 }
144
IsDeviceActive()145 static void IsDeviceActive()
146 {
147 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
148 int device = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
149 bool devActiveStatus = audioSystemMgr->IsDeviceActive(ActiveDeviceType(device));
150 cout << "GetDevice Active : " << devActiveStatus << endl;
151 }
152
HandleRingerMode(char option)153 static void HandleRingerMode(char option)
154 {
155 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
156 if (option == 'r') {
157 int ringMode = static_cast<int32_t>(audioSystemMgr->GetRingerMode());
158 cout << "Get Ringer Mode : " << ringMode << endl;
159 } else {
160 int ringMode = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
161 cout << "Set Ringer Mode : " << ringMode << endl;
162 audioSystemMgr->SetRingerMode(static_cast<AudioRingerMode>(ringMode));
163 }
164 }
165
NoValueError()166 static void NoValueError()
167 {
168 char option[AudioPolicyTest::OPT_SHORT_LEN];
169 cout << "option ";
170 int len = snprintf_s(option, sizeof(option), sizeof(option) - 1, "-%c", optopt);
171 if (len <= 0) {
172 cout << "NoValueError: snprintf_s error : buffer allocation fails";
173 return;
174 }
175
176 cout << option << " needs a value" << endl << endl;
177 PrintUsage();
178 }
179
UnknownOptionError()180 static void UnknownOptionError()
181 {
182 char option[AudioPolicyTest::OPT_SHORT_LEN];
183 int len = snprintf_s(option, sizeof(option), sizeof(option) - 1, "-%c", optopt);
184 if (len <= 0) {
185 cout << "unknown option: snprintf_s error : buffer allocation fails";
186 return;
187 }
188 cout << "unknown option: " << option << endl << endl;
189 PrintUsage();
190 }
191
main(int argc,char * argv[])192 int main(int argc, char* argv[])
193 {
194 int opt = 0;
195 if (((argc >= AudioPolicyTest::SECOND_ARG) && !strcmp(argv[AudioPolicyTest::FIRST_ARG], "--help")) ||
196 (argc == AudioPolicyTest::FIRST_ARG)) {
197 PrintUsage();
198 return ERR_INVALID_PARAM;
199 }
200
201 int streamType = static_cast<int32_t>(AudioSystemManager::AudioVolumeType::STREAM_MUSIC);
202 while ((opt = getopt(argc, argv, ":V:U:S:D:M:R:d:s:vmru")) != -1) {
203 switch (opt) {
204 case 'V':
205 case 'v':
206 HandleVolume(streamType, opt);
207 break;
208 case 'M':
209 case 'm':
210 HandleMute(streamType, opt);
211 break;
212 case 'U':
213 case 'u':
214 HandleMicMute(opt);
215 break;
216 case 'S':
217 SetStreamType(streamType);
218 break;
219 case 's':
220 IsStreamActive();
221 break;
222 case 'D':
223 SetDeviceActive(argc, argv);
224 break;
225 case 'd':
226 IsDeviceActive();
227 break;
228 case 'R':
229 case 'r':
230 HandleRingerMode(opt);
231 break;
232 case ':':
233 NoValueError();
234 break;
235 case '?':
236 UnknownOptionError();
237 break;
238 default:
239 break;
240 }
241 }
242
243 return 0;
244 }
245