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