Home | History | Annotate | Download | only in binder
      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 
     23 namespace ipc {
     24 namespace binder {
     25 
     26 namespace {
     27 const int kInvalidInstanceId = -1;
     28 }  // namespace
     29 
     30 BluetoothGattServerBinderServer::BluetoothGattServerBinderServer(
     31     bluetooth::Adapter* adapter) : adapter_(adapter) {
     32   CHECK(adapter_);
     33 }
     34 
     35 bool BluetoothGattServerBinderServer::RegisterServer(
     36     const android::sp<IBluetoothGattServerCallback>& callback) {
     37   VLOG(2) << __func__;
     38   bluetooth::GattServerFactory* gatt_server_factory =
     39       adapter_->GetGattServerFactory();
     40 
     41   return RegisterInstanceBase(callback, gatt_server_factory);
     42 }
     43 
     44 void BluetoothGattServerBinderServer::UnregisterServer(int server_id) {
     45   VLOG(2) << __func__;
     46   UnregisterInstanceBase(server_id);
     47 }
     48 
     49 void BluetoothGattServerBinderServer::UnregisterAll() {
     50   VLOG(2) << __func__;
     51   UnregisterAllBase();
     52 }
     53 
     54 bool BluetoothGattServerBinderServer::BeginServiceDeclaration(
     55     int server_id, bool is_primary, const bluetooth::UUID& uuid,
     56     std::unique_ptr<bluetooth::GattIdentifier>* out_id) {
     57   VLOG(2) << __func__;
     58   CHECK(out_id);
     59   std::lock_guard<std::mutex> lock(*maps_lock());
     60 
     61   auto gatt_server = GetGattServer(server_id);
     62   if (!gatt_server) {
     63     LOG(ERROR) << "Unknown server_id: " << server_id;
     64     return false;
     65   }
     66 
     67   auto service_id = gatt_server->BeginServiceDeclaration(uuid, is_primary);
     68   if (!service_id) {
     69     LOG(ERROR) << "Failed to begin service declaration - server_id: "
     70                << server_id << " UUID: " << uuid.ToString();
     71     return false;
     72   }
     73 
     74   out_id->swap(service_id);
     75 
     76   return true;
     77 }
     78 
     79 bool BluetoothGattServerBinderServer::AddCharacteristic(
     80     int server_id, const bluetooth::UUID& uuid,
     81     int properties, int permissions,
     82     std::unique_ptr<bluetooth::GattIdentifier>* out_id) {
     83   VLOG(2) << __func__;
     84   CHECK(out_id);
     85   std::lock_guard<std::mutex> lock(*maps_lock());
     86 
     87   auto gatt_server = GetGattServer(server_id);
     88   if (!gatt_server) {
     89     LOG(ERROR) << "Unknown server_id: " << server_id;
     90     return false;
     91   }
     92 
     93   auto char_id = gatt_server->AddCharacteristic(uuid, properties, permissions);
     94   if (!char_id) {
     95     LOG(ERROR) << "Failed to add characteristic - server_id: "
     96                << server_id << " UUID: " << uuid.ToString();
     97     return false;
     98   }
     99 
    100   out_id->swap(char_id);
    101 
    102   return true;
    103 }
    104 
    105 bool BluetoothGattServerBinderServer::AddDescriptor(
    106     int server_id, const bluetooth::UUID& uuid, int permissions,
    107     std::unique_ptr<bluetooth::GattIdentifier>* out_id) {
    108   VLOG(2) << __func__;
    109   CHECK(out_id);
    110   std::lock_guard<std::mutex> lock(*maps_lock());
    111 
    112   auto gatt_server = GetGattServer(server_id);
    113   if (!gatt_server) {
    114     LOG(ERROR) << "Unknown server_id: " << server_id;
    115     return false;
    116   }
    117 
    118   auto desc_id = gatt_server->AddDescriptor(uuid, permissions);
    119   if (!desc_id) {
    120     LOG(ERROR) << "Failed to add descriptor - server_id: "
    121                << server_id << " UUID: " << uuid.ToString();
    122     return false;
    123   }
    124 
    125   out_id->swap(desc_id);
    126 
    127   return true;
    128 }
    129 
    130 bool BluetoothGattServerBinderServer::EndServiceDeclaration(int server_id) {
    131   VLOG(2) << __func__;
    132   std::lock_guard<std::mutex> lock(*maps_lock());
    133 
    134   auto gatt_server = GetGattServer(server_id);
    135   if (!gatt_server) {
    136     LOG(ERROR) << "Unknown server_id: " << server_id;
    137     return false;
    138   }
    139 
    140   // Create a weak pointer and pass that to the callback to prevent a potential
    141   // use after free.
    142   android::wp<BluetoothGattServerBinderServer> weak_ptr_to_this(this);
    143   auto callback = [=](
    144       bluetooth::BLEStatus status,
    145       const bluetooth::GattIdentifier& service_id) {
    146     auto sp_to_this = weak_ptr_to_this.promote();
    147     if (!sp_to_this.get()) {
    148       VLOG(2) << "BluetoothLowEnergyBinderServer was deleted";
    149       return;
    150     }
    151 
    152     std::lock_guard<std::mutex> lock(*maps_lock());
    153 
    154     auto gatt_cb = GetGattServerCallback(server_id);
    155     if (!gatt_cb.get()) {
    156       VLOG(2) << "The callback was deleted";
    157       return;
    158     }
    159 
    160     gatt_cb->OnServiceAdded(status, service_id);
    161   };
    162 
    163   if (!gatt_server->EndServiceDeclaration(callback)) {
    164     LOG(ERROR) << "Failed to end service declaration";
    165     return false;
    166   }
    167 
    168   return true;
    169 }
    170 
    171 bool BluetoothGattServerBinderServer::SendResponse(
    172     int server_id, const std::string& device_address,
    173     int request_id, int status, int offset,
    174     const std::vector<uint8_t>& value) {
    175   VLOG(2) << __func__;
    176   std::lock_guard<std::mutex> lock(*maps_lock());
    177 
    178   auto gatt_server = GetGattServer(server_id);
    179   if (!gatt_server) {
    180     LOG(ERROR) << "Unknown server_id: " << server_id;
    181     return false;
    182   }
    183 
    184   return gatt_server->SendResponse(
    185       device_address, request_id, static_cast<bluetooth::GATTError>(status),
    186       offset, value);
    187 }
    188 
    189 bool BluetoothGattServerBinderServer::SendNotification(
    190     int server_id,
    191     const std::string& device_address,
    192     const bluetooth::GattIdentifier& characteristic_id,
    193     bool confirm,
    194     const std::vector<uint8_t>& value) {
    195   VLOG(2) << __func__;
    196   std::lock_guard<std::mutex> lock(*maps_lock());
    197 
    198   auto gatt_server = GetGattServer(server_id);
    199   if (!gatt_server) {
    200     LOG(ERROR) << "Unknown server_id: " << server_id;
    201     return false;
    202   }
    203 
    204   // Create a weak pointer and pass that to the callback to prevent a potential
    205   // use after free.
    206   android::wp<BluetoothGattServerBinderServer> weak_ptr_to_this(this);
    207   auto callback = [=](bluetooth::GATTError error) {
    208     auto sp_to_this = weak_ptr_to_this.promote();
    209     if (!sp_to_this.get()) {
    210       VLOG(2) << "BluetoothLowEnergyBinderServer was deleted";
    211       return;
    212     }
    213 
    214     std::lock_guard<std::mutex> lock(*maps_lock());
    215 
    216     auto gatt_cb = GetGattServerCallback(server_id);
    217     if (!gatt_cb.get()) {
    218       VLOG(2) << "The callback was deleted";
    219       return;
    220     }
    221 
    222     gatt_cb->OnNotificationSent(device_address, error);
    223   };
    224 
    225   if (!gatt_server->SendNotification(device_address, characteristic_id,
    226                                      confirm, value, callback)) {
    227     LOG(ERROR) << "Failed to send notification";
    228     return false;
    229   }
    230 
    231   return true;
    232 }
    233 
    234 void BluetoothGattServerBinderServer::OnCharacteristicReadRequest(
    235     bluetooth::GattServer* gatt_server,
    236     const std::string& device_address,
    237     int request_id, int offset, bool is_long,
    238     const bluetooth::GattIdentifier& characteristic_id) {
    239   VLOG(2) << __func__;
    240   std::lock_guard<std::mutex> lock(*maps_lock());
    241 
    242   auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
    243   if (!gatt_cb.get()) {
    244     LOG(WARNING) << "Callback for this GattServer was deleted.";
    245     return;
    246   }
    247 
    248   gatt_cb->OnCharacteristicReadRequest(
    249       device_address, request_id, offset, is_long, characteristic_id);
    250 }
    251 
    252 void BluetoothGattServerBinderServer::OnDescriptorReadRequest(
    253     bluetooth::GattServer* gatt_server,
    254     const std::string& device_address,
    255     int request_id, int offset, bool is_long,
    256     const bluetooth::GattIdentifier& descriptor_id) {
    257   VLOG(2) << __func__;
    258   std::lock_guard<std::mutex> lock(*maps_lock());
    259 
    260   auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
    261   if (!gatt_cb.get()) {
    262     LOG(WARNING) << "Callback for this GattServer was deleted.";
    263     return;
    264   }
    265 
    266   gatt_cb->OnDescriptorReadRequest(
    267       device_address, request_id, offset, is_long, descriptor_id);
    268 }
    269 
    270 android::sp<IBluetoothGattServerCallback>
    271 BluetoothGattServerBinderServer::GetGattServerCallback(int server_id) {
    272   auto cb = GetCallback(server_id);
    273   return android::sp<IBluetoothGattServerCallback>(
    274       static_cast<IBluetoothGattServerCallback*>(cb.get()));
    275 }
    276 
    277 std::shared_ptr<bluetooth::GattServer>
    278 BluetoothGattServerBinderServer::GetGattServer(int server_id) {
    279   return std::static_pointer_cast<bluetooth::GattServer>(
    280       GetInstance(server_id));
    281 }
    282 
    283 void BluetoothGattServerBinderServer::OnRegisterInstanceImpl(
    284     bluetooth::BLEStatus status,
    285     android::sp<IInterface> callback,
    286     bluetooth::BluetoothInstance* instance) {
    287   VLOG(1) << __func__ << " instance ID: " << instance->GetInstanceId()
    288           << " status: " << status;
    289   bluetooth::GattServer* gatt_server =
    290       static_cast<bluetooth::GattServer*>(instance);
    291   gatt_server->SetDelegate(this);
    292 
    293   android::sp<IBluetoothGattServerCallback> cb(
    294       static_cast<IBluetoothGattServerCallback*>(callback.get()));
    295   cb->OnServerRegistered(
    296       status,
    297       (status == bluetooth::BLE_STATUS_SUCCESS) ?
    298           instance->GetInstanceId() : kInvalidInstanceId);
    299 }
    300 
    301 void BluetoothGattServerBinderServer::OnCharacteristicWriteRequest(
    302     bluetooth::GattServer* gatt_server,
    303     const std::string& device_address,
    304     int request_id, int offset, bool is_prepare_write, bool need_response,
    305     const std::vector<uint8_t>& value,
    306     const bluetooth::GattIdentifier& characteristic_id) {
    307   VLOG(2) << __func__;
    308   std::lock_guard<std::mutex> lock(*maps_lock());
    309 
    310   auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
    311   if (!gatt_cb.get()) {
    312     LOG(WARNING) << "Callback for this GattServer was deleted.";
    313     return;
    314   }
    315 
    316   gatt_cb->OnCharacteristicWriteRequest(
    317       device_address, request_id, offset, is_prepare_write, need_response,
    318       value, characteristic_id);
    319 }
    320 
    321 void BluetoothGattServerBinderServer::OnDescriptorWriteRequest(
    322     bluetooth::GattServer* gatt_server,
    323     const std::string& device_address,
    324     int request_id, int offset, bool is_prepare_write, bool need_response,
    325     const std::vector<uint8_t>& value,
    326     const bluetooth::GattIdentifier& descriptor_id) {
    327   VLOG(2) << __func__;
    328   std::lock_guard<std::mutex> lock(*maps_lock());
    329 
    330   auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
    331   if (!gatt_cb.get()) {
    332     LOG(WARNING) << "Callback for this GattServer was deleted.";
    333     return;
    334   }
    335 
    336   gatt_cb->OnDescriptorWriteRequest(
    337       device_address, request_id, offset, is_prepare_write, need_response,
    338       value, descriptor_id);
    339 }
    340 
    341 void BluetoothGattServerBinderServer::OnExecuteWriteRequest(
    342     bluetooth::GattServer* gatt_server,
    343     const std::string& device_address,
    344     int request_id, bool is_execute) {
    345   VLOG(2) << __func__;
    346   std::lock_guard<std::mutex> lock(*maps_lock());
    347 
    348   auto gatt_cb = GetGattServerCallback(gatt_server->GetInstanceId());
    349   if (!gatt_cb.get()) {
    350     LOG(WARNING) << "Callback for this GattServer was deleted.";
    351     return;
    352   }
    353 
    354   gatt_cb->OnExecuteWriteRequest(device_address, request_id, is_execute);
    355 }
    356 
    357 }  // namespace binder
    358 }  // namespace ipc
    359