1 /*
2 * Copyright (C) 2021 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 const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
112 int32_t result;
113 Disconnect(*device, result);
114 delete device;
115 bool ret = reply.WriteInt32(result);
116 if (!ret) {
117 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
118 return ERR_INVALID_VALUE;
119 }
120 return NO_ERROR;
121 }
122
IsConnectedInner(MessageParcel & data,MessageParcel & reply)123 ErrCode BluetoothMapMseStub::IsConnectedInner(MessageParcel &data, MessageParcel &reply)
124 {
125 HILOGI("BluetoothMapMseStub::IsConnectedInner Triggered!");
126 const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
127 bool result;
128 IsConnected(*device, result);
129 delete device;
130 bool ret = reply.WriteBool(result);
131 if (!ret) {
132 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
133 return ERR_INVALID_VALUE;
134 }
135 return NO_ERROR;
136 }
137
GetConnectedDevicesInner(MessageParcel & data,MessageParcel & reply)138 ErrCode BluetoothMapMseStub::GetConnectedDevicesInner(MessageParcel &data, MessageParcel &reply)
139 {
140 HILOGI("BluetoothMapMseStub::GetConnectDevicesInner Triggered!");
141 std::vector<BluetoothRawAddress> devices;
142 GetConnectedDevices(devices);
143 reply.WriteInt32(devices.size());
144 int num = devices.size();
145 for (int i = 0; i < num; i++) {
146 bool ret = reply.WriteParcelable(&devices[i]);
147 if (!ret) {
148 HILOGE("WriteParcelable<GetConnectDevicesInner> failed");
149 return ERR_INVALID_VALUE;
150 }
151 }
152 return NO_ERROR;
153 }
154
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)155 ErrCode BluetoothMapMseStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
156 {
157 HILOGI("BluetoothMapMseStub::GetDevicesByStates Triggered!");
158 std::vector<int32_t> states;
159 if (!data.ReadInt32Vector(&states)) {
160 HILOGW("BluetoothMapMseStub::GetDevicesByStatesInner: get tmpState failed.");
161 return INVALID_DATA;
162 }
163 std::vector<BluetoothRawAddress> rawDevices;
164 GetDevicesByStates(states, rawDevices);
165 reply.WriteInt32(rawDevices.size());
166 int num = rawDevices.size();
167 for (int i = 0; i < num; i++) {
168 bool ret = reply.WriteParcelable(&rawDevices[i]);
169 if (!ret) {
170 HILOGE("WriteParcelable<GetDevicesByStatesInner> failed");
171 return ERR_INVALID_VALUE;
172 }
173 }
174 return NO_ERROR;
175 }
176
GetConnectionStateInner(MessageParcel & data,MessageParcel & reply)177 ErrCode BluetoothMapMseStub::GetConnectionStateInner(MessageParcel &data, MessageParcel &reply)
178 {
179 HILOGI("BluetoothMapMseStub::GetConnectionStateInner Triggered!");
180 const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
181 int32_t result;
182 GetConnectionState(*device, result);
183 delete device;
184 bool ret = reply.WriteInt32(result);
185 if (!ret) {
186 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
187 return ERR_INVALID_VALUE;
188 }
189 return NO_ERROR;
190 }
191
SetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)192 ErrCode BluetoothMapMseStub::SetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
193 {
194 HILOGI("BluetoothMapMseStub::SetConnectionStrategyInner Triggered!");
195 const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
196 int32_t strategy = data.ReadInt32();
197 bool result;
198 SetConnectionStrategy(*device, strategy, result);
199 delete device;
200 bool ret = reply.WriteBool(result);
201 if (!ret) {
202 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
203 return ERR_INVALID_VALUE;
204 }
205 return NO_ERROR;
206 }
207
GetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)208 ErrCode BluetoothMapMseStub::GetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
209 {
210 HILOGI("BluetoothMapMseStub::GetConnectionStrategyInner Triggered!");
211 const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
212 int32_t result;
213 GetConnectionStrategy(*device, result);
214 delete device;
215 bool ret = reply.WriteInt32(result);
216 if (!ret) {
217 HILOGE("BluetoothMapMseStub: reply writing failed in: %{public}s.", __func__);
218 return ERR_INVALID_VALUE;
219 }
220 return NO_ERROR;
221 }
222
GrantPermissionInner(MessageParcel & data,MessageParcel & reply)223 ErrCode BluetoothMapMseStub::GrantPermissionInner(MessageParcel &data, MessageParcel &reply)
224 {
225 HILOGI("BluetoothMapMseStub::GrantPermissionInner Triggered!");
226 const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
227 bool allow = data.ReadBool();
228 bool save = data.ReadBool();
229 GrantPermission(*device, allow, save);
230 delete device;
231 return NO_ERROR;
232 }
233 } // namespace Bluetooth
234 } // namespace OHOS
235