1 /*
2 * Copyright (C) 2022 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 #include "bluetooth_errorcode.h"
16 #include "bluetooth_pan_proxy.h"
17 #include "bluetooth_log.h"
18 #include "refbase.h"
19
20 namespace OHOS {
21 namespace Bluetooth {
Disconnect(const BluetoothRawAddress & device)22 int32_t BluetoothPanProxy::Disconnect(const BluetoothRawAddress &device)
23 {
24 MessageParcel data;
25 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
26 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
27 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
28
29 MessageParcel reply;
30 MessageOption option(MessageOption::TF_SYNC);
31
32 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_DISCONNECT),
33 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
34
35 return reply.ReadInt32();
36 }
37
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)38 int32_t BluetoothPanProxy::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
39 {
40 MessageParcel data;
41 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
42 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
43 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
44
45 MessageParcel reply;
46 MessageOption option(MessageOption::TF_SYNC);
47
48 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_GET_DEVICE_STATE),
49 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
50
51 // read error code
52 int32_t errCode = reply.ReadInt32();
53 if (errCode != BT_NO_ERROR) {
54 HILOGE("reply errCode: %{public}d", errCode);
55 return errCode;
56 }
57 // read state
58 state = reply.ReadInt32();
59 return BT_NO_ERROR;
60 }
61
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)62 int32_t BluetoothPanProxy::GetDevicesByStates(const std::vector<int32_t> &states,
63 std::vector<BluetoothRawAddress>& result)
64 {
65 MessageParcel data;
66 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
67 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
68 CHECK_AND_RETURN_LOG_RET(WriteParcelableInt32Vector(states, data), BT_ERR_IPC_TRANS_FAILED, "write device error");
69
70 MessageParcel reply;
71 MessageOption option(MessageOption::TF_SYNC);
72
73 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_GET_DEVICES_BY_STATES),
74 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
75
76 // read error code
77 int32_t errCode = reply.ReadInt32();
78 if (errCode != BT_NO_ERROR) {
79 HILOGE("reply errCode: %{public}d", errCode);
80 return errCode;
81 }
82
83 // read size
84 int32_t rawAddsSize = reply.ReadInt32();
85
86 // read devices
87 for (int i = 0; i < rawAddsSize; i++) {
88 std::unique_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
89 result.push_back(*address);
90 }
91 return BT_NO_ERROR;
92 }
93
RegisterObserver(const sptr<IBluetoothPanObserver> observer)94 int32_t BluetoothPanProxy::RegisterObserver(const sptr<IBluetoothPanObserver> observer)
95 {
96 MessageParcel data;
97 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
98 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
99 CHECK_AND_RETURN_LOG_RET(data.WriteRemoteObject(observer->AsObject()),
100 BT_ERR_IPC_TRANS_FAILED, "Write object error");
101
102 MessageParcel reply;
103 MessageOption option(MessageOption::TF_ASYNC);
104
105 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_REGISTER_OBSERVER),
106 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
107
108 return BT_NO_ERROR;
109 }
110
DeregisterObserver(const sptr<IBluetoothPanObserver> observer)111 int32_t BluetoothPanProxy::DeregisterObserver(const sptr<IBluetoothPanObserver> observer)
112 {
113 MessageParcel data;
114 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
115 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
116 CHECK_AND_RETURN_LOG_RET(data.WriteRemoteObject(observer->AsObject()),
117 BT_ERR_IPC_TRANS_FAILED, "Write object error");
118
119 MessageParcel reply;
120 MessageOption option(MessageOption::TF_ASYNC);
121
122 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_DEREGISTER_OBSERVER),
123 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
124
125 return BT_NO_ERROR;
126 }
127
SetTethering(const bool value)128 int32_t BluetoothPanProxy::SetTethering(const bool value)
129 {
130 MessageParcel data;
131 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
132 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
133 CHECK_AND_RETURN_LOG_RET(data.WriteBool(value), BT_ERR_IPC_TRANS_FAILED, "write value error");
134
135 MessageParcel reply;
136 MessageOption option(MessageOption::TF_SYNC);
137
138 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_SET_TETHERING),
139 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
140
141 return reply.ReadInt32();
142 }
143
IsTetheringOn(bool & result)144 int32_t BluetoothPanProxy::IsTetheringOn(bool &result)
145 {
146 MessageParcel data;
147 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
148 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
149
150 MessageParcel reply;
151 MessageOption option(MessageOption::TF_SYNC);
152
153 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_IS_TETHERING_ON),
154 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
155
156 int32_t ret = reply.ReadInt32();
157 if (ret != BT_NO_ERROR) {
158 HILOGE("internal error. ret:%{public}d", ret);
159 return ret;
160 }
161
162 result = reply.ReadInt32();
163 return BT_NO_ERROR;
164 }
165
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)166 bool BluetoothPanProxy::WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)
167 {
168 if (!reply.WriteInt32(parcelableVector.size())) {
169 HILOGE("write ParcelableVector failed");
170 return false;
171 }
172
173 for (auto parcelable : parcelableVector) {
174 if (!reply.WriteInt32(parcelable)) {
175 HILOGE("write ParcelableVector failed");
176 return false;
177 }
178 }
179 return true;
180 }
181 } // namespace Bluetooth
182 } // namespace OHOS