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