1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_ANDROID_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_ANDROID_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "base/android/jni_android.h"
15 #include "base/android/scoped_java_ref.h"
16 #include "base/macros.h"
17 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
18 #include "device/bluetooth/bluetooth_remote_gatt_service.h"
19 
20 namespace device {
21 
22 class BluetoothAdapterAndroid;
23 class BluetoothRemoteGattServiceAndroid;
24 
25 // BluetoothRemoteGattCharacteristicAndroid along with its owned Java class
26 // org.chromium.device.bluetooth.ChromeBluetoothRemoteGattCharacteristic
27 // implement BluetootGattCharacteristic.
28 class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid
29     : public BluetoothRemoteGattCharacteristic {
30  public:
31   // Create a BluetoothRemoteGattCharacteristicAndroid instance and associated
32   // Java
33   // ChromeBluetoothRemoteGattCharacteristic using the provided
34   // |bluetooth_gatt_characteristic_wrapper|.
35   //
36   // The ChromeBluetoothRemoteGattCharacteristic instance will hold a Java
37   // reference
38   // to |bluetooth_gatt_characteristic_wrapper|.
39   static std::unique_ptr<BluetoothRemoteGattCharacteristicAndroid> Create(
40       BluetoothAdapterAndroid* adapter,
41       BluetoothRemoteGattServiceAndroid* service,
42       const std::string& instance_id,
43       const base::android::JavaRef<
44           jobject>& /* BluetoothGattCharacteristicWrapper */
45       bluetooth_gatt_characteristic_wrapper,
46       const base::android::JavaRef<
47           jobject>& /* ChromeBluetoothDevice */ chrome_bluetooth_device);
48 
49   ~BluetoothRemoteGattCharacteristicAndroid() override;
50 
51   // Returns the associated ChromeBluetoothRemoteGattCharacteristic Java object.
52   base::android::ScopedJavaLocalRef<jobject> GetJavaObject();
53 
54   // BluetoothRemoteGattCharacteristic interface:
55   std::string GetIdentifier() const override;
56   BluetoothUUID GetUUID() const override;
57   const std::vector<uint8_t>& GetValue() const override;
58   BluetoothRemoteGattService* GetService() const override;
59   Properties GetProperties() const override;
60   Permissions GetPermissions() const override;
61   std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() const override;
62   BluetoothRemoteGattDescriptor* GetDescriptor(
63       const std::string& identifier) const override;
64   std::vector<BluetoothRemoteGattDescriptor*> GetDescriptorsByUUID(
65       const BluetoothUUID& uuid) const override;
66   void ReadRemoteCharacteristic(ValueCallback callback,
67                                 ErrorCallback error_callback) override;
68   void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
69                                  base::OnceClosure callback,
70                                  ErrorCallback error_callback) override;
71 
72   // Called when value changed event occurs.
73   void OnChanged(JNIEnv* env,
74                  const base::android::JavaParamRef<jobject>& jcaller,
75                  const base::android::JavaParamRef<jbyteArray>& value);
76 
77   // Called when Read operation completes.
78   void OnRead(JNIEnv* env,
79               const base::android::JavaParamRef<jobject>& jcaller,
80               int32_t status,
81               const base::android::JavaParamRef<jbyteArray>& value);
82 
83   // Called when Write operation completes.
84   void OnWrite(JNIEnv* env,
85                const base::android::JavaParamRef<jobject>& jcaller,
86                int32_t status);
87 
88   // Creates a Bluetooth GATT descriptor object and adds it to |descriptors_|,
89   // DCHECKing that it has not already been created.
90   void CreateGattRemoteDescriptor(
91       JNIEnv* env,
92       const base::android::JavaParamRef<jobject>& caller,
93       const base::android::JavaParamRef<jstring>& instanceId,
94       const base::android::JavaParamRef<
95           jobject>& /* BluetoothGattDescriptorWrapper */
96       bluetooth_gatt_descriptor_wrapper,
97       const base::android::JavaParamRef<
98           jobject>& /* ChromeBluetoothCharacteristic */
99       chrome_bluetooth_characteristic);
100 
101  protected:
102   void SubscribeToNotifications(BluetoothRemoteGattDescriptor* ccc_descriptor,
103                                 base::OnceClosure callback,
104                                 ErrorCallback error_callback) override;
105   void UnsubscribeFromNotifications(
106       BluetoothRemoteGattDescriptor* ccc_descriptor,
107       base::OnceClosure callback,
108       ErrorCallback error_callback) override;
109 
110  private:
111   BluetoothRemoteGattCharacteristicAndroid(
112       BluetoothAdapterAndroid* adapter,
113       BluetoothRemoteGattServiceAndroid* service,
114       const std::string& instance_id);
115 
116   // Populates |descriptors_| from Java objects if necessary.
117   void EnsureDescriptorsCreated() const;
118 
119   // The adapter and service associated with this characteristic. It's ok to
120   // store a raw pointers here since they indirectly own this instance.
121   BluetoothAdapterAndroid* adapter_;
122   BluetoothRemoteGattServiceAndroid* service_;
123 
124   // Java object
125   // org.chromium.device.bluetooth.ChromeBluetoothRemoteGattCharacteristic.
126   base::android::ScopedJavaGlobalRef<jobject> j_characteristic_;
127 
128   // Adapter unique instance ID.
129   std::string instance_id_;
130 
131   // ReadRemoteCharacteristic callbacks and pending state.
132   bool read_pending_ = false;
133   ValueCallback read_callback_;
134   ErrorCallback read_error_callback_;
135 
136   // WriteRemoteCharacteristic callbacks and pending state.
137   bool write_pending_ = false;
138   base::OnceClosure write_callback_;
139   ErrorCallback write_error_callback_;
140 
141   std::vector<uint8_t> value_;
142 
143   DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristicAndroid);
144 };
145 
146 }  // namespace device
147 
148 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_ANDROID_H_
149