]> git.basschouten.com Git - openhab-addons.git/blob
92a3bb2e500b7b5898c433b328057714d0dce6b9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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
34     protected final int handle;
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     public GattDescriptor getDescriptor() {
85         return GattDescriptor.getDescriptor(uuid);
86     }
87
88     public enum GattDescriptor {
89         // Descriptors
90         CHARACTERISTIC_EXTENDED_PROPERTIES(0x2900),
91         CHARACTERISTIC_USER_DESCRIPTION(0x2901),
92         CLIENT_CHARACTERISTIC_CONFIGURATION(0x2902),
93         SERVER_CHARACTERISTIC_CONFIGURATION(0x2903),
94         CHARACTERISTIC_PRESENTATION_FORMAT(0x2904),
95         CHARACTERISTIC_AGGREGATE_FORMAT(0x2905),
96         VALID_RANGE(0x2906),
97         EXTERNAL_REPORT_REFERENCE(0x2907),
98         REPORT_REFERENCE(0x2908),
99         NUMBER_OF_DIGITALS(0x2909),
100         TRIGGER_SETTING(0x290A);
101
102         private static Map<UUID, GattDescriptor> uuidToServiceMapping;
103
104         private final UUID uuid;
105
106         private GattDescriptor(long key) {
107             this.uuid = BluetoothBindingConstants.createBluetoothUUID(key);
108         }
109
110         private static void initMapping() {
111             uuidToServiceMapping = new HashMap<>();
112             for (GattDescriptor s : values()) {
113                 uuidToServiceMapping.put(s.uuid, s);
114             }
115         }
116
117         public static GattDescriptor getDescriptor(UUID uuid) {
118             if (uuidToServiceMapping == null) {
119                 initMapping();
120             }
121             return uuidToServiceMapping.get(uuid);
122         }
123
124         /**
125          * @return the key
126          */
127         public UUID getUUID() {
128             return uuid;
129         }
130     }
131 }