1 /*
2  * Copyright (C) 2021-2023 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_proxy.h"
17 
18 #include "message_option.h"
19 #include "message_parcel.h"
20 #include "native_call_manager_utils.h"
21 
22 #include "call_manager_errors.h"
23 
24 namespace OHOS {
25 namespace Telephony {
26 static const int32_t MAX_SIZE = 10000;
BluetoothCallProxy(const sptr<IRemoteObject> & impl)27 BluetoothCallProxy::BluetoothCallProxy(const sptr<IRemoteObject> &impl)
28     : IRemoteProxy<IBluetoothCall>(impl)
29 {}
30 
AnswerCall()31 int32_t BluetoothCallProxy::AnswerCall()
32 {
33     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_ANSWER_CALL);
34 }
35 
RejectCall()36 int32_t BluetoothCallProxy::RejectCall()
37 {
38     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_REJECT_CALL);
39 }
40 
HangUpCall()41 int32_t BluetoothCallProxy::HangUpCall()
42 {
43     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_DISCONNECT_CALL);
44 }
45 
GetCallState()46 int32_t BluetoothCallProxy::GetCallState()
47 {
48     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_GET_CALL_STATE);
49 }
50 
HoldCall()51 int32_t BluetoothCallProxy::HoldCall()
52 {
53     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_HOLD_CALL);
54 }
55 
UnHoldCall()56 int32_t BluetoothCallProxy::UnHoldCall()
57 {
58     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_UNHOLD_CALL);
59 }
60 
SwitchCall()61 int32_t BluetoothCallProxy::SwitchCall()
62 {
63     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_SWAP_CALL);
64 }
65 
StartDtmf(char str)66 int32_t BluetoothCallProxy::StartDtmf(char str)
67 {
68     MessageParcel dataParcel;
69     if (!dataParcel.WriteInterfaceToken(BluetoothCallProxy::GetDescriptor())) {
70         TELEPHONY_LOGE("write descriptor fail");
71         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
72     }
73     dataParcel.WriteInt8(str);
74     MessageParcel replyParcel;
75     int32_t error = SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_START_DTMF, dataParcel, replyParcel);
76     if (error != TELEPHONY_SUCCESS) {
77         TELEPHONY_LOGE("Function StartDtmf! errCode:%{public}d", error);
78         return error;
79     }
80     return replyParcel.ReadInt32();
81 }
82 
StopDtmf()83 int32_t BluetoothCallProxy::StopDtmf()
84 {
85     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_STOP_DTMF);
86 }
87 
CombineConference()88 int32_t BluetoothCallProxy::CombineConference()
89 {
90     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_COMBINE_CONFERENCE);
91 }
92 
SeparateConference()93 int32_t BluetoothCallProxy::SeparateConference()
94 {
95     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_SEPARATE_CONFERENCE);
96 }
97 
KickOutFromConference()98 int32_t BluetoothCallProxy::KickOutFromConference()
99 {
100     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_KICK_OUT_CONFERENCE);
101 }
102 
GetCurrentCallList(int32_t slotId)103 std::vector<CallAttributeInfo> BluetoothCallProxy::GetCurrentCallList(int32_t slotId)
104 {
105     std::vector<CallAttributeInfo> callVec;
106     MessageParcel dataParcel;
107     if (!dataParcel.WriteInterfaceToken(BluetoothCallProxy::GetDescriptor())) {
108         TELEPHONY_LOGE("write descriptor fail");
109         return callVec;
110     }
111     dataParcel.WriteInt32(slotId);
112     MessageParcel replyParcel;
113     int32_t error =
114         SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_GET_CURRENT_CALL_LIST, dataParcel, replyParcel);
115     if (error != TELEPHONY_SUCCESS) {
116         TELEPHONY_LOGE("Function GetCurrentCallList call failed! errCode:%{public}d", error);
117         return callVec;
118     }
119     int32_t vecCnt = replyParcel.ReadInt32();
120     if (vecCnt <= 0 || vecCnt >= MAX_SIZE) {
121         TELEPHONY_LOGE("vector size is error");
122         return callVec;
123     }
124     for (int32_t i = 0; i < vecCnt; i++) {
125         CallAttributeInfo callAttributeInfo = NativeCallManagerUtils::ReadCallAttributeInfo(replyParcel);
126         TELEPHONY_LOGI("ready to push callAttributeInfo, callId: %{public}d", callAttributeInfo.callId);
127         callVec.push_back(callAttributeInfo);
128     }
129     return callVec;
130 }
131 
SendRequest(BluetoothCallInterfaceCode code)132 int32_t BluetoothCallProxy::SendRequest(BluetoothCallInterfaceCode code)
133 {
134     MessageParcel dataParcel;
135     if (!dataParcel.WriteInterfaceToken(BluetoothCallProxy::GetDescriptor())) {
136         TELEPHONY_LOGE("BluetoothCallInterfaceCode:%{public}d write descriptor fail", static_cast<int32_t>(code));
137         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
138     }
139     MessageParcel replyParcel;
140     int32_t error = SendRequest(code, dataParcel, replyParcel);
141     if (error != TELEPHONY_SUCCESS) {
142         TELEPHONY_LOGE("BluetoothCallInterfaceCode:%{public}d errCode:%{public}d", static_cast<int32_t>(code), error);
143         return error;
144     }
145     return replyParcel.ReadInt32();
146 }
147 
SendRequest(BluetoothCallInterfaceCode code,MessageParcel & dataParcel,MessageParcel & replyParcel)148 int32_t BluetoothCallProxy::SendRequest(
149     BluetoothCallInterfaceCode code, MessageParcel &dataParcel, MessageParcel &replyParcel)
150 {
151     auto remote = Remote();
152     if (remote == nullptr) {
153         TELEPHONY_LOGE(
154             "BluetoothCallInterfaceCode:%{public}d function Remote() return nullptr!", static_cast<int32_t>(code));
155         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
156     }
157     MessageOption option;
158     return remote->SendRequest(static_cast<int32_t>(code), dataParcel, replyParcel, option);
159 }
160 } // namespace Telephony
161 } // namespace OHOS
162