1//
2//  Copyright (C) 2015 Google, Inc.
3//
4//  Licensed under the Apache License, Version 2.0 (the "License");
5//  you may not use this file except in compliance with the License.
6//  You may obtain a copy of the License at:
7//
8//  http://www.apache.org/licenses/LICENSE-2.0
9//
10//  Unless required by applicable law or agreed to in writing, software
11//  distributed under the License is distributed on an "AS IS" BASIS,
12//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//  See the License for the specific language governing permissions and
14//  limitations under the License.
15//
16
17#include "service/ipc/binder/bluetooth_gatt_server_binder_server.h"
18
19#include <base/logging.h>
20
21#include "service/adapter.h"
22
23using ::android::String8;
24using ::android::String16;
25using ::android::binder::Status;
26
27using ::android::bluetooth::IBluetoothGattServerCallback;
28
29namespace ipc {
30namespace binder {
31
32namespace {
33const int kInvalidInstanceId = -1;
34}  // namespace
35
36BluetoothGattServerBinderServer::BluetoothGattServerBinderServer(
37    bluetooth::Adapter* adapter)
38    : adapter_(adapter) {
39  CHECK(adapter_);
40}
41
42Status BluetoothGattServerBinderServer::RegisterServer(
43    const ::android::sp<IBluetoothGattServerCallback>& callback,
44    bool* _aidl_return) {
45  VLOG(2) << __func__;
46  bluetooth::GattServerFactory* gatt_server_factory =
47      adapter_->GetGattServerFactory();
48
49  *_aidl_return = RegisterInstanceBase(callback, gatt_server_factory);
50  return Status::ok();
51}
52
53Status BluetoothGattServerBinderServer::UnregisterServer(int server_id) {
54  VLOG(2) << __func__;
55  UnregisterInstanceBase(server_id);
56  return Status::ok();
57}
58
59Status BluetoothGattServerBinderServer::UnregisterAll() {
60  VLOG(2) << __func__;
61  UnregisterAllBase();
62  return Status::ok();
63}
64
65Status BluetoothGattServerBinderServer::AddService(
66    int server_id, const android::bluetooth::BluetoothGattService& service,
67    bool* _aidl_return) {
68  VLOG(2) << __func__;
69  std::lock_guard<std::mutex> lock(*maps_lock());
70
71  auto gatt_server = GetGattServer(server_id);
72  if (!gatt_server) {
73    LOG(ERROR) << "Unknown server_id: " << server_id;
74    *_aidl_return = false;
75    return Status::ok();
76  }
77
78  // Create a weak pointer and pass that to the callback to prevent a potential
79  // use after free.
80  android::wp<BluetoothGattServerBinderServer> weak_ptr_to_this(this);
81  auto callback = [=](bluetooth::BLEStatus status,
82                      const bluetooth::Service& service) {
83    auto sp_to_this = weak_ptr_to_this.promote();
84    if (!sp_to_this.get()) {
85      VLOG(2) << "BluetoothLowEnergyBinderServer was deleted";
86      return;
87    }
88
89    std::lock_guard<std::mutex> lock(*maps_lock());
90
91    auto gatt_cb = GetGattServerCallback(server_id);
92    if (!gatt_cb.get()) {
93      VLOG(2) << "The callback was deleted";
94      return;
95    }
96
97    gatt_cb->OnServiceAdded(status, service);
98  };
99
100  if (!gatt_server->AddService(service, callback)) {
101    LOG(ERROR) << "Failed to add service";
102    *_aidl_return = false;
103    return Status::ok();
104  }
105
106  *_aidl_return = true;
107  return Status::ok();
108}
109
110Status BluetoothGattServerBinderServer::SendResponse(
111    int server_id, const String16& device_address, int request_id, int status,
112    int offset, const std::vector<uint8_t>& value, bool* _aidl_return) {
113  VLOG(2) << __func__;
114  std::lock_guard<std::mutex> lock(*maps_lock());
115
116  auto gatt_server = GetGattServer(server_id);
117  if (!gatt_server) {
118    LOG(ERROR) << "Unknown server_id: " << server_id;
119    *_aidl_return = false;
120    return Status::ok();
121  }
122
123  *_aidl_return = gatt_server->SendResponse(
124      std::string(String8(device_address).string()), request_id,
125      static_cast<bluetooth::GATTError>(status), offset, value);
126
127  return Status::ok();
128}
129
130Status BluetoothGattServerBinderServer::SendNotification(
131    int server_id, const String16& device_address, int handle, bool confirm,
132    const std::vector<uint8_t>& value, bool* _aidl_return) {
133  VLOG(2) << __func__;
134  std::lock_guard<std::mutex> lock(*maps_lock());
135
136  auto gatt_server = GetGattServer(server_id);
137  if (!gatt_server) {
138    LOG(ERROR) << "Unknown server_id: " << server_id;
139    *_aidl_return = false;
140    return Status::ok();
141  }
142
143  // Create a weak pointer and pass that to the callback to prevent a potential
144  // use after free.
145  android::wp<BluetoothGattServerBinderServer> weak_ptr_to_this(this);
146  auto callback = [=](bluetooth::GATTError error) {
147    auto sp_to_this = weak_ptr_to_this.promote();
148    if (!sp_to_this.get()) {
149      VLOG(2) << "BluetoothLowEnergyBinderServer was deleted";
150      return;
151    }
152
153    std::lock_guard<std::mutex> lock(*maps_lock());
154
155    auto gatt_cb = GetGattServerCallback(server_id);
156    if (!gatt_cb.get()) {
157      VLOG(2) << "The callback was deleted";
158      return;
159    }
160
161    gatt_cb->OnNotificationSent(device_address, error);
162  };
163
164  if (!gatt_server->SendNotification(
165          std::string(String8(device_address).string()), handle, confirm, value,
166          callback)) {
167    LOG(ERROR) << "Failed to send notification";
168    *_aidl_return = false;
169    return Status::ok();
170  }
171
172  *_aidl_return = true;
173  return Status::ok();
174}
175
176void BluetoothGattServerBinderServer::OnCharacteristicReadRequest(
177    bluetooth::GattServer* gatt_server, const std::string& device_address,
178    int request_id, int offset, bool is_long, uint16_t handle) {
179  VLOG(2) << __func__;
180  std::lock_guard<std::mutex> lock(*maps_lock());
181
182  auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
183  if (!gatt_cb.get()) {
184    LOG(WARNING) << "Callback for this GattServer was deleted.";
185    return;
186  }
187
188  gatt_cb->OnCharacteristicReadRequest(
189      String16(device_address.c_str(), device_address.length()), request_id,
190      offset, is_long, handle);
191}
192
193void BluetoothGattServerBinderServer::OnDescriptorReadRequest(
194    bluetooth::GattServer* gatt_server, const std::string& device_address,
195    int request_id, int offset, bool is_long, uint16_t handle) {
196  VLOG(2) << __func__;
197  std::lock_guard<std::mutex> lock(*maps_lock());
198
199  auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
200  if (!gatt_cb.get()) {
201    LOG(WARNING) << "Callback for this GattServer was deleted.";
202    return;
203  }
204
205  gatt_cb->OnDescriptorReadRequest(
206      String16(device_address.c_str(), device_address.length()), request_id,
207      offset, is_long, handle);
208}
209
210android::sp<IBluetoothGattServerCallback>
211BluetoothGattServerBinderServer::GetGattServerCallback(int server_id) {
212  auto cb = GetCallback(server_id);
213  return android::sp<IBluetoothGattServerCallback>(
214      static_cast<IBluetoothGattServerCallback*>(cb.get()));
215}
216
217std::shared_ptr<bluetooth::GattServer>
218BluetoothGattServerBinderServer::GetGattServer(int server_id) {
219  return std::static_pointer_cast<bluetooth::GattServer>(
220      GetInstance(server_id));
221}
222
223void BluetoothGattServerBinderServer::OnRegisterInstanceImpl(
224    bluetooth::BLEStatus status, android::sp<IInterface> callback,
225    bluetooth::BluetoothInstance* instance) {
226  VLOG(1) << __func__ << " instance ID: " << instance->GetInstanceId()
227          << " status: " << status;
228  bluetooth::GattServer* gatt_server =
229      static_cast<bluetooth::GattServer*>(instance);
230  gatt_server->SetDelegate(this);
231
232  android::sp<IBluetoothGattServerCallback> cb(
233      static_cast<IBluetoothGattServerCallback*>(callback.get()));
234  cb->OnServerRegistered(status, (status == bluetooth::BLE_STATUS_SUCCESS)
235                                     ? instance->GetInstanceId()
236                                     : kInvalidInstanceId);
237}
238
239void BluetoothGattServerBinderServer::OnCharacteristicWriteRequest(
240    bluetooth::GattServer* gatt_server, const std::string& device_address,
241    int request_id, int offset, bool is_prepare_write, bool need_response,
242    const std::vector<uint8_t>& value, uint16_t handle) {
243  VLOG(2) << __func__;
244  std::lock_guard<std::mutex> lock(*maps_lock());
245
246  auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
247  if (!gatt_cb.get()) {
248    LOG(WARNING) << "Callback for this GattServer was deleted.";
249    return;
250  }
251
252  gatt_cb->OnCharacteristicWriteRequest(
253      String16(device_address.c_str(), device_address.length()), request_id,
254      offset, is_prepare_write, need_response, value, handle);
255}
256
257void BluetoothGattServerBinderServer::OnDescriptorWriteRequest(
258    bluetooth::GattServer* gatt_server, const std::string& device_address,
259    int request_id, int offset, bool is_prepare_write, bool need_response,
260    const std::vector<uint8_t>& value, uint16_t handle) {
261  VLOG(2) << __func__;
262  std::lock_guard<std::mutex> lock(*maps_lock());
263
264  auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
265  if (!gatt_cb.get()) {
266    LOG(WARNING) << "Callback for this GattServer was deleted.";
267    return;
268  }
269
270  gatt_cb->OnDescriptorWriteRequest(
271      String16(device_address.c_str(), device_address.length()), request_id,
272      offset, is_prepare_write, need_response, value, handle);
273}
274
275void BluetoothGattServerBinderServer::OnExecuteWriteRequest(
276    bluetooth::GattServer* gatt_server, const std::string& device_address,
277    int request_id, bool is_execute) {
278  VLOG(2) << __func__;
279  std::lock_guard<std::mutex> lock(*maps_lock());
280
281  auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
282  if (!gatt_cb.get()) {
283    LOG(WARNING) << "Callback for this GattServer was deleted.";
284    return;
285  }
286
287  gatt_cb->OnExecuteWriteRequest(
288      String16(device_address.c_str(), device_address.length()), request_id,
289      is_execute);
290}
291
292void BluetoothGattServerBinderServer::OnConnectionStateChanged(
293    bluetooth::GattServer* gatt_server, const std::string& device_address,
294    bool connected) {
295  VLOG(2) << __func__;
296  std::lock_guard<std::mutex> lock(*maps_lock());
297
298  auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
299  if (!gatt_cb.get()) {
300    LOG(WARNING) << "Callback for this GattServer was deleted.";
301    return;
302  }
303
304  gatt_cb->OnConnectionStateChanged(
305      String16(device_address.c_str(), device_address.length()), connected);
306}
307
308}  // namespace binder
309}  // namespace ipc
310