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 "call_base.h" 17 18#include <securec.h> 19 20#include "audio_control_manager.h" 21#include "bluetooth_call_manager.h" 22#include "call_manager_errors.h" 23#include "cellular_call_connection.h" 24#include "common_type.h" 25#include "ffrt.h" 26#include "telephony_log_wrapper.h" 27#include "voip_call.h" 28#include "voip_call_connection.h" 29#include "call_manager_info.h" 30#include "call_voice_assistant_manager.h" 31 32namespace OHOS { 33namespace Telephony { 34static const int MILLI_TO_BASE = 1000; 35 36CallBase::CallBase(DialParaInfo &info) 37 : callId_(info.callId), callType_(info.callType), videoState_(info.videoState), accountNumber_(info.number), 38 bundleName_(info.bundleName), callRunningState_(CallRunningState::CALL_RUNNING_STATE_CREATE), 39 conferenceState_(TelConferenceState::TEL_CONFERENCE_IDLE), startTime_(0), 40 direction_(CallDirection::CALL_DIRECTION_IN), policyFlag_(0), callState_(info.callState), autoAnswerState_(false), 41 canUnHoldState_(true), canSwitchCallState_(true), answerVideoState_(0), isSpeakerphoneOn_(false), 42 callEndedType_(CallEndedType::UNKNOWN), callBeginTime_(0), callCreateTime_(0), callEndTime_(0), ringBeginTime_(0), 43 ringEndTime_(0), answerType_(CallAnswerType::CALL_ANSWER_MISSED), accountId_(info.accountId), 44 crsType_(info.crsType), originalCallType_(info.originalCallType), isMuted_(false), numberLocation_("default"), 45 blockReason_(0), isEccContact_(false), celiaCallType_(-1), extraParams_(info.extraParams), isAnswered_(false) 46{ 47 (void)memset_s(&contactInfo_, sizeof(ContactInfo), 0, sizeof(ContactInfo)); 48 (void)memset_s(&numberMarkInfo_, sizeof(NumberMarkInfo), 0, sizeof(NumberMarkInfo)); 49} 50 51CallBase::CallBase(DialParaInfo &info, AppExecFwk::PacMap &extras) 52 : callId_(info.callId), callType_(info.callType), videoState_(info.videoState), accountNumber_(info.number), 53 bundleName_(info.bundleName), callRunningState_(CallRunningState::CALL_RUNNING_STATE_CREATE), 54 conferenceState_(TelConferenceState::TEL_CONFERENCE_IDLE), startTime_(0), 55 direction_(CallDirection::CALL_DIRECTION_OUT), policyFlag_(0), callState_(info.callState), 56 autoAnswerState_(false), canUnHoldState_(true), canSwitchCallState_(true), answerVideoState_(0), 57 isSpeakerphoneOn_(false), callEndedType_(CallEndedType::UNKNOWN), callBeginTime_(0), callCreateTime_(0), 58 callEndTime_(0), ringBeginTime_(0), ringEndTime_(0), answerType_(CallAnswerType::CALL_ANSWER_MISSED), 59 accountId_(info.accountId), crsType_(info.crsType), originalCallType_(info.originalCallType), isMuted_(false), 60 numberLocation_("default"), blockReason_(0), isEccContact_(false), celiaCallType_(-1), 61 extraParams_(info.extraParams), isAnswered_(false) 62{ 63 (void)memset_s(&contactInfo_, sizeof(ContactInfo), 0, sizeof(ContactInfo)); 64 (void)memset_s(&numberMarkInfo_, sizeof(NumberMarkInfo), 0, sizeof(NumberMarkInfo)); 65} 66 67CallBase::~CallBase() {} 68 69int32_t CallBase::DialCallBase() 70{ 71 std::lock_guard<std::mutex> lock(mutex_); 72 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_CONNECTING; 73 TELEPHONY_LOGI("start to set audio"); 74 // Set audio, set hands-free 75 ffrt::submit([=]() { HangUpVoipCall(); }); 76 return TELEPHONY_SUCCESS; 77} 78 79void CallBase::HangUpVoipCall() 80{ 81 std::vector<CallAttributeInfo> callAttributeInfo = CallObjectManager::GetAllCallInfoList(); 82 std::vector<CallAttributeInfo>::iterator it = callAttributeInfo.begin(); 83 while (it != callAttributeInfo.end()) { 84 CallAttributeInfo callinfo = (*it); 85 TelCallState callState = callinfo.callState; 86 ++it; 87 if (callinfo.callType == CallType::TYPE_VOIP) { 88 sptr<CallBase> tempCall = CallObjectManager::GetOneCallObject(callinfo.callId); 89 sptr<VoIPCall> call = static_cast<VoIPCall *>(static_cast<void *>(tempCall.GetRefPtr())); 90 if (call == nullptr) { 91 TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callinfo.callId); 92 break; 93 } 94 if (call->GetTelCallState() == TelCallState::CALL_STATUS_ACTIVE) { 95 TELEPHONY_LOGI("the voip call with callId %{public}d is active, no need to hangup", call->GetCallID()); 96 continue; 97 } 98 call->HangUpCall(ErrorReason::CELLULAR_CALL_EXISTS); 99 } 100 } 101} 102 103int32_t CallBase::IncomingCallBase() 104{ 105 std::lock_guard<std::mutex> lock(mutex_); 106 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_RINGING; 107 return TELEPHONY_SUCCESS; 108} 109 110int32_t CallBase::AnswerCallBase() 111{ 112 if (!IsCurrentRinging()) { 113 TELEPHONY_LOGW("the device is currently not ringing"); 114 return CALL_ERR_PHONE_ANSWER_IS_BUSY; 115 } 116 return TELEPHONY_SUCCESS; 117} 118 119int32_t CallBase::RejectCallBase() 120{ 121 answerType_ = CallAnswerType::CALL_ANSWER_REJECT; 122 return TELEPHONY_SUCCESS; 123} 124 125void CallBase::SetExtraParams(AAFwk::WantParams extraParams) 126{ 127 extraParams_ = extraParams; 128} 129 130void CallBase::GetCallAttributeBaseInfo(CallAttributeInfo &info) 131{ 132 std::lock_guard<std::mutex> lock(mutex_); 133 (void)memset_s(info.accountNumber, kMaxNumberLen, 0, kMaxNumberLen); 134 if (accountNumber_.length() > static_cast<size_t>(kMaxNumberLen)) { 135 TELEPHONY_LOGE("Number out of limit!"); 136 return; 137 } 138 if (memcpy_s(info.accountNumber, kMaxNumberLen, accountNumber_.c_str(), accountNumber_.length()) == 0) { 139 info.speakerphoneOn = isSpeakerphoneOn_; 140 info.videoState = videoState_; 141 info.startTime = startTime_; 142 info.callType = callType_; 143 info.callId = callId_; 144 info.callState = callState_; 145 info.conferenceState = conferenceState_; 146 info.callBeginTime = callBeginTime_; 147 info.callCreateTime = callCreateTime_; 148 info.callEndTime = callEndTime_; 149 info.ringBeginTime = ringBeginTime_; 150 info.ringEndTime = ringEndTime_; 151 info.callDirection = direction_; 152 info.answerType = answerType_; 153 info.accountId = accountId_; 154 info.crsType = crsType_; 155 info.originalCallType = originalCallType_; 156 info.isEccContact = isEccContact_; 157 info.celiaCallType = celiaCallType_; 158 info.extraParamsString = AAFwk::WantParamWrapper(extraParams_).ToString(); 159 if (memset_s(info.numberLocation, kMaxNumberLen, 0, kMaxNumberLen) != EOK) { 160 TELEPHONY_LOGE("memset_s numberLocation fail"); 161 return; 162 } 163 if (memcpy_s(info.numberLocation, kMaxNumberLen, numberLocation_.c_str(), numberLocation_.length()) != EOK) { 164 TELEPHONY_LOGE("memcpy_s numberLocation fail"); 165 return; 166 } 167 if (memcpy_s(info.contactName, kMaxNumberLen, contactInfo_.name.c_str(), contactInfo_.name.length()) != EOK) { 168 TELEPHONY_LOGE("memcpy_s contact name fail"); 169 } 170 info.numberMarkInfo = numberMarkInfo_; 171 info.blockReason = blockReason_; 172 if (bundleName_.length() > static_cast<size_t>(kMaxBundleNameLen)) { 173 TELEPHONY_LOGE("Number out of limit!"); 174 return; 175 } 176 errno_t result = memcpy_s(info.bundleName, kMaxBundleNameLen, bundleName_.c_str(), bundleName_.length()); 177 if (result != EOK) { 178 TELEPHONY_LOGE("memcpy_s failed!"); 179 } 180 } 181} 182 183int32_t CallBase::GetCallID() 184{ 185 std::lock_guard<std::mutex> lock(mutex_); 186 return callId_; 187} 188 189CallType CallBase::GetCallType() 190{ 191 return callType_; 192} 193 194CallRunningState CallBase::GetCallRunningState() 195{ 196 std::lock_guard<std::mutex> lock(mutex_); 197 return callRunningState_; 198} 199 200// transfer from external call state to callmanager local state 201int32_t CallBase::SetTelCallState(TelCallState nextState) 202{ 203 std::lock_guard<std::mutex> lock(mutex_); 204 if (callRunningState_ != CallRunningState::CALL_RUNNING_STATE_CREATE && callState_ == nextState && 205 nextState != TelCallState::CALL_STATUS_DIALING) { 206 TELEPHONY_LOGI("Call state duplication %{public}d", nextState); 207 return CALL_ERR_NOT_NEW_STATE; 208 } 209 callState_ = nextState; 210 switch (nextState) { 211 case TelCallState::CALL_STATUS_DIALING: 212 StateChangesToDialing(); 213 break; 214 case TelCallState::CALL_STATUS_INCOMING: 215 StateChangesToIncoming(); 216 break; 217 case TelCallState::CALL_STATUS_WAITING: 218 StateChangesToWaiting(); 219 break; 220 case TelCallState::CALL_STATUS_ACTIVE: 221 StateChangesToActive(); 222 break; 223 case TelCallState::CALL_STATUS_HOLDING: 224 StateChangesToHolding(); 225 break; 226 case TelCallState::CALL_STATUS_DISCONNECTED: 227 StateChangesToDisconnected(); 228 break; 229 case TelCallState::CALL_STATUS_DISCONNECTING: 230 StateChangesToDisconnecting(); 231 break; 232 case TelCallState::CALL_STATUS_ALERTING: 233 StateChangesToAlerting(); 234 break; 235 default: 236 break; 237 } 238 return TELEPHONY_SUCCESS; 239} 240 241void CallBase::StateChangesToDialing() 242{ 243 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_DIALING; 244} 245 246void CallBase::StateChangesToIncoming() 247{ 248 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_RINGING; 249 ringBeginTime_ = time(nullptr); 250} 251 252void CallBase::StateChangesToWaiting() 253{ 254 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_RINGING; 255 ringBeginTime_ = time(nullptr); 256} 257 258void CallBase::StateChangesToActive() 259{ 260 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_ACTIVE; 261 if (callBeginTime_ == 0) { 262 callBeginTime_ = ringEndTime_ = time(nullptr); 263 if (callType_ == CallType::TYPE_VOIP) { 264 struct timeval tv; 265 gettimeofday(&tv, nullptr); 266 startTime_ = tv.tv_sec * MILLI_TO_BASE + tv.tv_usec / MILLI_TO_BASE; 267 } else { 268 startTime_ = callBeginTime_; 269 } 270 answerType_ = CallAnswerType::CALL_ANSWER_ACTIVED; 271 } 272} 273 274void CallBase::StateChangesToHolding() 275{ 276 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_HOLD; 277 if (conferenceState_ == TelConferenceState::TEL_CONFERENCE_ACTIVE) { 278 conferenceState_ = TelConferenceState::TEL_CONFERENCE_DISCONNECTED; 279 } 280} 281 282void CallBase::StateChangesToDisconnected() 283{ 284 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_ENDED; 285 if (conferenceState_ == TelConferenceState::TEL_CONFERENCE_DISCONNECTING || 286 conferenceState_ == TelConferenceState::TEL_CONFERENCE_ACTIVE) { 287 conferenceState_ = TelConferenceState::TEL_CONFERENCE_DISCONNECTED; 288 } 289 callEndTime_ = time(nullptr); 290 if (ringEndTime_ == 0) { 291 ringEndTime_ = time(nullptr); 292 } 293} 294 295void CallBase::StateChangesToDisconnecting() 296{ 297 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_ENDING; 298 if (conferenceState_ == TelConferenceState::TEL_CONFERENCE_ACTIVE) { 299 conferenceState_ = TelConferenceState::TEL_CONFERENCE_DISCONNECTING; 300 } 301 if (ringEndTime_ == 0) { 302 ringEndTime_ = time(nullptr); 303 } 304} 305 306void CallBase::StateChangesToAlerting() 307{ 308 callRunningState_ = CallRunningState::CALL_RUNNING_STATE_DIALING; 309 ringBeginTime_ = time(nullptr); 310} 311 312TelCallState CallBase::GetTelCallState() 313{ 314 std::lock_guard<std::mutex> lock(mutex_); 315 return callState_; 316} 317 318void CallBase::SetAutoAnswerState(bool flag) 319{ 320 std::lock_guard<std::mutex> lock(mutex_); 321 autoAnswerState_ = flag; 322 TELEPHONY_LOGI("NeedAutoAnswer:%{public}d", autoAnswerState_); 323} 324 325bool CallBase::GetAutoAnswerState() 326{ 327 std::lock_guard<std::mutex> lock(mutex_); 328 return autoAnswerState_; 329} 330 331void CallBase::SetAnswerVideoState(int32_t videoState) 332{ 333 std::lock_guard<std::mutex> lock(mutex_); 334 answerVideoState_ = videoState; 335 TELEPHONY_LOGI("set answer video state :%{public}d", answerVideoState_); 336} 337 338int32_t CallBase::GetAnswerVideoState() 339{ 340 std::lock_guard<std::mutex> lock(mutex_); 341 return answerVideoState_; 342} 343 344void CallBase::SetCanUnHoldState(bool flag) 345{ 346 std::lock_guard<std::mutex> lock(mutex_); 347 canUnHoldState_ = flag; 348 TELEPHONY_LOGI("CanUnHoldState:%{public}d", canUnHoldState_); 349} 350 351bool CallBase::GetCanUnHoldState() 352{ 353 std::lock_guard<std::mutex> lock(mutex_); 354 TELEPHONY_LOGI("CanUnHoldState:%{public}d", canUnHoldState_); 355 return canUnHoldState_; 356} 357 358void CallBase::SetCanSwitchCallState(bool flag) 359{ 360 std::lock_guard<std::mutex> lock(mutex_); 361 canSwitchCallState_ = flag; 362 TELEPHONY_LOGI("CanSwitchCallState:%{public}d", canSwitchCallState_); 363} 364 365bool CallBase::GetCanSwitchCallState() 366{ 367 std::lock_guard<std::mutex> lock(mutex_); 368 TELEPHONY_LOGI("CanSwitchCallState:%{public}d", canSwitchCallState_); 369 return canSwitchCallState_; 370} 371 372void CallBase::SetTelConferenceState(TelConferenceState state) 373{ 374 std::lock_guard<std::mutex> lock(mutex_); 375 conferenceState_ = state; 376 TELEPHONY_LOGI("SetTelConferenceState, callId:%{public}d, state:%{public}d", callId_, state); 377} 378 379TelConferenceState CallBase::GetTelConferenceState() 380{ 381 std::lock_guard<std::mutex> lock(mutex_); 382 return conferenceState_; 383} 384 385VideoStateType CallBase::GetVideoStateType() 386{ 387 std::lock_guard<std::mutex> lock(mutex_); 388 return videoState_; 389} 390 391void CallBase::SetVideoStateType(VideoStateType mediaType) 392{ 393 std::lock_guard<std::mutex> lock(mutex_); 394 videoState_ = mediaType; 395} 396 397int32_t CallBase::GetCrsType() 398{ 399 std::lock_guard<std::mutex> lock(mutex_); 400 return crsType_; 401} 402 403void CallBase::SetCrsType(int32_t crsType) 404{ 405 std::lock_guard<std::mutex> lock(mutex_); 406 crsType_ = crsType; 407} 408 409int32_t CallBase::GetOriginalCallType() 410{ 411 std::lock_guard<std::mutex> lock(mutex_); 412 return originalCallType_; 413} 414 415void CallBase::SetOriginalCallType(int32_t originalCallType) 416{ 417 std::lock_guard<std::mutex> lock(mutex_); 418 originalCallType_ = originalCallType; 419} 420 421void CallBase::SetIsEccContact(bool isEccContact) 422{ 423 std::lock_guard<std::mutex> lock(mutex_); 424 isEccContact_ = isEccContact; 425} 426 427void CallBase::SetNumberLocation(std::string numberLocation) 428{ 429 CallVoiceAssistantManager::GetInstance()->UpdateNumberLocation(numberLocation, callId_); 430 std::lock_guard<std::mutex> lock(mutex_); 431 numberLocation_ = numberLocation; 432} 433 434int32_t CallBase::GetAccountId() 435{ 436 return accountId_; 437} 438 439std::string CallBase::GetNumberLocation() 440{ 441 std::lock_guard<std::mutex> lock(mutex_); 442 return numberLocation_; 443} 444 445void CallBase::SetPolicyFlag(PolicyFlag flag) 446{ 447 std::lock_guard<std::mutex> lock(mutex_); 448 policyFlag_ |= flag; 449} 450 451uint64_t CallBase::GetPolicyFlag() 452{ 453 std::lock_guard<std::mutex> lock(mutex_); 454 return policyFlag_; 455} 456 457ContactInfo CallBase::GetCallerInfo() 458{ 459 std::lock_guard<std::mutex> lock(mutex_); 460 return contactInfo_; 461} 462 463void CallBase::SetCallerInfo(const ContactInfo &info) 464{ 465 CallVoiceAssistantManager::GetInstance()->UpdateContactInfo(info, callId_); 466 std::lock_guard<std::mutex> lock(mutex_); 467 contactInfo_ = info; 468} 469 470NumberMarkInfo CallBase::GetNumberMarkInfo() 471{ 472 std::lock_guard<std::mutex> lock(mutex_); 473 return numberMarkInfo_; 474} 475 476void CallBase::SetNumberMarkInfo(const NumberMarkInfo &numberMarkInfo) 477{ 478 std::lock_guard<std::mutex> lock(mutex_); 479 numberMarkInfo_ = numberMarkInfo; 480} 481 482void CallBase::SetBlockReason(const int32_t &blockReason) 483{ 484 std::lock_guard<std::mutex> lock(mutex_); 485 blockReason_ = blockReason; 486} 487 488void CallBase::SetCallRunningState(CallRunningState callRunningState) 489{ 490 std::lock_guard<std::mutex> lock(mutex_); 491 callRunningState_ = callRunningState; 492} 493 494void CallBase::SetStartTime(int64_t startTime) 495{ 496 std::lock_guard<std::mutex> lock(mutex_); 497 startTime_ = startTime; 498} 499 500void CallBase::SetCallBeginTime(time_t callBeginTime) 501{ 502 std::lock_guard<std::mutex> lock(mutex_); 503 callBeginTime_ = callBeginTime; 504} 505 506void CallBase::SetCallCreateTime(time_t callCreateTime) 507{ 508 std::lock_guard<std::mutex> lock(mutex_); 509 callCreateTime_ = callCreateTime; 510} 511 512void CallBase::SetCallEndTime(time_t callEndTime) 513{ 514 std::lock_guard<std::mutex> lock(mutex_); 515 callEndTime_ = callEndTime; 516} 517 518void CallBase::SetRingBeginTime(time_t ringBeginTime) 519{ 520 std::lock_guard<std::mutex> lock(mutex_); 521 ringBeginTime_ = ringBeginTime; 522} 523 524void CallBase::SetRingEndTime(time_t ringEndTime) 525{ 526 std::lock_guard<std::mutex> lock(mutex_); 527 ringEndTime_ = ringEndTime; 528} 529 530void CallBase::SetAnswerType(CallAnswerType answerType) 531{ 532 std::lock_guard<std::mutex> lock(mutex_); 533 answerType_ = answerType; 534} 535 536CallEndedType CallBase::GetCallEndedType() 537{ 538 std::lock_guard<std::mutex> lock(mutex_); 539 return callEndedType_; 540} 541 542int32_t CallBase::SetCallEndedType(CallEndedType callEndedType) 543{ 544 std::lock_guard<std::mutex> lock(mutex_); 545 callEndedType_ = callEndedType; 546 return TELEPHONY_SUCCESS; 547} 548 549void CallBase::SetCallId(int32_t callId) 550{ 551 std::lock_guard<std::mutex> lock(mutex_); 552 callId_ = callId; 553} 554 555void CallBase::SetCeliaCallType(int32_t celiaCallType) 556{ 557 std::lock_guard<std::mutex> lock(mutex_); 558 celiaCallType_ = celiaCallType; 559} 560 561bool CallBase::CheckVoicemailNumber(std::string phoneNumber) 562{ 563 return false; 564} 565 566bool CallBase::IsSpeakerphoneEnabled() 567{ 568 std::shared_ptr<BluetoothCallManager> bluetoothCallManager = std::make_shared<BluetoothCallManager>(); 569 // Gets whether the device can be started from the configuration 570 if (bluetoothCallManager->IsBtAvailble()) { 571 return false; 572 } 573 return true; 574} 575 576bool CallBase::IsCurrentRinging() 577{ 578 std::lock_guard<std::mutex> lock(mutex_); 579 return (callRunningState_ == CallRunningState::CALL_RUNNING_STATE_RINGING) ? true : false; 580} 581 582std::string CallBase::GetAccountNumber() 583{ 584 return accountNumber_; 585} 586 587void CallBase::SetAccountNumber(const std::string accountNumber) 588{ 589 accountNumber_ = accountNumber; 590} 591 592bool CallBase::IsAnsweredCall() 593{ 594 std::lock_guard<std::mutex> lock(mutex_); 595 return isAnswered_; 596} 597 598void CallBase::SetAnsweredCall(bool isAnswered) 599{ 600 std::lock_guard<std::mutex> lock(mutex_); 601 isAnswered_ = isAnswered; 602} 603 604int32_t CallBase::SetSpeakerphoneOn(bool speakerphoneOn) 605{ 606 isSpeakerphoneOn_ = speakerphoneOn; 607 return TELEPHONY_SUCCESS; 608} 609 610bool CallBase::IsSpeakerphoneOn() 611{ 612 return isSpeakerphoneOn_; 613} 614 615bool CallBase::IsAliveState() 616{ 617 return !(callState_ == TelCallState::CALL_STATUS_IDLE || callState_ == TelCallState::CALL_STATUS_DISCONNECTED || 618 callState_ == TelCallState::CALL_STATUS_DISCONNECTING); 619} 620 621void CallBase::SetBundleName(const char *bundleName) 622{ 623 bundleName_ = bundleName; 624} 625 626void CallBase::SetCallType(CallType callType) 627{ 628 callType_ = callType; 629} 630 631int32_t CallBase::SetMicPhoneState(bool isMuted) 632{ 633 isMuted_ = isMuted; 634 return TELEPHONY_SUCCESS; 635} 636 637bool CallBase::IsMuted() 638{ 639 return isMuted_; 640} 641 642void CallBase::SetCallDirection(CallDirection direction) 643{ 644 direction_ = direction; 645} 646 647CallDirection CallBase::GetCallDirection() 648{ 649 return direction_; 650} 651} // namespace Telephony 652} // namespace OHOS 653