]> git.basschouten.com Git - openhab-addons.git/blob
ec6c131caad45361356a1d7ea8dce96197ecb775
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.bluetooth;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.UUID;
18
19 /**
20  * The {@link BluetoothDescriptor} class defines the Bluetooth descriptor.
21  * <p>
22  * Descriptors are defined attributes that describe a characteristic value.
23  * <p>
24  * https://www.bluetooth.com/specifications/gatt/descriptors
25  *
26  * @author Chris Jackson - Initial contribution
27  * @author Kai Kreuzer - added constructor and fixed setValue method
28  */
29 public class BluetoothDescriptor {
30
31     protected final BluetoothCharacteristic characteristic;
32     protected final UUID uuid;
33     protected final int handle;
34     protected byte[] value;
35
36     /**
37      * The main constructor
38      *
39      * @param characteristic the characteristic that this class describes
40      * @param uuid the uuid of the descriptor
41      */
42     public BluetoothDescriptor(BluetoothCharacteristic characteristic, UUID uuid, int handle) {
43         this.characteristic = characteristic;
44         this.uuid = uuid;
45         this.handle = handle;
46     }
47
48     /**
49      * Returns the characteristic this descriptor belongs to.
50      *
51      * @return
52      */
53     BluetoothCharacteristic getCharacteristic() {
54         return characteristic;
55     }
56
57     /**
58      * Returns the permissions for this descriptor.
59      *
60      * @return the permissions
61      */
62     public int getPermissions() {
63         return 0;
64     }
65
66     /**
67      * Returns the UUID of this descriptor.
68      *
69      * @return the UUID
70      */
71     public UUID getUuid() {
72         return uuid;
73     }
74
75     /**
76      * Returns the handle for this descriptor
77      *
78      * @return the handle for the descriptor
79      */
80     public int getHandle() {
81         return handle;
82     }
83
84     /**
85      * Returns the stored value for this descriptor. It doesn't read remote data.
86      *
87      * @return the value of the descriptor
88      */
89     public byte[] getValue() {
90         return value;
91     }
92
93     /**
94      * Sets the stored value for this descriptor. It doesn't update remote data.
95      *
96      * @param value the value for this descriptor instance
97      */
98     public void setValue(byte[] value) {
99         this.value = value;
100     }
101
102     public GattDescriptor getDescriptor() {
103         return GattDescriptor.getDescriptor(uuid);
104     }
105
106     public enum GattDescriptor {
107         // Descriptors
108         CHARACTERISTIC_EXTENDED_PROPERTIES(0x2900),
109         CHARACTERISTIC_USER_DESCRIPTION(0x2901),
110         CLIENT_CHARACTERISTIC_CONFIGURATION(0x2902),
111         SERVER_CHARACTERISTIC_CONFIGURATION(0x2903),
112         CHARACTERISTIC_PRESENTATION_FORMAT(0x2904),
113         CHARACTERISTIC_AGGREGATE_FORMAT(0x2905),
114         VALID_RANGE(0x2906),
115         EXTERNAL_REPORT_REFERENCE(0x2907),
116         REPORT_REFERENCE(0x2908),
117         NUMBER_OF_DIGITALS(0x2909),
118         TRIGGER_SETTING(0x290A);
119
120         private static Map<UUID, GattDescriptor> uuidToServiceMapping;
121
122         private final UUID uuid;
123
124         private GattDescriptor(long key) {
125             this.uuid = BluetoothBindingConstants.createBluetoothUUID(key);
126         }
127
128         private static void initMapping() {
129             uuidToServiceMapping = new HashMap<>();
130             for (GattDescriptor s : values()) {
131                 uuidToServiceMapping.put(s.uuid, s);
132             }
133         }
134
135         public static GattDescriptor getDescriptor(UUID uuid) {
136             if (uuidToServiceMapping == null) {
137                 initMapping();
138             }
139             return uuidToServiceMapping.get(uuid);
140         }
141
142         /**
143          * @return the key
144          */
145         public UUID getUUID() {
146             return uuid;
147         }
148     }
149 }