• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_service.h"
17 
18 #include "bluetooth_call_manager.h"
19 #include "call_manager_errors.h"
20 #include "telephony_errors.h"
21 #include "telephony_log_wrapper.h"
22 #include "telephony_permission.h"
23 
24 namespace OHOS {
25 namespace Telephony {
BluetoothCallService()26 BluetoothCallService::BluetoothCallService()
27     : callControlManagerPtr_(DelayedSingleton<CallControlManager>::GetInstance()),
28     sendDtmfState_(false), sendDtmfCallId_(ERR_ID)
29 {}
30 
~BluetoothCallService()31 BluetoothCallService::~BluetoothCallService()
32 {}
33 
AnswerCall()34 int32_t BluetoothCallService::AnswerCall()
35 {
36     if (!TelephonyPermission::CheckPermission(Permission::ANSWER_CALL)) {
37         TELEPHONY_LOGE("Permission denied!");
38         return TELEPHONY_ERR_PERMISSION_ERR;
39     }
40     int32_t callId = ERR_ID;
41     int32_t ret = AnswerCallPolicy(callId);
42     if (ret != TELEPHONY_SUCCESS) {
43         TELEPHONY_LOGE("AnswerCallPolicy failed!");
44         return ret;
45     }
46     sptr<CallBase> call = GetOneCallObject(callId);
47     if (call == nullptr) {
48         TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callId);
49         return TELEPHONY_ERR_LOCAL_PTR_NULL;
50     }
51     VideoStateType videoState = call->GetVideoStateType();
52     if (callControlManagerPtr_ != nullptr) {
53         return callControlManagerPtr_->AnswerCall(callId, static_cast<int32_t>(videoState));
54     } else {
55         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
56         return TELEPHONY_ERR_LOCAL_PTR_NULL;
57     }
58 }
59 
RejectCall()60 int32_t BluetoothCallService::RejectCall()
61 {
62     if (!TelephonyPermission::CheckPermission(Permission::ANSWER_CALL)) {
63         TELEPHONY_LOGE("Permission denied!");
64         return TELEPHONY_ERR_PERMISSION_ERR;
65     }
66     int32_t callId = ERR_ID;
67     bool rejectWithMessage = false;
68     std::u16string textMessage = Str8ToStr16("");
69     int32_t ret = RejectCallPolicy(callId);
70     if (ret != TELEPHONY_SUCCESS) {
71         TELEPHONY_LOGE("RejectCallPolicy failed!");
72         return ret;
73     }
74     if (callControlManagerPtr_ != nullptr) {
75         return callControlManagerPtr_->RejectCall(callId, rejectWithMessage, textMessage);
76     } else {
77         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
78         return TELEPHONY_ERR_LOCAL_PTR_NULL;
79     }
80 }
81 
HangUpCall()82 int32_t BluetoothCallService::HangUpCall()
83 {
84     if (!TelephonyPermission::CheckPermission(Permission::ANSWER_CALL)) {
85         TELEPHONY_LOGE("Permission denied!");
86         return TELEPHONY_ERR_PERMISSION_ERR;
87     }
88     int32_t callId = ERR_ID;
89     int32_t ret = HangUpPolicy(callId);
90     if (ret != TELEPHONY_SUCCESS) {
91         TELEPHONY_LOGE("HangUpPolicy failed!");
92         return ret;
93     }
94     if (callControlManagerPtr_ != nullptr) {
95         return callControlManagerPtr_->HangUpCall(callId);
96     } else {
97         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
98         return TELEPHONY_ERR_LOCAL_PTR_NULL;
99     }
100 }
101 
GetCallState()102 int32_t BluetoothCallService::GetCallState()
103 {
104     TELEPHONY_LOGI("Entry BluetoothCallService GetCallState");
105     int32_t numActive = GetCallNum(TelCallState::CALL_STATUS_ACTIVE);
106     int32_t numHeld = GetCallNum(TelCallState::CALL_STATUS_HOLDING);
107     int32_t numDial = GetCallNum(TelCallState::CALL_STATUS_DIALING);
108     int32_t numAlert = GetCallNum(TelCallState::CALL_STATUS_ALERTING);
109     int32_t numIncoming = GetCallNum(TelCallState::CALL_STATUS_INCOMING);
110     int32_t numWait = GetCallNum(TelCallState::CALL_STATUS_WAITING);
111     int32_t numDisconnected = GetCallNum(TelCallState::CALL_STATUS_DISCONNECTED);
112     int32_t numDisconnecting = GetCallNum(TelCallState::CALL_STATUS_DISCONNECTING);
113     int32_t callState = static_cast<int32_t>(TelCallState::CALL_STATUS_IDLE);
114     std::string number = "";
115     if (numHeld > 0) {
116         callState = static_cast<int32_t>(TelCallState::CALL_STATUS_IDLE);
117         number = GetCallNumber(TelCallState::CALL_STATUS_HOLDING);
118     }
119     if (numActive > 0) {
120         callState = static_cast<int32_t>(TelCallState::CALL_STATUS_IDLE);
121         number = GetCallNumber(TelCallState::CALL_STATUS_ACTIVE);
122     }
123     if (numDial > 0) {
124         callState = static_cast<int32_t>(TelCallState::CALL_STATUS_DIALING);
125         number = GetCallNumber(TelCallState::CALL_STATUS_DIALING);
126     }
127     if (numAlert > 0) {
128         callState = static_cast<int32_t>(TelCallState::CALL_STATUS_ALERTING);
129         number = GetCallNumber(TelCallState::CALL_STATUS_ALERTING);
130     }
131     if (numIncoming > 0) {
132         callState = static_cast<int32_t>(TelCallState::CALL_STATUS_INCOMING);
133         number = GetCallNumber(TelCallState::CALL_STATUS_INCOMING);
134     }
135     if (numWait > 0) {
136         callState = static_cast<int32_t>(TelCallState::CALL_STATUS_IDLE);
137         number = GetCallNumber(TelCallState::CALL_STATUS_WAITING);
138     }
139     if (numDisconnected > 0) {
140         callState = static_cast<int32_t>(TelCallState::CALL_STATUS_IDLE);
141         number = GetCallNumber(TelCallState::CALL_STATUS_DISCONNECTED);
142     }
143     if (numDisconnecting > 0) {
144         callState = static_cast<int32_t>(TelCallState::CALL_STATUS_IDLE);
145         number = GetCallNumber(TelCallState::CALL_STATUS_DISCONNECTING);
146     }
147     return DelayedSingleton<BluetoothCallManager>::GetInstance()->
148         SendBtCallState(numActive, numHeld, callState, number);
149 }
150 
HoldCall()151 int32_t BluetoothCallService::HoldCall()
152 {
153     if (!TelephonyPermission::CheckPermission(Permission::ANSWER_CALL)) {
154         TELEPHONY_LOGE("Permission denied!");
155         return TELEPHONY_ERR_PERMISSION_ERR;
156     }
157     int32_t callId = ERR_ID;
158     int32_t ret = HoldCallPolicy(callId);
159     if (ret != TELEPHONY_SUCCESS) {
160         TELEPHONY_LOGE("HoldCallPolicy failed!");
161         return ret;
162     }
163     if (callControlManagerPtr_ != nullptr) {
164         return callControlManagerPtr_->HoldCall(callId);
165     } else {
166         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
167         return TELEPHONY_ERR_LOCAL_PTR_NULL;
168     }
169 }
170 
UnHoldCall()171 int32_t BluetoothCallService::UnHoldCall()
172 {
173     if (!TelephonyPermission::CheckPermission(Permission::ANSWER_CALL)) {
174         TELEPHONY_LOGE("Permission denied!");
175         return TELEPHONY_ERR_PERMISSION_ERR;
176     }
177     int32_t callId = ERR_ID;
178     int32_t ret = UnHoldCallPolicy(callId);
179     if (ret != TELEPHONY_SUCCESS) {
180         TELEPHONY_LOGE("UnHoldCallPolicy failed!");
181         return ret;
182     }
183     if (callControlManagerPtr_ != nullptr) {
184         return callControlManagerPtr_->UnHoldCall(callId);
185     } else {
186         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
187         return TELEPHONY_ERR_LOCAL_PTR_NULL;
188     }
189 }
190 
SwitchCall()191 int32_t BluetoothCallService::SwitchCall()
192 {
193     if (!TelephonyPermission::CheckPermission(Permission::ANSWER_CALL)) {
194         TELEPHONY_LOGE("Permission denied!");
195         return TELEPHONY_ERR_PERMISSION_ERR;
196     }
197     int32_t callId = ERR_ID;
198     int32_t ret = SwitchCallPolicy(callId);
199     if (ret != TELEPHONY_SUCCESS) {
200         TELEPHONY_LOGE("SwitchCallPolicy failed!");
201         return ret;
202     }
203     if (callControlManagerPtr_ != nullptr) {
204         return callControlManagerPtr_->SwitchCall(callId);
205     } else {
206         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
207         return TELEPHONY_ERR_LOCAL_PTR_NULL;
208     }
209 }
210 
StartDtmf(char str)211 int32_t BluetoothCallService::StartDtmf(char str)
212 {
213     int32_t callId = ERR_ID;
214     int32_t ret = StartDtmfPolicy(callId);
215     if (ret != TELEPHONY_SUCCESS) {
216         TELEPHONY_LOGE("StartDtmfPolicy failed!");
217         return ret;
218     }
219     if (callControlManagerPtr_ != nullptr) {
220         {
221             std::lock_guard<std::mutex> guard(lock_);
222             sendDtmfState_ = true;
223             sendDtmfCallId_ = callId;
224         }
225         return callControlManagerPtr_->StartDtmf(callId, str);
226     } else {
227         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
228         return TELEPHONY_ERR_LOCAL_PTR_NULL;
229     }
230 }
231 
StopDtmf()232 int32_t BluetoothCallService::StopDtmf()
233 {
234     int32_t callId = ERR_ID;
235     if (callControlManagerPtr_ != nullptr) {
236         {
237             std::lock_guard<std::mutex> guard(lock_);
238             callId = sendDtmfCallId_;
239             sendDtmfState_ = false;
240             sendDtmfCallId_ = ERR_ID;
241         }
242         return callControlManagerPtr_->StopDtmf(callId);
243     } else {
244         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
245         return TELEPHONY_ERR_LOCAL_PTR_NULL;
246     }
247 }
248 
CombineConference()249 int32_t BluetoothCallService::CombineConference()
250 {
251     int32_t callId = ERR_ID;
252     int32_t ret = CombineConferencePolicy(callId);
253     if (ret != TELEPHONY_SUCCESS) {
254         TELEPHONY_LOGE("CombineConferencePolicy failed!");
255         return ret;
256     }
257     if (callControlManagerPtr_ != nullptr) {
258         return callControlManagerPtr_->CombineConference(callId);
259     } else {
260         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
261         return TELEPHONY_ERR_LOCAL_PTR_NULL;
262     }
263 }
264 
SeparateConference()265 int32_t BluetoothCallService::SeparateConference()
266 {
267     int32_t callId = ERR_ID;
268     if (callControlManagerPtr_ != nullptr) {
269         return callControlManagerPtr_->SeparateConference(callId);
270     } else {
271         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
272         return TELEPHONY_ERR_LOCAL_PTR_NULL;
273     }
274 }
275 
KickOutFromConference()276 int32_t BluetoothCallService::KickOutFromConference()
277 {
278     int32_t callId = ERR_ID;
279     if (callControlManagerPtr_ != nullptr) {
280         return callControlManagerPtr_->KickOutFromConference(callId);
281     } else {
282         TELEPHONY_LOGE("callControlManagerPtr_ is nullptr!");
283         return TELEPHONY_ERR_LOCAL_PTR_NULL;
284     }
285 }
286 
GetCurrentCallList(int32_t slotId)287 std::vector<CallAttributeInfo> BluetoothCallService::GetCurrentCallList(int32_t slotId)
288 {
289     if (!TelephonyPermission::CheckPermission(Permission::GET_TELEPHONY_STATE)) {
290         TELEPHONY_LOGE("BluetoothCallService::GetCurrentCallList, Permission denied!");
291         std::vector<CallAttributeInfo> vec;
292         vec.clear();
293         return vec;
294     }
295     return GetCallInfoList(slotId);
296 }
297 } // namespace Telephony
298 } // namespace OHOS
299