1 /*
2 * Copyright (C) 2021 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_ble_advertise_callback_proxy.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
BluetoothBleAdvertiseCallbackProxy(const sptr<IRemoteObject> & impl)21 BluetoothBleAdvertiseCallbackProxy::BluetoothBleAdvertiseCallbackProxy(const sptr<IRemoteObject> &impl)
22 : IRemoteProxy<IBluetoothBleAdvertiseCallback>(impl)
23 {}
24
~BluetoothBleAdvertiseCallbackProxy()25 BluetoothBleAdvertiseCallbackProxy::~BluetoothBleAdvertiseCallbackProxy()
26 {}
27
OnStartResultEvent(int32_t result,int32_t advHandle,int32_t opcode)28 void BluetoothBleAdvertiseCallbackProxy::OnStartResultEvent(int32_t result, int32_t advHandle, int32_t opcode)
29 {
30 MessageParcel data;
31 if (!data.WriteInterfaceToken(BluetoothBleAdvertiseCallbackProxy::GetDescriptor())) {
32 HILOGE("[OnStartResultEvent] fail: write interface token failed.");
33 return;
34 }
35
36 if (!data.WriteInt32(result)) {
37 HILOGE("[OnStartResultEvent] fail: write result failed");
38 return;
39 }
40
41 if (!data.WriteInt32(advHandle)) {
42 HILOGE("[OnStartResultEvent] fail: write advHandle failed");
43 return;
44 }
45
46 if (!data.WriteInt32(opcode)) {
47 HILOGE("[OnStartResultEvent] fail: write opcode failed");
48 return;
49 }
50
51 MessageParcel reply;
52 MessageOption option = {MessageOption::TF_ASYNC};
53 int error = InnerTransact(
54 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_START_RESULT_EVENT, option, data, reply);
55 if (error != NO_ERROR) {
56 HILOGE("failed, error: %{public}d", error);
57 return;
58 }
59 }
60
OnEnableResultEvent(int32_t result,int32_t advHandle)61 void BluetoothBleAdvertiseCallbackProxy::OnEnableResultEvent(int32_t result, int32_t advHandle)
62 {
63 MessageParcel data;
64 if (!data.WriteInterfaceToken(BluetoothBleAdvertiseCallbackProxy::GetDescriptor())) {
65 HILOGE("[OnEnableResultEvent] fail: write interface token failed.");
66 return;
67 }
68
69 if (!data.WriteInt32(result)) {
70 HILOGE("[OnEnableResultEvent] fail: write result failed");
71 return;
72 }
73
74 if (!data.WriteInt32(advHandle)) {
75 HILOGE("[OnEnableResultEvent] fail: write advHandle failed");
76 return;
77 }
78
79 MessageParcel reply;
80 MessageOption option = {MessageOption::TF_ASYNC};
81 int error = InnerTransact(
82 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_ENABLE_RESULT_EVENT,
83 option, data, reply);
84 if (error != NO_ERROR) {
85 HILOGE("failed, error: %{public}d", error);
86 return;
87 }
88 }
89
OnDisableResultEvent(int32_t result,int32_t advHandle)90 void BluetoothBleAdvertiseCallbackProxy::OnDisableResultEvent(int32_t result, int32_t advHandle)
91 {
92 MessageParcel data;
93 if (!data.WriteInterfaceToken(BluetoothBleAdvertiseCallbackProxy::GetDescriptor())) {
94 HILOGE("[OnDisableResultEvent] fail: write interface token failed.");
95 return;
96 }
97
98 if (!data.WriteInt32(result)) {
99 HILOGE("[OnDisableResultEvent] fail: write result failed");
100 return;
101 }
102
103 if (!data.WriteInt32(advHandle)) {
104 HILOGE("[OnDisableResultEvent] fail: write advHandle failed");
105 return;
106 }
107
108 MessageParcel reply;
109 MessageOption option = {MessageOption::TF_ASYNC};
110 int error = InnerTransact(
111 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_DISABLE_RESULT_EVENT,
112 option, data, reply);
113 if (error != NO_ERROR) {
114 HILOGE("failed, error: %{public}d", error);
115 return;
116 }
117 }
118
OnStopResultEvent(int32_t result,int32_t advHandle)119 void BluetoothBleAdvertiseCallbackProxy::OnStopResultEvent(int32_t result, int32_t advHandle)
120 {
121 MessageParcel data;
122 if (!data.WriteInterfaceToken(BluetoothBleAdvertiseCallbackProxy::GetDescriptor())) {
123 HILOGE("[OnStopResultEvent] fail: write interface token failed.");
124 return;
125 }
126
127 if (!data.WriteInt32(result)) {
128 HILOGE("[OnStopResultEvent] fail: write result failed");
129 return;
130 }
131
132 if (!data.WriteInt32(advHandle)) {
133 HILOGE("[OnStopResultEvent] fail: write advHandle failed");
134 return;
135 }
136
137 MessageParcel reply;
138 MessageOption option = {MessageOption::TF_ASYNC};
139 int error = InnerTransact(
140 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_STOP_RESULT_EVENT,
141 option, data, reply);
142 if (error != NO_ERROR) {
143 HILOGE("failed, error: %{public}d", error);
144 return;
145 }
146 }
147
OnAutoStopAdvEvent(int32_t advHandle)148 void BluetoothBleAdvertiseCallbackProxy::OnAutoStopAdvEvent(int32_t advHandle)
149 {
150 MessageParcel data;
151 if (!data.WriteInterfaceToken(BluetoothBleAdvertiseCallbackProxy::GetDescriptor())) {
152 HILOGE("[OnAutoStopAdvEvent] fail: write interface token failed.");
153 return;
154 }
155
156 if (!data.WriteInt32(advHandle)) {
157 HILOGE("[OnAutoStopAdvEvent] fail: write result failed");
158 return;
159 }
160
161 MessageParcel reply;
162 MessageOption option = {MessageOption::TF_ASYNC};
163 int error = InnerTransact(
164 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_AUTO_STOP_EVENT, option, data, reply);
165 if (error != NO_ERROR) {
166 HILOGE("BleCentralManagerCallBackProxy::OnScanCallback done fail, error: %{public}d", error);
167 return;
168 }
169 }
170
OnSetAdvDataEvent(int32_t result,int32_t advHandle)171 void BluetoothBleAdvertiseCallbackProxy::OnSetAdvDataEvent(int32_t result, int32_t advHandle)
172 {
173 return;
174 }
175
OnChangeAdvResultEvent(int32_t result,int32_t advHandle)176 void BluetoothBleAdvertiseCallbackProxy::OnChangeAdvResultEvent(int32_t result, int32_t advHandle)
177 {
178 MessageParcel data;
179 if (!data.WriteInterfaceToken(BluetoothBleAdvertiseCallbackProxy::GetDescriptor())) {
180 HILOGE("[OnChangeAdvResultEvent] fail: write interface token failed.");
181 return;
182 }
183
184 if (!data.WriteInt32(result)) {
185 HILOGE("[OnChangeAdvResultEvent] fail: write result failed");
186 return;
187 }
188
189 if (!data.WriteInt32(advHandle)) {
190 HILOGE("[OnChangeAdvResultEvent] fail: write advHandle failed");
191 return;
192 }
193
194 MessageParcel reply;
195 MessageOption option = {MessageOption::TF_ASYNC};
196 int error = InnerTransact(
197 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_CHANGE_ADV_RESULT, option, data, reply);
198 if (error != NO_ERROR) {
199 HILOGE("BleCentralManagerCallBackProxy::OnChangeAdvResultEvent done fail, error: %{public}d", error);
200 return;
201 }
202 }
203
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)204 ErrCode BluetoothBleAdvertiseCallbackProxy::InnerTransact(
205 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
206 {
207 auto remote = Remote();
208 if (remote == nullptr) {
209 HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
210 return OBJECT_NULL;
211 }
212 int err = remote->SendRequest(code, data, reply, flags);
213 switch (err) {
214 case NO_ERROR: {
215 return NO_ERROR;
216 }
217 case DEAD_OBJECT: {
218 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
219 return DEAD_OBJECT;
220 }
221 default: {
222 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
223 return TRANSACTION_ERR;
224 }
225 }
226 }
227 } // namespace Bluetooth
228 } // namespace OHOS
229