1 /*
2 * Copyright (C) 2021-2022 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_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "string_ex.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
BluetoothGattServerStub()23 BluetoothGattServerStub::BluetoothGattServerStub()
24 {
25 HILOGD("%{public}s start.", __func__);
26 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_ADD_SERVICE)] =
27 &BluetoothGattServerStub::AddServiceInner;
28 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_CLEAR_SERVICES)] =
29 &BluetoothGattServerStub::ClearServicesInner;
30 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_CANCEL_CONNECTION)] =
31 &BluetoothGattServerStub::CancelConnectionInner;
32 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_REGISTER)] =
33 &BluetoothGattServerStub::RegisterApplicationInner;
34 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_DEREGISTER)] =
35 &BluetoothGattServerStub::DeregisterApplicationInner;
36 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_NOTIFY_CLIENT)] =
37 &BluetoothGattServerStub::NotifyClientInner;
38 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_REMOVE_SERVICE)] =
39 &BluetoothGattServerStub::RemoveServiceInner;
40 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_RESPOND_CHARACTERISTIC_READ)] =
41 &BluetoothGattServerStub::RespondCharacteristicReadInner;
42 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_RESPOND_CHARACTERISTIC_WRITE)] =
43 &BluetoothGattServerStub::RespondCharacteristicWriteInner;
44 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_RESPOND_DESCRIPTOR_READ)] =
45 &BluetoothGattServerStub::RespondDescriptorReadInner;
46 memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerStub::Code::GATT_SERVER_RESPOND_DESCRIPTOR_WRITE)] =
47 &BluetoothGattServerStub::RespondDescriptorWriteInner;
48 }
49
~BluetoothGattServerStub()50 BluetoothGattServerStub::~BluetoothGattServerStub()
51 {
52 HILOGD("%{public}s start.", __func__);
53 memberFuncMap_.clear();
54 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)55 int BluetoothGattServerStub::OnRemoteRequest(
56 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
57 {
58 HILOGD("BluetoothGattServerStub::OnRemoteRequest, cmd = %{public}u, flags= %{public}d", code, option.GetFlags());
59 std::u16string descriptor = BluetoothGattServerStub::GetDescriptor();
60 std::u16string remoteDescriptor = data.ReadInterfaceToken();
61 if (descriptor != remoteDescriptor) {
62 HILOGI("local descriptor is not equal to remote");
63 return ERR_INVALID_STATE;
64 }
65
66 auto itFunc = memberFuncMap_.find(code);
67 if (itFunc != memberFuncMap_.end()) {
68 auto memberFunc = itFunc->second;
69 if (memberFunc != nullptr) {
70 return (this->*memberFunc)(data, reply);
71 }
72 }
73 HILOGW("BluetoothGattServerStub::OnRemoteRequest, default case, need check.");
74 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
75 }
AddServiceInner(MessageParcel & data,MessageParcel & reply)76 ErrCode BluetoothGattServerStub::AddServiceInner(MessageParcel &data, MessageParcel &reply)
77 {
78 int32_t appID = data.ReadInt32();
79 std::shared_ptr<BluetoothGattService> service(data.ReadParcelable<BluetoothGattService>());
80 if (!service) {
81 return TRANSACTION_ERR;
82 }
83 int result = AddService(appID, service.get());
84 bool ret = reply.WriteInt32(result);
85 if (!ret) {
86 HILOGE("BluetoothGattServerStub: reply writing failed in: %{public}s.", __func__);
87 return ERR_INVALID_VALUE;
88 }
89 return NO_ERROR;
90 }
ClearServicesInner(MessageParcel & data,MessageParcel & reply)91 ErrCode BluetoothGattServerStub::ClearServicesInner(MessageParcel &data, MessageParcel &reply)
92 {
93 int appId = data.ReadInt32();
94 ClearServices(appId);
95 return NO_ERROR;
96 }
97
CancelConnectionInner(MessageParcel & data,MessageParcel & reply)98 ErrCode BluetoothGattServerStub::CancelConnectionInner(MessageParcel &data, MessageParcel &reply)
99 {
100 std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
101 if (!device) {
102 return TRANSACTION_ERR;
103 }
104 CancelConnection(*device);
105
106 return NO_ERROR;
107 }
RegisterApplicationInner(MessageParcel & data,MessageParcel & reply)108 ErrCode BluetoothGattServerStub::RegisterApplicationInner(MessageParcel &data, MessageParcel &reply)
109 {
110 sptr<IRemoteObject> remote = data.ReadRemoteObject();
111 const sptr<IBluetoothGattServerCallback> callback = OHOS::iface_cast<IBluetoothGattServerCallback>(remote);
112 int result = RegisterApplication(callback);
113 bool ret = reply.WriteInt32(result);
114 if (!ret) {
115 HILOGE("BluetoothGattServerStub: reply writing failed in: %{public}s.", __func__);
116 return ERR_INVALID_VALUE;
117 }
118 return NO_ERROR;
119 }
DeregisterApplicationInner(MessageParcel & data,MessageParcel & reply)120 ErrCode BluetoothGattServerStub::DeregisterApplicationInner(MessageParcel &data, MessageParcel &reply)
121 {
122 int appId = data.ReadInt32();
123 int result = DeregisterApplication(appId);
124 bool ret = reply.WriteInt32(result);
125 if (!ret) {
126 HILOGE("BluetoothGattServerStub: reply writing failed in: %{public}s.", __func__);
127 return ERR_INVALID_VALUE;
128 }
129 return NO_ERROR;
130 }
NotifyClientInner(MessageParcel & data,MessageParcel & reply)131 ErrCode BluetoothGattServerStub::NotifyClientInner(MessageParcel &data, MessageParcel &reply)
132 {
133 std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
134 if (!device) {
135 return TRANSACTION_ERR;
136 }
137 std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
138 if (!characteristic) {
139 return TRANSACTION_ERR;
140 }
141 bool needConfirm = data.ReadBool();
142 int result = NotifyClient(*device, characteristic.get(), needConfirm);
143 bool ret = reply.WriteInt32(result);
144 if (!ret) {
145 HILOGE("BluetoothGattServerStub: reply writing failed in: %{public}s.", __func__);
146 return ERR_INVALID_VALUE;
147 }
148 return NO_ERROR;
149 }
RemoveServiceInner(MessageParcel & data,MessageParcel & reply)150 ErrCode BluetoothGattServerStub::RemoveServiceInner(MessageParcel &data, MessageParcel &reply)
151 {
152 int appId = data.ReadInt32();
153 std::shared_ptr<BluetoothGattService> service(data.ReadParcelable<BluetoothGattService>());
154 if (!service) {
155 return TRANSACTION_ERR;
156 }
157 int result = RemoveService(appId, *service);
158 bool ret = reply.WriteInt32(result);
159 if (!ret) {
160 HILOGE("BluetoothGattServerStub: reply writing failed in: %{public}s.", __func__);
161 return ERR_INVALID_VALUE;
162 }
163 return NO_ERROR;
164 }
RespondCharacteristicReadInner(MessageParcel & data,MessageParcel & reply)165 ErrCode BluetoothGattServerStub::RespondCharacteristicReadInner(MessageParcel &data, MessageParcel &reply)
166 {
167 std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
168 if (!device) {
169 return TRANSACTION_ERR;
170 }
171 std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
172 if (!characteristic) {
173 return TRANSACTION_ERR;
174 }
175 int ret1 = data.ReadInt32();
176 int result = RespondCharacteristicRead(*device, characteristic.get(), ret1);
177 bool ret2 = reply.WriteInt32(result);
178 if (!ret2) {
179 HILOGE("BluetoothGattServerStub: reply writing failed in: %{public}s.", __func__);
180 return ERR_INVALID_VALUE;
181 }
182 return NO_ERROR;
183 }
RespondCharacteristicWriteInner(MessageParcel & data,MessageParcel & reply)184 ErrCode BluetoothGattServerStub::RespondCharacteristicWriteInner(MessageParcel &data, MessageParcel &reply)
185 {
186 std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
187 if (!device) {
188 return TRANSACTION_ERR;
189 }
190 std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
191 if (!characteristic) {
192 return TRANSACTION_ERR;
193 }
194 int ret1 = data.ReadInt32();
195 int result = RespondCharacteristicWrite(*device, *characteristic, ret1);
196 bool ret2 = reply.WriteInt32(result);
197 if (!ret2) {
198 HILOGE("BluetoothGattServerStub: reply writing failed in: %{public}s.", __func__);
199 return ERR_INVALID_VALUE;
200 }
201 return NO_ERROR;
202 }
RespondDescriptorReadInner(MessageParcel & data,MessageParcel & reply)203 ErrCode BluetoothGattServerStub::RespondDescriptorReadInner(MessageParcel &data, MessageParcel &reply)
204 {
205 std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
206 if (!device) {
207 return TRANSACTION_ERR;
208 }
209 std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
210 if (!descriptor) {
211 return TRANSACTION_ERR;
212 }
213 int ret1 = data.ReadInt32();
214 int result = RespondDescriptorRead(*device, descriptor.get(), ret1);
215 bool ret2 = reply.WriteInt32(result);
216 if (!ret2) {
217 HILOGE("BluetoothGattServerStub: reply writing failed in: %{public}s.", __func__);
218 return ERR_INVALID_VALUE;
219 }
220 return NO_ERROR;
221 }
RespondDescriptorWriteInner(MessageParcel & data,MessageParcel & reply)222 ErrCode BluetoothGattServerStub::RespondDescriptorWriteInner(MessageParcel &data, MessageParcel &reply)
223 {
224 std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
225 if (!device) {
226 return TRANSACTION_ERR;
227 }
228 std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
229 if (!descriptor) {
230 return TRANSACTION_ERR;
231 }
232 int ret1 = data.ReadInt32();
233 int result = RespondDescriptorWrite(*device, *descriptor, ret1);
234 bool ret2 = reply.WriteInt32(result);
235 if (!ret2) {
236 HILOGE("BluetoothGattServerStub: reply writing failed in: %{public}s.", __func__);
237 return ERR_INVALID_VALUE;
238 }
239 return NO_ERROR;
240 }
241 } // namespace Bluetooth
242 } // namespace OHOS