1 // 2 // Copyright 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 #pragma once 18 19 #include <string> 20 #include <vector> 21 22 #include <base/macros.h> 23 #include <utils/String16.h> 24 #include <utils/Vector.h> 25 26 #include <android/bluetooth/BnBluetooth.h> 27 #include <android/bluetooth/IBluetoothCallback.h> 28 #include <android/bluetooth/IBluetoothGattClient.h> 29 #include <android/bluetooth/IBluetoothGattServer.h> 30 #include <android/bluetooth/IBluetoothLeAdvertiser.h> 31 #include <android/bluetooth/IBluetoothLeScanner.h> 32 #include <android/bluetooth/IBluetoothLowEnergy.h> 33 #include <bluetooth/uuid.h> 34 35 #include "service/adapter.h" 36 #include "service/ipc/binder/remote_callback_list.h" 37 38 using android::String16; 39 using android::binder::Status; 40 41 using android::bluetooth::BnBluetooth; 42 using android::bluetooth::IBluetoothCallback; 43 using android::bluetooth::IBluetoothGattClient; 44 using android::bluetooth::IBluetoothGattServer; 45 using android::bluetooth::IBluetoothLowEnergy; 46 using android::bluetooth::IBluetoothLeAdvertiser; 47 using android::bluetooth::IBluetoothLeScanner; 48 49 namespace ipc { 50 namespace binder { 51 52 // Implements the server side of the IBluetooth Binder interface. 53 class BluetoothBinderServer : public BnBluetooth, 54 public bluetooth::Adapter::Observer { 55 public: 56 explicit BluetoothBinderServer(bluetooth::Adapter* adapter); 57 ~BluetoothBinderServer() override; 58 59 // IBluetooth overrides: 60 Status IsEnabled(bool* _aidl_return) override; 61 Status GetState(int32_t* _aidl_return) override; 62 Status Enable(bool start_restricted, bool* _aidl_return) override; 63 Status EnableNoAutoConnect(bool* _aidl_return) override; 64 Status Disable(bool* _aidl_return) override; 65 66 Status GetAddress(::android::String16* _aidl_return) override; 67 Status GetUUIDs( 68 ::std::vector<::android::bluetooth::UUID>* _aidl_return) override; 69 Status SetName(const ::android::String16& name, bool* _aidl_return) override; 70 Status GetName(::android::String16* _aidl_return) override; 71 72 Status RegisterCallback( 73 const ::android::sp<IBluetoothCallback>& callback) override; 74 Status UnregisterCallback( 75 const ::android::sp<IBluetoothCallback>& callback) override; 76 Status IsMultiAdvertisementSupported(bool* _aidl_return) override; 77 Status GetLowEnergyInterface( 78 ::android::sp<IBluetoothLowEnergy>* _aidl_return) override; 79 Status GetLeAdvertiserInterface( 80 ::android::sp<IBluetoothLeAdvertiser>* _aidl_return) override; 81 Status GetLeScannerInterface( 82 ::android::sp<IBluetoothLeScanner>* _aidl_return) override; 83 Status GetGattClientInterface( 84 ::android::sp<IBluetoothGattClient>* _aidl_return) override; 85 Status GetGattServerInterface( 86 ::android::sp<IBluetoothGattServer>* _aidl_return) override; 87 88 android::status_t dump( 89 int fd, const android::Vector<android::String16>& args) override; 90 91 // bluetooth::Adapter::Observer overrides: 92 void OnAdapterStateChanged(bluetooth::Adapter* adapter, 93 bluetooth::AdapterState prev_state, 94 bluetooth::AdapterState new_state) override; 95 96 private: 97 bluetooth::Adapter* adapter_; // weak 98 RemoteCallbackList<IBluetoothCallback> callbacks_; 99 100 // The IBluetoothLowEnergy interface handle. This is lazily initialized on the 101 // first call to GetLowEnergyInterface(). 102 android::sp<IBluetoothLowEnergy> low_energy_interface_; 103 104 // The IBluetoothLeAdvertiser interface handle. This is lazily initialized on 105 // the first call to GetLeAdvertiserInterface(). 106 android::sp<IBluetoothLeAdvertiser> le_advertiser_interface_; 107 108 // The IBluetoothLeScanner interface handle. This is lazily initialized on the 109 // first call to GetLeScannerInterface(). 110 android::sp<IBluetoothLeScanner> le_scanner_interface_; 111 112 // The IBluetoothGattClient interface handle. This is lazily initialized on 113 // the first call to GetGattClientInterface(). 114 android::sp<IBluetoothGattClient> gatt_client_interface_; 115 116 // The IBluetoothGattServer interface handle. This is lazily initialized on 117 // the first call to GetGattServerInterface(). 118 android::sp<IBluetoothGattServer> gatt_server_interface_; 119 120 DISALLOW_COPY_AND_ASSIGN(BluetoothBinderServer); 121 }; 122 123 } // namespace binder 124 } // namespace ipc 125