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