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_server_callback_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "string_ex.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
BluetoothGattServerCallbackStub()23 BluetoothGattServerCallbackStub::BluetoothGattServerCallbackStub()
24 {
25 HILOGD("%{public}s start.", __func__);
26 memberFuncMap_[static_cast<uint32_t>(
27 BluetoothGattServerCallbackStub::Code::GATT_SERVER_CALLBACK_CHARACTERISTIC_READREQUEST)] =
28 &BluetoothGattServerCallbackStub::OnCharacteristicReadRequestInner;
29 memberFuncMap_[static_cast<uint32_t>(
30 BluetoothGattServerCallbackStub::Code::GATT_SERVER_CALLBACK_CONNECTIONSTATE_CHANGED)] =
31 &BluetoothGattServerCallbackStub::OnConnectionStateChangedInner;
32 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerCallbackStub::Code::GATT_SERVER_CALLBACK_ADD_SERVICE)] =
33 &BluetoothGattServerCallbackStub::OnAddServiceInner;
34 memberFuncMap_[static_cast<uint32_t>(
35 BluetoothGattServerCallbackStub::Code::GATT_SERVER_CALLBACK_CHARACTERISTIC_WRITE_REQUEST)] =
36 &BluetoothGattServerCallbackStub::OnCharacteristicWriteRequestInner;
37 memberFuncMap_[static_cast<uint32_t>(
38 BluetoothGattServerCallbackStub::Code::GATT_SERVER_CALLBACK_DESCRIPTOR_READ_REQUEST)] =
39 &BluetoothGattServerCallbackStub::OnDescriptorReadRequestInner;
40 memberFuncMap_[static_cast<uint32_t>(
41 BluetoothGattServerCallbackStub::Code::GATT_SERVER_CALLBACK_DESCRIPTOR_WRITE_REQUEST)] =
42 &BluetoothGattServerCallbackStub::OnDescriptorWriteRequestInner;
43 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerCallbackStub::Code::GATT_SERVER_CALLBACK_MTU_CHANGED)] =
44 &BluetoothGattServerCallbackStub::OnMtuChangedInner;
45 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerCallbackStub::Code::GATT_SERVER_CALLBACK_NOTIFY_CONFIRM)] =
46 &BluetoothGattServerCallbackStub::OnNotifyConfirmInner;
47 memberFuncMap_[static_cast<uint32_t>(
48 BluetoothGattServerCallbackStub::Code::GATT_SERVER_CALLBACK_CONNECTION_PARAMETER_CHANGED)] =
49 &BluetoothGattServerCallbackStub::OnConnectionParameterChangedInner;
50 HILOGD("%{public}s ends.", __func__);
51 }
52
~BluetoothGattServerCallbackStub()53 BluetoothGattServerCallbackStub::~BluetoothGattServerCallbackStub()
54 {
55 HILOGD("%{public}s start.", __func__);
56 memberFuncMap_.clear();
57 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)58 int BluetoothGattServerCallbackStub::OnRemoteRequest(
59 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
60 {
61 HILOGD("BluetoothGattServerCallbackStub::OnRemoteRequest, cmd = %{public}u, flags= %{public}d",
62 code, option.GetFlags());
63 std::u16string descriptor = BluetoothGattServerCallbackStub::GetDescriptor();
64 std::u16string remoteDescriptor = data.ReadInterfaceToken();
65 if (descriptor != remoteDescriptor) {
66 HILOGI("local descriptor is not equal to remote");
67 return ERR_INVALID_STATE;
68 }
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("BluetoothGattServerCallbackStub::OnRemoteRequest, default case, need check.");
78 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
79 };
OnCharacteristicReadRequestInner(MessageParcel & data,MessageParcel & reply)80 ErrCode BluetoothGattServerCallbackStub::OnCharacteristicReadRequestInner(MessageParcel &data, MessageParcel &reply)
81 {
82 const BluetoothGattDevice *device = data.ReadParcelable<BluetoothGattDevice>();
83 const BluetoothGattCharacteristic *characteristic = data.ReadParcelable<BluetoothGattCharacteristic>();
84
85 OnCharacteristicReadRequest(*device, *characteristic);
86
87 return NO_ERROR;
88 }
OnConnectionStateChangedInner(MessageParcel & data,MessageParcel & reply)89 ErrCode BluetoothGattServerCallbackStub::OnConnectionStateChangedInner(MessageParcel &data, MessageParcel &reply)
90 {
91 const BluetoothGattDevice *device = data.ReadParcelable<BluetoothGattDevice>();
92 int32_t ret = data.ReadInt32();
93 int32_t state = data.ReadInt32();
94 OnConnectionStateChanged(*device, ret, state);
95
96 return NO_ERROR;
97 }
OnAddServiceInner(MessageParcel & data,MessageParcel & reply)98 ErrCode BluetoothGattServerCallbackStub::OnAddServiceInner(MessageParcel &data, MessageParcel &reply)
99 {
100 HILOGI("BluetoothGattServerCallbackStub::OnAddServiceInner Triggered!");
101 int32_t ret = data.ReadInt32();
102 const BluetoothGattService *service = data.ReadParcelable<BluetoothGattService>();
103
104 OnAddService(ret, *service);
105
106 return NO_ERROR;
107 }
OnCharacteristicWriteRequestInner(MessageParcel & data,MessageParcel & reply)108 ErrCode BluetoothGattServerCallbackStub::OnCharacteristicWriteRequestInner(MessageParcel &data, MessageParcel &reply)
109 {
110 const BluetoothGattDevice *device = data.ReadParcelable<BluetoothGattDevice>();
111 const BluetoothGattCharacteristic *characteristic = data.ReadParcelable<BluetoothGattCharacteristic>();
112 bool needRespones = data.ReadBool();
113
114 OnCharacteristicWriteRequest(*device, *characteristic, needRespones);
115
116 return NO_ERROR;
117 }
OnDescriptorReadRequestInner(MessageParcel & data,MessageParcel & reply)118 ErrCode BluetoothGattServerCallbackStub::OnDescriptorReadRequestInner(MessageParcel &data, MessageParcel &reply)
119 {
120 const BluetoothGattDevice *device = data.ReadParcelable<BluetoothGattDevice>();
121 const BluetoothGattDescriptor *descriptor = data.ReadParcelable<BluetoothGattDescriptor>();
122
123 OnDescriptorReadRequest(*device, *descriptor);
124
125 return NO_ERROR;
126 }
OnDescriptorWriteRequestInner(MessageParcel & data,MessageParcel & reply)127 ErrCode BluetoothGattServerCallbackStub::OnDescriptorWriteRequestInner(MessageParcel &data, MessageParcel &reply)
128 {
129 const BluetoothGattDevice *device = data.ReadParcelable<BluetoothGattDevice>();
130 const BluetoothGattDescriptor *descriptor = data.ReadParcelable<BluetoothGattDescriptor>();
131
132 OnDescriptorWriteRequest(*device, *descriptor);
133
134 return NO_ERROR;
135 }
OnMtuChangedInner(MessageParcel & data,MessageParcel & reply)136 ErrCode BluetoothGattServerCallbackStub::OnMtuChangedInner(MessageParcel &data, MessageParcel &reply)
137 {
138 const BluetoothGattDevice *device = data.ReadParcelable<BluetoothGattDevice>();
139 int32_t mtu = data.ReadInt32();
140
141 OnMtuChanged(*device, mtu);
142
143 return NO_ERROR;
144 }
OnNotifyConfirmInner(MessageParcel & data,MessageParcel & reply)145 ErrCode BluetoothGattServerCallbackStub::OnNotifyConfirmInner(MessageParcel &data, MessageParcel &reply)
146 {
147 const BluetoothGattDevice *device = data.ReadParcelable<BluetoothGattDevice>();
148 const BluetoothGattCharacteristic *characteristic = data.ReadParcelable<BluetoothGattCharacteristic>();
149 int32_t result = data.ReadInt32();
150
151 OnNotifyConfirm(*device, *characteristic, result);
152
153 return NO_ERROR;
154 }
OnConnectionParameterChangedInner(MessageParcel & data,MessageParcel & reply)155 ErrCode BluetoothGattServerCallbackStub::OnConnectionParameterChangedInner(MessageParcel &data, MessageParcel &reply)
156 {
157 const BluetoothGattDevice *device = data.ReadParcelable<BluetoothGattDevice>();
158 int32_t interval = data.ReadInt32();
159 int32_t latency = data.ReadInt32();
160 int32_t timeout = data.ReadInt32();
161 int32_t status = data.ReadInt32();
162
163 OnConnectionParameterChanged(*device, interval, latency, timeout, status);
164
165 return NO_ERROR;
166 }
167 } // namespace Bluetooth
168 } // namespace OHOS