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 "conference_base.h"
17
18#include <string_ex.h>
19
20#include "call_object_manager.h"
21#include "call_manager_errors.h"
22#include "telephony_log_wrapper.h"
23
24namespace OHOS {
25namespace Telephony {
26ConferenceBase::ConferenceBase()
27    : mainCallId_(ERR_ID), state_(CONFERENCE_STATE_IDLE), oldState_(CONFERENCE_STATE_IDLE), beginTime_(0),
28      conferenceType_(CallType::TYPE_CS)
29{
30    subCallIdSet_.clear();
31}
32
33ConferenceBase::~ConferenceBase()
34{
35    std::lock_guard<std::mutex> lock(conferenceMutex_);
36    subCallIdSet_.clear();
37}
38
39int32_t ConferenceBase::GetMainCall()
40{
41    std::lock_guard<std::mutex> lock(conferenceMutex_);
42    return mainCallId_;
43}
44
45int32_t ConferenceBase::SetMainCall(int32_t callId)
46{
47    if (callId < ERR_ID) {
48        TELEPHONY_LOGE("callId is invalid:%{public}d", callId);
49        return CALL_ERR_INVALID_CALLID;
50    }
51    std::lock_guard<std::mutex> lock(conferenceMutex_);
52    mainCallId_ = callId;
53    return TELEPHONY_SUCCESS;
54}
55
56ConferenceState ConferenceBase::GetConferenceState()
57{
58    std::lock_guard<std::mutex> lock(conferenceMutex_);
59    return state_;
60}
61
62void ConferenceBase::SetConferenceState(ConferenceState state)
63{
64    std::lock_guard<std::mutex> lock(conferenceMutex_);
65    state_ = state;
66}
67
68void ConferenceBase::SetOldConferenceState(ConferenceState state)
69{
70    std::lock_guard<std::mutex> lock(conferenceMutex_);
71    oldState_ = state;
72}
73
74ConferenceState ConferenceBase::GetOldConferenceState()
75{
76    std::lock_guard<std::mutex> lock(conferenceMutex_);
77    return oldState_;
78}
79
80int32_t ConferenceBase::GetSubCallIdList(int32_t callId, std::vector<std::u16string> &callIdList)
81{
82    bool flag = false;
83    callIdList.clear();
84    std::lock_guard<std::mutex> lock(conferenceMutex_);
85    for (auto it = subCallIdSet_.begin(); it != subCallIdSet_.end(); ++it) {
86        if (*it == callId) {
87            flag = true;
88        }
89        callIdList.push_back(Str8ToStr16(std::to_string(*it)));
90        TELEPHONY_LOGI("subCallId_:%{public}d", *it);
91    }
92    if (!flag) {
93        callIdList.clear();
94        TELEPHONY_LOGW("the call is not in the conference, callId:%{public}d", callId);
95        return CALL_ERR_THE_CALL_IS_NOT_IN_THE_CONFERENCE;
96    }
97    return TELEPHONY_SUCCESS;
98}
99
100std::set<int32_t> ConferenceBase::GetSubCallIdList()
101{
102    std::lock_guard<std::mutex> lock(conferenceMutex_);
103    return subCallIdSet_;
104}
105
106int32_t ConferenceBase::GetCallIdListForConference(int32_t callId, std::vector<std::u16string> &callIdList)
107{
108    bool flag = false;
109    callIdList.clear();
110    std::lock_guard<std::mutex> lock(conferenceMutex_);
111    for (auto it = subCallIdSet_.begin(); it != subCallIdSet_.end(); ++it) {
112        if (*it == callId) {
113            flag = true;
114        }
115        callIdList.push_back(Str8ToStr16(std::to_string(*it)));
116        TELEPHONY_LOGI("subCallId_:%{public}d", *it);
117    }
118    if (mainCallId_ == callId) {
119        flag = true;
120    }
121    callIdList.push_back(Str8ToStr16(std::to_string(mainCallId_)));
122    if (!flag) {
123        callIdList.clear();
124        TELEPHONY_LOGW("the call is not in the conference, callId:%{public}d", callId);
125        return CALL_ERR_THE_CALL_IS_NOT_IN_THE_CONFERENCE;
126    }
127    return TELEPHONY_SUCCESS;
128}
129} // namespace Telephony
130} // namespace OHOS
131