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 <string>
21 #include <vector>
22 
23 #include <base/macros.h>
24 #include <binder/IBinder.h>
25 #include <binder/IInterface.h>
26 
27 #include <bluetooth/gatt_identifier.h>
28 
29 namespace ipc {
30 namespace binder {
31 
32 // This class defines the Binder IPC interface for receiving callbacks related
33 // to Bluetooth GATT server-role operations.
34 // TODO(armansito): This class was written based on a new design doc proposal.
35 // We need to add an AIDL for this to the framework code.
36 //
37 // NOTE: KEEP THIS FILE UP-TO-DATE with the corresponding AIDL, otherwise this
38 // won't be compatible with the Android framework.
39 /* oneway */ class IBluetoothGattServerCallback : public android::IInterface {
40  public:
41   DECLARE_META_INTERFACE(BluetoothGattServerCallback);
42 
43   static const char kServiceName[];
44 
45   // Transaction codes for interface methods.
46   enum {
47     ON_SERVER_REGISTERED_TRANSACTION = android::IBinder::FIRST_CALL_TRANSACTION,
48     ON_SERVICE_ADDED_TRANSACTION,
49     ON_CHARACTERISTIC_READ_REQUEST_TRANSACTION,
50     ON_DESCRIPTOR_READ_REQUEST_TRANSACTION,
51     ON_CHARACTERISTIC_WRITE_REQUEST_TRANSACTION,
52     ON_DESCRIPTOR_WRITE_REQUEST_TRANSACTION,
53     ON_EXECUTE_WRITE_REQUEST_TRANSACTION,
54     ON_NOTIFICATION_SENT_TRANSACTION,
55   };
56 
57   virtual void OnServerRegistered(int status, int server_if) = 0;
58 
59   virtual void OnServiceAdded(
60       int status, const bluetooth::GattIdentifier& service_id) = 0;
61 
62   virtual void OnCharacteristicReadRequest(
63       const std::string& device_address,
64       int request_id, int offset, bool is_long,
65       const bluetooth::GattIdentifier& characteristic_id) = 0;
66 
67   virtual void OnDescriptorReadRequest(
68       const std::string& device_address,
69       int request_id, int offset, bool is_long,
70       const bluetooth::GattIdentifier& descriptor_id) = 0;
71 
72   virtual void OnCharacteristicWriteRequest(
73       const std::string& device_address,
74       int request_id, int offset, bool is_prepare_write, bool need_response,
75       const std::vector<uint8_t>& value,
76       const bluetooth::GattIdentifier& characteristic_id) = 0;
77 
78   virtual void OnDescriptorWriteRequest(
79       const std::string& device_address,
80       int request_id, int offset, bool is_prepare_write, bool need_response,
81       const std::vector<uint8_t>& value,
82       const bluetooth::GattIdentifier& descriptor_id) = 0;
83 
84   virtual void OnExecuteWriteRequest(
85       const std::string& device_address,
86       int request_id, bool is_execute) = 0;
87 
88   virtual void OnNotificationSent(const std::string& device_address,
89                                   int status) = 0;
90 
91  private:
92   DISALLOW_COPY_AND_ASSIGN(IBluetoothGattServerCallback);
93 };
94 
95 // The Binder server interface to IBluetoothGattServerCallback. A class that
96 // implements IBluetoothGattServerCallback must inherit from this class.
97 class BnBluetoothGattServerCallback
98     : public android::BnInterface<IBluetoothGattServerCallback> {
99  public:
100   BnBluetoothGattServerCallback() = default;
101   virtual ~BnBluetoothGattServerCallback() = default;
102 
103  private:
104   virtual android::status_t onTransact(
105       uint32_t code,
106       const android::Parcel& data,
107       android::Parcel* reply,
108       uint32_t flags = 0);
109 
110   DISALLOW_COPY_AND_ASSIGN(BnBluetoothGattServerCallback);
111 };
112 
113 // The Binder client interface to IBluetoothGattServerCallback.
114 class BpBluetoothGattServerCallback
115     : public android::BpInterface<IBluetoothGattServerCallback> {
116  public:
117   explicit BpBluetoothGattServerCallback(
118       const android::sp<android::IBinder>& impl);
119   virtual ~BpBluetoothGattServerCallback() = default;
120 
121   // IBluetoothGattServerCallback overrides:
122   void OnServerRegistered(int status, int server_if) override;
123   void OnServiceAdded(int status,
124                       const bluetooth::GattIdentifier& service_id) override;
125   void OnCharacteristicReadRequest(
126       const std::string& device_address,
127       int request_id, int offset, bool is_long,
128       const bluetooth::GattIdentifier& characteristic_id) override;
129   void OnDescriptorReadRequest(
130       const std::string& device_address,
131       int request_id, int offset, bool is_long,
132       const bluetooth::GattIdentifier& descriptor_id) override;
133   void OnCharacteristicWriteRequest(
134       const std::string& device_address,
135       int request_id, int offset, bool is_prepare_write, bool need_response,
136       const std::vector<uint8_t>& value,
137       const bluetooth::GattIdentifier& characteristic_id) override;
138   void OnDescriptorWriteRequest(
139       const std::string& device_address,
140       int request_id, int offset, bool is_prepare_write, bool need_response,
141       const std::vector<uint8_t>& value,
142       const bluetooth::GattIdentifier& descriptor_id) override;
143   void OnExecuteWriteRequest(
144       const std::string& device_address,
145       int request_id, bool is_execute) override;
146   void OnNotificationSent(const std::string& device_address,
147                           int status) override;
148 
149  private:
150   DISALLOW_COPY_AND_ASSIGN(BpBluetoothGattServerCallback);
151 };
152 
153 }  // namespace binder
154 }  // namespace ipc
155