1 /*
2  * Copyright (C) 2021 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 "bluetooth_call_test.h"
17 
18 #include <iostream>
19 
20 #include "call_manager_errors.h"
21 
22 #include "bluetooth_call_client.h"
23 #include "call_manager_test_types.h"
24 #include "call_manager_callback_test.h"
25 
26 namespace OHOS {
27 namespace Telephony {
28 using namespace Security::AccessToken;
29 using Security::AccessToken::AccessTokenID;
30 BluetoothCallClient &bluetoothCallClient = DelayedRefSingleton<BluetoothCallClient>::GetInstance();
31 
32 HapInfoParams testInfoParamsCase = {
33     .userID = 1,
34     .bundleName = "tel_call_manager_ui_test",
35     .instIndex = 0,
36     .appIDDesc = "test",
37     .isSystemApp = true,
38 };
39 
40 PermissionDef testPermPlaceCallDefCase = {
41     .permissionName = "ohos.permission.PLACE_CALL",
42     .bundleName = "tel_call_manager_ui_test",
43     .grantMode = 1, // SYSTEM_GRANT
44     .availableLevel = APL_SYSTEM_BASIC,
45     .label = "label",
46     .labelId = 1,
47     .description = "Test call maneger",
48     .descriptionId = 1,
49 };
50 
51 PermissionStateFull testPlaceCallStateCase = {
52     .permissionName = "ohos.permission.PLACE_CALL",
53     .isGeneral = true,
54     .resDeviceID = { "local" },
55     .grantStatus = { PermissionState::PERMISSION_GRANTED },
56     .grantFlags = { 2 }, // PERMISSION_USER_SET
57 };
58 
59 PermissionDef testPermSetTelephonyStateDefCase = {
60     .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
61     .bundleName = "tel_call_manager_ui_test",
62     .grantMode = 1, // SYSTEM_GRANT
63     .availableLevel = APL_SYSTEM_BASIC,
64     .label = "label",
65     .labelId = 1,
66     .description = "Test call maneger",
67     .descriptionId = 1,
68 };
69 
70 PermissionStateFull testSetTelephonyStateCase = {
71     .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
72     .isGeneral = true,
73     .resDeviceID = { "local" },
74     .grantStatus = { PermissionState::PERMISSION_GRANTED },
75     .grantFlags = { 2 }, // PERMISSION_USER_SET
76 };
77 
78 PermissionDef testPermGetTelephonyStateDefCase = {
79     .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
80     .bundleName = "tel_call_manager_ui_test",
81     .grantMode = 1, // SYSTEM_GRANT
82     .availableLevel = APL_SYSTEM_BASIC,
83     .label = "label",
84     .labelId = 1,
85     .description = "Test call maneger",
86     .descriptionId = 1,
87 };
88 
89 PermissionStateFull testGetTelephonyStateCase = {
90     .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
91     .isGeneral = true,
92     .resDeviceID = { "local" },
93     .grantStatus = { PermissionState::PERMISSION_GRANTED },
94     .grantFlags = { 2 }, // PERMISSION_USER_SET
95 };
96 
97 PermissionDef testPermAnswerCallDefCase = {
98     .permissionName = "ohos.permission.ANSWER_CALL",
99     .bundleName = "tel_call_manager_ui_test",
100     .grantMode = 1, // SYSTEM_GRANT
101     .availableLevel = APL_SYSTEM_BASIC,
102     .label = "label",
103     .labelId = 1,
104     .description = "Test call maneger",
105     .descriptionId = 1,
106 };
107 
108 PermissionStateFull testAnswerCallStateCase = {
109     .permissionName = "ohos.permission.ANSWER_CALL",
110     .isGeneral = true,
111     .resDeviceID = { "local" },
112     .grantStatus = { PermissionState::PERMISSION_GRANTED },
113     .grantFlags = { 2 }, // PERMISSION_USER_SET
114 };
115 
116 HapPolicyParams testPolicyParamsCase = {
117     .apl = APL_SYSTEM_BASIC,
118     .domain = "test.domain",
119     .permList = { testPermPlaceCallDefCase, testPermSetTelephonyStateDefCase, testPermGetTelephonyStateDefCase,
120         testPermAnswerCallDefCase },
121     .permStateList = { testPlaceCallStateCase, testSetTelephonyStateCase, testGetTelephonyStateCase,
122         testAnswerCallStateCase },
123 };
124 
125 class AccessToken {
126 public:
AccessToken()127     AccessToken()
128     {
129         currentTokenID_ = GetSelfTokenID();
130         AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParamsCase, testPolicyParamsCase);
131         accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
132         SetSelfTokenID(tokenIdEx.tokenIDEx);
133     }
~AccessToken()134     ~AccessToken()
135     {
136         AccessTokenKit::DeleteToken(accessID_);
137         SetSelfTokenID(currentTokenID_);
138     }
139 
140 private:
141     AccessTokenID currentTokenID_ = 0;
142     AccessTokenID accessID_ = 0;
143 };
144 
Init()145 int32_t BluetoothCallTest::Init()
146 {
147     AccessToken token;
148     bluetoothCallClient.Init();
149     std::unique_ptr<CallManagerCallbackTest> callbackPtr = std::make_unique<CallManagerCallbackTest>();
150     if (callbackPtr == nullptr) {
151         std::cout << "make_unique CallManagerCallbackTest failed!" << std::endl;
152         return TELEPHONY_ERROR;
153     }
154     int32_t ret = bluetoothCallClient.RegisterCallBack(std::move(callbackPtr));
155     if (ret != TELEPHONY_SUCCESS) {
156         std::cout << "RegisterCallBack failed!" << std::endl;
157         return TELEPHONY_ERROR;
158     }
159     std::cout << "RegisterCallBack success!" << std::endl;
160     InitFunMap();
161     return TELEPHONY_SUCCESS;
162 }
163 
RunBluetoothCallTest()164 void BluetoothCallTest::RunBluetoothCallTest()
165 {
166     int32_t interfaceNum = OHOS::Telephony::DEFAULT_VALUE;
167     const int32_t exitNumber = 1000;
168     while (true) {
169         PrintfUsage();
170         std::cin >> interfaceNum;
171         if (interfaceNum == exitNumber) {
172             std::cout << "start to exit now...." << std::endl;
173             break;
174         }
175         auto itFunc = memberFuncMap_.find(interfaceNum);
176         if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) {
177             auto BluetoothCallFunc = itFunc->second;
178             (this->*BluetoothCallFunc)();
179             continue;
180         }
181         std::cout << "err: invalid input!" << std::endl;
182     }
183     memberFuncMap_.clear();
184 }
185 
PrintfUsage()186 void BluetoothCallTest::PrintfUsage()
187 {
188     std::cout << "\n\n-----------bluetooth call test start--------------\n";
189     std::cout << "usage:please input a cmd num:\n";
190     std::cout << "0:dial\n";
191     std::cout << "1:answer\n";
192     std::cout << "2:reject\n";
193     std::cout << "3:hangup\n";
194     std::cout << "4:getCallState\n";
195     std::cout << "5:hold\n";
196     std::cout << "6:unhold\n";
197     std::cout << "7:switchCall\n";
198     std::cout << "8:combineConference\n";
199     std::cout << "9:separateConference\n";
200     std::cout << "10:startDtmf\n";
201     std::cout << "11:stopRtt\n";
202     std::cout << "12:isRinging\n";
203     std::cout << "13:hasCall\n";
204     std::cout << "14:isNewCallAllowed\n";
205     std::cout << "15:isInEmergencyCall\n";
206     std::cout << "16:setMuted\n";
207     std::cout << "17:muteRinger\n";
208     std::cout << "18:setAudioDevice\n";
209     std::cout << "19:GetCurrentCallList\n";
210     std::cout << "20:kickOutFromConference\n";
211     std::cout << "1000:exit\n";
212 }
213 
InitFunMap()214 void BluetoothCallTest::InitFunMap()
215 {
216     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_DIAL)] = &BluetoothCallTest::DialCall;
217     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_ANSWER)] = &BluetoothCallTest::AnswerCall;
218     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_REJECT)] = &BluetoothCallTest::RejectCall;
219     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_HANGUP)] = &BluetoothCallTest::HangUpCall;
220     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_GET_CALL_STATE)] =
221         &BluetoothCallTest::GetCallState;
222     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_HOLD)] = &BluetoothCallTest::HoldCall;
223     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_UNHOLD)] = &BluetoothCallTest::UnHoldCall;
224     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_SWITCH)] = &BluetoothCallTest::SwitchCall;
225     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_COMBINE_CONFERENCE)] =
226         &BluetoothCallTest::CombineConference;
227     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_SEPARATE_CONFERENCE)] =
228         &BluetoothCallTest::SeparateConference;
229     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_KICK_OUT_CONFERENCE)] =
230         &BluetoothCallTest::KickOutFromConference;
231     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_START_DTMF)] = &BluetoothCallTest::StartDtmf;
232     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_STOP_DTMF)] = &BluetoothCallTest::StopDtmf;
233     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_IS_RINGING)] = &BluetoothCallTest::IsRinging;
234     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_HAS_CALL)] = &BluetoothCallTest::HasCall;
235     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_IS_NEW_CALL_ALLOWED)] =
236         &BluetoothCallTest::IsNewCallAllowed;
237     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_IS_IN_EMERGENCY_CALL)] =
238         &BluetoothCallTest::IsInEmergencyCall;
239     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_SET_MUTED)] = &BluetoothCallTest::SetMute;
240     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_MUTE_RINGER)] = &BluetoothCallTest::MuteRinger;
241     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_SET_AUDIO_DEVICE)] =
242         &BluetoothCallTest::SetAudioDevice;
243     memberFuncMap_[static_cast<uint32_t>(BluetoothCallFuncCode::TEST_GET_CURRENT_CALL_LIST)] =
244         &BluetoothCallTest::GetCurrentCallList;
245 }
246 
DialCall()247 void BluetoothCallTest::DialCall()
248 {
249     AccessToken token;
250     int32_t accountId = DEFAULT_ACCOUNT_ID;
251     int32_t videoCallState = DEFAULT_VIDEO_STATE;
252     int32_t dialScene = DEFAULT_DIAL_SCENE;
253     int32_t dialType = DEFAULT_DIAL_TYPE;
254     int32_t callType = DEFAULT_CALL_TYPE;
255     std::u16string u16PhoneNumber;
256     std::string tmpStr;
257     AppExecFwk::PacMap dialInfo;
258     std::cout << "-------Dial-------" << std::endl;
259     std::cout << "please input phone number:" << std::endl;
260     u16PhoneNumber.clear();
261     tmpStr.clear();
262     std::cin >> tmpStr;
263     u16PhoneNumber = Str8ToStr16(tmpStr);
264     std::cout << "you want to call " << tmpStr << std::endl;
265     std::cout << "please input accountId:" << std::endl;
266     std::cin >> accountId;
267     std::cout << "please input videoState[0:audio,1:video]:" << std::endl;
268     std::cin >> videoCallState;
269     std::cout << "please input dialScene[0:normal,1:privileged,2:emergency]:" << std::endl;
270     std::cin >> dialScene;
271     std::cout << "please input dialType[0:carrier,1:voice mail,2:ott]:" << std::endl;
272     std::cin >> dialType;
273     std::cout << "please input callType[0:cs,1:ims,2:ott]:" << std::endl;
274     std::cin >> callType;
275 
276     dialInfo.PutIntValue("accountId", accountId);
277     dialInfo.PutIntValue("videoState", videoCallState);
278     dialInfo.PutIntValue("dialScene", dialScene);
279     dialInfo.PutIntValue("dialType", dialType);
280     dialInfo.PutIntValue("callType", callType);
281     int32_t ret = bluetoothCallClient.DialCall(u16PhoneNumber, dialInfo);
282     std::cout << "return value:" << ret << std::endl;
283 }
284 
AnswerCall()285 void BluetoothCallTest::AnswerCall()
286 {
287     AccessToken token;
288     std::cout << "------Answer------" << std::endl;
289     int32_t ret = bluetoothCallClient.AnswerCall();
290     std::cout << "return value:" << ret << std::endl;
291 }
292 
RejectCall()293 void BluetoothCallTest::RejectCall()
294 {
295     AccessToken token;
296     std::cout << "------Reject------" << std::endl;
297     int32_t ret = bluetoothCallClient.RejectCall();
298     std::cout << "return value:" << ret << std::endl;
299 }
300 
HangUpCall()301 void BluetoothCallTest::HangUpCall()
302 {
303     AccessToken token;
304     std::cout << "------HangUpCall------" << std::endl;
305     int32_t ret = bluetoothCallClient.HangUpCall();
306     std::cout << "return value:" << ret << std::endl;
307 }
308 
GetCallState()309 void BluetoothCallTest::GetCallState()
310 {
311     std::cout << "------GetCallState------" << std::endl;
312     int32_t ret = bluetoothCallClient.GetCallState();
313     std::cout << "return value:" << ret << std::endl;
314 }
315 
HoldCall()316 void BluetoothCallTest::HoldCall()
317 {
318     AccessToken token;
319     std::cout << "------HoldCall------" << std::endl;
320     int32_t ret = bluetoothCallClient.HoldCall();
321     std::cout << "return value:" << ret << std::endl;
322 }
323 
UnHoldCall()324 void BluetoothCallTest::UnHoldCall()
325 {
326     AccessToken token;
327     std::cout << "------UnHoldCall------" << std::endl;
328     int32_t ret = bluetoothCallClient.UnHoldCall();
329     std::cout << "return value:" << ret << std::endl;
330 }
331 
CombineConference()332 void BluetoothCallTest::CombineConference()
333 {
334     std::cout << "------CombineConference------" << std::endl;
335     int32_t ret = bluetoothCallClient.CombineConference();
336     std::cout << "return value:" << ret << std::endl;
337 }
338 
SeparateConference()339 void BluetoothCallTest::SeparateConference()
340 {
341     std::cout << "------SeparateConference------" << std::endl;
342     int32_t ret = bluetoothCallClient.SeparateConference();
343     std::cout << "return value:" << ret << std::endl;
344 }
345 
KickOutFromConference()346 void BluetoothCallTest::KickOutFromConference()
347 {
348     std::cout << "------KickOutFromConference------" << std::endl;
349     int32_t ret = bluetoothCallClient.KickOutFromConference();
350     std::cout << "return value:" << ret << std::endl;
351 }
352 
SwitchCall()353 void BluetoothCallTest::SwitchCall()
354 {
355     AccessToken token;
356     std::cout << "------SwitchCall------" << std::endl;
357     int32_t ret = bluetoothCallClient.SwitchCall();
358     std::cout << "return value:" << ret << std::endl;
359 }
360 
HasCall()361 void BluetoothCallTest::HasCall()
362 {
363     std::cout << "------HasCall------" << std::endl;
364     int32_t ret = bluetoothCallClient.HasCall();
365     std::cout << "return value:" << ret << std::endl;
366 }
367 
IsNewCallAllowed()368 void BluetoothCallTest::IsNewCallAllowed()
369 {
370     std::cout << "------IsNewCallAllowed------" << std::endl;
371     bool enabled = false;
372     int32_t ret = bluetoothCallClient.IsNewCallAllowed(enabled);
373     std::cout << "ret:" << ret << std::endl;
374     std::cout << "enabled value:" << enabled << std::endl;
375 }
376 
IsRinging()377 void BluetoothCallTest::IsRinging()
378 {
379     AccessToken token;
380     std::cout << "------IsRinging------" << std::endl;
381     bool enabled = false;
382     int32_t ret = bluetoothCallClient.IsRinging(enabled);
383     std::cout << "ret:" << ret << std::endl;
384     std::cout << "enabled value:" << enabled << std::endl;
385 }
386 
IsInEmergencyCall()387 void BluetoothCallTest::IsInEmergencyCall()
388 {
389     AccessToken token;
390     std::cout << "------IsInEmergencyCall------" << std::endl;
391     bool enabled = false;
392     int32_t ret = bluetoothCallClient.IsInEmergencyCall(enabled);
393     std::cout << "ret:" << ret << std::endl;
394     std::cout << "enabled value:" << enabled << std::endl;
395 }
396 
StartDtmf()397 void BluetoothCallTest::StartDtmf()
398 {
399     char c = DEFAULT_VALUE;
400     std::cout << "------StartDtmf------" << std::endl;
401     std::cout << "Please enter to send dtmf characters:" << std::endl;
402     std::cin >> c;
403     int32_t ret = bluetoothCallClient.StartDtmf(c);
404     std::cout << "return value:" << ret << std::endl;
405 }
406 
StopDtmf()407 void BluetoothCallTest::StopDtmf()
408 {
409     std::cout << "------StopDtmf------" << std::endl;
410     int32_t ret = bluetoothCallClient.StopDtmf();
411     std::cout << "return value:" << ret << std::endl;
412 }
413 
SetMute()414 void BluetoothCallTest::SetMute()
415 {
416     int32_t isMute = DEFAULT_VALUE;
417     std::cout << "------SetMute------" << std::endl;
418     std::cout << "please input mute state(0:false 1:true):" << std::endl;
419     std::cin >> isMute;
420     int32_t ret = bluetoothCallClient.SetMuted((isMute == 1) ? true : false);
421     std::cout << "return value:" << ret << std::endl;
422 }
423 
MuteRinger()424 void BluetoothCallTest::MuteRinger()
425 {
426     AccessToken token;
427     std::cout << "------MuteRinger------" << std::endl;
428     int32_t ret = bluetoothCallClient.MuteRinger();
429     std::cout << "return value:" << ret << std::endl;
430 }
431 
SetAudioDevice()432 void BluetoothCallTest::SetAudioDevice()
433 {
434     int32_t deviceType = DEFAULT_VALUE;
435     std::cout << "------SetAudioDevice------" << std::endl;
436     std::cout << "please input device type(0:earpiece 1:speaker 2:wired headset 3:bluetooth sco):" << std::endl;
437     std::cin >> deviceType;
438     AudioDeviceType device = static_cast<AudioDeviceType>(deviceType);
439     std::string address = "0C:D7:46:14:AA:33";
440     int32_t ret = bluetoothCallClient.SetAudioDevice(device, address);
441     std::cout << "return value:" << ret << std::endl;
442 }
443 
GetCurrentCallList()444 void BluetoothCallTest::GetCurrentCallList()
445 {
446     AccessToken token;
447     int32_t slotId = DEFAULT_VALUE;
448     std::cout << "------GetCurrentCallList------" << std::endl;
449     std::cout << "Please enter slotId:" << std::endl;
450     std::cin >> slotId;
451     std::vector<CallAttributeInfo> callVec = bluetoothCallClient.GetCurrentCallList(slotId);
452     std::cout << "call list count:" << callVec.size() << std::endl;
453     std::vector<CallAttributeInfo>::iterator it = callVec.begin();
454     for (; it != callVec.end(); ++it) {
455         std::cout << "number:" << (*it).accountNumber << ", callState:"
456             << static_cast<int32_t>((*it).callState) << std::endl;
457     }
458 }
459 } // namespace Telephony
460 } // namespace OHOS
461