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_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "string_ex.h"
20
21 using std::placeholders::_1;
22 using std::placeholders::_2;
23 using std::placeholders::_3;
24 namespace OHOS {
25 namespace Bluetooth {
26 const std::map<uint32_t, std::function<ErrCode(
27 BluetoothBleAdvertiseCallbackStub *, MessageParcel &, MessageParcel &)>>
28 BluetoothBleAdvertiseCallbackStub::memberFuncMap_ = {
29 {static_cast<uint32_t>(
30 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_AUTO_STOP_EVENT),
31 BluetoothBleAdvertiseCallbackStub::OnAutoStopAdvEventInner},
32 {static_cast<uint32_t>(
33 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_START_RESULT_EVENT),
34 BluetoothBleAdvertiseCallbackStub::OnStartResultEventInner},
35 {static_cast<uint32_t>(
36 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_ENABLE_RESULT_EVENT),
37 BluetoothBleAdvertiseCallbackStub::OnEnableResultEventInner},
38 {static_cast<uint32_t>(
39 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_DISABLE_RESULT_EVENT),
40 BluetoothBleAdvertiseCallbackStub::OnDisableResultEventInner},
41 {static_cast<uint32_t>(
42 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_STOP_RESULT_EVENT),
43 BluetoothBleAdvertiseCallbackStub::OnStopResultEventInner},
44 {static_cast<uint32_t>(
45 BluetoothBleAdvertiseCallbackInterfaceCode::BT_BLE_ADVERTISE_CALLBACK_SET_ADV_DATA),
46 BluetoothBleAdvertiseCallbackStub::OnSetAdvDataEventInner},
47 };
48
BluetoothBleAdvertiseCallbackStub()49 BluetoothBleAdvertiseCallbackStub::BluetoothBleAdvertiseCallbackStub()
50 {
51 HILOGD("start.");
52 }
53
~BluetoothBleAdvertiseCallbackStub()54 BluetoothBleAdvertiseCallbackStub::~BluetoothBleAdvertiseCallbackStub()
55 {
56 HILOGD("start.");
57 }
58
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)59 int BluetoothBleAdvertiseCallbackStub::OnRemoteRequest(
60 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
61 {
62 HILOGD("BluetoothBleAdvertiseCallbackStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d",
63 code, option.GetFlags());
64 if (BluetoothBleAdvertiseCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
65 HILOGI("local descriptor is not equal to remote");
66 return ERR_INVALID_STATE;
67 }
68
69 auto itFunc = memberFuncMap_.find(code);
70 if (itFunc != memberFuncMap_.end()) {
71 auto memberFunc = itFunc->second;
72 if (memberFunc != nullptr) {
73 return memberFunc(this, data, reply);
74 }
75 }
76
77 HILOGW("BluetoothBleAdvertiseCallbackStub::OnRemoteRequest, default case, need check.");
78 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
79 }
80
81 __attribute__((no_sanitize("cfi")))
OnStartResultEventInner(BluetoothBleAdvertiseCallbackStub * stub,MessageParcel & data,MessageParcel & reply)82 ErrCode BluetoothBleAdvertiseCallbackStub::OnStartResultEventInner(
83 BluetoothBleAdvertiseCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
84 {
85 const int32_t result = static_cast<int32_t>(data.ReadInt32());
86 const int32_t advHandle = static_cast<int32_t>(data.ReadInt32());
87 const int32_t opcode = static_cast<int32_t>(data.ReadInt32());
88
89 stub->OnStartResultEvent(result, advHandle, opcode);
90 return NO_ERROR;
91 }
92
93 __attribute__((no_sanitize("cfi")))
OnEnableResultEventInner(BluetoothBleAdvertiseCallbackStub * stub,MessageParcel & data,MessageParcel & reply)94 ErrCode BluetoothBleAdvertiseCallbackStub::OnEnableResultEventInner(
95 BluetoothBleAdvertiseCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
96 {
97 const int32_t result = static_cast<int32_t>(data.ReadInt32());
98 const int32_t advHandle = static_cast<int32_t>(data.ReadInt32());
99 stub->OnEnableResultEvent(result, advHandle);
100 return NO_ERROR;
101 }
102
103 __attribute__((no_sanitize("cfi")))
OnDisableResultEventInner(BluetoothBleAdvertiseCallbackStub * stub,MessageParcel & data,MessageParcel & reply)104 ErrCode BluetoothBleAdvertiseCallbackStub::OnDisableResultEventInner(
105 BluetoothBleAdvertiseCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
106 {
107 const int32_t result = static_cast<int32_t>(data.ReadInt32());
108 const int32_t advHandle = static_cast<int32_t>(data.ReadInt32());
109 stub->OnDisableResultEvent(result, advHandle);
110 return NO_ERROR;
111 }
112
113 __attribute__((no_sanitize("cfi")))
OnStopResultEventInner(BluetoothBleAdvertiseCallbackStub * stub,MessageParcel & data,MessageParcel & reply)114 ErrCode BluetoothBleAdvertiseCallbackStub::OnStopResultEventInner(
115 BluetoothBleAdvertiseCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
116 {
117 const int32_t result = static_cast<int32_t>(data.ReadInt32());
118 const int32_t advHandle = static_cast<int32_t>(data.ReadInt32());
119 stub->OnStopResultEvent(result, advHandle);
120 return NO_ERROR;
121 }
122
123 __attribute__((no_sanitize("cfi")))
OnAutoStopAdvEventInner(BluetoothBleAdvertiseCallbackStub * stub,MessageParcel & data,MessageParcel & reply)124 ErrCode BluetoothBleAdvertiseCallbackStub::OnAutoStopAdvEventInner(
125 BluetoothBleAdvertiseCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
126 {
127 const int32_t advHandle = static_cast<int32_t>(data.ReadInt32());
128 stub->OnAutoStopAdvEvent(advHandle);
129 return NO_ERROR;
130 }
131
132 __attribute__((no_sanitize("cfi")))
OnSetAdvDataEventInner(BluetoothBleAdvertiseCallbackStub * stub,MessageParcel & data,MessageParcel & reply)133 ErrCode BluetoothBleAdvertiseCallbackStub::OnSetAdvDataEventInner(
134 BluetoothBleAdvertiseCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
135 {
136 const int32_t result = static_cast<int32_t>(data.ReadInt32());
137 const int32_t advHandle = static_cast<int32_t>(data.ReadInt32());
138 stub->OnSetAdvDataEvent(result, advHandle);
139 return NO_ERROR;
140 }
141 } // namespace Bluetooth
142 } // namespace OHOS
143