1 /*
2 * Copyright (C) 2021-2022 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_pbap_pse_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "string_ex.h"
20 namespace OHOS {
21 namespace Bluetooth {
BluetoothPbapPseStub()22 BluetoothPbapPseStub::BluetoothPbapPseStub()
23 {
24 HILOGD("%{public}s start.", __func__);
25 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPseStub::Code::PBAP_PSE_GET_DEVICE_STATE)] =
26 &BluetoothPbapPseStub::GetDeviceStateInner;
27 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPseStub::Code::PBAP_PSE_GET_DEVICES_BY_STATES)] =
28 &BluetoothPbapPseStub::GetDevicesByStatesInner;
29 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPseStub::Code::PBAP_PSE_DISCONNECT)] =
30 &BluetoothPbapPseStub::DisconnectInner;
31 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPseStub::Code::PBAP_PSE_SET_CONNECTION_STRATEGY)] =
32 &BluetoothPbapPseStub::SetConnectionStrategyInner;
33 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPseStub::Code::PBAP_PSE_GET_CONNECTION_STRATEGY)] =
34 &BluetoothPbapPseStub::GetConnectionStrategyInner;
35 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPseStub::Code::PBAP_PSE_GRANT_PERMISSION)] =
36 &BluetoothPbapPseStub::GrantPermissionInner;
37 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPseStub::Code::PBAP_PSE_SET_DEVICE_PASSWORD)] =
38 &BluetoothPbapPseStub::SetDevicePasswordInner;
39 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPseStub::Code::PBAP_PSE_REGISTER_OBSERVER)] =
40 &BluetoothPbapPseStub::RegisterObserverInner;
41 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPseStub::Code::PBAP_PSE_DEREGISTER_OBSERVER)] =
42 &BluetoothPbapPseStub::DeregisterObserverInner;
43 }
44
~BluetoothPbapPseStub()45 BluetoothPbapPseStub::~BluetoothPbapPseStub()
46 {
47 HILOGD("%{public}s start.", __func__);
48 memberFuncMap_.clear();
49 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)50 int BluetoothPbapPseStub::OnRemoteRequest(
51 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
52 {
53 HILOGD("BluetoothPbapPseStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
54 std::u16string descriptor = BluetoothPbapPseStub::GetDescriptor();
55 std::u16string remoteDescriptor = data.ReadInterfaceToken();
56 if (descriptor != remoteDescriptor) {
57 HILOGI("local descriptor is not equal to remote");
58 return ERR_INVALID_STATE;
59 }
60
61 auto itFunc = memberFuncMap_.find(code);
62 if (itFunc != memberFuncMap_.end()) {
63 auto memberFunc = itFunc->second;
64 if (memberFunc != nullptr) {
65 return (this->*memberFunc)(data, reply);
66 }
67 }
68 HILOGW("BluetoothPbapPseStub::OnRemoteRequest, default case, need check.");
69 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70 };
71
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)72 ErrCode BluetoothPbapPseStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
73 {
74 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
75 if (!device) {
76 return TRANSACTION_ERR;
77 }
78 int result = GetDeviceState(*device);
79 bool ret = reply.WriteInt32(result);
80 if (!ret) {
81 HILOGE("BluetoothPbapPseStub: reply writing failed in: %{public}s.", __func__);
82 return ERR_INVALID_VALUE;
83 }
84 return NO_ERROR;
85 }
86
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)87 ErrCode BluetoothPbapPseStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
88 {
89 HILOGI("BluetoothPbapPseStub::GetDevicesByStatesInner starts");
90 std::vector<int32_t> state;
91 if (!data.ReadInt32Vector(&state)) {
92 HILOGW("BluetoothPbapPseStub::GetDevicesByStatesInner: get tmpState failed.");
93 return INVALID_DATA;
94 }
95 std::vector<BluetoothRawAddress> rawDevices;
96 GetDevicesByStates(state, rawDevices);
97 reply.WriteInt32(rawDevices.size());
98 int num = rawDevices.size();
99 for (int i = 0; i < num; i++) {
100 bool ret = reply.WriteParcelable(&rawDevices[i]);
101 if (!ret) {
102 HILOGE("WriteParcelable<GetDevicesByStatesInner> failed");
103 return ERR_INVALID_VALUE;
104 }
105 }
106 return NO_ERROR;
107 }
108
DisconnectInner(MessageParcel & data,MessageParcel & reply)109 ErrCode BluetoothPbapPseStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
110 {
111 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
112 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
113 if (!device) {
114 return TRANSACTION_ERR;
115 }
116 int result = Disconnect(*device);
117 bool ret = reply.WriteInt32(result);
118 if (!ret) {
119 HILOGE("BluetoothPbapPseStub: reply writing failed in: %{public}s.", __func__);
120 return ERR_INVALID_VALUE;
121 }
122 return NO_ERROR;
123 }
124
SetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)125 ErrCode BluetoothPbapPseStub::SetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
126 {
127 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
128 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
129 if (!device) {
130 return TRANSACTION_ERR;
131 }
132 int strategy = data.ReadInt32();
133 int result = SetConnectionStrategy(*device, strategy);
134 bool ret = reply.WriteInt32(result);
135 if (!ret) {
136 HILOGE("BluetoothPbapPseStub: reply writing failed in: %{public}s.", __func__);
137 return ERR_INVALID_VALUE;
138 }
139 return NO_ERROR;
140 }
141
GetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)142 ErrCode BluetoothPbapPseStub::GetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
143 {
144 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
145 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
146 if (!device) {
147 return TRANSACTION_ERR;
148 }
149 int result = GetConnectionStrategy(*device);
150 bool ret = reply.WriteInt32(result);
151 if (!ret) {
152 HILOGE("BluetoothPbapPseStub: reply writing failed in: %{public}s.", __func__);
153 return ERR_INVALID_VALUE;
154 }
155 return NO_ERROR;
156 }
157
GrantPermissionInner(MessageParcel & data,MessageParcel & reply)158 ErrCode BluetoothPbapPseStub::GrantPermissionInner(MessageParcel &data, MessageParcel &reply)
159 {
160 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
161 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
162 if (!device) {
163 return TRANSACTION_ERR;
164 }
165 bool allow = data.ReadBool();
166 bool save = data.ReadBool();
167 GrantPermission(*device, allow, save);
168
169 return NO_ERROR;
170 }
171
SetDevicePasswordInner(MessageParcel & data,MessageParcel & reply)172 ErrCode BluetoothPbapPseStub::SetDevicePasswordInner(MessageParcel &data, MessageParcel &reply)
173 {
174 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
175 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
176 if (!device) {
177 return TRANSACTION_ERR;
178 }
179 const std::string password = data.ReadString();
180 const std::string userId = data.ReadString();
181 int result = SetDevicePassword(*device, password, userId);
182 bool ret = reply.WriteInt32(result);
183 if (!ret) {
184 HILOGE("BluetoothPbapPseStub: reply writing failed in: %{public}s.", __func__);
185 return ERR_INVALID_VALUE;
186 }
187 return NO_ERROR;
188 }
189
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)190 ErrCode BluetoothPbapPseStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
191 {
192 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
193 sptr<IRemoteObject> remote = data.ReadRemoteObject();
194 const sptr<IBluetoothPbapPseObserver> observer = OHOS::iface_cast<IBluetoothPbapPseObserver>(remote);
195 RegisterObserver(observer);
196
197 return NO_ERROR;
198 }
199
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)200 ErrCode BluetoothPbapPseStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
201 {
202 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
203 sptr<IRemoteObject> remote = data.ReadRemoteObject();
204 const sptr<IBluetoothPbapPseObserver> observer = OHOS::iface_cast<IBluetoothPbapPseObserver>(remote);
205 DeregisterObserver(observer);
206
207 return NO_ERROR;
208 }
209 } // namespace Bluetooth
210 } // namespace OHOS