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_stub.h"
17
18 #include <string_ex.h>
19
20 #include "call_manager_errors.h"
21 #include "telephony_log_wrapper.h"
22
23 #include "message_option.h"
24 #include "message_parcel.h"
25 #include "call_manager_utils.h"
26
27 #include "call_control_manager.h"
28
29 namespace OHOS {
30 namespace Telephony {
BluetoothCallStub()31 BluetoothCallStub::BluetoothCallStub()
32 {
33 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_ANSWER_CALL)] =
34 [this](MessageParcel &data, MessageParcel &reply) { return OnAnswerCall(data, reply); };
35 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_REJECT_CALL)] =
36 [this](MessageParcel &data, MessageParcel &reply) { return OnRejectCall(data, reply); };
37 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_HOLD_CALL)] =
38 [this](MessageParcel &data, MessageParcel &reply) { return OnHoldCall(data, reply); };
39 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_UNHOLD_CALL)] =
40 [this](MessageParcel &data, MessageParcel &reply) { return OnUnHoldCall(data, reply); };
41 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_DISCONNECT_CALL)] =
42 [this](MessageParcel &data, MessageParcel &reply) { return OnHangUpCall(data, reply); };
43 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_GET_CALL_STATE)] =
44 [this](MessageParcel &data, MessageParcel &reply) { return OnGetBtCallState(data, reply); };
45 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_SWAP_CALL)] =
46 [this](MessageParcel &data, MessageParcel &reply) { return OnSwitchCall(data, reply); };
47 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_COMBINE_CONFERENCE)] =
48 [this](MessageParcel &data, MessageParcel &reply) { return OnCombineConference(data, reply); };
49 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_SEPARATE_CONFERENCE)] =
50 [this](MessageParcel &data, MessageParcel &reply) { return OnSeparateConference(data, reply); };
51 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_KICK_OUT_CONFERENCE)] =
52 [this](MessageParcel &data, MessageParcel &reply) { return OnKickOutFromConference(data, reply); };
53 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_START_DTMF)] =
54 [this](MessageParcel &data, MessageParcel &reply) { return OnStartDtmf(data, reply); };
55 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_STOP_DTMF)] =
56 [this](MessageParcel &data, MessageParcel &reply) { return OnStopDtmf(data, reply); };
57 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_GET_CURRENT_CALL_LIST)] =
58 [this](MessageParcel &data, MessageParcel &reply) { return OnGetCurrentCallList(data, reply); };
59 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_ADD_AUDIO_DEVICE)] =
60 [this](MessageParcel &data, MessageParcel &reply) { return OnAddAudioDeviceList(data, reply); };
61 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_REMOVE_AUDIO_DEVICE)] =
62 [this](MessageParcel &data, MessageParcel &reply) { return OnRemoveAudioDeviceList(data, reply); };
63 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_RESET_NEARLINK_AUDIO_DEVICE)] =
64 [this](MessageParcel &data, MessageParcel &reply) { return OnResetNearlinkDeviceList(data, reply); };
65 memberFuncMap_[static_cast<uint32_t>(BluetoothCallInterfaceCode::INTERFACE_BT_RESET_BT_HEARINGAID_AUDIO_DEVICE)] =
66 [this](MessageParcel &data, MessageParcel &reply) { return OnResetBtHearingAidDeviceList(data, reply); };
67 }
68
~BluetoothCallStub()69 BluetoothCallStub::~BluetoothCallStub()
70 {
71 memberFuncMap_.clear();
72 }
73
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)74 int32_t BluetoothCallStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
75 MessageOption &option)
76 {
77 std::u16string myDescriptor = BluetoothCallStub::GetDescriptor();
78 std::u16string remoteDescriptor = data.ReadInterfaceToken();
79 if (myDescriptor != remoteDescriptor) {
80 TELEPHONY_LOGE("descriptor checked fail !");
81 return TELEPHONY_ERR_DESCRIPTOR_MISMATCH;
82 }
83 TELEPHONY_LOGI("OnReceived, cmd = %{public}u", code);
84 auto itFunc = memberFuncMap_.find(code);
85 if (itFunc != memberFuncMap_.end()) {
86 auto memberFunc = itFunc->second;
87 if (memberFunc != nullptr) {
88 return memberFunc(data, reply);
89 }
90 }
91 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
92 }
93
OnAnswerCall(MessageParcel & data,MessageParcel & reply)94 int32_t BluetoothCallStub::OnAnswerCall(MessageParcel &data, MessageParcel &reply)
95 {
96 int32_t result = AnswerCall();
97 TELEPHONY_LOGI("result:%{public}d", result);
98 if (!reply.WriteInt32(result)) {
99 TELEPHONY_LOGE("fail to write parcel");
100 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
101 }
102 return result;
103 }
104
OnRejectCall(MessageParcel & data,MessageParcel & reply)105 int32_t BluetoothCallStub::OnRejectCall(MessageParcel &data, MessageParcel &reply)
106 {
107 int32_t result = RejectCall();
108 TELEPHONY_LOGI("result:%{public}d", result);
109 if (!reply.WriteInt32(result)) {
110 TELEPHONY_LOGE("fail to write parcel");
111 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
112 }
113 return result;
114 }
115
OnHangUpCall(MessageParcel & data,MessageParcel & reply)116 int32_t BluetoothCallStub::OnHangUpCall(MessageParcel &data, MessageParcel &reply)
117 {
118 int32_t result = HangUpCall();
119 TELEPHONY_LOGI("result:%{public}d", result);
120 if (!reply.WriteInt32(result)) {
121 TELEPHONY_LOGE("fail to write parcel");
122 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
123 }
124 return result;
125 }
126
OnGetBtCallState(MessageParcel & data,MessageParcel & reply)127 int32_t BluetoothCallStub::OnGetBtCallState(MessageParcel &data, MessageParcel &reply)
128 {
129 int32_t result = GetCallState();
130 TELEPHONY_LOGI("result:%{public}d", result);
131 if (!reply.WriteInt32(result)) {
132 TELEPHONY_LOGE("fail to write parcel");
133 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
134 }
135 return TELEPHONY_SUCCESS;
136 }
137
OnHoldCall(MessageParcel & data,MessageParcel & reply)138 int32_t BluetoothCallStub::OnHoldCall(MessageParcel &data, MessageParcel &reply)
139 {
140 int32_t result = HoldCall();
141 TELEPHONY_LOGI("result:%{public}d", result);
142 if (!reply.WriteInt32(result)) {
143 TELEPHONY_LOGE("fail to write parcel");
144 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
145 }
146 return result;
147 }
148
OnUnHoldCall(MessageParcel & data,MessageParcel & reply)149 int32_t BluetoothCallStub::OnUnHoldCall(MessageParcel &data, MessageParcel &reply)
150 {
151 int32_t result = UnHoldCall();
152 TELEPHONY_LOGI("result:%{public}d", result);
153 if (!reply.WriteInt32(result)) {
154 TELEPHONY_LOGE("fail to write parcel");
155 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
156 }
157 return result;
158 }
159
OnSwitchCall(MessageParcel & data,MessageParcel & reply)160 int32_t BluetoothCallStub::OnSwitchCall(MessageParcel &data, MessageParcel &reply)
161 {
162 int32_t result = SwitchCall();
163 TELEPHONY_LOGI("result:%{public}d", result);
164 if (!reply.WriteInt32(result)) {
165 TELEPHONY_LOGE("fail to write parcel");
166 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
167 }
168 return result;
169 }
170
OnCombineConference(MessageParcel & data,MessageParcel & reply)171 int32_t BluetoothCallStub::OnCombineConference(MessageParcel &data, MessageParcel &reply)
172 {
173 int32_t result = CombineConference();
174 TELEPHONY_LOGI("result:%{public}d", result);
175 if (!reply.WriteInt32(result)) {
176 TELEPHONY_LOGE("fail to write parcel");
177 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
178 }
179 return TELEPHONY_SUCCESS;
180 }
181
OnSeparateConference(MessageParcel & data,MessageParcel & reply)182 int32_t BluetoothCallStub::OnSeparateConference(MessageParcel &data, MessageParcel &reply)
183 {
184 int32_t result = SeparateConference();
185 TELEPHONY_LOGI("result:%{public}d", result);
186 if (!reply.WriteInt32(result)) {
187 TELEPHONY_LOGE("fail to write parcel");
188 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
189 }
190 return TELEPHONY_SUCCESS;
191 }
192
OnKickOutFromConference(MessageParcel & data,MessageParcel & reply)193 int32_t BluetoothCallStub::OnKickOutFromConference(MessageParcel &data, MessageParcel &reply)
194 {
195 int32_t result = KickOutFromConference();
196 TELEPHONY_LOGI("result:%{public}d", result);
197 if (!reply.WriteInt32(result)) {
198 TELEPHONY_LOGE("fail to write parcel");
199 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
200 }
201 return TELEPHONY_SUCCESS;
202 }
203
OnStartDtmf(MessageParcel & data,MessageParcel & reply)204 int32_t BluetoothCallStub::OnStartDtmf(MessageParcel &data, MessageParcel &reply)
205 {
206 int32_t result = TELEPHONY_ERR_FAIL;
207 char str = static_cast<char>(data.ReadInt8());
208 result = StartDtmf(str);
209 TELEPHONY_LOGI("result:%{public}d", result);
210 if (!reply.WriteInt32(result)) {
211 TELEPHONY_LOGE("fail to write parcel");
212 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
213 }
214 return TELEPHONY_SUCCESS;
215 }
216
OnStopDtmf(MessageParcel & data,MessageParcel & reply)217 int32_t BluetoothCallStub::OnStopDtmf(MessageParcel &data, MessageParcel &reply)
218 {
219 int32_t result = TELEPHONY_ERR_FAIL;
220 result = StopDtmf();
221 TELEPHONY_LOGI("result:%{public}d", result);
222 if (!reply.WriteInt32(result)) {
223 TELEPHONY_LOGE("fail to write parcel");
224 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
225 }
226 return TELEPHONY_SUCCESS;
227 }
228
OnGetCurrentCallList(MessageParcel & data,MessageParcel & reply)229 int32_t BluetoothCallStub::OnGetCurrentCallList(MessageParcel &data, MessageParcel &reply)
230 {
231 int32_t slotId = data.ReadInt32();
232 std::vector<CallAttributeInfo> callVec = GetCurrentCallList(slotId);
233 reply.WriteInt32(callVec.size());
234 std::vector<CallAttributeInfo>::iterator it = callVec.begin();
235 for (; it != callVec.end(); ++it) {
236 CallManagerUtils::WriteCallAttributeInfo(*it, reply);
237 }
238 return TELEPHONY_SUCCESS;
239 }
240
OnAddAudioDeviceList(MessageParcel & data,MessageParcel & reply)241 int32_t BluetoothCallStub::OnAddAudioDeviceList(MessageParcel &data, MessageParcel &reply)
242 {
243 std::string address = data.ReadString();
244 int32_t deviceType = data.ReadInt32();
245 std::string name = data.ReadString();
246 int32_t result = AddAudioDeviceList(address, deviceType, name);
247 TELEPHONY_LOGI("result:%{public}d", result);
248 if (!reply.WriteInt32(result)) {
249 TELEPHONY_LOGE("fail to write parcel");
250 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
251 }
252 return result;
253 }
254
OnRemoveAudioDeviceList(MessageParcel & data,MessageParcel & reply)255 int32_t BluetoothCallStub::OnRemoveAudioDeviceList(MessageParcel &data, MessageParcel &reply)
256 {
257 std::string address = data.ReadString();
258 int32_t deviceType = data.ReadInt32();
259 int32_t result = RemoveAudioDeviceList(address, deviceType);
260 TELEPHONY_LOGI("result:%{public}d", result);
261 if (!reply.WriteInt32(result)) {
262 TELEPHONY_LOGE("fail to write parcel");
263 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
264 }
265 return result;
266 }
267
OnResetNearlinkDeviceList(MessageParcel & data,MessageParcel & reply)268 int32_t BluetoothCallStub::OnResetNearlinkDeviceList(MessageParcel &data, MessageParcel &reply)
269 {
270 int32_t result = ResetNearlinkDeviceList();
271 TELEPHONY_LOGI("result:%{public}d", result);
272 if (!reply.WriteInt32(result)) {
273 TELEPHONY_LOGE("fail to write parcel");
274 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
275 }
276 return result;
277 }
278
OnResetBtHearingAidDeviceList(MessageParcel & data,MessageParcel & reply)279 int32_t BluetoothCallStub::OnResetBtHearingAidDeviceList(MessageParcel &data, MessageParcel &reply)
280 {
281 int32_t result = ResetBtHearingAidDeviceList();
282 TELEPHONY_LOGI("result:%{public}d", result);
283 if (!reply.WriteInt32(result)) {
284 TELEPHONY_LOGE("fail to write parcel");
285 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
286 }
287 return result;
288 }
289 } // namespace Telephony
290 } // namespace OHOS
291