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 <base/macros.h> 20 21#include <android/bluetooth/BnBluetoothGattServer.h> 22#include <android/bluetooth/IBluetoothGattServerCallback.h> 23 24#include "service/gatt_server.h" 25#include "service/ipc/binder/interface_with_instances_base.h" 26 27using android::bluetooth::BnBluetoothGattServer; 28using android::bluetooth::IBluetoothGattServerCallback; 29 30using ::android::binder::Status; 31 32namespace bluetooth { 33class Adapter; 34} // namespace bluetooth 35 36namespace ipc { 37namespace binder { 38 39// Implements the server side of the IBluetoothGattServer interface. 40class BluetoothGattServerBinderServer : public BnBluetoothGattServer, 41 public InterfaceWithInstancesBase, 42 public bluetooth::GattServer::Delegate { 43 public: 44 explicit BluetoothGattServerBinderServer(bluetooth::Adapter* adapter); 45 ~BluetoothGattServerBinderServer() override = default; 46 47 // IBluetoothGattServer overrides: 48 Status RegisterServer( 49 const ::android::sp<::android::bluetooth::IBluetoothGattServerCallback>& 50 callback, 51 bool* _aidl_return) override; 52 Status UnregisterServer(int32_t server_id) override; 53 Status UnregisterAll() override; 54 Status AddService(int32_t server_id, 55 const ::android::bluetooth::BluetoothGattService& service, 56 bool* _aidl_return) override; 57 Status SendResponse(int32_t server_id, 58 const ::android::String16& device_address, 59 int32_t request_id, int32_t status, int32_t offset, 60 const ::std::vector<uint8_t>& value, 61 bool* _aidl_return) override; 62 Status SendNotification(int32_t server_id, 63 const ::android::String16& device_address, int handle, 64 bool confirm, const ::std::vector<uint8_t>& value, 65 bool* _aidl_return) override; 66 67 // bluetooth::GattServer::Delegate overrides: 68 void OnCharacteristicReadRequest(bluetooth::GattServer* gatt_server, 69 const std::string& device_address, 70 int request_id, int offset, bool is_long, 71 uint16_t handle) override; 72 void OnDescriptorReadRequest(bluetooth::GattServer* gatt_server, 73 const std::string& device_address, 74 int request_id, int offset, bool is_long, 75 uint16_t handle) override; 76 void OnCharacteristicWriteRequest(bluetooth::GattServer* gatt_server, 77 const std::string& device_address, 78 int request_id, int offset, 79 bool is_prepare_write, bool need_response, 80 const std::vector<uint8_t>& value, 81 uint16_t handle) override; 82 void OnDescriptorWriteRequest(bluetooth::GattServer* gatt_server, 83 const std::string& device_address, 84 int request_id, int offset, 85 bool is_prepare_write, bool need_response, 86 const std::vector<uint8_t>& value, 87 uint16_t handle) override; 88 void OnExecuteWriteRequest(bluetooth::GattServer* gatt_server, 89 const std::string& device_address, int request_id, 90 bool is_execute) override; 91 void OnConnectionStateChanged(bluetooth::GattServer* gatt_server, 92 const std::string& device_addres, 93 bool connected) override; 94 95 private: 96 // Returns a pointer to the IBluetoothGattServerCallback instance 97 // associated with |server_id|. Returns NULL if such a callback cannot be 98 // found. 99 android::sp<IBluetoothGattServerCallback> GetGattServerCallback( 100 int server_id); 101 102 // Returns a pointer to the GattServer instance associated with |server_id|. 103 // Returns NULL if such an instance cannot be found. 104 std::shared_ptr<bluetooth::GattServer> GetGattServer(int server_id); 105 106 // InterfaceWithInstancesBase override: 107 void OnRegisterInstanceImpl(bluetooth::BLEStatus status, 108 android::sp<IInterface> callback, 109 bluetooth::BluetoothInstance* instance) override; 110 111 bluetooth::Adapter* adapter_; // weak 112 113 DISALLOW_COPY_AND_ASSIGN(BluetoothGattServerBinderServer); 114}; 115 116} // namespace binder 117} // namespace ipc 118