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 <cstdint>
20 #include <memory>
21 #include <vector>
22 
23 #include <base/macros.h>
24 #include <binder/IBinder.h>
25 #include <binder/IInterface.h>
26 
27 #include <bluetooth/binder/IBluetoothGattServerCallback.h>
28 #include <bluetooth/gatt_identifier.h>
29 
30 namespace ipc {
31 namespace binder {
32 
33 // This class defines the Binder IPC interface for interacting with Bluetooth
34 // GATT server-role features.
35 // TODO(armansito): This class was written based on a new design doc proposal.
36 // We need to add an AIDL for this to the framework code.
37 //
38 // NOTE: KEEP THIS FILE UP-TO-DATE with the corresponding AIDL, otherwise this
39 // won't be compatible with the Android framework.
40 class IBluetoothGattServer : public android::IInterface {
41  public:
42   DECLARE_META_INTERFACE(BluetoothGattServer);
43 
44   static const char kServiceName[];
45 
46   // Transaction codes for interface methods.
47   enum {
48     REGISTER_SERVER_TRANSACTION = android::IBinder::FIRST_CALL_TRANSACTION,
49     UNREGISTER_SERVER_TRANSACTION,
50     UNREGISTER_ALL_TRANSACTION,
51     BEGIN_SERVICE_DECLARATION_TRANSACTION,
52     ADD_INCLUDED_SERVICE_TRANSACTION,
53     ADD_CHARACTERISTIC_TRANSACTION,
54     ADD_DESCRIPTOR_TRANSACTION,
55     END_SERVICE_DECLARATION_TRANSACTION,
56     REMOVE_SERVICE_TRANSACTION,
57     CLEAR_SERVICES_TRANSACTION,
58     SEND_RESPONSE_TRANSACTION,
59     SEND_NOTIFICATION_TRANSACTION,
60   };
61 
62   virtual bool RegisterServer(
63       const android::sp<IBluetoothGattServerCallback>& callback) = 0;
64   virtual void UnregisterServer(int server_if) = 0;
65   virtual void UnregisterAll() = 0;
66 
67   virtual bool BeginServiceDeclaration(
68       int server_if, bool is_primary, const bluetooth::UUID& uuid,
69       std::unique_ptr<bluetooth::GattIdentifier>* out_id) = 0;
70   virtual bool AddCharacteristic(
71       int server_if, const bluetooth::UUID& uuid,
72       int properties, int permissions,
73       std::unique_ptr<bluetooth::GattIdentifier>* out_id) = 0;
74   virtual bool AddDescriptor(
75       int server_if, const bluetooth::UUID& uuid, int permissions,
76       std::unique_ptr<bluetooth::GattIdentifier>* out_id) = 0;
77   virtual bool EndServiceDeclaration(int server_if) = 0;
78 
79   virtual bool SendResponse(
80       int server_if,
81       const std::string& device_address,
82       int request_id,
83       int status, int offset,
84       const std::vector<uint8_t>& value) = 0;
85 
86   virtual bool SendNotification(
87       int server_if,
88       const std::string& device_address,
89       const bluetooth::GattIdentifier& characteristic_id,
90       bool confirm,
91       const std::vector<uint8_t>& value) = 0;
92 
93   // TODO(armansito): Complete the API definition.
94 
95  private:
96   DISALLOW_COPY_AND_ASSIGN(IBluetoothGattServer);
97 };
98 
99 // The Binder server interface to IBluetoothGattServer. A class that implements
100 // IBluetoothGattServer must inherit from this class.
101 class BnBluetoothGattServer
102     : public android::BnInterface<IBluetoothGattServer> {
103  public:
104   BnBluetoothGattServer() = default;
105   virtual ~BnBluetoothGattServer() = default;
106 
107  private:
108   virtual android::status_t onTransact(
109       uint32_t code,
110       const android::Parcel& data,
111       android::Parcel* reply,
112       uint32_t flags = 0);
113 
114   DISALLOW_COPY_AND_ASSIGN(BnBluetoothGattServer);
115 };
116 
117 // The Binder client interface to IBluetoothGattServer.
118 class BpBluetoothGattServer
119     : public android::BpInterface<IBluetoothGattServer> {
120  public:
121   explicit BpBluetoothGattServer(const android::sp<android::IBinder>& impl);
122   virtual ~BpBluetoothGattServer() = default;
123 
124   // IBluetoothGattServer overrides:
125   bool RegisterServer(
126       const android::sp<IBluetoothGattServerCallback>& callback) override;
127   void UnregisterServer(int server_if) override;
128   void UnregisterAll() override;
129   bool BeginServiceDeclaration(
130       int server_if, bool is_primary, const bluetooth::UUID& uuid,
131       std::unique_ptr<bluetooth::GattIdentifier>* out_id) override;
132   bool AddCharacteristic(
133       int server_if, const bluetooth::UUID& uuid,
134       int properties, int permissions,
135       std::unique_ptr<bluetooth::GattIdentifier>* out_id) override;
136   bool AddDescriptor(
137       int server_if, const bluetooth::UUID& uuid, int permissions,
138       std::unique_ptr<bluetooth::GattIdentifier>* out_id) override;
139   bool EndServiceDeclaration(int server_if) override;
140   bool SendResponse(
141       int server_if,
142       const std::string& device_address,
143       int request_id,
144       int status, int offset,
145       const std::vector<uint8_t>& value) override;
146   bool SendNotification(
147       int server_if,
148       const std::string& device_address,
149       const bluetooth::GattIdentifier& characteristic_id,
150       bool confirm,
151       const std::vector<uint8_t>& value) override;
152 
153  private:
154   DISALLOW_COPY_AND_ASSIGN(BpBluetoothGattServer);
155 };
156 
157 }  // namespace binder
158 }  // namespace ipc
159