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