]> git.basschouten.com Git - openhab-addons.git/blob
0f9ac4c6f194379c6fadad7540dc111f51dd7773
[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 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * The {@link BluetoothDescriptor} class defines the Bluetooth descriptor.
24  * <p>
25  * Descriptors are defined attributes that describe a characteristic value.
26  * <p>
27  * https://www.bluetooth.com/specifications/gatt/descriptors
28  *
29  * @author Chris Jackson - Initial contribution
30  * @author Kai Kreuzer - added constructor and fixed setValue method
31  */
32 @NonNullByDefault
33 public class BluetoothDescriptor {
34
35     protected final BluetoothCharacteristic characteristic;
36     protected final UUID uuid;
37
38     protected final int handle;
39
40     /**
41      * The main constructor
42      *
43      * @param characteristic the characteristic that this class describes
44      * @param uuid the uuid of the descriptor
45      */
46     public BluetoothDescriptor(BluetoothCharacteristic characteristic, UUID uuid, int handle) {
47         this.characteristic = characteristic;
48         this.uuid = uuid;
49         this.handle = handle;
50     }
51
52     /**
53      * Returns the characteristic this descriptor belongs to.
54      *
55      * @return
56      */
57     BluetoothCharacteristic getCharacteristic() {
58         return characteristic;
59     }
60
61     /**
62      * Returns the permissions for this descriptor.
63      *
64      * @return the permissions
65      */
66     public int getPermissions() {
67         return 0;
68     }
69
70     /**
71      * Returns the UUID of this descriptor.
72      *
73      * @return the UUID
74      */
75     public UUID getUuid() {
76         return uuid;
77     }
78
79     /**
80      * Returns the handle for this descriptor
81      *
82      * @return the handle for the descriptor
83      */
84     public int getHandle() {
85         return handle;
86     }
87
88     public @Nullable GattDescriptor getDescriptor() {
89         return GattDescriptor.getDescriptor(uuid);
90     }
91
92     public enum GattDescriptor {
93         // Descriptors
94         CHARACTERISTIC_EXTENDED_PROPERTIES(0x2900),
95         CHARACTERISTIC_USER_DESCRIPTION(0x2901),
96         CLIENT_CHARACTERISTIC_CONFIGURATION(0x2902),
97         SERVER_CHARACTERISTIC_CONFIGURATION(0x2903),
98         CHARACTERISTIC_PRESENTATION_FORMAT(0x2904),
99         CHARACTERISTIC_AGGREGATE_FORMAT(0x2905),
100         VALID_RANGE(0x2906),
101         EXTERNAL_REPORT_REFERENCE(0x2907),
102         REPORT_REFERENCE(0x2908),
103         NUMBER_OF_DIGITALS(0x2909),
104         TRIGGER_SETTING(0x290A);
105
106         private static @Nullable Map<UUID, GattDescriptor> uuidToServiceMapping;
107
108         private final UUID uuid;
109
110         private GattDescriptor(long key) {
111             this.uuid = BluetoothBindingConstants.createBluetoothUUID(key);
112         }
113
114         public static @Nullable GattDescriptor getDescriptor(UUID uuid) {
115             Map<UUID, GattDescriptor> localServiceMapping = uuidToServiceMapping;
116             if (localServiceMapping == null) {
117                 localServiceMapping = new HashMap<>();
118                 for (GattDescriptor s : values()) {
119                     localServiceMapping.put(s.uuid, s);
120                 }
121                 uuidToServiceMapping = localServiceMapping;
122             }
123             return localServiceMapping.get(uuid);
124         }
125
126         /**
127          * @return the key
128          */
129         public UUID getUUID() {
130             return uuid;
131         }
132     }
133 }