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_map_mse_stub.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
BluetoothMapMseStub()21 BluetoothMapMseStub::BluetoothMapMseStub()
22 {
23 HILOGD("%{public}s start.", __func__);
24 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_REGISTER_OBSERVER)] =
25 &BluetoothMapMseStub::RegisterObserverInner;
26 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_DE_REGISTER_OBSERVER)] =
27 &BluetoothMapMseStub::DeregisterObserverInner;
28 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_GET_STATE)] =
29 &BluetoothMapMseStub::GetStateInner;
30 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_DISCONNECT)] =
31 &BluetoothMapMseStub::DisconnectInner;
32 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_ISCONNECTED)] =
33 &BluetoothMapMseStub::IsConnectedInner;
34 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_GET_CONNECTED_DEVICES)] =
35 &BluetoothMapMseStub::GetConnectedDevicesInner;
36 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_GET_DEVICES_BY_STATES)] =
37 &BluetoothMapMseStub::GetDevicesByStatesInner;
38 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_GET_CONNECTION_STATE)] =
39 &BluetoothMapMseStub::GetConnectionStateInner;
40 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_SET_CONNECTION_STRATEGY)] =
41 &BluetoothMapMseStub::SetConnectionStrategyInner;
42 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_GET_CONNECTION_STRATEGY)] =
43 &BluetoothMapMseStub::GetConnectionStrategyInner;
44 memberFuncMap_[static_cast<uint32_t>(IBluetoothMapMse::Code::MSE_GRANT_PERMISSION)] =
45 &BluetoothMapMseStub::GrantPermissionInner;
46 }
47
~BluetoothMapMseStub()48 BluetoothMapMseStub::~BluetoothMapMseStub()
49 {
50 HILOGD("%{public}s start.", __func__);
51 memberFuncMap_.clear();
52 }
53
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)54 int BluetoothMapMseStub::OnRemoteRequest(
55 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
56 {
57 HILOGD("BluetoothMapMseStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d",
58 code,
59 option.GetFlags());
60 std::u16string descriptor = BluetoothMapMseStub::GetDescriptor();
61 std::u16string remoteDescriptor = data.ReadInterfaceToken();
62 if (descriptor != remoteDescriptor) {
63 HILOGI("local descriptor is not equal to remote");
64 return ERR_INVALID_STATE;
65 }
66 auto itFunc = memberFuncMap_.find(code);
67 if (itFunc != memberFuncMap_.end()) {
68 auto memberFunc = itFunc->second;
69 if (memberFunc != nullptr) {
70 return (this->*memberFunc)(data, reply);
71 }
72 }
73 HILOGW("BluetoothMapMseStub::OnRemoteRequest, default case, need check.");
74 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
75 }
76
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)77 ErrCode BluetoothMapMseStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
78 {
79 HILOGI("BluetoothMapMseStub::RegisterObserverInner Triggered!");
80 sptr<IRemoteObject> remote = data.ReadRemoteObject();
81 const sptr<IBluetoothMapMseObserver> callback = OHOS::iface_cast<IBluetoothMapMseObserver>(remote);
82 RegisterObserver(callback);
83 return NO_ERROR;
84 }
85
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)86 ErrCode BluetoothMapMseStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
87 {
88 HILOGI("BluetoothMapMseStub::DeregisterObserverInner Triggered!");
89 sptr<IRemoteObject> remote = data.ReadRemoteObject();
90 const sptr<IBluetoothMapMseObserver> callback = OHOS::iface_cast<IBluetoothMapMseObserver>(remote);
91 DeregisterObserver(callback);
92 return NO_ERROR;
93 }
94
GetStateInner(MessageParcel & data,MessageParcel & reply)95 ErrCode BluetoothMapMseStub::GetStateInner(MessageParcel &data, MessageParcel &reply)
96 {
97 HILOGI("BluetoothMapMseStub::GetStateInner Triggered!");
98 int32_t result;
99 GetState(result);
100 bool ret = reply.WriteInt32(result);
101 if (!ret) {
102 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
103 return ERR_INVALID_VALUE;
104 }
105 return NO_ERROR;
106 }
107
DisconnectInner(MessageParcel & data,MessageParcel & reply)108 ErrCode BluetoothMapMseStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
109 {
110 HILOGI("BluetoothMapMseStub::DisconnectInner Triggered!");
111 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
112 if (!device) {
113 return TRANSACTION_ERR;
114 }
115 int32_t result;
116 Disconnect(*device, result);
117 bool ret = reply.WriteInt32(result);
118 if (!ret) {
119 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
120 return ERR_INVALID_VALUE;
121 }
122 return NO_ERROR;
123 }
124
IsConnectedInner(MessageParcel & data,MessageParcel & reply)125 ErrCode BluetoothMapMseStub::IsConnectedInner(MessageParcel &data, MessageParcel &reply)
126 {
127 HILOGI("BluetoothMapMseStub::IsConnectedInner Triggered!");
128 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
129 if (!device) {
130 return TRANSACTION_ERR;
131 }
132 bool result;
133 IsConnected(*device, result);
134 bool ret = reply.WriteBool(result);
135 if (!ret) {
136 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
137 return ERR_INVALID_VALUE;
138 }
139 return NO_ERROR;
140 }
141
GetConnectedDevicesInner(MessageParcel & data,MessageParcel & reply)142 ErrCode BluetoothMapMseStub::GetConnectedDevicesInner(MessageParcel &data, MessageParcel &reply)
143 {
144 HILOGI("BluetoothMapMseStub::GetConnectDevicesInner Triggered!");
145 std::vector<BluetoothRawAddress> devices;
146 GetConnectedDevices(devices);
147 reply.WriteInt32(devices.size());
148 int num = devices.size();
149 for (int i = 0; i < num; i++) {
150 bool ret = reply.WriteParcelable(&devices[i]);
151 if (!ret) {
152 HILOGE("WriteParcelable<GetConnectDevicesInner> failed");
153 return ERR_INVALID_VALUE;
154 }
155 }
156 return NO_ERROR;
157 }
158
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)159 ErrCode BluetoothMapMseStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
160 {
161 HILOGI("BluetoothMapMseStub::GetDevicesByStates Triggered!");
162 std::vector<int32_t> states;
163 if (!data.ReadInt32Vector(&states)) {
164 HILOGW("BluetoothMapMseStub::GetDevicesByStatesInner: get tmpState failed.");
165 return INVALID_DATA;
166 }
167 std::vector<BluetoothRawAddress> rawDevices;
168 GetDevicesByStates(states, rawDevices);
169 reply.WriteInt32(rawDevices.size());
170 int num = rawDevices.size();
171 for (int i = 0; i < num; i++) {
172 bool ret = reply.WriteParcelable(&rawDevices[i]);
173 if (!ret) {
174 HILOGE("WriteParcelable<GetDevicesByStatesInner> failed");
175 return ERR_INVALID_VALUE;
176 }
177 }
178 return NO_ERROR;
179 }
180
GetConnectionStateInner(MessageParcel & data,MessageParcel & reply)181 ErrCode BluetoothMapMseStub::GetConnectionStateInner(MessageParcel &data, MessageParcel &reply)
182 {
183 HILOGI("BluetoothMapMseStub::GetConnectionStateInner Triggered!");
184 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
185 if (!device) {
186 return TRANSACTION_ERR;
187 }
188 int32_t result;
189 GetConnectionState(*device, result);
190 bool ret = reply.WriteInt32(result);
191 if (!ret) {
192 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
193 return ERR_INVALID_VALUE;
194 }
195 return NO_ERROR;
196 }
197
SetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)198 ErrCode BluetoothMapMseStub::SetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
199 {
200 HILOGI("BluetoothMapMseStub::SetConnectionStrategyInner Triggered!");
201 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
202 if (!device) {
203 return TRANSACTION_ERR;
204 }
205 int32_t strategy = data.ReadInt32();
206 bool result;
207 SetConnectionStrategy(*device, strategy, result);
208 bool ret = reply.WriteBool(result);
209 if (!ret) {
210 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
211 return ERR_INVALID_VALUE;
212 }
213 return NO_ERROR;
214 }
215
GetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)216 ErrCode BluetoothMapMseStub::GetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
217 {
218 HILOGI("BluetoothMapMseStub::GetConnectionStrategyInner Triggered!");
219 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
220 if (!device) {
221 return TRANSACTION_ERR;
222 }
223 int32_t result;
224 GetConnectionStrategy(*device, result);
225 bool ret = reply.WriteInt32(result);
226 if (!ret) {
227 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
228 return ERR_INVALID_VALUE;
229 }
230 return NO_ERROR;
231 }
232
GrantPermissionInner(MessageParcel & data,MessageParcel & reply)233 ErrCode BluetoothMapMseStub::GrantPermissionInner(MessageParcel &data, MessageParcel &reply)
234 {
235 HILOGI("BluetoothMapMseStub::GrantPermissionInner Triggered!");
236 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
237 if (!device) {
238 return TRANSACTION_ERR;
239 }
240 bool allow = data.ReadBool();
241 bool save = data.ReadBool();
242 GrantPermission(*device, allow, save);
243 return NO_ERROR;
244 }
245 } // namespace Bluetooth
246 } // namespace OHOS
247