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_gatt_client_callback_stub.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
BluetoothGattClientCallbackStub()21 BluetoothGattClientCallbackStub::BluetoothGattClientCallbackStub()
22 {
23 HILOGD("%{public}s start.", __func__);
24 memberFuncMap_[static_cast<uint32_t>(
25 IBluetoothGattClientCallback::Code::BT_GATT_CLIENT_CALLBACK_CONNECT_STATE_CHANGE)] =
26 &BluetoothGattClientCallbackStub::OnConnectionStateChangedInner;
27 memberFuncMap_[static_cast<uint32_t>(
28 IBluetoothGattClientCallback::Code::BT_GATT_CLIENT_CALLBACK_CHARACTER_CHANGE)] =
29 &BluetoothGattClientCallbackStub::OnCharacteristicChangedInner;
30 memberFuncMap_[static_cast<uint32_t>(IBluetoothGattClientCallback::Code::BT_GATT_CLIENT_CALLBACK_CHARACTER_READ)] =
31 &BluetoothGattClientCallbackStub::OnCharacteristicReadInner;
32 memberFuncMap_[static_cast<uint32_t>(IBluetoothGattClientCallback::Code::BT_GATT_CLIENT_CALLBACK_CHARACTER_WRITE)] =
33 &BluetoothGattClientCallbackStub::OnCharacteristicWriteInner;
34 memberFuncMap_[static_cast<uint32_t>(IBluetoothGattClientCallback::Code::BT_GATT_CLIENT_CALLBACK_DESCRIPTOR_READ)] =
35 &BluetoothGattClientCallbackStub::OnDescriptorReadInner;
36 memberFuncMap_[static_cast<uint32_t>(
37 IBluetoothGattClientCallback::Code::BT_GATT_CLIENT_CALLBACK_DESCRIPTOR_WRITE)] =
38 &BluetoothGattClientCallbackStub::OnDescriptorWriteInner;
39 memberFuncMap_[static_cast<uint32_t>(IBluetoothGattClientCallback::Code::BT_GATT_CLIENT_CALLBACK_MTU_UPDATE)] =
40 &BluetoothGattClientCallbackStub::OnMtuChangedInner;
41 memberFuncMap_[static_cast<uint32_t>(
42 IBluetoothGattClientCallback::Code::BT_GATT_CLIENT_CALLBACK_SERVICES_DISCOVER)] =
43 &BluetoothGattClientCallbackStub::OnServicesDiscoveredInner;
44 memberFuncMap_[static_cast<uint32_t>(
45 IBluetoothGattClientCallback::Code::BT_GATT_CLIENT_CALLBACK_CONNECTION_PARA_CHANGE)] =
46 &BluetoothGattClientCallbackStub::OnConnectionParameterChangedInner;
47 memberFuncMap_[static_cast<uint32_t>(
48 IBluetoothGattClientCallback::Code::BT_GATT_CLIENT_CALLBACK_SERVICES_CHANGED)] =
49 &BluetoothGattClientCallbackStub::OnServicesChangedInner;
50 }
51
~BluetoothGattClientCallbackStub()52 BluetoothGattClientCallbackStub::~BluetoothGattClientCallbackStub()
53 {
54 HILOGD("%{public}s start.", __func__);
55 memberFuncMap_.clear();
56 }
57
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)58 int BluetoothGattClientCallbackStub::OnRemoteRequest(
59 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
60 {
61 HILOGD("BluetoothGattClientCallbackStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d",
62 code,
63 option.GetFlags());
64 std::u16string descriptor = BluetoothGattClientCallbackStub::GetDescriptor();
65 std::u16string remoteDescriptor = data.ReadInterfaceToken();
66 if (descriptor != remoteDescriptor) {
67 HILOGI("local descriptor is not equal to remote");
68 return ERR_INVALID_STATE;
69 }
70 auto itFunc = memberFuncMap_.find(code);
71 if (itFunc != memberFuncMap_.end()) {
72 auto memberFunc = itFunc->second;
73 if (memberFunc != nullptr) {
74 return (this->*memberFunc)(data, reply);
75 }
76 }
77 HILOGW("BluetoothGattClientCallbackStub::OnRemoteRequest, default case, need check.");
78 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
79 }
80
OnConnectionStateChangedInner(MessageParcel & data,MessageParcel & reply)81 ErrCode BluetoothGattClientCallbackStub::OnConnectionStateChangedInner(MessageParcel &data, MessageParcel &reply)
82 {
83 HILOGI("BluetoothGattClientCallbackStub::OnConnectionStateChangedInner Triggered!");
84 int32_t state = data.ReadInt32();
85 int32_t newState = data.ReadInt32();
86 OnConnectionStateChanged(state, newState);
87 return NO_ERROR;
88 }
89
OnCharacteristicChangedInner(MessageParcel & data,MessageParcel & reply)90 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicChangedInner(MessageParcel &data, MessageParcel &reply)
91 {
92 HILOGI("BluetoothGattClientCallbackStub::OnCharacteristicChangedInner Triggered!");
93 const BluetoothGattCharacteristic *characteristic = data.ReadParcelable<BluetoothGattCharacteristic>();
94 OnCharacteristicChanged(*characteristic);
95 return NO_ERROR;
96 }
97
OnCharacteristicReadInner(MessageParcel & data,MessageParcel & reply)98 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicReadInner(MessageParcel &data, MessageParcel &reply)
99 {
100 HILOGI("BluetoothGattClientCallbackStub::OnCharacteristicReadInner Triggered!");
101 int32_t ret = data.ReadInt32();
102 const BluetoothGattCharacteristic *characteristic = data.ReadParcelable<BluetoothGattCharacteristic>();
103 OnCharacteristicRead(ret, *characteristic);
104 return NO_ERROR;
105 }
106
OnCharacteristicWriteInner(MessageParcel & data,MessageParcel & reply)107 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicWriteInner(MessageParcel &data, MessageParcel &reply)
108 {
109 HILOGI("BluetoothGattClientCallbackStub::OnCharacteristicWriteInner Triggered!");
110 int32_t ret = data.ReadInt32();
111 const BluetoothGattCharacteristic *characteristic = data.ReadParcelable<BluetoothGattCharacteristic>();
112 OnCharacteristicWrite(ret, *characteristic);
113 return NO_ERROR;
114 }
115
OnDescriptorReadInner(MessageParcel & data,MessageParcel & reply)116 ErrCode BluetoothGattClientCallbackStub::OnDescriptorReadInner(MessageParcel &data, MessageParcel &reply)
117 {
118 HILOGI("BluetoothGattClientCallbackStub::OnDescriptorReadInner Triggered!");
119 int32_t ret = data.ReadInt32();
120 const BluetoothGattDescriptor *descriptor = data.ReadParcelable<BluetoothGattDescriptor>();
121 OnDescriptorRead(ret, *descriptor);
122 return NO_ERROR;
123 }
124
OnDescriptorWriteInner(MessageParcel & data,MessageParcel & reply)125 ErrCode BluetoothGattClientCallbackStub::OnDescriptorWriteInner(MessageParcel &data, MessageParcel &reply)
126 {
127 HILOGI("BluetoothGattClientCallbackStub::OnDescriptorWriteInner Triggered!");
128 int32_t ret = data.ReadInt32();
129 const BluetoothGattDescriptor *descriptor = data.ReadParcelable<BluetoothGattDescriptor>();
130 OnDescriptorWrite(ret, *descriptor);
131 return NO_ERROR;
132 }
133
OnMtuChangedInner(MessageParcel & data,MessageParcel & reply)134 ErrCode BluetoothGattClientCallbackStub::OnMtuChangedInner(MessageParcel &data, MessageParcel &reply)
135 {
136 HILOGI("BluetoothGattClientCallbackStub::OnMtuChangedInner Triggered!");
137 int32_t state = data.ReadInt32();
138 int32_t mtu = data.ReadInt32();
139 OnMtuChanged(state, mtu);
140 return NO_ERROR;
141 }
142
OnServicesDiscoveredInner(MessageParcel & data,MessageParcel & reply)143 ErrCode BluetoothGattClientCallbackStub::OnServicesDiscoveredInner(MessageParcel &data, MessageParcel &reply)
144 {
145 HILOGI("BluetoothGattClientCallbackStub::OnServicesDiscoveredInner Triggered!");
146 int32_t status = data.ReadInt32();
147 OnServicesDiscovered(status);
148 return NO_ERROR;
149 }
150
OnConnectionParameterChangedInner(MessageParcel & data,MessageParcel & reply)151 ErrCode BluetoothGattClientCallbackStub::OnConnectionParameterChangedInner(MessageParcel &data, MessageParcel &reply)
152 {
153 HILOGI("BluetoothGattClientCallbackStub::OnConnectionParameterChangedInner Triggered!");
154 int32_t interval = data.ReadInt32();
155 int32_t latency = data.ReadInt32();
156 int32_t timeout = data.ReadInt32();
157 int32_t status = data.ReadInt32();
158 OnConnectionParameterChanged(interval, latency, timeout, status);
159 return NO_ERROR;
160 }
161
OnServicesChangedInner(MessageParcel & data,MessageParcel & reply)162 ErrCode BluetoothGattClientCallbackStub::OnServicesChangedInner(MessageParcel &data, MessageParcel &reply)
163 {
164 HILOGI("BluetoothGattClientCallbackStub::OnServicesChangedInner Triggered!");
165 int32_t num = data.ReadInt32();
166 std::vector<BluetoothGattService> service;
167 for (int i = num; i > 0; i--) {
168 std::unique_ptr<BluetoothGattService> dev(data.ReadParcelable<BluetoothGattService>());
169 service.push_back(*dev);
170 }
171 OnServicesChanged(service);
172 return NO_ERROR;
173 }
174 } // namespace Bluetooth
175 } // namespace OHOS