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_object_manager.h"
17
18 #include "call_manager_errors.h"
19 #include "telephony_log_wrapper.h"
20
21 namespace OHOS {
22 namespace Telephony {
23 std::list<sptr<CallBase>> CallObjectManager::callObjectPtrList_;
24 std::mutex CallObjectManager::listMutex_;
25 int32_t CallObjectManager::callId_ = CALL_START_ID;
26
CallObjectManager()27 CallObjectManager::CallObjectManager()
28 {
29 callObjectPtrList_.clear();
30 }
31
~CallObjectManager()32 CallObjectManager::~CallObjectManager()
33 {
34 std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
35 while (it != callObjectPtrList_.end()) {
36 (*it) = nullptr;
37 callObjectPtrList_.erase(it++);
38 }
39 }
40
AddOneCallObject(sptr<CallBase> & call)41 int32_t CallObjectManager::AddOneCallObject(sptr<CallBase> &call)
42 {
43 if (call == nullptr) {
44 return TELEPHONY_ERR_LOCAL_PTR_NULL;
45 }
46 std::lock_guard<std::mutex> lock(listMutex_);
47 std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
48 for (; it != callObjectPtrList_.end(); ++it) {
49 if ((*it)->GetCallID() == call->GetCallID()) {
50 TELEPHONY_LOGE("this call has existed yet!");
51 return CALL_ERR_PHONE_CALL_ALREADY_EXISTS;
52 }
53 }
54 callObjectPtrList_.emplace_back(call);
55 TELEPHONY_LOGI("AddOneCallObject success! callId:%{public}d,call list size:%{public}zu", call->GetCallID(),
56 callObjectPtrList_.size());
57 return TELEPHONY_SUCCESS;
58 }
59
DeleteOneCallObject(int32_t callId)60 int32_t CallObjectManager::DeleteOneCallObject(int32_t callId)
61 {
62 std::lock_guard<std::mutex> lock(listMutex_);
63 std::list<sptr<CallBase>>::iterator it;
64 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
65 if ((*it)->GetCallID() == callId) {
66 callObjectPtrList_.erase(it);
67 TELEPHONY_LOGI("DeleteOneCallObject success! call list size:%{public}zu", callObjectPtrList_.size());
68 break;
69 }
70 }
71 return TELEPHONY_SUCCESS;
72 }
73
DeleteOneCallObject(sptr<CallBase> & call)74 void CallObjectManager::DeleteOneCallObject(sptr<CallBase> &call)
75 {
76 if (call == nullptr) {
77 TELEPHONY_LOGE("call is null!");
78 return;
79 }
80 std::lock_guard<std::mutex> lock(listMutex_);
81 callObjectPtrList_.remove(call);
82 TELEPHONY_LOGI("DeleteOneCallObject success! callList size:%{public}zu", callObjectPtrList_.size());
83 }
84
GetOneCallObject(int32_t callId)85 sptr<CallBase> CallObjectManager::GetOneCallObject(int32_t callId)
86 {
87 sptr<CallBase> retPtr = nullptr;
88 std::lock_guard<std::mutex> lock(listMutex_);
89 std::list<sptr<CallBase>>::iterator it = CallObjectManager::callObjectPtrList_.begin();
90 for (; it != callObjectPtrList_.end(); ++it) {
91 if ((*it)->GetCallID() == callId) {
92 retPtr = *it;
93 break;
94 }
95 }
96 return retPtr;
97 }
98
GetOneCallObject(std::string & phoneNumber)99 sptr<CallBase> CallObjectManager::GetOneCallObject(std::string &phoneNumber)
100 {
101 if (phoneNumber.empty()) {
102 TELEPHONY_LOGE("call is null!");
103 return nullptr;
104 }
105 sptr<CallBase> retPtr = nullptr;
106 std::lock_guard<std::mutex> lock(listMutex_);
107 std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
108 for (; it != callObjectPtrList_.end(); ++it) {
109 if ((*it)->GetAccountNumber() == phoneNumber) {
110 TELEPHONY_LOGI("GetOneCallObject success!");
111 retPtr = *it;
112 break;
113 }
114 }
115 return retPtr;
116 }
117
HasNewCall()118 int32_t CallObjectManager::HasNewCall()
119 {
120 std::lock_guard<std::mutex> lock(listMutex_);
121 std::list<sptr<CallBase>>::iterator it;
122 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
123 if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CREATE ||
124 (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CONNECTING ||
125 (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_DIALING) {
126 TELEPHONY_LOGE("there is already a new call[callId:%{public}d,state:%{public}d], please redial later",
127 (*it)->GetCallID(), (*it)->GetCallRunningState());
128 return CALL_ERR_DIAL_IS_BUSY;
129 }
130 }
131 return TELEPHONY_SUCCESS;
132 }
133
IsNewCallAllowedCreate()134 bool CallObjectManager::IsNewCallAllowedCreate()
135 {
136 bool ret = true;
137 std::lock_guard<std::mutex> lock(listMutex_);
138 std::list<sptr<CallBase>>::iterator it;
139 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
140 if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CREATE ||
141 (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CONNECTING ||
142 (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_DIALING ||
143 (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
144 TELEPHONY_LOGE("there is already a new call, please redial later");
145 ret = false;
146 break;
147 }
148 }
149 return ret;
150 }
151
GetCarrierCallList(std::list<int32_t> & list)152 int32_t CallObjectManager::GetCarrierCallList(std::list<int32_t> &list)
153 {
154 list.clear();
155 std::lock_guard<std::mutex> lock(listMutex_);
156 std::list<sptr<CallBase>>::iterator it;
157 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
158 if ((*it)->GetCallType() == CallType::TYPE_CS || (*it)->GetCallType() == CallType::TYPE_IMS) {
159 list.emplace_back((*it)->GetCallID());
160 }
161 }
162 return TELEPHONY_SUCCESS;
163 }
164
HasRingingMaximum()165 bool CallObjectManager::HasRingingMaximum()
166 {
167 int32_t ringingCount = 0;
168 std::lock_guard<std::mutex> lock(listMutex_);
169 std::list<sptr<CallBase>>::iterator it;
170 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
171 // Count the number of calls in the ringing state
172 if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
173 ringingCount++;
174 }
175 }
176 if (ringingCount >= RINGING_CALL_NUMBER_LEN) {
177 return true;
178 }
179 return false;
180 }
181
HasDialingMaximum()182 bool CallObjectManager::HasDialingMaximum()
183 {
184 int32_t dialingCount = 0;
185 std::lock_guard<std::mutex> lock(listMutex_);
186 std::list<sptr<CallBase>>::iterator it;
187 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
188 // Count the number of calls in the active state
189 if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_ACTIVE) {
190 dialingCount++;
191 }
192 }
193 if (dialingCount >= DIALING_CALL_NUMBER_LEN) {
194 return true;
195 }
196 return false;
197 }
198
HasEmergencyCall()199 bool CallObjectManager::HasEmergencyCall()
200 {
201 std::lock_guard<std::mutex> lock(listMutex_);
202 std::list<sptr<CallBase>>::iterator it;
203 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
204 if ((*it)->GetEmergencyState()) {
205 return true;
206 }
207 }
208 return false;
209 }
210
GetNewCallId()211 int32_t CallObjectManager::GetNewCallId()
212 {
213 int32_t ret = 0;
214 std::lock_guard<std::mutex> lock(listMutex_);
215 ret = ++callId_;
216 return ret;
217 }
218
IsCallExist(int32_t callId)219 bool CallObjectManager::IsCallExist(int32_t callId)
220 {
221 std::lock_guard<std::mutex> lock(listMutex_);
222 std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
223 for (; it != callObjectPtrList_.end(); ++it) {
224 if ((*it)->GetCallID() == callId) {
225 TELEPHONY_LOGW("the call is exist.");
226 return true;
227 }
228 }
229 return false;
230 }
231
IsCallExist(std::string & phoneNumber)232 bool CallObjectManager::IsCallExist(std::string &phoneNumber)
233 {
234 if (phoneNumber.empty()) {
235 return false;
236 }
237 std::lock_guard<std::mutex> lock(listMutex_);
238 std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
239 for (; it != callObjectPtrList_.end(); ++it) {
240 if ((*it)->GetAccountNumber() == phoneNumber) {
241 return true;
242 }
243 }
244 TELEPHONY_LOGI("the call is does not exist.");
245 return false;
246 }
247
HasCallExist()248 bool CallObjectManager::HasCallExist()
249 {
250 std::lock_guard<std::mutex> lock(listMutex_);
251 if (callObjectPtrList_.empty()) {
252 TELEPHONY_LOGI("call list size:%{public}zu", callObjectPtrList_.size());
253 return false;
254 }
255 return true;
256 }
257
HasRingingCall()258 bool CallObjectManager::HasRingingCall()
259 {
260 bool ret = false;
261 std::lock_guard<std::mutex> lock(listMutex_);
262 std::list<sptr<CallBase>>::iterator it;
263 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
264 // Count the number of calls in the ringing state
265 if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
266 ret = true;
267 break;
268 }
269 }
270 return ret;
271 }
272
GetCallState(int32_t callId)273 TelCallState CallObjectManager::GetCallState(int32_t callId)
274 {
275 TelCallState retState = TelCallState::CALL_STATUS_IDLE;
276 std::lock_guard<std::mutex> lock(listMutex_);
277 std::list<sptr<CallBase>>::iterator it = CallObjectManager::callObjectPtrList_.begin();
278 for (; it != callObjectPtrList_.end(); ++it) {
279 if ((*it)->GetCallID() == callId) {
280 retState = (*it)->GetTelCallState();
281 break;
282 }
283 }
284 return retState;
285 }
286
GetOneCallObject(CallRunningState callState)287 sptr<CallBase> CallObjectManager::GetOneCallObject(CallRunningState callState)
288 {
289 std::lock_guard<std::mutex> lock(listMutex_);
290 std::list<sptr<CallBase>>::reverse_iterator it;
291 for (it = callObjectPtrList_.rbegin(); it != callObjectPtrList_.rend(); ++it) {
292 if ((*it)->GetCallRunningState() == callState) {
293 return (*it);
294 }
295 }
296 return nullptr;
297 }
298
IsCallExist(CallType callType,TelCallState callState)299 bool CallObjectManager::IsCallExist(CallType callType, TelCallState callState)
300 {
301 std::lock_guard<std::mutex> lock(listMutex_);
302 std::list<sptr<CallBase>>::iterator it;
303 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
304 if ((*it)->GetCallType() == callType && (*it)->GetTelCallState() == callState) {
305 return true;
306 }
307 }
308 TELEPHONY_LOGI("the call is does not exist.");
309 return false;
310 }
311
IsCallExist(TelCallState callState)312 bool CallObjectManager::IsCallExist(TelCallState callState)
313 {
314 std::lock_guard<std::mutex> lock(listMutex_);
315 std::list<sptr<CallBase>>::iterator it;
316 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
317 if ((*it)->GetTelCallState() == callState) {
318 return true;
319 }
320 }
321 TELEPHONY_LOGI("the call is does not exist.");
322 return false;
323 }
324
IsCallExist(TelCallState callState,int32_t & callId)325 bool CallObjectManager::IsCallExist(TelCallState callState, int32_t &callId)
326 {
327 std::lock_guard<std::mutex> lock(listMutex_);
328 std::list<sptr<CallBase>>::iterator it;
329 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
330 if ((*it)->GetTelCallState() == callState) {
331 callId = (*it)->GetCallID();
332 return true;
333 }
334 }
335 TELEPHONY_LOGI("the call is does not exist.");
336 return false;
337 }
338
IsConferenceCallExist(TelConferenceState state,int32_t & callId)339 bool CallObjectManager::IsConferenceCallExist(TelConferenceState state, int32_t &callId)
340 {
341 std::lock_guard<std::mutex> lock(listMutex_);
342 std::list<sptr<CallBase>>::iterator it;
343 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
344 if ((*it)->GetTelConferenceState() == state) {
345 callId = (*it)->GetCallID();
346 return true;
347 }
348 }
349 TELEPHONY_LOGI("the call is does not exist.");
350 return false;
351 }
352
GetCallNum(TelCallState callState)353 int32_t CallObjectManager::GetCallNum(TelCallState callState)
354 {
355 int32_t num = 0;
356 std::lock_guard<std::mutex> lock(listMutex_);
357 std::list<sptr<CallBase>>::iterator it;
358 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
359 if ((*it)->GetTelCallState() == callState) {
360 ++num;
361 continue;
362 }
363 }
364 TELEPHONY_LOGI("callState:%{public}d, num:%{public}d", callState, num);
365 return num;
366 }
367
GetCallNumber(TelCallState callState)368 std::string CallObjectManager::GetCallNumber(TelCallState callState)
369 {
370 std::string number = "";
371 std::lock_guard<std::mutex> lock(listMutex_);
372 std::list<sptr<CallBase>>::iterator it;
373 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
374 if ((*it)->GetTelCallState() == callState) {
375 number = (*it)->GetAccountNumber();
376 break;
377 }
378 }
379 return number;
380 }
381
GetCallInfoList(int32_t slotId)382 std::vector<CallAttributeInfo> CallObjectManager::GetCallInfoList(int32_t slotId)
383 {
384 std::vector<CallAttributeInfo> callVec;
385 CallAttributeInfo info;
386 callVec.clear();
387 std::lock_guard<std::mutex> lock(listMutex_);
388 std::list<sptr<CallBase>>::iterator it;
389 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
390 (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
391 (*it)->GetCallAttributeInfo(info);
392 if (info.accountId == slotId && info.callType != CallType::TYPE_OTT) {
393 callVec.emplace_back(info);
394 }
395 }
396 return callVec;
397 }
398 } // namespace Telephony
399 } // namespace OHOS
400