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_RESULT_EVENT, option, data, reply);
55 if (error != NO_ERROR) {
56 HILOGE("BleCentralManagerCallBackProxy::OnScanCallback done fail, error: %{public}d", error);
57 return;
58 }
59 }
60
OnAutoStopAdvEvent(int32_t advHandle)61 void BluetoothBleAdvertiseCallbackProxy::OnAutoStopAdvEvent(int32_t advHandle)
62 {
63 MessageParcel data;
64 if (!data.WriteInterfaceToken(BluetoothBleAdvertiseCallbackProxy::GetDescriptor())) {
65 HILOGE("[OnAutoStopAdvEvent] fail: write interface token failed.");
66 return;
67 }
68
69 if (!data.WriteInt32(advHandle)) {
70 HILOGE("[OnAutoStopAdvEvent] fail: write result failed");
71 return;
72 }
73
74 MessageParcel reply;
75 MessageOption option = {MessageOption::TF_ASYNC};
76 int error = InnerTransact(
77 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_AUTO_STOP_EVENT, option, data, reply);
78 if (error != NO_ERROR) {
79 HILOGE("BleCentralManagerCallBackProxy::OnScanCallback done fail, error: %{public}d", error);
80 return;
81 }
82 }
83
OnSetAdvDataEvent(int32_t result,int32_t advHandle)84 void BluetoothBleAdvertiseCallbackProxy::OnSetAdvDataEvent(int32_t result, int32_t advHandle)
85 {
86 return;
87 }
88
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)89 ErrCode BluetoothBleAdvertiseCallbackProxy::InnerTransact(
90 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
91 {
92 auto remote = Remote();
93 if (remote == nullptr) {
94 HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
95 return OBJECT_NULL;
96 }
97 int err = remote->SendRequest(code, data, reply, flags);
98 switch (err) {
99 case NO_ERROR: {
100 return NO_ERROR;
101 }
102 case DEAD_OBJECT: {
103 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
104 return DEAD_OBJECT;
105 }
106 default: {
107 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
108 return TRANSACTION_ERR;
109 }
110 }
111 }
112 } // namespace Bluetooth
113 } // namespace OHOS
114