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 #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/IBluetoothA2dpSink.h> 28 #include <android/bluetooth/IBluetoothA2dpSource.h> 29 #include <android/bluetooth/IBluetoothAvrcpControl.h> 30 #include <android/bluetooth/IBluetoothAvrcpTarget.h> 31 #include <android/bluetooth/IBluetoothCallback.h> 32 #include <android/bluetooth/IBluetoothGattClient.h> 33 #include <android/bluetooth/IBluetoothGattServer.h> 34 #include <android/bluetooth/IBluetoothLeAdvertiser.h> 35 #include <android/bluetooth/IBluetoothLeScanner.h> 36 #include <android/bluetooth/IBluetoothLowEnergy.h> 37 38 #include "bluetooth/uuid.h" 39 #include "service/adapter.h" 40 #include "service/ipc/binder/remote_callback_list.h" 41 42 using android::String16; 43 using android::binder::Status; 44 45 using android::bluetooth::BnBluetooth; 46 using android::bluetooth::IBluetoothA2dpSink; 47 using android::bluetooth::IBluetoothA2dpSource; 48 using android::bluetooth::IBluetoothAvrcpControl; 49 using android::bluetooth::IBluetoothAvrcpTarget; 50 using android::bluetooth::IBluetoothCallback; 51 using android::bluetooth::IBluetoothGattClient; 52 using android::bluetooth::IBluetoothGattServer; 53 using android::bluetooth::IBluetoothLeAdvertiser; 54 using android::bluetooth::IBluetoothLeScanner; 55 using android::bluetooth::IBluetoothLowEnergy; 56 57 namespace ipc { 58 namespace binder { 59 60 class BluetoothA2dpSinkBinderServer; 61 class BluetoothA2dpSourceBinderServer; 62 class BluetoothAvrcpTargetBinderServer; 63 64 // Implements the server side of the IBluetooth Binder interface. 65 class BluetoothBinderServer : public BnBluetooth, 66 public bluetooth::Adapter::Observer { 67 public: 68 explicit BluetoothBinderServer(bluetooth::Adapter* adapter); 69 ~BluetoothBinderServer() override; 70 71 // IBluetooth overrides: 72 Status IsEnabled(bool* _aidl_return) override; 73 Status GetState(int32_t* _aidl_return) override; 74 Status Enable(bool* _aidl_return) override; 75 Status EnableNoAutoConnect(bool* _aidl_return) override; 76 Status Disable(bool* _aidl_return) override; 77 78 Status GetAddress(::android::String16* _aidl_return) override; 79 Status GetUUIDs( 80 ::std::vector<::android::bluetooth::UUID>* _aidl_return) override; 81 Status SetName(const ::android::String16& name, bool* _aidl_return) override; 82 Status GetName(::android::String16* _aidl_return) override; 83 Status SetScanMode(int32_t scan_mode, bool* _aidl_return) override; 84 Status SetScanEnable(bool scan_enable, bool* _aidl_return) override; 85 Status SspReply(const ::android::String16& device_address, int32_t variant, 86 bool accept, int32_t passkey, bool* _aidl_return) override; 87 Status CreateBond(const ::android::String16& device_address, 88 int32_t transport, bool* _aidl_return) override; 89 Status GetBondedDevices(bool* _aidl_return) override; 90 Status RemoveBond(const ::android::String16& device_address, 91 bool* _aidl_return) override; 92 Status GetRemoteDeviceProperties(const ::android::String16& device_address, 93 bool* _aidl_return) override; 94 95 Status RegisterCallback( 96 const ::android::sp<IBluetoothCallback>& callback) override; 97 Status UnregisterCallback( 98 const ::android::sp<IBluetoothCallback>& callback) override; 99 Status IsMultiAdvertisementSupported(bool* _aidl_return) override; 100 Status GetA2dpSinkInterface( 101 ::android::sp<IBluetoothA2dpSink>* _aidl_return) override; 102 Status GetA2dpSourceInterface( 103 ::android::sp<IBluetoothA2dpSource>* _aidl_return) override; 104 Status GetLowEnergyInterface( 105 ::android::sp<IBluetoothLowEnergy>* _aidl_return) override; 106 Status GetLeAdvertiserInterface( 107 ::android::sp<IBluetoothLeAdvertiser>* _aidl_return) override; 108 Status GetLeScannerInterface( 109 ::android::sp<IBluetoothLeScanner>* _aidl_return) override; 110 Status GetGattClientInterface( 111 ::android::sp<IBluetoothGattClient>* _aidl_return) override; 112 Status GetGattServerInterface( 113 ::android::sp<IBluetoothGattServer>* _aidl_return) override; 114 Status GetAvrcpControlInterface( 115 ::android::sp<IBluetoothAvrcpControl>* _aidl_return) override; 116 Status GetAvrcpTargetInterface( 117 ::android::sp<IBluetoothAvrcpTarget>* _aidl_return) override; 118 119 android::status_t dump( 120 int fd, const android::Vector<android::String16>& args) override; 121 122 // bluetooth::Adapter::Observer overrides: 123 void OnAdapterStateChanged(bluetooth::Adapter* adapter, 124 bluetooth::AdapterState prev_state, 125 bluetooth::AdapterState new_state) override; 126 127 void OnDeviceConnectionStateChanged(bluetooth::Adapter* adapter, 128 const std::string& device_address, 129 bool connected) override; 130 131 void OnScanEnableChanged(bluetooth::Adapter* adapter, 132 bool scan_enabled) override; 133 134 void OnSspRequest(bluetooth::Adapter* adapter, 135 const std::string& device_address, 136 const std::string& device_name, int cod, 137 int pairing_variant, int pass_key) override; 138 139 void OnBondStateChanged(bluetooth::Adapter* adapter, int status, 140 const std::string& device_address, 141 int state) override; 142 void OnGetBondedDevices( 143 bluetooth::Adapter* adapter, int status, 144 const std::vector<std::string>& bonded_devices) override; 145 void OnGetRemoteDeviceProperties( 146 bluetooth::Adapter* adapter, int status, 147 const std::string& device_address, 148 const bluetooth::RemoteDeviceProps& properties) override; 149 void OnDeviceFound(bluetooth::Adapter* adapter, 150 const bluetooth::RemoteDeviceProps& properties) override; 151 152 private: 153 bluetooth::Adapter* adapter_; // weak 154 RemoteCallbackList<IBluetoothCallback> callbacks_; 155 156 // The IBluetoothA2dpSink interface handle. This is lazily initialized on the 157 // first call to GetA2dpSinkInterface(). 158 android::sp<BluetoothA2dpSinkBinderServer> a2dp_sink_interface_; 159 160 // The IBluetoothA2dpSource interface handle. This is lazily initialized on 161 // the first call to GetA2dpSourceInterface(). 162 android::sp<BluetoothA2dpSourceBinderServer> a2dp_source_interface_; 163 164 // The IBluetoothLowEnergy interface handle. This is lazily initialized on the 165 // first call to GetLowEnergyInterface(). 166 android::sp<IBluetoothLowEnergy> low_energy_interface_; 167 168 // The IBluetoothLeAdvertiser interface handle. This is lazily initialized on 169 // the first call to GetLeAdvertiserInterface(). 170 android::sp<IBluetoothLeAdvertiser> le_advertiser_interface_; 171 172 // The IBluetoothLeScanner interface handle. This is lazily initialized on the 173 // first call to GetLeScannerInterface(). 174 android::sp<IBluetoothLeScanner> le_scanner_interface_; 175 176 // The IBluetoothGattClient interface handle. This is lazily initialized on 177 // the first call to GetGattClientInterface(). 178 android::sp<IBluetoothGattClient> gatt_client_interface_; 179 180 // The IBluetoothGattServer interface handle. This is lazily initialized on 181 // the first call to GetGattServerInterface(). 182 android::sp<IBluetoothGattServer> gatt_server_interface_; 183 184 // The IBluetoothAvrcpControl interface handle. This is lazily initialized on 185 // the first call to GetAvrcpControlInterface(). 186 android::sp<IBluetoothAvrcpControl> avrcp_control_interface_; 187 188 // The IBluetoothAvrcpTarget interface handle. This is lazily initialized on 189 // the first call to GetAvrcpTargetInterface(). 190 android::sp<BluetoothAvrcpTargetBinderServer> avrcp_target_interface_; 191 192 DISALLOW_COPY_AND_ASSIGN(BluetoothBinderServer); 193 }; 194 195 } // namespace binder 196 } // namespace ipc 197