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