1 /*
2 * Copyright (C) 2025 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 "nearlink_call_client.h"
17
18 #include <memory>
19 #include "call_manager_proxy.h"
20 #include "system_ability_definition.h"
21 #include "telephony_errors.h"
22
23 namespace OHOS {
24 namespace Telephony {
25 constexpr int32_t DEVICE_NEARLINK = 10;
26
NearlinkCallClient()27 NearlinkCallClient::NearlinkCallClient() {}
28
~NearlinkCallClient()29 NearlinkCallClient::~NearlinkCallClient() {}
30
Init()31 void NearlinkCallClient::Init()
32 {
33 TELEPHONY_LOGI("NearlinkCallClient init:");
34 if (callManagerProxyPtr_ == nullptr) {
35 callManagerProxyPtr_ = DelayedSingleton<CallManagerProxy>::GetInstance();
36 }
37 callManagerProxyPtr_->Init(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID);
38 sptr<IRemoteObject> iRemoteObjectPtr = callManagerProxyPtr_->GetProxyObjectPtr(PROXY_BLUETOOTH_CALL);
39 if (iRemoteObjectPtr == nullptr) {
40 TELEPHONY_LOGE("GetProxyObjectPtr failed!");
41 return;
42 }
43 bluetoothCallProxyPtr_ = iface_cast<IBluetoothCall>(iRemoteObjectPtr);
44 if (bluetoothCallProxyPtr_ == nullptr) {
45 TELEPHONY_LOGE("iface_cast<IBluetoothCall> failed!");
46 return;
47 }
48 TELEPHONY_LOGI("NearlinkCallClient init success!");
49 }
50
UnInit()51 void NearlinkCallClient::UnInit()
52 {
53 if (callManagerProxyPtr_ == nullptr) {
54 TELEPHONY_LOGE("init first please!");
55 return;
56 }
57
58 callManagerProxyPtr_->UnInit();
59 callManagerProxyPtr_ = nullptr;
60 bluetoothCallProxyPtr_ = nullptr;
61 }
62
RegisterCallBack(std::unique_ptr<CallManagerCallback> callback)63 int32_t NearlinkCallClient::RegisterCallBack(std::unique_ptr<CallManagerCallback> callback)
64 {
65 if (callManagerProxyPtr_ == nullptr) {
66 TELEPHONY_LOGE("init first please!");
67 return TELEPHONY_ERR_UNINIT;
68 }
69
70 return callManagerProxyPtr_->RegisterCallBack(std::move(callback));
71 }
72
UnRegisterCallBack()73 int32_t NearlinkCallClient::UnRegisterCallBack()
74 {
75 if (callManagerProxyPtr_ == nullptr) {
76 TELEPHONY_LOGE("init first please!");
77 return TELEPHONY_ERR_UNINIT;
78 }
79
80 return callManagerProxyPtr_->UnRegisterCallBack();
81 }
82
AnswerCall()83 int32_t NearlinkCallClient::AnswerCall()
84 {
85 if (bluetoothCallProxyPtr_ == nullptr) {
86 TELEPHONY_LOGE("init first please!");
87 return TELEPHONY_ERR_UNINIT;
88 }
89
90 return bluetoothCallProxyPtr_->AnswerCall();
91 }
92
RejectCall()93 int32_t NearlinkCallClient::RejectCall()
94 {
95 if (bluetoothCallProxyPtr_ == nullptr) {
96 TELEPHONY_LOGE("init first please!");
97 return TELEPHONY_ERR_UNINIT;
98 }
99
100 return bluetoothCallProxyPtr_->RejectCall();
101 }
102
HangUpCall()103 int32_t NearlinkCallClient::HangUpCall()
104 {
105 if (bluetoothCallProxyPtr_ == nullptr) {
106 TELEPHONY_LOGE("init first please!");
107 return TELEPHONY_ERR_UNINIT;
108 }
109
110 return bluetoothCallProxyPtr_->HangUpCall();
111 }
112
GetCallState()113 int32_t NearlinkCallClient::GetCallState()
114 {
115 if (bluetoothCallProxyPtr_ == nullptr) {
116 TELEPHONY_LOGE("init first please!");
117 return TELEPHONY_ERR_UNINIT;
118 }
119
120 return bluetoothCallProxyPtr_->GetCallState();
121 }
122
GetCurrentCallList(int32_t slotId)123 std::vector<CallAttributeInfo> NearlinkCallClient::GetCurrentCallList(int32_t slotId)
124 {
125 if (bluetoothCallProxyPtr_ == nullptr) {
126 TELEPHONY_LOGE("init first please!");
127 return std::vector<CallAttributeInfo>();
128 }
129
130 return bluetoothCallProxyPtr_->GetCurrentCallList(slotId);
131 }
132
AddAudioDevice(const std::string & address,const std::string & name)133 int32_t NearlinkCallClient::AddAudioDevice(const std::string &address, const std::string &name)
134 {
135 if (bluetoothCallProxyPtr_ == nullptr) {
136 TELEPHONY_LOGE("init first please!");
137 return TELEPHONY_ERR_UNINIT;
138 }
139
140 return bluetoothCallProxyPtr_->AddAudioDeviceList(address, DEVICE_NEARLINK, name);
141 }
142
RemoveAudioDevice(const std::string & address)143 int32_t NearlinkCallClient::RemoveAudioDevice(const std::string &address)
144 {
145 if (bluetoothCallProxyPtr_ == nullptr) {
146 TELEPHONY_LOGE("init first please!");
147 return TELEPHONY_ERR_UNINIT;
148 }
149
150 return bluetoothCallProxyPtr_->RemoveAudioDeviceList(address, DEVICE_NEARLINK);
151 }
152
ResetNearlinkDeviceList()153 int32_t NearlinkCallClient::ResetNearlinkDeviceList()
154 {
155 if (bluetoothCallProxyPtr_ == nullptr) {
156 TELEPHONY_LOGE("init first please!");
157 return TELEPHONY_ERR_UNINIT;
158 }
159
160 return bluetoothCallProxyPtr_->ResetNearlinkDeviceList();
161 }
162 } // namespace Telephony
163 } // namespace OHOS