• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
AddAudioDeviceList(const std::string & address,int32_t deviceType,const std::string & name)132 int32_t BluetoothCallProxy::AddAudioDeviceList(const std::string &address, int32_t deviceType,
133     const std::string &name)
134 {
135     MessageParcel dataParcel;
136     if (!dataParcel.WriteInterfaceToken(BluetoothCallProxy::GetDescriptor())) {
137         TELEPHONY_LOGE("write descriptor fail");
138         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
139     }
140     dataParcel.WriteString(address);
141     dataParcel.WriteInt32(deviceType);
142     dataParcel.WriteString(name);
143     MessageParcel replyParcel;
144     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_ADD_AUDIO_DEVICE, dataParcel, replyParcel);
145 }
146 
RemoveAudioDeviceList(const std::string & address,int32_t deviceType)147 int32_t BluetoothCallProxy::RemoveAudioDeviceList(const std::string &address, int32_t deviceType)
148 {
149     MessageParcel dataParcel;
150     if (!dataParcel.WriteInterfaceToken(BluetoothCallProxy::GetDescriptor())) {
151         TELEPHONY_LOGE("write descriptor fail");
152         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
153     }
154     dataParcel.WriteString(address);
155     dataParcel.WriteInt32(deviceType);
156     MessageParcel replyParcel;
157     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_REMOVE_AUDIO_DEVICE, dataParcel, replyParcel);
158 }
159 
ResetNearlinkDeviceList()160 int32_t BluetoothCallProxy::ResetNearlinkDeviceList()
161 {
162     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_RESET_NEARLINK_AUDIO_DEVICE);
163 }
164 
ResetBtHearingAidDeviceList()165 int32_t BluetoothCallProxy::ResetBtHearingAidDeviceList()
166 {
167     return SendRequest(BluetoothCallInterfaceCode::INTERFACE_BT_RESET_BT_HEARINGAID_AUDIO_DEVICE);
168 }
169 
SendRequest(BluetoothCallInterfaceCode code)170 int32_t BluetoothCallProxy::SendRequest(BluetoothCallInterfaceCode code)
171 {
172     MessageParcel dataParcel;
173     if (!dataParcel.WriteInterfaceToken(BluetoothCallProxy::GetDescriptor())) {
174         TELEPHONY_LOGE("BluetoothCallInterfaceCode:%{public}d write descriptor fail", static_cast<int32_t>(code));
175         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
176     }
177     MessageParcel replyParcel;
178     int32_t error = SendRequest(code, dataParcel, replyParcel);
179     if (error != TELEPHONY_SUCCESS) {
180         TELEPHONY_LOGE("BluetoothCallInterfaceCode:%{public}d errCode:%{public}d", static_cast<int32_t>(code), error);
181         return error;
182     }
183     return replyParcel.ReadInt32();
184 }
185 
SendRequest(BluetoothCallInterfaceCode code,MessageParcel & dataParcel,MessageParcel & replyParcel)186 int32_t BluetoothCallProxy::SendRequest(
187     BluetoothCallInterfaceCode code, MessageParcel &dataParcel, MessageParcel &replyParcel)
188 {
189     auto remote = Remote();
190     if (remote == nullptr) {
191         TELEPHONY_LOGE(
192             "BluetoothCallInterfaceCode:%{public}d function Remote() return nullptr!", static_cast<int32_t>(code));
193         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
194     }
195     MessageOption option;
196     return remote->SendRequest(static_cast<int32_t>(code), dataParcel, replyParcel, option);
197 }
198 } // namespace Telephony
199 } // namespace OHOS
200