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 
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)176 ErrCode BluetoothBleAdvertiseCallbackProxy::InnerTransact(
177     uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
178 {
179     auto remote = Remote();
180     if (remote == nullptr) {
181         HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
182         return OBJECT_NULL;
183     }
184     int err = remote->SendRequest(code, data, reply, flags);
185     switch (err) {
186         case NO_ERROR: {
187             return NO_ERROR;
188         }
189         case DEAD_OBJECT: {
190             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
191             return DEAD_OBJECT;
192         }
193         default: {
194             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
195             return TRANSACTION_ERR;
196         }
197     }
198 }
199 }  // namespace Bluetooth
200 }  // namespace OHOS
201