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 "service/gatt_server.h"
22 #include "service/common/bluetooth/binder/IBluetoothGattServer.h"
23 #include "service/common/bluetooth/binder/IBluetoothGattServerCallback.h"
24 #include "service/ipc/binder/interface_with_instances_base.h"
25 
26 namespace bluetooth {
27 class Adapter;
28 }  // namespace bluetooth
29 
30 namespace ipc {
31 namespace binder {
32 
33 // Implements the server side of the IBluetoothGattServer interface.
34 class BluetoothGattServerBinderServer : public BnBluetoothGattServer,
35                                         public InterfaceWithInstancesBase,
36                                         public bluetooth::GattServer::Delegate {
37  public:
38   explicit BluetoothGattServerBinderServer(bluetooth::Adapter* adapter);
39   ~BluetoothGattServerBinderServer() override = default;
40 
41   // IBluetoothGattServer overrides:
42   bool RegisterServer(
43       const android::sp<IBluetoothGattServerCallback>& callback) override;
44   void UnregisterServer(int server_id) override;
45   void UnregisterAll() override;
46   bool BeginServiceDeclaration(
47       int server_id, bool is_primary, const bluetooth::UUID& uuid,
48       std::unique_ptr<bluetooth::GattIdentifier>* out_id) override;
49   bool AddCharacteristic(
50       int server_id, const bluetooth::UUID& uuid,
51       int properties, int permissions,
52       std::unique_ptr<bluetooth::GattIdentifier>* out_id) override;
53   bool AddDescriptor(
54       int server_id, const bluetooth::UUID& uuid, int permissions,
55       std::unique_ptr<bluetooth::GattIdentifier>* out_id) override;
56   bool EndServiceDeclaration(int server_id) override;
57   bool SendResponse(int server_id, const std::string& device_address,
58                     int request_id, int status, int offset,
59                     const std::vector<uint8_t>& value) override;
60   bool SendNotification(
61       int server_id,
62       const std::string& device_address,
63       const bluetooth::GattIdentifier& characteristic_id,
64       bool confirm,
65       const std::vector<uint8_t>& value) override;
66 
67   // bluetooth::GattServer::Delegate overrides:
68   void OnCharacteristicReadRequest(
69       bluetooth::GattServer* gatt_server,
70       const std::string& device_address,
71       int request_id, int offset, bool is_long,
72       const bluetooth::GattIdentifier& characteristic_id) override;
73   void OnDescriptorReadRequest(
74       bluetooth::GattServer* gatt_server,
75       const std::string& device_address,
76       int request_id, int offset, bool is_long,
77       const bluetooth::GattIdentifier& descriptor_id) override;
78   void OnCharacteristicWriteRequest(
79       bluetooth::GattServer* gatt_server,
80       const std::string& device_address,
81       int request_id, int offset, bool is_prepare_write, bool need_response,
82       const std::vector<uint8_t>& value,
83       const bluetooth::GattIdentifier& characteristic_id) override;
84   void OnDescriptorWriteRequest(
85       bluetooth::GattServer* gatt_server,
86       const std::string& device_address,
87       int request_id, int offset, bool is_prepare_write, bool need_response,
88       const std::vector<uint8_t>& value,
89       const bluetooth::GattIdentifier& descriptor_id) override;
90   void OnExecuteWriteRequest(
91       bluetooth::GattServer* gatt_server,
92       const std::string& device_address,
93       int request_id, bool is_execute) 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>
100       GetGattServerCallback(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(
108       bluetooth::BLEStatus status,
109       android::sp<IInterface> callback,
110       bluetooth::BluetoothInstance* instance) override;
111 
112   bluetooth::Adapter* adapter_;  // weak
113 
114   DISALLOW_COPY_AND_ASSIGN(BluetoothGattServerBinderServer);
115 };
116 
117 }  // namespace binder
118 }  // namespace ipc
119