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         if (!address) {
90             return TRANSACTION_ERR;
91         }
92         result.push_back(*address);
93     }
94     return BT_NO_ERROR;
95 }
96 
RegisterObserver(const sptr<IBluetoothPanObserver> observer)97 int32_t BluetoothPanProxy::RegisterObserver(const sptr<IBluetoothPanObserver> observer)
98 {
99     MessageParcel data;
100     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
101         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
102     CHECK_AND_RETURN_LOG_RET(data.WriteRemoteObject(observer->AsObject()),
103         BT_ERR_IPC_TRANS_FAILED, "Write object error");
104 
105     MessageParcel reply;
106     MessageOption option(MessageOption::TF_ASYNC);
107 
108     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_REGISTER_OBSERVER),
109         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
110 
111     return BT_NO_ERROR;
112 }
113 
DeregisterObserver(const sptr<IBluetoothPanObserver> observer)114 int32_t BluetoothPanProxy::DeregisterObserver(const sptr<IBluetoothPanObserver> observer)
115 {
116     MessageParcel data;
117     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
118         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
119     CHECK_AND_RETURN_LOG_RET(data.WriteRemoteObject(observer->AsObject()),
120         BT_ERR_IPC_TRANS_FAILED, "Write object error");
121 
122     MessageParcel reply;
123     MessageOption option(MessageOption::TF_ASYNC);
124 
125     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_DEREGISTER_OBSERVER),
126         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
127 
128     return BT_NO_ERROR;
129 }
130 
SetTethering(const bool value)131 int32_t BluetoothPanProxy::SetTethering(const bool value)
132 {
133     MessageParcel data;
134     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
135         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
136     CHECK_AND_RETURN_LOG_RET(data.WriteBool(value), BT_ERR_IPC_TRANS_FAILED, "write value error");
137 
138     MessageParcel reply;
139     MessageOption option(MessageOption::TF_SYNC);
140 
141     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_SET_TETHERING),
142         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
143 
144     return reply.ReadInt32();
145 }
146 
IsTetheringOn(bool &result)147 int32_t BluetoothPanProxy::IsTetheringOn(bool &result)
148 {
149     MessageParcel data;
150     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
151         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
152 
153     MessageParcel reply;
154     MessageOption option(MessageOption::TF_SYNC);
155 
156     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_IS_TETHERING_ON),
157         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
158 
159     int32_t ret = reply.ReadInt32();
160     if (ret != BT_NO_ERROR) {
161         HILOGE("internal error. ret:%{public}d", ret);
162         return ret;
163     }
164 
165     result = reply.ReadInt32();
166     return BT_NO_ERROR;
167 }
168 
WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)169 bool BluetoothPanProxy::WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)
170 {
171     if (!reply.WriteInt32(parcelableVector.size())) {
172         HILOGE("write ParcelableVector failed");
173         return false;
174     }
175 
176     for (auto parcelable : parcelableVector) {
177         if (!reply.WriteInt32(parcelable)) {
178             HILOGE("write ParcelableVector failed");
179             return false;
180         }
181     }
182     return true;
183 }
184 }  // namespace Bluetooth
185 }  // namespace OHOS