1 /*
2  * Copyright (C) 2023 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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_ipc_pbap_pse_proxy"
17 #endif
18 
19 #include "bluetooth_log.h"
20 #include "bluetooth_errorcode.h"
21 #include "bluetooth_pbap_pse_proxy.h"
22 
23 namespace OHOS {
24 namespace Bluetooth {
25 const int32_t PBAP_PSE_READ_DEVICE_MAX_SIZE = 0x100;
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)26 int32_t BluetoothPbapPseProxy::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
27 {
28     MessageParcel data;
29     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()),
30         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
31     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
32 
33     MessageParcel reply;
34     MessageOption option(MessageOption::TF_SYNC);
35 
36     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothPbapPseInterfaceCode::PBAP_PSE_GET_DEVICE_STATE,
37         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
38 
39     int32_t ret = reply.ReadInt32();
40     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
41     state = reply.ReadInt32();
42     return BT_NO_ERROR;
43 }
44 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & rawDevices)45 int32_t BluetoothPbapPseProxy::GetDevicesByStates(
46     const std::vector<int32_t> &states, std::vector<BluetoothRawAddress> &rawDevices)
47 {
48     MessageParcel data;
49     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()),
50         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
51     CHECK_AND_RETURN_LOG_RET(data.WriteInt32Vector(states), BT_ERR_IPC_TRANS_FAILED, "WriteInt32Vector error");
52 
53     MessageParcel reply;
54     MessageOption option(MessageOption::TF_SYNC);
55 
56     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothPbapPseInterfaceCode::PBAP_PSE_GET_DEVICES_BY_STATES,
57         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
58 
59     int32_t ret = reply.ReadInt32();
60     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
61     int32_t devNum = reply.ReadInt32();
62     CHECK_AND_RETURN_LOG_RET((devNum >= 0 && devNum < PBAP_PSE_READ_DEVICE_MAX_SIZE),
63         BT_ERR_IPC_TRANS_FAILED, "Invalid devNum: %{public}d", devNum);
64 
65     for (int32_t i = 0; i < devNum; i++) {
66         std::shared_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
67         CHECK_AND_RETURN_LOG_RET((address != nullptr), BT_ERR_IPC_TRANS_FAILED, "address is nullptr");
68         rawDevices.push_back(*address);
69     }
70     return BT_NO_ERROR;
71 }
72 
Disconnect(const BluetoothRawAddress & device)73 int32_t BluetoothPbapPseProxy::Disconnect(const BluetoothRawAddress &device)
74 {
75     MessageParcel data;
76     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()),
77         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
78     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
79 
80     MessageParcel reply;
81     MessageOption option(MessageOption::TF_SYNC);
82 
83     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothPbapPseInterfaceCode::PBAP_PSE_DISCONNECT,
84         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
85 
86     return reply.ReadInt32();
87 }
88 
SetConnectionStrategy(const BluetoothRawAddress & device,int32_t strategy)89 int32_t BluetoothPbapPseProxy::SetConnectionStrategy(const BluetoothRawAddress &device, int32_t strategy)
90 {
91     MessageParcel data;
92     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()),
93         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
94     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
95     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(strategy), BT_ERR_IPC_TRANS_FAILED, "Write strategy error");
96 
97     MessageParcel reply;
98     MessageOption option(MessageOption::TF_SYNC);
99 
100     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothPbapPseInterfaceCode::PBAP_PSE_SET_CONNECTION_STRATEGY,
101         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
102 
103     return reply.ReadInt32();
104 }
105 
GetConnectionStrategy(const BluetoothRawAddress & device,int32_t & strategy)106 int32_t BluetoothPbapPseProxy::GetConnectionStrategy(const BluetoothRawAddress &device, int32_t &strategy)
107 {
108     MessageParcel data;
109     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()),
110         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
111     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
112 
113     MessageParcel reply;
114     MessageOption option(MessageOption::TF_SYNC);
115 
116     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothPbapPseInterfaceCode::PBAP_PSE_GET_CONNECTION_STRATEGY,
117         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
118 
119     int32_t ret = reply.ReadInt32();
120     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
121     strategy = reply.ReadInt32();
122     return BT_NO_ERROR;
123 }
124 
SetShareType(const BluetoothRawAddress & device,int32_t shareType)125 int32_t BluetoothPbapPseProxy::SetShareType(const BluetoothRawAddress &device, int32_t shareType)
126 {
127     MessageParcel data;
128     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()),
129         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
130     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
131     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(shareType), BT_ERR_IPC_TRANS_FAILED, "Write shareType error");
132 
133     MessageParcel reply;
134     MessageOption option(MessageOption::TF_SYNC);
135 
136     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothPbapPseInterfaceCode::PBAP_PSE_SET_SHARE_TYPE,
137         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
138 
139     return reply.ReadInt32();
140 }
141 
GetShareType(const BluetoothRawAddress & device,int32_t & shareType)142 int32_t BluetoothPbapPseProxy::GetShareType(const BluetoothRawAddress &device, int32_t &shareType)
143 {
144     MessageParcel data;
145     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()),
146         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
147     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
148 
149     MessageParcel reply;
150     MessageOption option(MessageOption::TF_SYNC);
151 
152     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothPbapPseInterfaceCode::PBAP_PSE_GET_SHARE_TYPE,
153         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
154 
155     int32_t ret = reply.ReadInt32();
156     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
157     shareType = reply.ReadInt32();
158     return BT_NO_ERROR;
159 }
160 
SetPhoneBookAccessAuthorization(const BluetoothRawAddress & device,int32_t accessAuthorization)161 int32_t BluetoothPbapPseProxy::SetPhoneBookAccessAuthorization(const BluetoothRawAddress &device,
162     int32_t accessAuthorization)
163 {
164     MessageParcel data;
165     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()),
166         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
167     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
168     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(accessAuthorization),
169         BT_ERR_IPC_TRANS_FAILED, "Write accessAuthorization error");
170 
171     MessageParcel reply;
172     MessageOption option(MessageOption::TF_SYNC);
173 
174     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothPbapPseInterfaceCode::PBAP_PSE_SET_ACCESS_AUTHORIZATION,
175         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
176 
177     return reply.ReadInt32();
178 }
179 
GetPhoneBookAccessAuthorization(const BluetoothRawAddress & device,int32_t & accessAuthorization)180 int32_t BluetoothPbapPseProxy::GetPhoneBookAccessAuthorization(const BluetoothRawAddress &device,
181     int32_t &accessAuthorization)
182 {
183     MessageParcel data;
184     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()),
185         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
186     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
187 
188     MessageParcel reply;
189     MessageOption option(MessageOption::TF_SYNC);
190 
191     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothPbapPseInterfaceCode::PBAP_PSE_GET_ACCESS_AUTHORIZATION,
192         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
193 
194     int32_t ret = reply.ReadInt32();
195     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
196     accessAuthorization = reply.ReadInt32();
197     return BT_NO_ERROR;
198 }
199 
RegisterObserver(const sptr<IBluetoothPbapPseObserver> & observer)200 void BluetoothPbapPseProxy::RegisterObserver(const sptr<IBluetoothPbapPseObserver> &observer)
201 {
202     MessageParcel data;
203     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()), "WriteInterfaceToken error");
204     CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "Write object error");
205 
206     MessageParcel reply;
207     MessageOption option(MessageOption::TF_SYNC);
208 
209     SEND_IPC_REQUEST_RETURN(BluetoothPbapPseInterfaceCode::PBAP_PSE_REGISTER_OBSERVER, data, reply, option);
210 }
211 
DeregisterObserver(const sptr<IBluetoothPbapPseObserver> & observer)212 void BluetoothPbapPseProxy::DeregisterObserver(const sptr<IBluetoothPbapPseObserver> &observer)
213 {
214     MessageParcel data;
215     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor()), "WriteInterfaceToken error");
216     CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "Write object error");
217 
218     MessageParcel reply;
219     MessageOption option(MessageOption::TF_SYNC);
220 
221     SEND_IPC_REQUEST_RETURN(BluetoothPbapPseInterfaceCode::PBAP_PSE_DEREGISTER_OBSERVER, data, reply, option);
222 }
223 }  // namespace Bluetooth
224 }  // namespace OHOS