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