]> git.basschouten.com Git - openhab-addons.git/blob
8ac763c1d180afd6ea110e56e7cba743e4e8d934
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 byte[] value;
34
35     /**
36      * The main constructor
37      *
38      * @param characteristic the characteristic that this class describes
39      * @param uuid the uuid of the descriptor
40      */
41     public BluetoothDescriptor(BluetoothCharacteristic characteristic, UUID uuid) {
42         this.characteristic = characteristic;
43         this.uuid = uuid;
44     }
45
46     /**
47      * Returns the characteristic this descriptor belongs to.
48      *
49      * @return
50      */
51     BluetoothCharacteristic getCharacteristic() {
52         return characteristic;
53     }
54
55     /**
56      * Returns the permissions for this descriptor.
57      *
58      * @return the permissions
59      */
60     public int getPermissions() {
61         return 0;
62     }
63
64     /**
65      * Returns the UUID of this descriptor.
66      *
67      * @return the UUID
68      */
69     public UUID getUuid() {
70         return uuid;
71     }
72
73     /**
74      * Returns the stored value for this descriptor. It doesn't read remote data.
75      *
76      * @return the value of the descriptor
77      */
78     public byte[] getValue() {
79         return value;
80     }
81
82     /**
83      * Sets the stored value for this descriptor. It doesn't update remote data.
84      *
85      * @param value the value for this descriptor instance
86      */
87     public void setValue(byte[] value) {
88         this.value = value;
89     }
90
91     public GattDescriptor getDescriptor() {
92         return GattDescriptor.getDescriptor(uuid);
93     }
94
95     public enum GattDescriptor {
96         // Descriptors
97         CHARACTERISTIC_EXTENDED_PROPERTIES(0x2900),
98         CHARACTERISTIC_USER_DESCRIPTION(0x2901),
99         CLIENT_CHARACTERISTIC_CONFIGURATION(0x2902),
100         SERVER_CHARACTERISTIC_CONFIGURATION(0x2903),
101         CHARACTERISTIC_PRESENTATION_FORMAT(0x2904),
102         CHARACTERISTIC_AGGREGATE_FORMAT(0x2905),
103         VALID_RANGE(0x2906),
104         EXTERNAL_REPORT_REFERENCE(0x2907),
105         REPORT_REFERENCE(0x2908),
106         NUMBER_OF_DIGITALS(0x2909),
107         TRIGGER_SETTING(0x290A);
108
109         private static Map<UUID, GattDescriptor> uuidToServiceMapping;
110
111         private final UUID uuid;
112
113         private GattDescriptor(long key) {
114             this.uuid = new UUID((key << 32) | 0x1000, BluetoothBindingConstants.BLUETOOTH_BASE_UUID);
115         }
116
117         private static void initMapping() {
118             uuidToServiceMapping = new HashMap<>();
119             for (GattDescriptor s : values()) {
120                 uuidToServiceMapping.put(s.uuid, s);
121             }
122         }
123
124         public static GattDescriptor getDescriptor(UUID uuid) {
125             if (uuidToServiceMapping == null) {
126                 initMapping();
127             }
128             return uuidToServiceMapping.get(uuid);
129         }
130
131         /**
132          * @return the key
133          */
134         public UUID getUUID() {
135             return uuid;
136         }
137     }
138 }