1/*
2 * Copyright (C) 2013 The Android Open Source Project
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
17package android.bluetooth;
18
19import java.util.UUID;
20
21/**
22 * Represents a Bluetooth GATT Descriptor
23 *
24 * <p> GATT Descriptors contain additional information and attributes of a GATT
25 * characteristic, {@link BluetoothGattCharacteristic}. They can be used to describe
26 * the characteristic's features or to control certain behaviours of the characteristic.
27 */
28public class BluetoothGattDescriptor {
29
30    /**
31     * Value used to enable notification for a client configuration descriptor
32     */
33    public static final byte[] ENABLE_NOTIFICATION_VALUE = {0x01, 0x00};
34
35    /**
36     * Value used to enable indication for a client configuration descriptor
37     */
38    public static final byte[] ENABLE_INDICATION_VALUE = {0x02, 0x00};
39
40    /**
41     * Value used to disable notifications or indicatinos
42     */
43    public static final byte[] DISABLE_NOTIFICATION_VALUE = {0x00, 0x00};
44
45    /**
46     * Descriptor read permission
47     */
48    public static final int PERMISSION_READ = 0x01;
49
50    /**
51     * Descriptor permission: Allow encrypted read operations
52     */
53    public static final int PERMISSION_READ_ENCRYPTED = 0x02;
54
55    /**
56     * Descriptor permission: Allow reading with man-in-the-middle protection
57     */
58    public static final int PERMISSION_READ_ENCRYPTED_MITM = 0x04;
59
60    /**
61     * Descriptor write permission
62     */
63    public static final int PERMISSION_WRITE = 0x10;
64
65    /**
66     * Descriptor permission: Allow encrypted writes
67     */
68    public static final int PERMISSION_WRITE_ENCRYPTED = 0x20;
69
70    /**
71     * Descriptor permission: Allow encrypted writes with man-in-the-middle
72     * protection
73     */
74    public static final int PERMISSION_WRITE_ENCRYPTED_MITM = 0x40;
75
76    /**
77     * Descriptor permission: Allow signed write operations
78     */
79    public static final int PERMISSION_WRITE_SIGNED = 0x80;
80
81    /**
82     * Descriptor permission: Allow signed write operations with
83     * man-in-the-middle protection
84     */
85    public static final int PERMISSION_WRITE_SIGNED_MITM = 0x100;
86
87    /**
88     * The UUID of this descriptor.
89     * @hide
90     */
91    protected UUID mUuid;
92
93    /**
94     * Instance ID for this descriptor.
95     * @hide
96     */
97    protected int mInstance;
98
99    /**
100     * Permissions for this descriptor
101     * @hide
102     */
103    protected int mPermissions;
104
105    /**
106     * Back-reference to the characteristic this descriptor belongs to.
107     * @hide
108     */
109    protected BluetoothGattCharacteristic mCharacteristic;
110
111    /**
112     * The value for this descriptor.
113     * @hide
114     */
115    protected byte[] mValue;
116
117    /**
118     * Create a new BluetoothGattDescriptor.
119     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
120     *
121     * @param uuid The UUID for this descriptor
122     * @param permissions Permissions for this descriptor
123     */
124    public BluetoothGattDescriptor(UUID uuid, int permissions) {
125        initDescriptor(null, uuid, 0, permissions);
126    }
127
128    /**
129     * Create a new BluetoothGattDescriptor.
130     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
131     *
132     * @param characteristic The characteristic this descriptor belongs to
133     * @param uuid The UUID for this descriptor
134     * @param permissions Permissions for this descriptor
135     */
136    /*package*/ BluetoothGattDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid,
137                                    int instance, int permissions) {
138        initDescriptor(characteristic, uuid, instance, permissions);
139    }
140
141    private void initDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid,
142                                int instance, int permissions) {
143        mCharacteristic = characteristic;
144        mUuid = uuid;
145        mInstance = instance;
146        mPermissions = permissions;
147    }
148
149    /**
150     * Returns the characteristic this descriptor belongs to.
151     * @return The characteristic.
152     */
153    public BluetoothGattCharacteristic getCharacteristic() {
154        return mCharacteristic;
155    }
156
157    /**
158     * Set the back-reference to the associated characteristic
159     * @hide
160     */
161    /*package*/ void setCharacteristic(BluetoothGattCharacteristic characteristic) {
162        mCharacteristic = characteristic;
163    }
164
165    /**
166     * Returns the UUID of this descriptor.
167     *
168     * @return UUID of this descriptor
169     */
170    public UUID getUuid() {
171        return mUuid;
172    }
173
174    /**
175     * Returns the instance ID for this descriptor.
176     *
177     * <p>If a remote device offers multiple descriptors with the same UUID,
178     * the instance ID is used to distuinguish between descriptors.
179     *
180     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
181     *
182     * @return Instance ID of this descriptor
183     * @hide
184     */
185    public int getInstanceId() {
186        return mInstance;
187    }
188
189    /**
190     * Returns the permissions for this descriptor.
191     *
192     * @return Permissions of this descriptor
193     */
194    public int getPermissions() {
195        return mPermissions;
196    }
197
198    /**
199     * Returns the stored value for this descriptor
200     *
201     * <p>This function returns the stored value for this descriptor as
202     * retrieved by calling {@link BluetoothGatt#readDescriptor}. The cached
203     * value of the descriptor is updated as a result of a descriptor read
204     * operation.
205     *
206     * @return Cached value of the descriptor
207     */
208    public byte[] getValue() {
209        return mValue;
210    }
211
212    /**
213     * Updates the locally stored value of this descriptor.
214     *
215     * <p>This function modifies the locally stored cached value of this
216     * descriptor. To send the value to the remote device, call
217     * {@link BluetoothGatt#writeDescriptor} to send the value to the
218     * remote device.
219     *
220     * @param value New value for this descriptor
221     * @return true if the locally stored value has been set, false if the
222     *              requested value could not be stored locally.
223     */
224    public boolean setValue(byte[] value) {
225        mValue = value;
226        return true;
227    }
228}
229