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 "ott_conference.h"
17 
18 #include <string_ex.h>
19 
20 #include "call_base.h"
21 #include "call_object_manager.h"
22 #include "call_manager_errors.h"
23 #include "telephony_log_wrapper.h"
24 #include "call_manager_inner_type.h"
25 
26 namespace OHOS {
27 namespace Telephony {
OttConference()28 OttConference::OttConference() : ConferenceBase()
29 {
30     conferenceType_ = CallType::TYPE_IMS;
31     maxSubCallLimits_ = CS_CONFERENCE_MAX_CALLS_CNT;
32     // get from configuration file
33 #ifdef ABILITY_CONFIG_SUPPORT
34     maxSubCallLimits_ = GetConfig(IMS_CONFERENCE_SUB_CALL_LIMITS);
35 #endif
36 }
37 
~OttConference()38 OttConference::~OttConference() {}
39 
JoinToConference(int32_t callId)40 int32_t OttConference::JoinToConference(int32_t callId)
41 {
42     std::lock_guard<std::mutex> lock(conferenceMutex_);
43     if (state_ != CONFERENCE_STATE_CREATING && state_ != CONFERENCE_STATE_ACTIVE &&
44         state_ != CONFERENCE_STATE_LEAVING) {
45         TELEPHONY_LOGE("the current conference status does not allow CombineConference");
46         return CALL_ERR_ILLEGAL_CALL_OPERATION;
47     }
48 
49     subCallIdSet_.insert(callId);
50     state_ = CONFERENCE_STATE_ACTIVE;
51     oldState_ = state_;
52     beginTime_ = time(nullptr);
53     TELEPHONY_LOGI("JoinToConference success, callId:%{public}d", callId);
54     return TELEPHONY_SUCCESS;
55 }
56 
LeaveFromConference(int32_t callId)57 int32_t OttConference::LeaveFromConference(int32_t callId)
58 {
59     std::lock_guard<std::mutex> lock(conferenceMutex_);
60     if (subCallIdSet_.find(callId) != subCallIdSet_.end()) {
61         subCallIdSet_.erase(callId);
62         if (mainCallId_ == callId) {
63             mainCallId_ = *subCallIdSet_.begin();
64         }
65     } else {
66         TELEPHONY_LOGE("separate conference failed, callId %{public}d not in conference", callId);
67         return CALL_ERR_CONFERENCE_SEPERATE_FAILED;
68     }
69     if (subCallIdSet_.empty()) {
70         mainCallId_ = ERR_ID;
71         state_ = CONFERENCE_STATE_IDLE;
72         oldState_ = state_;
73         beginTime_ = 0;
74     }
75     return TELEPHONY_SUCCESS;
76 }
77 
HoldConference(int32_t callId)78 int32_t OttConference::HoldConference(int32_t callId)
79 {
80     std::lock_guard<std::mutex> lock(conferenceMutex_);
81     if (state_ == CONFERENCE_STATE_HOLDING) {
82         TELEPHONY_LOGI("HoldConference success");
83         return TELEPHONY_SUCCESS;
84     }
85     if (subCallIdSet_.find(callId) != subCallIdSet_.end()) {
86         subCallIdSet_.erase(callId);
87     } else {
88         TELEPHONY_LOGE("separate conference failed, callId %{public}d not in conference", callId);
89         return CALL_ERR_CONFERENCE_SEPERATE_FAILED;
90     }
91     if (subCallIdSet_.empty()) {
92         mainCallId_ = ERR_ID;
93         state_ = CONFERENCE_STATE_IDLE;
94         oldState_ = state_;
95         beginTime_ = 0;
96     }
97     state_ = CONFERENCE_STATE_HOLDING;
98     oldState_ = state_;
99     return TELEPHONY_SUCCESS;
100 }
101 
CanCombineConference()102 int32_t OttConference::CanCombineConference()
103 {
104     std::lock_guard<std::mutex> lock(conferenceMutex_);
105     if (subCallIdSet_.size() >= maxSubCallLimits_) {
106         TELEPHONY_LOGE("already %{public}zu calls in the conference, exceed limits!", subCallIdSet_.size());
107         return CALL_ERR_CONFERENCE_CALL_EXCEED_LIMIT;
108     }
109     return TELEPHONY_SUCCESS;
110 }
111 
CanSeparateConference()112 int32_t OttConference::CanSeparateConference()
113 {
114     std::lock_guard<std::mutex> lock(conferenceMutex_);
115     if (subCallIdSet_.empty()) {
116         TELEPHONY_LOGE("no call is currently in the conference!");
117         return CALL_ERR_CONFERENCE_NOT_EXISTS;
118     }
119     if (state_ != CONFERENCE_STATE_ACTIVE) {
120         TELEPHONY_LOGE("call is not active!");
121         return CALL_ERR_CONFERENCE_CALL_IS_NOT_ACTIVE;
122     }
123     return TELEPHONY_SUCCESS;
124 }
125 
CanKickOutFromConference()126 int32_t OttConference::CanKickOutFromConference()
127 {
128     std::lock_guard<std::mutex> lock(conferenceMutex_);
129     if (subCallIdSet_.empty()) {
130         TELEPHONY_LOGE("no call is currently in the conference!");
131         return CALL_ERR_CONFERENCE_NOT_EXISTS;
132     }
133     return TELEPHONY_SUCCESS;
134 }
135 } // namespace Telephony
136 } // namespace OHOS
137