• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
75     int result = GetDeviceState(*device);
76     bool ret = reply.WriteInt32(result);
77     if (!ret) {
78         HILOGE("BluetoothPbapPseStub: reply writing failed in: %{public}s.", __func__);
79         return ERR_INVALID_VALUE;
80     }
81     return NO_ERROR;
82 }
83 
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)84 ErrCode BluetoothPbapPseStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
85 {
86     HILOGI("BluetoothPbapPseStub::GetDevicesByStatesInner starts");
87     std::vector<int32_t> state;
88     if (!data.ReadInt32Vector(&state)) {
89         HILOGW("BluetoothPbapPseStub::GetDevicesByStatesInner: get tmpState failed.");
90         return INVALID_DATA;
91     }
92     std::vector<BluetoothRawAddress> rawDevices;
93     GetDevicesByStates(state, rawDevices);
94     reply.WriteInt32(rawDevices.size());
95     int num = rawDevices.size();
96     for (int i = 0; i < num; i++) {
97         bool ret = reply.WriteParcelable(&rawDevices[i]);
98         if (!ret) {
99             HILOGE("WriteParcelable<GetDevicesByStatesInner> failed");
100             return ERR_INVALID_VALUE;
101         }
102     }
103     return NO_ERROR;
104 }
105 
DisconnectInner(MessageParcel & data,MessageParcel & reply)106 ErrCode BluetoothPbapPseStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
107 {
108     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
109     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
110     int result = Disconnect(*device);
111     bool ret = reply.WriteInt32(result);
112     if (!ret) {
113         HILOGE("BluetoothPbapPseStub: reply writing failed in: %{public}s.", __func__);
114         return ERR_INVALID_VALUE;
115     }
116     return NO_ERROR;
117 }
118 
SetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)119 ErrCode BluetoothPbapPseStub::SetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
120 {
121     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
122     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
123     int strategy = data.ReadInt32();
124     int result = SetConnectionStrategy(*device, strategy);
125     bool ret = reply.WriteInt32(result);
126     if (!ret) {
127         HILOGE("BluetoothPbapPseStub: reply writing failed in: %{public}s.", __func__);
128         return ERR_INVALID_VALUE;
129     }
130     return NO_ERROR;
131 }
132 
GetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)133 ErrCode BluetoothPbapPseStub::GetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
134 {
135     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
136     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
137     int result = GetConnectionStrategy(*device);
138     bool ret = reply.WriteInt32(result);
139     if (!ret) {
140         HILOGE("BluetoothPbapPseStub: reply writing failed in: %{public}s.", __func__);
141         return ERR_INVALID_VALUE;
142     }
143     return NO_ERROR;
144 }
145 
GrantPermissionInner(MessageParcel & data,MessageParcel & reply)146 ErrCode BluetoothPbapPseStub::GrantPermissionInner(MessageParcel &data, MessageParcel &reply)
147 {
148     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
149     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
150     bool allow = data.ReadBool();
151     bool save = data.ReadBool();
152     GrantPermission(*device, allow, save);
153 
154     return NO_ERROR;
155 }
156 
SetDevicePasswordInner(MessageParcel & data,MessageParcel & reply)157 ErrCode BluetoothPbapPseStub::SetDevicePasswordInner(MessageParcel &data, MessageParcel &reply)
158 {
159     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
160     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
161     const std::string password = data.ReadString();
162     const std::string userId = data.ReadString();
163     int result = SetDevicePassword(*device, password, userId);
164     bool ret = reply.WriteInt32(result);
165     if (!ret) {
166         HILOGE("BluetoothPbapPseStub: reply writing failed in: %{public}s.", __func__);
167         return ERR_INVALID_VALUE;
168     }
169     return NO_ERROR;
170 }
171 
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)172 ErrCode BluetoothPbapPseStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
173 {
174     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
175     sptr<IRemoteObject> remote = data.ReadRemoteObject();
176     const sptr<IBluetoothPbapPseObserver> observer = OHOS::iface_cast<IBluetoothPbapPseObserver>(remote);
177     RegisterObserver(observer);
178 
179     return NO_ERROR;
180 }
181 
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)182 ErrCode BluetoothPbapPseStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
183 {
184     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
185     sptr<IRemoteObject> remote = data.ReadRemoteObject();
186     const sptr<IBluetoothPbapPseObserver> observer = OHOS::iface_cast<IBluetoothPbapPseObserver>(remote);
187     DeregisterObserver(observer);
188 
189     return NO_ERROR;
190 }
191 }  // namespace Bluetooth
192 }  // namespace OHOS